0

You can see on Etherscan that I have 2 types of tokens in my wallet:

https://etherscan.io/address/0xb8fd65fc6919c5b97453eaba6b895144597985b2#tokentxns

2.3 OmiseGo and 10,000 Bitball

I have a short script to get the balance of ERC20 contracts, which works perfectly when I lookup my OmiseGo balance, but returns 0 for my Bitball balance.

let btb_token_address =     "0x382117315856a533549ea621542ccce13e54ae82";
let omisego_token_address = "0xd26114cd6EE289AccF82350c8d8487fedB8A0C07";
let walletAddress =         "0xb8fd65fc6919c5b97453eaba6b895144597985b2";
// The minimum ABI to get ERC20 Token balance
let minABI = [{"constant":true,"inputs":[],"name":"mintingFinished","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"unpause","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"}],"name":"mint","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"paused","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"finishMinting","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"pause","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"},{"name":"_releaseTime","type":"uint256"}],"name":"mintTimelocked","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[],"name":"MintFinished","type":"event"},{"anonymous":false,"inputs":[],"name":"Pause","type":"event"},{"anonymous":false,"inputs":[],"name":"Unpause","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}];
// Get ERC20 Token contract instance

let contract = web3.eth.contract(minABI).at(btb_token_address);
//let contract = web3.eth.contract(minABI).at(omisego_token_address ); //working

// Call balanceOf function
contract.balanceOf(walletAddress, (error, balance) => {
  // Get decimals
  contract.decimals((error, decimals) => {
    // calculate a balance
    balance = balance.div(10**decimals);
    console.log(balance.toString());
  });
});

Why is my script returning 0 for Bitball?

5
  • 10**decimals > Number.MAX_SAFE_INTEGER Commented Aug 28, 2019 at 15:27
  • And note that in web3.js v1.x, BigNumber has been replaced with BN, which supports only integer values, so you cannot use div either (it will just truncate the result to 0). Commented Aug 28, 2019 at 15:30
  • Add bignumber.js or decimal.js to your package.json file, and use one of them instead. Commented Aug 28, 2019 at 15:31
  • Something like balance = new BigNumber(balance.toString() + "e-" + decimals.toString());. Commented Aug 28, 2019 at 15:32
  • @goodvibration thanks for your suggestion, I have added the bignumber CDN and your latest comment but I am still seeing a balance of 0. Before I format the number to be the correct decimal place, I receive a 0 value Commented Aug 28, 2019 at 15:44

1 Answer 1

2

Where did you get the 0x382117315856a533549ea621542ccce13e54ae82 address from?

If you click BitBall token in your token list, you'll notice that it leads to https://etherscan.io/token/0x06e0feb0d74106c7ada8497754074d222ec6bcdf?a=0xb8fd65fc6919c5b97453eaba6b895144597985b2

So the real token address is 0x06e0feb0d74106c7ada8497754074d222ec6bcdf

1
  • Oops! That makes sense, it seems like there are a few fake BTB tokens, that is frustrating Commented Aug 28, 2019 at 16:08

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.