balanceOf giving wrong value on Binance chain using Ethers.js

I just started with blockchain programming, I tried experimenting with ethers.js and BSC. While using Token_contract.balanceOf("address") some contacts are giving wrong values, Example correct value is 34.81 while contract returns .000000000034818073, nine zeros added before real value .I am getting this value after correcting the format Wei to Eth const balanceFormatted = ethers.utils.formatUnits(tokholdings, 18); however most of the contract gives exact values after format corrected What could be the reason ?

I am using same abi for all contacts .

const Genabi = [ "constructor(string symbol, string name)", "function transferFrom(address from, address to, uint value)", "function balanceOf(address owner) view returns (uint balance)", "event Transfer(address indexed from, address indexed to, address value)" ];

HFS /FEG contracts have this issue. While fomo /waultx gives correct balance

Answers 1

  • You should use decimals() to format the correct balance. Most tokens use 18 (just as ETH) but some of them use 6 , 5 or any other is also possible. So, prior to get balanceof() you should call decimals() so you know how to interpret the output of balanceof()

Related Questions