I want to use number as normal integer, not in decimal.

For example, when I use uniswap function IUniswapV2Router02Instance.methods.swapETHForExactTokens(1, , , ).send() in my project, an argument integer (1) is recognized as decimal (0.000000000000000001) .

This is the transaction on etherscan. https://ropsten.etherscan.io/tx/0x14d16174a7b12c1a77356b60b1fca7385c7cad583991d70b8a4423faf6198e43

Does anyone know why this problem happens and how to solve, please?

  • Yes, 1 = 1 wei. You need to use "1" followed by zeros, according to the number of decimals configured in your ERC20 token (which is 18 in most cases). – goodvibration Jul 17 '20 at 5:11
  • 1
    This number might be larger than Number.MAX_SAFE_INTEGER, so pass it as a string just to be on the safe side. – goodvibration Jul 17 '20 at 5:13
  • Thank you! How can I pass string to uint256 ? – gnxvw Jul 17 '20 at 5:21
  • 1
    web3.js takes care of that. – goodvibration Jul 17 '20 at 5:22
  • 1
    You can use Web3.utils.toBN(10).pow(decimals) I suppose. – goodvibration Jul 17 '20 at 5:35

In wallets and other Dapp interfaces different tokens and assets are displayed as having decimals. Since the EVM doesn't really support normal decimal datatypes such as floats for example what most ERC20 tokens do is they use integers and instead define a number of decimals (usually 18).

So when you receive 1 BAT for example the smart contract will really register it as you having 1 * 10^18 BAT just that the wallet displays it as 1 since the BAT smart contract defines its decimals as being 18. The same goes for Ether.

With Ether the base unit even has a name: wei, which is equivalent to 1 * 10^-18 ETH or 0.000000000000000001 ETH. So on a low level when you send tokens or ETH using code it treats the value as if it were in the base unit, in the case of ETH wei so when you send 1 unit of ether you are really sending 1 wei. Now 1 wei displayed in your wallet appears as a tiny decimal fraction. Hope that cleared things up!

  • Thank you so much ! I understood – gnxvw Jul 20 '20 at 0:47

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.