1

I got this result from calling to a method in a ERC20 token I have deployed on a testnet.

contract MyToken is ERC20, ReentrancyGuard {}

calling method,

 let value = await ptrToken.balanceOf(wallet.address)

I want to convert the resultant big number to a string or number.

BigNumber { _hex: '0x021e19e0c9bab2400000', _isBigNumber: true }

I have tried the following three ways but, resulted 'undefined' as the output in nodejs.

1) const toEther = (_n) => {
    ethers.utils.formatUnits(_n, 'ether')
}

2) toTokens = (_n) => {
    ethers.utils.formatEther(_n)
}
3) const toEther = (_n) => {
    ethers.utils.formatUnits(_n.toString(), 'ether')
}

Passing the value to _n in both above functions resulted 'undefined'

DeFi
  • 77
  • 1
  • 8

2 Answers2

2

Try this

const theUsedAccount = await provider.send("eth_requestAccounts", [0]);
const balance =  await provider.getBalance(theUsedAccount[0])
console.log("account used",theUsedAccount)
console.log("balance",ethers.utils.formatEther(parseInt(balance._hex).toString()))
1

Can't you use BigInt ?

let bigVal = BigInt(value._hex);

//bigVal + 1n;
//bigVal > Number.MAX_SAFE_INTEGER

You can do all the normal comparisons.

ibrahim tanyalcin
  • 5,643
  • 3
  • 16
  • 22