In the Exchange contract of Uniswap v1, the addLiquidity function involves calculating token_amount using the formula

token_amount: uint256 = msg.value * token_reserve / eth_reserve + 1

Why is there a + 1 in the code? Should it not be written as below?

token_amount: uint256 = msg.value * token_reserve / eth_reserve

The expression msg.value * token_reserve / eth_reserve is the floor of the real amount.

So msg.value * token_reserve / eth_reserve + 1 is most likely intended to be the ceiling of it.

BTW, it's not an ideal way for computing ceiling.

When msg.value * token_reserve is divisible by eth_reserve, it actually gives 'ceiling plus 1'.

IMO, they should have used (msg.value * token_reserve - 1) / eth_reserve + 1.

But the 1 stands for '1 wei', so it's probably not that critical either way...

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.