Ether
IWETH9 提取功能 - 這個功能將乙太幣發送到哪裡?
我編寫了以下函式,它將 DAI 交換回 WETH,然後提取 WETH 並返回 Ether。
在 REMIX 上執行該功能後,我在合約餘額中獲得了我想要的乙太幣,但我不明白為什麼。我應該很高興,但是當我查看 WETH9 提取函式的實現時,看起來應該將乙太幣發送到 msg.sender。所以我不明白為什麼它被發送到契約餘額?我錯過了什麼?
function _returnToEth() public payable { require(getTokenBalance(DAI) > 0, "No funds to be redistributed"); (bool _success, uint24 _poolFee) = _uniswapV3PoolExists(DAI, WETH); require(_success, "Pool does not exist"); uint _amountIn = getTokenBalance(DAI); ISwapRouter.ExactInputSingleParams memory params = ISwapRouter.ExactInputSingleParams({ tokenIn: DAI, tokenOut: WETH, fee: _poolFee, recipient: address(this), deadline: block.timestamp + 15, amountIn: _amountIn, amountOutMinimum: 1, sqrtPriceLimitX96: 0 }); // Approve the SwapRouter contract for the amount of DAI IERC20(DAI).approve(address(SwapRouter), _amountIn); SwapRouter.exactInputSingle(params); // Unwrap WETH to ETH and send to this contract uint balanceWETH = IWETH9(WETH).balanceOf(address(this)); IERC20(WETH).approve(address(this), balanceWETH); if (balanceWETH > 0) { IWETH9(WETH).withdraw(balanceWETH); }
當您執行時
IWETH9(WETH).withdraw(balanceWETH)
,您的合約正在呼叫 WETH 合約。所以對於 WETH 合約的上下文,你的合約是 msg.sender。這就是為什麼要向它發送資金的原因。