1

I am creating an ERC20 token and have specified decimal as 2 since the token can have values like 10.32

When I minted 1000 coins using smart contract invocation from my dapp, I got 10 coins in my wallet. Shall I assume that in mint function inside smart contract, I should multiple amount with 100 (due to 2 decimals) before actual number of coins are minted? Or is there another way to handle this?

Update: I invoked balanceOf method and it returns the balance correctly as 1000 without any conversion but in my dapp, it shows as 10.

2 Answers 2

2

Since EVM cannot handle decimals, you need to specify decimals. So when you say your token has 2 decimals, this means the min unit of your token exists is 0.01.

So the value you say Mint amount is 1000 (it actually 1000/(10^2)).

In short, if you need to send/transfer/mint n tokens you have to pass n*(10^decimals).

In your case, if you want to mint 1000 tokens you have to pass 1000 * 100 to your function parameter.

6
  • When you say function parameter, do you mean smart contract method which mints the coin or the frontend where user is asked how many coins need to be minted? I am trying to understand where exactly this conversion need to be done - in smart contracts or outside?
    – eth.block
    Feb 2, 2018 at 12:58
  • When I mean function param, I mean the contract function. You can modify your frontend in a way that when user enters a value n you make it 100*n (as you have 2 decimal places) as your UI can take decimal values. But your smart contract should get 100*n. Feb 2, 2018 at 16:58
  • Thanks for the clarification. One last thing, when I use balanceOf method of ERC20, I get the correct amount of tokens that I intended to mint but MetaMask wallet shows balance as N / 10^2. Why this discrepancy?
    – eth.block
    Feb 3, 2018 at 13:28
  • This is because some wallets or I should say UI already implements this. So when you want to mint n tokens you multiply it by 10^decimasl and when you retrieve balance you must see n that is why most wallets divide the balance you receive by 10^decimals Feb 3, 2018 at 14:11
  • Since Solidity doesn't support floating numbers, how do we handle them. For example, in my case token amounts could be 75.62 or 1015.50 but when they transfer function is called with these amounts, smart contract converts this to integer (I assume, to 75 and 1015 respectively). How to make that work?
    – eth.block
    Feb 9, 2018 at 3:59
0

The ether blockchain uses unsigned integer (uint) which doesn't accept decimals. So you need to mention the number of decimal places in your token. based on that the ether blockchain will take the decimal places from the last digit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.