-
Hi everyone, I created a test function to play around with adding and removing liquidity using the Router V2 contract on a forked ETH mainnet rpc. I observed that the amount of tokens I received on removing liquidity were less than what were added during the add liquidity call. Both calls are happening in the same transaction, so I'm assuming pool reserves ratio is also not changing. Also, I think there was no fees for both these calls. Any idea what could be the reason here? Test snippetaddress user = makeAddr("user");
uint256 INITIAL_ETH_BALANCE = 10 ether;
uint256 INITIAL_TOKEN_BALANCE = 10000e18;
function test_removeLiquidity() public {
deal(user, INITIAL_ETH_BALANCE);
deal(DAI, user, INITIAL_TOKEN_BALANCE);
vm.startPrank(user);
IERC20(DAI).approve(UNISWAP_V2_ROUTER_02, type(uint256).max);
(uint256 amountAs, uint256 amountBs, uint256 liquidity) = routerV2
.addLiquidityETH{value: 1 gwei}({
token: DAI,
amountTokenDesired: 3000e9,
amountTokenMin: 0,
amountETHMin: 0,
to: user,
deadline: block.timestamp
});
vm.stopPrank();
console.log("\n=== ADD LIQUIDIY ===");
console.log("DAI added", amountAs);
console.log("ETH added", amountBs);
console.log("Shares minted", liquidity);
uint256 startingEthBalance = user.balance;
uint256 startingDaiBalance = dai.balanceOf(user);
vm.startPrank(user);
wethDaiPair.approve(UNISWAP_V2_ROUTER_02, type(uint256).max);
// token, liquidity, amountTokenMin, amountETHMin, to, deadline
(uint256 amountAe, uint256 amountBe) = routerV2.removeLiquidityETH({
token: DAI,
liquidity: liquidity,
amountTokenMin: 0,
amountETHMin: 0,
to: user,
deadline: block.timestamp
});
vm.stopPrank();
uint256 endingEthBalance = user.balance;
uint256 endingDaiBalance = dai.balanceOf(user);
assertEq(wethDaiPair.balanceOf(user), 0);
assertEq(endingDaiBalance - startingDaiBalance, amountAe);
assertEq(endingEthBalance - startingEthBalance, amountBe);
console.log("\n=== REMOVE LIQUIDIY ===");
console.log("DAI returned", amountAe);
console.log("ETH returned", amountBe);
console.log("\n=== NET DIFFERENCE ===");
console.log("DAI difference", amountAs - amountAe);
console.log("ETH difference", amountBs - amountBe);
} Output
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
AMM pools round down numbers when liquidity is withdrawn. I think this is what you are seeing here |
Beta Was this translation helpful? Give feedback.
-
How you do that toggle feature? |
Beta Was this translation helpful? Give feedback.
AMM pools round down numbers when liquidity is withdrawn. I think this is what you are seeing here