1

When I'm working with 18 decimals. I have to convert the token inside of a function, for instance, one user want to transfer 20 token... I must operate that amount inside transfer function: amount*10^18? because I'm performing also transaction fees... And I don't know how to express the amount of fees: 1 or 1*10^18. The wallets always convert the amounts automatically when they see a global public variable decimals? Because I am doing tests on truffle, and when I´m working out transactions... the contract only send 50 tokens (not 50*10^18), so I have to perform web3.toWei in the entrance parameter.....

0

Normally your contract will only handle tokens with full precision (with 18 decimals in your case). This simplifies the programming as you can assume all parameters are in the same units and you do not need to convert.

The conversion and formatting is done the frontend-ui, where the use can select to express the value in different units. For example same wallets allow to express amounts in miliethers instead of ethers.

The same has to be done when you require input parameters. The frontend allows the user to enter the amount of tokens, 0.1 for example, and it will convert to 0.1 * 10^18 when sending the transaction to the contract.

0

Try formating the user input (example 1 ETH) to WEI (1000000000000000000)

var amount = $("#amounnttobuy").val();

var amount = new BigNumber(amount);
//convert from ETH/18 DECIMAL TOKEN to WEI
var amount = web3.toWei(amount, "ether")

Take time to read the web3.js documentation dear

https://github.com/ethereum/wiki/wiki/JavaScript-API

https://etherconverter.online/

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.