简体   繁体   中英

Unable to get metamask balance and send amount through metamask extension using javascript(web3)

I've integrated metamask on my site, but I need to send amount and also check the balance on ERC 20 token using metamask but nothing works for me.I try different coding and methods on WEB3.

Kindly provide the executed code to me.

But using below code I'm unable to get any details relate ERC20 tokens , but for me ,ETH is working fine.

Here is my code:

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());
});
});
}

});
});

})
});

You must pass arguments like this:

contract.balanceOf(address,{},function (err, transactionHash){
        console.log(transactionHash);
        window.alert(transactionHash)
    });

you have missed empty {} in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM