【问题标题】:Someone has managed to get the balance of an ERC20 Token with Web3.js@1.0.0?有人设法通过 Web3.js@1.0.0 获得 ERC20 代币的余额?
【发布时间】:2019-05-10 13:08:00
【问题描述】:

此代码适用于版本 0.20.6,但对于新版本它不再适用。我一直在阅读文档并测试示例,但我还没有让它工作。 这是下面的代码。

let contract = web3.eth.contract(ABI).at(tokenAddress);
  contract.balanceOf(walletAddress, (error, balance) => {
    contract.decimals((error, decimals) => {
      balance = balance.div(10 ** decimals);
      console.log(balance.toString());
    });
  });

请告诉我一些处理这个问题的方法。谢谢!

【问题讨论】:

    标签: blockchain ethereum web3 web3js


    【解决方案1】:

    web3@1.0 中,您必须使用myContract.methods.myMethod() 并且可以使用.then 进行响应。也可以使用回调。

    对于合约实例new web3.eth.Contract(jsonInterface, address, options)

    或者可以稍后将地址添加到实例myContract.address = '0x1234FFDD...';

    let contract = web3.eth.contract(ABI, tokenAddress);
      contract.methods.balanceOf(walletAddress).call((error, balance) => {
        contract.methods.decimals().call((error, decimals) => {
          balance = balance.div(10 ** decimals);
          console.log(balance.toString());
        });
      });
    

    【讨论】:

      猜你喜欢
      • 2018-05-04
      • 2021-07-28
      • 2021-08-31
      • 2022-12-23
      • 2019-04-06
      • 2018-08-01
      • 2020-01-31
      • 2021-10-08
      • 1970-01-01
      相关资源
      最近更新 更多