[英]Unable to get metamask balance and send amount through metamask extension using javascript(web3)
我已經在網站上集成了metamask,但我需要發送金額並使用metamask檢查ERC 20令牌上的余額,但對我來說沒有用。我嘗試在WEB3上使用不同的編碼和方法。
請提供執行的代碼給我。
但是使用下面的代碼我無法獲得與ERC20令牌相關的任何詳細信息,但是對我來說,ETH工作正常。
這是我的代碼:
var contractadd = "0xd26114cd6ee289accf82350c8d8487fedb8a0c07";
$.getJSON( "https://api.etherscan.io/api?module=contract&action=getabi&address="+contractadd, function( data ) {
var abivalue = (data.result);
if (typeof web3 !== "undefined") {
// Use Mist/MetaMask's provider
window.web3 = new Web3(web3.currentProvider);
} else {
alert("Metamask extension not added on your browser");return false;
}
const contract_address = contractadd;
var userAddress = '0x12A922212C20EFe9b8d5799CC67C619F9F1617b9';
var amount = 0.2;
web3.eth.getBlock('latest',function(err,getBlock) {
web3.eth.getGasPrice(function(err, getGasPrice) {
web3.eth.getAccounts(function(err, accounts){
if (err != null) {
console.log(err)
}
else if (accounts.length === 0) {
alert("Your metamask account is locked please unlock");return false;
}
else {
var contractAddress = contractadd;
var FromAddress = accounts[0];
var ToAddress = "hghfghfg";
var Amount = 0.5;
var Key = "";
var Amount=Amount*1000000000000000000;
const abi =JSON.parse(abivalue);
let contract = web3.eth.contract(abi).at(contractAddress);
contract.balanceOf(FromAddress, (error, balance) => {
contract.decimals((error, decimals) => {
alert(balance);
console.log(balance.toString());
});
});
}
});
});
})
});
您必須傳遞這樣的參數:
contract.balanceOf(address,{},function (err, transactionHash){
console.log(transactionHash);
window.alert(transactionHash)
});
您錯過了代碼中的空{}。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.