1

I need to remove an exact amount of token0 or token1 from a Uniswap V2 pair. To achieve this, I must calculate the amount of LP tokens to burn. The calculation formula is straightforward:

enter image description here

Where:

  • LPToRedeem: Amount of LP tokens to be burned to withdraw TokenAmountToWithdraw.
  • TokenAmountToWithdraw: Amount of token0 or token1 to be removed from the pair.
  • TotalTokenReserveInPair: Total amount of token0 or token1 balance in pair.
  • LPTotalSupply: Total supply of the LP tokens.

Despite the simplicity of the formula, implementing this in Solidity can be quite challenging due to precision loss issues. I've tried adding a small buffer amount to TokenAmountToWithdraw to ensure that the LPToRedeem calculated is slightly higher, thereby ensuring the token amount withdrawn is at least TokenAmountToWithdraw. However, determining the exact buffer amount to account for precision loss is complex.

One possible but inefficient solution is to withdraw all liquidity, remove the exact token0 or token1 amount, and then re-add the remaining liquidity. This method, however, is highly gas-inefficient and produces less readable transaction logs for researchers.

I'm looking for a more efficient way to precisely remove a specified amount of token0 or token1 from a Uniswap V2 pair without incurring significant precision loss or gas costs.

1 Answer 1

0

We can invert the actual formula adding a factor to the numerator like this:

LPToRedeem0 = (amount0*totalLP + reserve0 - 1)/reserve0;
LPToRedeem1 = (amount1*totalLP + reserve1 - 1)/reserve1;
LPToRedeem = max(LPToRedeem0, LPToRedeem1);

with reserve0 the TotalTokenReserveInPair for token0, and amount0 the TokenAmountToWithdraw for token0, etc..

LPToRedeem is what you are looking for, and I think this is the best you can do. At least the amount you withdraw is guaranteed to be above (amount0, amount1).

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.