I have two tokens, tokenA which has a decimal value of 18 and tokenB with 9 decimals. I would like to buy tokenB for tokenA or vica-versa. The question is that do I need to make any conversions with the wei values because the different decimals in Solidity? I know that decimals are important when we convert from wei to ether for instance, but if I keep both token amounts in wei, I assume the below calculation will be correct without managing the two amount variable differently.

// calculating the output amount of DAI for X ETH
address[] memory path;
path[0] = token18Dec; // ETH
path[1] = token9Dec; // DAI token with 9 decimals
amounts = uniswapRouter.getAmountsOut(ethBudget, path)[1];

// calculating the output amount of ETH for X DAI
address[] memory path;
path[0] = token9Dec; // ETH
path[1] = token18Dec; // DAI token with 9 decimals
amounts = uniswapRouter.getAmountsOut(daiTokenBalance, path)[1];

1 Answer 1


Yes, you should convert both to the Wei as solidity by default deals any integer as in Wei

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.