简体   繁体   中英

Solidity get eth amount when removing uniswap liquidity

So I am removing some liquidity from a ERC20>ETH pool but I want to store the ETH amount that I get when removing the liquidity.

function removeETHLiquidityFromToken() public {

// approve token
IERC20(tokenAddressLP).approve(UNISWAP_ROUTER_ADDRESS, IERC20(tokenAddressLP).balanceOf(address(this)));

// remove liquidity
uniswapRouter.removeLiquidityETH(tokenAddress, IERC20(tokenAddressLP).balanceOf(address(this)), 0, 0, address(this), now + 20);

// I want to use the ETH value later in this function

}

This works perfectly fine, it removes the liquidity.

But how can I get that amount of ETH so I can use it after?

https://uniswap.org/docs/v2/smart-contracts/router02/#removeliquidityeth

The function returns two values. external returns (uint amountToken, uint amountETH);

Therefore you could identify a uint ethReceived; and assign the second uint returned to it like this:

( ,ethReceived) = uniswapRouter.removeLiquidityETH(tokenAddress, IERC20(tokenAddressLP).balanceOf(address(this)), 0, 0, address(this), now + 20);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM