2

whats the correct way of setting the totalSupply and why?

decimals = 18;
totalSupply = 10000

or

decimals = 18;
totalSupply = 10000 * (10 ** uint256(decimals));

Whats the correct way? What's the difference? What is (10 ** uint256(decimals)) actually doing?

Seems like the last option is the correct one, because if I dont use the second option, my tokens in Metamask connected to Ganache will show always 0 balance.

Also

10000 === 10000 * (10 ** uint256(decimals)) ?

Also another question, how can I call the methods transfer or transferFrom with a decimal value, sending 2.5 tokens for example? How would that be done, since the type is uint and I cant pass decimals.

1 Answer 1

6

The second way is correct. In Solidity, ** is the exponentiation operator, so 10 ** 18 is the same as 1018. So this:

uint256 totalSupply = 10000 * (10 ** uint256(decimals));

Means the total supply will be 10,000 × 1018 tokens. We use 10n because Solidity doesn´t (fully) support decimal numbers, only integers. In wallets, dApps, etc. this number is converted back to the actual balance with decimals.

To specify 2.5 tokens, for example, you would use 2.5 × 1018 (= 2,500,000,000,000,000,000) instead. You can reverse it by multiplying with 10-18 instead.

2
  • 2
    So, the supply is actually 10000, but its represented as 10,000 × 10^18 due to decimals, and the wallet apps make to divide the amount to display the correct supply of (10000).
    – redigaffi
    Commented Apr 1, 2021 at 12:05
  • Yes, that´s correct.
    – Morten
    Commented Apr 1, 2021 at 12:08

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.