Skip to main content
added 4 characters in body
Source Link
Kof
  • 3k
  • 8
  • 27

The fromWei function converts from wei to ether, meaning it divides the number by 1e18.

ERC20 tokens allows a custom decimal point precision, USDC specifically uses 6 decimal precision. So you just need to divide your result by 1e6 -

const balance = parseInt(result) / 6;10**6;

But ideally, to support any ERC20 token, read the number of decimals from the contract and use that; also better use a library that knows how to work with big numbers, because JS has issues when it comes to big/real numbers.

async function getBalance() {
   const result = await contract.methods.balanceOf(walletAddress).call();
   const decimals = await contract.methods.decimals().call();
   const balance = new BigNumber(result).div(10 ** decimals);
   return balance;
}

The fromWei function converts from wei to ether, meaning it divides the number by 1e18.

ERC20 tokens allows a custom decimal point precision, USDC specifically uses 6 decimal precision. So you just need to divide your result by 1e6 -

const balance = parseInt(result) / 6;

But ideally, to support any ERC20 token, read the number of decimals from the contract and use that; also better use a library that knows how to work with big numbers, because JS has issues when it comes to big/real numbers.

async function getBalance() {
   const result = await contract.methods.balanceOf(walletAddress).call();
   const decimals = await contract.methods.decimals().call();
   const balance = new BigNumber(result).div(10 ** decimals);
   return balance;
}

The fromWei function converts from wei to ether, meaning it divides the number by 1e18.

ERC20 tokens allows a custom decimal point precision, USDC specifically uses 6 decimal precision. So you just need to divide your result by 1e6 -

const balance = parseInt(result) / 10**6;

But ideally, to support any ERC20 token, read the number of decimals from the contract and use that; also better use a library that knows how to work with big numbers, because JS has issues when it comes to big/real numbers.

async function getBalance() {
   const result = await contract.methods.balanceOf(walletAddress).call();
   const decimals = await contract.methods.decimals().call();
   const balance = new BigNumber(result).div(10 ** decimals);
   return balance;
}
Source Link
Kof
  • 3k
  • 8
  • 27

The fromWei function converts from wei to ether, meaning it divides the number by 1e18.

ERC20 tokens allows a custom decimal point precision, USDC specifically uses 6 decimal precision. So you just need to divide your result by 1e6 -

const balance = parseInt(result) / 6;

But ideally, to support any ERC20 token, read the number of decimals from the contract and use that; also better use a library that knows how to work with big numbers, because JS has issues when it comes to big/real numbers.

async function getBalance() {
   const result = await contract.methods.balanceOf(walletAddress).call();
   const decimals = await contract.methods.decimals().call();
   const balance = new BigNumber(result).div(10 ** decimals);
   return balance;
}