i'm trying to create a smart contract which removes user's lp share from the pool doing these steps:
- calculates the current user's balance of LP tokens for a particular pair
- removes their liquidity from the pool and sends all the assets back to them
contract UniswapLiquidity {
address UniswapV2Router = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
address UniswapV2Factory = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f;
address WETH = 0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6;
event Logs(string message, uint value);
function removeLiquidity(
address _tokenA,
address _tokenB
) external returns (address pair) {
//get pair address
pair = IUniswapV2Factory(UniswapV2Factory).getPair(_tokenA, _tokenB);
uint liquidity = IERC20(pair).balanceOf(msg.sender);
//approve router to remove our liquidity token
require(IERC20(pair).approve(address(this), liquidity));
require(IERC20(pair).approve(UniswapV2Router, liquidity));
(uint amountA, uint amountB) = IUniswapV2Router02(UniswapV2Router)
.removeLiquidity(
_tokenA,
_tokenB,
liquidity,
1001, // 0, 1 or a calculation dont make any sense
1001,
msg.sender,
block.timestamp + 20 seconds
);
emit Logs("amount A gotten", amountA);
emit Logs("amount B gotten", amountB);
emit Logs("Liquidity token burnt", liquidity);
}
}
but im getting execution reverted due to ds-math-sub-underflow, though i tried different solutions, like extra approves and attempting to remove only 10% of the share using safemath, it didnt work out
IUniswapV2Pair(pair).transferFrom(msg.sender, pair, liquidity); // send liquidity to pair
whilemsg.sender
is being actually the smart contract's address instead of the user's wallet