All crypto transactions are made in the smallest value - Wei. But to improve the user experience, on the frontend the transfer amounts are usually inputed in an
ETH style. Users want to input value
0.0001 TokenA instead of
100000000000000 Wei TokenA. Standard web3 methods are inconvenient for easy converting. The most convenient conversion method is to use the
Moralis.Units helper functions.
To convert ERC20 token to Wei, you need to specify the amount of tokens and number of decimals.
//Example: We want to convert 0.5 BUSD. It has 18 decimalsconst busdInWei = Moralis.Units.Token("0.5", "18")// expected output: 500000000000000000 Wei
The number of decimals for a token can normally be found on Etherscan, on the website of the project or from Moralis API.
To convert native asset to Wei, you need to specify the amount of native crypto.
//Example: We want to convert 0.5 ETH to Weiconst ethInWei = Moralis.Units.ETH("0.5")// expected output: 500000000000000000 Wei