1

I have this code to show the erc20 token balance of an account

   var tokenAddress = '0xb97E9bBB6fd49865709d3F1576e8506ad640a13B';
var walletAddress = '0x62f28320f688A7A4e0021c55d7ffD1acd770A133';



    function getERC20TokenBalance() {

      let minABI = [
        // balanceOf
        {
          "constant":true,
          "inputs":[{"name":"_owner","type":"address"}],
          "name":"balanceOf",
          "outputs":[{"name":"balance","type":"uint256"}],
          "type":"function"
        },
        // decimals
        {
          "constant":true,
          "inputs":[],
          "name":"decimals",
          "outputs":[{"name":"","type":"uint8"}],
          "type":"function"
        }
      ];


      let contract2 = web3.eth.contract(minABI).at(tokenAddress);

      web3.eth.getAccounts(function (error, walletAddress) {
          contract2.balanceOf(walletAddress, function(err, result) {
              if(err) {
                  console.log(err, 'err');
              } else {
                console.log(result);
                  document.getElementById('agiAvailable').innerHTML = result + ' AGI';

              }
          });
      });
    }
    getERC20TokenBalance();

    function onAddressChange(e) {

      if(tokenAddress != "" && walletAddress != "") {
        getERC20TokenBalance(tokenAddress, walletAddress, (balance) => {
          console.log(balance.toString());
        });
      }
    }

But that output is 0, which is not the correct balance

1 Answer 1

1

I hope that this will answer your question:

walletAddress = '0x62f28320f688A7A4e0021c55d7ffD1acd770A133'walletAddress[0] = '0'.

And when you call contract2.balanceOf(walletAddress[0]), web3 most likely converts that '0' into the zero address 0x0000000000000000000000000000000000000000', whose balance is apparently 0.

Side note:

You're obviously using web3.js v0.x here; I think that web3.js v1.x would possibly throw an "invalid value for address type" exception on this, so it might be worth upgrading your web3.js version.

11
  • but this wasnt the issue, even with removing the [0], its still not working.
    – Coding Joe
    Commented Jan 19, 2020 at 11:31
  • @CodingJoe: This is definitely an issue. There could be more than one issue of course. Please fix it to begin with, then indicate what error you're getting. Commented Jan 19, 2020 at 11:34
  • @CodingJoe: And BTW, fix up your code first. Why are you calling getERC20TokenBalance() "out of nowhere", without doing anything with its return value, and without passing any input parameters to it??? Why are you not doing anything with the callback parameter which you are passing to it??? How do you expect this function to execute that callback which you are passing??? Code must be well organized. Don't just "throw stuff into it" hoping something would eventually work. Commented Jan 19, 2020 at 11:36
  • i updated the code- i dont get any error but the output is still not correct- it says =0
    – Coding Joe
    Commented Jan 19, 2020 at 11:36
  • @CodingJoe: Please read the comment above. Commented Jan 19, 2020 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.