diff --git a/contracts/misc/WETHGateway.sol b/contracts/misc/WETHGateway.sol index 336e8de2..1ddceaa0 100644 --- a/contracts/misc/WETHGateway.sol +++ b/contracts/misc/WETHGateway.sol @@ -131,6 +131,37 @@ contract WETHGateway is IWETHGateway, Ownable { _safeTransferETH(msg.sender, amount); } + /** + * @dev withdraws the WETH _reserves of msg.sender. + * @param lendingPool address of the targeted underlying lending pool + * @param amount amount of aWETH to withdraw and receive native ETH + * @param to address of the user who will receive native ETH + */ + function withdrawETHWithPermit( + address lendingPool, + uint256 amount, + address to, + uint256 deadline, + uint8 permitV, + bytes32 permitR, + bytes32 permitS + ) external override { + IAToken aWETH = IAToken(ILendingPool(lendingPool).getReserveData(address(WETH)).aTokenAddress); + uint256 userBalance = aWETH.balanceOf(msg.sender); + uint256 amountToWithdraw = amount; + + // if amount is equal to uint(-1), the user wants to redeem everything + if (amount == type(uint256).max) { + amountToWithdraw = userBalance; + } + // chosing to permit `amount`and not `amountToWithdraw`, easier for frontends, intregrators. + aWETH.permit(msg.sender, address(this), amount, deadline, permitV, permitR, permitS); + aWETH.transferFrom(msg.sender, address(this), amountToWithdraw); + ILendingPool(lendingPool).withdraw(address(WETH), amountToWithdraw, address(this)); + WETH.withdraw(amountToWithdraw); + _safeTransferETH(to, amountToWithdraw); + } + /** * @dev transfer ETH to an address, revert if it fails. * @param to recipient of the transfer