0

I have created ERC20 token with 18 decimals. From web3 (application front end) when I transfer 100 tokens, on etherscan it shows 0.0000000000000001 . When I call to get the balance then on UI it shows 100 tokens. How I can fix this?

Method signature: function transfer(address _to, uint256 _value) public returns (bool) { }

The following is a transaction link for reference.

https://kovan.etherscan.io/tx/0x7b50a2f1a013a0b15eb702f99d09d9a1c16a39bfa179329e246b6ba5b2318b12

1 Answer 1

0

When I call to get the balance then on UI it shows 100 tokens.

No, it shows you 100 base units of your token, and since your token decimal-resolution is 18, this is equivalent to 100 / 10 ^ 18 (0.0000000000000001) tokens.


In order to fix this in your web3-based application, replace 100 with "100e18".

If you have decimals at hand, then you can also use:

`100e${decimals}`

More generally, you should:

  • Take input x that you receive from the user, and pass to the contract function:
`${x}e+${decimals}`
  • Take output x that you receive from the contract function, and pass to the user:
`${x}e-${decimals}`
1
  • 1
    Side note for future readers: Truffle 4.x, which relies on BigNumber, could handle stuff like "100e18". Truffle 5.x, which relies on BN, cannot. Instead, you should do something like new BN(100).mul(new BN(10).pow(new BN(18))), or simply new BN(10).pow(new BN(20)). Nov 17, 2020 at 18:43

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.