I'm making a tutorial about how to create an AMM ( Automated Market Maker ), but I have a problem with the decimals. Both tokens have different decimals, and when I want to transfer them, the transaction will be wrong, because while 1 token of token 1 ( that is equal to 1 * 1e18 ) will be more than 1 token of token 2 ( that is equal to 1 * 1e6 ).

I'm asking if someone has knowledge about what is the correct way to handle this.

You can see the code here https://solidity-by-example.org/defi/constant-sum-amm/

  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented Dec 22, 2022 at 14:30

2 Answers 2


Look at how other contracts that deal with ERC20s do it, but you just have to call decimals() on that specific token and do all your calculations taking it into account, or ignore them altogether. Decimals are a concept that exists only for better UX. Uniswap doesnt even bother (https://github.com/Uniswap/v2-core/blob/master/contracts/UniswapV2Pair.sol)

  • I eas loking for a contract that deals with this but I don't find anything, but thats a god idea, i'm think take the lowest units and when i call the transfer function make a multiplication per the restand units. Commented Dec 22, 2022 at 17:35
  • Man i was loking for this contract for a long, in the uniswap V3 documentation don't show how it does the internal logic with the operations, only explains how to call the function that do it Commented Dec 22, 2022 at 17:41
  • That's v2 though, cause i'm not really familiar with v3, but its probably the same thing, all the calculations are made in wei anyways
    – Foxxxey
    Commented Dec 22, 2022 at 18:46
  • Yes, it is, thanks bro Commented Dec 22, 2022 at 19:24

If you want to change the decimal values of the tokens you want then you can directly inherit the function and override it to the required decimal value to return. Like just import and override the function.

  • Yes, but in this case a can't override the function decimals becouse the tokens that i want to implementate are ones of the must importan, just like Tether ( that actually uses 6 decimals ) , and another like dai that uses 18 decimals, and i wanna make it compatible with those Commented Dec 22, 2022 at 15:56
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Dec 22, 2022 at 23:00

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.