简体   繁体   English

无法使用javascript(web3)通过metamask扩展获取metamask余额并发送金额

[英]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. 我已经在网站上集成了metamask,但我需要发送金额并使用metamask检查ERC 20令牌上的余额,但对我来说没有用。我尝试在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. 但是使用下面的代码我无法获得与ERC20令牌相关的任何详细信息,但是对我来说,ETH工作正常。

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. 您错过了代码中的空{}。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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