0

I'm trying to implement the question in a web3js app I have.

The code I have so far is:

// Get ERC20 Token contract instance
let contract = new Web3Client.eth.Contract(minABI, tokenAddress);

// Call balanceOf function
contract.methods.balanceOf(walletAddress).call((error, balance) => {
  contract.methods.decimals().call((error, decimals) => {
    balance = Web3Client.utils.toBN(balance);
    balance = balance.divn(10 ** decimals).toString();

  });
});

This gives me an error:

if (!val) throw new Error(msg || 'Assertion failed');
              ^

Error: Assertion failed
    at assert (/home/noah/Dev/ERC20Tutorial/node_modules/number-to-bn/node_modules/bn.js/lib/bn.js:6:21)
    at BN.idivn (/home/noah/Dev/ERC20Tutorial/node_modules/number-to-bn/node_modules/bn.js/lib/bn.js:2471:5)
    at BN.divn (/home/noah/Dev/ERC20Tutorial/node_modules/number-to-bn/node_modules/bn.js/lib/bn.js:2484:25)
    at Object.callback (/home/noah/Dev/ERC20Tutorial/index.js:43:23)
    at sendTxCallback (/home/noah/Dev/ERC20Tutorial/node_modules/web3-core-method/lib/index.js:533:29)
    at /home/noah/Dev/ERC20Tutorial/node_modules/web3-core-requestmanager/lib/index.js:308:9
    at XMLHttpRequest.request.onreadystatechange (/home/noah/Dev/ERC20Tutorial/node_modules/web3-providers-http/lib/index.js:98:13)
    at XMLHttpRequestEventTarget.dispatchEvent (/home/noah/Dev/ERC20Tutorial/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:34:22)
    at XMLHttpRequest._setReadyState (/home/noah/Dev/ERC20Tutorial/node_modules/xhr2-cookies/dist/xml-http-request.js:208:14)
    at XMLHttpRequest._onHttpResponseEnd (/home/noah/Dev/ERC20Tutorial/node_modules/xhr2-cookies/dist/xml-http-request.js:318:14)
    at IncomingMessage.<anonymous> (/home/noah/Dev/ERC20Tutorial/node_modules/xhr2-cookies/dist/xml-http-request.js:289:61)
    at IncomingMessage.emit (node:events:377:35)
    at endReadableNT (node:internal/streams/readable:1312:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

Curious as I don't know what would be causing the issue. There are a few different tutorials that walk through this, but none of them are using current web3 versions.

2
  • How are you assigning the walletAddress variable? Mind the format differences: "0xAB123…" vs. "AB123" vs. 0xAB123.
    – FRAGA
    May 24 at 2:57
  • @FRAGA It's very much a toy app. This code snippet was to be used in an article I'm writing. So WalletAddress is just a wallet address in a string that's hardcoded. so it's in the "0xab123" format. The balance is shown accurately, the error only occurs when I try to perform math on it.
    – NJTech
    May 25 at 5:18
0

If you just want to just get the balance, then all you have to do is:

contract.methods.balanceOf(walletAddress)
  .call((error, balance) => {
    console.log(web3.utils.fromWei(balance));
    // Do whatever else you want with the code.
  }

Since .balanceOf() returns the account balance in Wei, you have to use web3.utils.fromWei() to convert it to your desired unit amount.

In the example above, I passed in only the balance as an argument without specifying what unit to convert to. This is because the default conversion value for the 2nd parameter is "ether", so if you wanted to convert to Ether, you can just pass in the first argument, the balance.

Or you can do it a cleaner way like this:

contract.methods.balanceOf(walletAddress)
  .call()
  .then(balance => console.log(web3.utils.fromWei(balance));

Or (the cleanest way):

async function getBalance() {
  const balance = await contract.methods.balanceOf(walletAddress).call();
  const balanceInWei = web3.utils.fromWei(balance);
  console.log(balanceInWei);
}

getBalance();

As far as the error, it seems to be an issue with the part of the code where you used

.divn(10 ** decimals)

Hope this helped! :)

4
  • so this would return me 29803630997051883414242659 with the particular wallet and token I'm using. I'm looking to have it formatted. I would like it to return 29803630.997051883414242659
    – NJTech
    May 25 at 5:33
  • After toying around with this a bit I can see there is a function that will do this for me in the utils library, and I was doing a bit extra. I can just use the web3.utils.fromWei() on the balance to get it formatted correctly. I still don't quite understand why my solution wouldn't work, as I don't see how it breaks any of the BN functionality. If you change your answer to include this portion I'll mark it as answered!
    – NJTech
    May 25 at 5:40
  • Sorry just saw this. Whenever you get the balance from an address it returns it in wei, so yes, you would have to use web3.utils.fromWei() to convert it to your desired unit (ether). I can't see the full code you wrote so I don't know why it broke. However, the error message does say the problem seems to be coming from ".divn(10 ** decimals)" you wrote. I see you wrote your web3 instance as "Web3Client", the conventional way and recommended since it's easier to read and less code is just calling it "web3" instead.
    – Adrian D.
    May 26 at 6:11
  • I revised my answer. Would be awesome if you gave it a thumbs up and marked it as answered! :D One of my first times helping, hope I was able to help you! (Took a lot of work tbh lol)
    – Adrian D.
    May 26 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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