I got it why we need decimals in ERC20s. But the thing I can't get it: When we are using transfer() method, should we write amount in wei (for ERC20s decimals count) or just integere. For example:

If we have a token with 3 decimals. I mint 100 of that token with mint(msg.sender, 100 * 10 ** 3). For transfer 5 tokens:

  1. transfer(_to, 5)
  2. transfer(_to, 5 * 10 ** 3)

Which one is true?


The second one is true.

You can check the OpenZeppelin implementation here.

  * For example, if `decimals` equals `2`, a balance of `505` tokens should
  * be displayed to a user as `5.05` (`505 / 10 ** 2`).

All computations are done on integers representing the value * 10 ** n_decimals. This is to avoid using floating point, rounding errors and so on...

So with 3 decimals :

To transfer 5 (displayed) tokens, you need to transfer 5 * 10 ** 3 tokens internally. Think of it as working with cents, thousandths or more inside your smart contract.

Just as if you were to transfer 1 ETH, you'd actually be transfering a value of 1000000000000000000 (Wei), or 10 ** 18 Wei, as ETH has 18 decimals.

  • Thanks! It is more clear now. Oct 31 '21 at 10:25

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.