I have a token contract which adds liquidity using addLiquidityETH. The LP Tokens are in the contract. Now, I need a function to remove the liquidity (to shake out MEV bots).
Now, i'm wondering why this codes works (even when i pass percent = 10000, which represents 100% of the LP tokens):
function removeLiquidity(uint256 percent) external onlyOwner {
// percent 5 = 0.05%
uint256 lpBalance = IERC20(lpPair).balanceOf(address(this));
uint256 lpAmount = lpBalance * percent / 10000;
IERC20(lpPair).approve(address(dexRouter), lpAmount);
dexRouter.removeLiquidityETH(
address(this),
lpAmount,
1, // slippage is unavoidable
1, // slippage is unavoidable
address(this),
block.timestamp
);
}
but this doesn't (returns TRANSFER_FROM_FAILED):
function removeLiquidity() external onlyOwner {
// percent 5 = 0.05%
uint256 lpBalance = IERC20(lpPair).balanceOf(address(this));
IERC20(lpPair).approve(address(dexRouter), lpBalance);
dexRouter.removeLiquidityETH(
address(this),
lpBalance,
1, // slippage is unavoidable
1, // slippage is unavoidable
address(this),
block.timestamp
);
}