2020-10-28 14:41:31 +00:00
|
|
|
// SPDX-License-Identifier: agpl-3.0
|
|
|
|
pragma solidity ^0.6.8;
|
2020-10-28 17:06:24 +00:00
|
|
|
pragma experimental ABIEncoderV2;
|
2020-10-28 14:41:31 +00:00
|
|
|
|
2020-11-02 16:01:47 +00:00
|
|
|
import {IWETH} from './interfaces/IWETH.sol';
|
|
|
|
import {IWETHGateway} from './interfaces/IWETHGateway.sol';
|
2020-10-28 14:41:31 +00:00
|
|
|
import {ILendingPool} from '../interfaces/ILendingPool.sol';
|
|
|
|
import {IAToken} from '../tokenization/interfaces/IAToken.sol';
|
|
|
|
import {ReserveLogic} from '../libraries/logic/ReserveLogic.sol';
|
|
|
|
import {ReserveConfiguration} from '../libraries/configuration/ReserveConfiguration.sol';
|
|
|
|
import {UserConfiguration} from '../libraries/configuration/UserConfiguration.sol';
|
|
|
|
import {Helpers} from '../libraries/helpers/Helpers.sol';
|
|
|
|
|
|
|
|
contract WETHGateway is IWETHGateway {
|
|
|
|
using ReserveConfiguration for ReserveConfiguration.Map;
|
|
|
|
using UserConfiguration for UserConfiguration.Map;
|
|
|
|
|
|
|
|
IWETH public immutable WETH;
|
2020-11-02 17:43:50 +00:00
|
|
|
ILendingPool public immutable POOL;
|
2020-11-03 12:45:54 +00:00
|
|
|
IAToken public immutable aWETH;
|
2020-10-28 14:41:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Sets the WETH address and the LendingPoolAddressesProvider address. Infinite approves lending pool.
|
2020-11-03 12:29:11 +00:00
|
|
|
* @param weth Address of the Wrapped Ether contract
|
|
|
|
* @param pool Address of the LendingPool contract
|
2020-10-28 14:41:31 +00:00
|
|
|
**/
|
2020-11-03 12:29:11 +00:00
|
|
|
constructor(address weth, address pool) public {
|
2020-11-03 12:45:54 +00:00
|
|
|
ILendingPool poolInstance = ILendingPool(pool);
|
2020-11-03 12:29:11 +00:00
|
|
|
WETH = IWETH(weth);
|
2020-11-03 12:45:54 +00:00
|
|
|
POOL = poolInstance;
|
|
|
|
aWETH = IAToken(poolInstance.getReserveData(weth).aTokenAddress);
|
2020-11-03 12:29:11 +00:00
|
|
|
IWETH(weth).approve(pool, uint256(-1));
|
2020-10-28 14:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev deposits WETH into the reserve, using native ETH. A corresponding amount of the overlying asset (aTokens)
|
|
|
|
* is minted.
|
|
|
|
* @param onBehalfOf address of the user who will receive the aTokens representing the deposit
|
|
|
|
* @param referralCode integrators are assigned a referral code and can potentially receive rewards.
|
|
|
|
**/
|
2020-11-02 17:43:50 +00:00
|
|
|
function depositETH(address onBehalfOf, uint16 referralCode) external override payable {
|
2020-10-28 14:41:31 +00:00
|
|
|
WETH.deposit{value: msg.value}();
|
2020-11-02 17:43:50 +00:00
|
|
|
POOL.deposit(address(WETH), msg.value, onBehalfOf, referralCode);
|
2020-10-28 14:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev withdraws the WETH _reserves of msg.sender.
|
2020-11-03 12:29:11 +00:00
|
|
|
* @param amount amount of aWETH to withdraw and receive native ETH
|
|
|
|
* @param to address of the user who will receive native ETH
|
2020-10-28 14:41:31 +00:00
|
|
|
*/
|
2020-11-03 12:29:11 +00:00
|
|
|
function withdrawETH(uint256 amount, address to) external override {
|
2020-11-03 12:45:54 +00:00
|
|
|
uint256 userBalance = aWETH.balanceOf(msg.sender);
|
2020-10-28 14:41:31 +00:00
|
|
|
uint256 amountToWithdraw = amount;
|
|
|
|
|
|
|
|
// if amount is equal to uint(-1), the user wants to redeem everything
|
|
|
|
if (amount == type(uint256).max) {
|
|
|
|
amountToWithdraw = userBalance;
|
|
|
|
}
|
2020-11-03 12:45:54 +00:00
|
|
|
aWETH.transferFrom(msg.sender, address(this), amountToWithdraw);
|
2020-11-02 17:43:50 +00:00
|
|
|
POOL.withdraw(address(WETH), amountToWithdraw, address(this));
|
2020-10-28 14:41:31 +00:00
|
|
|
WETH.withdraw(amountToWithdraw);
|
2020-11-03 12:29:11 +00:00
|
|
|
safeTransferETH(to, amountToWithdraw);
|
2020-10-28 14:41:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-28 17:06:24 +00:00
|
|
|
* @dev repays a borrow on the WETH reserve, for the specified amount (or for the whole amount, if uint256(-1) is specified).
|
|
|
|
* @param amount the amount to repay, or uint256(-1) if the user wants to repay everything
|
|
|
|
* @param rateMode the rate mode to repay
|
|
|
|
* @param onBehalfOf the address for which msg.sender is repaying
|
2020-10-28 14:41:31 +00:00
|
|
|
*/
|
|
|
|
function repayETH(
|
|
|
|
uint256 amount,
|
|
|
|
uint256 rateMode,
|
|
|
|
address onBehalfOf
|
|
|
|
) external override payable {
|
2020-11-02 16:01:47 +00:00
|
|
|
(uint256 stableDebt, uint256 variableDebt) = Helpers.getUserCurrentDebtMemory(
|
2020-10-28 14:41:31 +00:00
|
|
|
onBehalfOf,
|
2020-11-02 17:43:50 +00:00
|
|
|
POOL.getReserveData(address(WETH))
|
2020-10-28 14:41:31 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
uint256 paybackAmount = ReserveLogic.InterestRateMode(rateMode) ==
|
|
|
|
ReserveLogic.InterestRateMode.STABLE
|
|
|
|
? stableDebt
|
|
|
|
: variableDebt;
|
|
|
|
|
|
|
|
if (amount < paybackAmount) {
|
|
|
|
paybackAmount = amount;
|
|
|
|
}
|
|
|
|
require(msg.value >= paybackAmount, 'msg.value is less than repayment amount');
|
|
|
|
WETH.deposit{value: paybackAmount}();
|
2020-11-02 17:43:50 +00:00
|
|
|
POOL.repay(address(WETH), msg.value, rateMode, onBehalfOf);
|
2020-10-28 14:41:31 +00:00
|
|
|
|
2020-10-28 17:06:24 +00:00
|
|
|
// refund remaining dust eth
|
2020-10-28 14:41:31 +00:00
|
|
|
if (msg.value > paybackAmount) safeTransferETH(msg.sender, msg.value - paybackAmount);
|
|
|
|
}
|
|
|
|
|
2020-10-28 17:06:24 +00:00
|
|
|
/**
|
|
|
|
* @dev transfer ETH to an address, revert if it fails.
|
|
|
|
* @param to recipient of the transfer
|
|
|
|
* @param value the amount to send
|
|
|
|
*/
|
2020-10-28 14:41:31 +00:00
|
|
|
function safeTransferETH(address to, uint256 value) internal {
|
|
|
|
(bool success, ) = to.call{value: value}(new bytes(0));
|
|
|
|
require(success, 'ETH_TRANSFER_FAILED');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Only WETH contract is allowed to transfer ETH here. Prevent other addresses to send Ether to this contract.
|
|
|
|
*/
|
|
|
|
receive() external payable {
|
2020-11-03 12:29:11 +00:00
|
|
|
require(msg.sender == address(WETH), 'Receive not allowed');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Revert fallback calls
|
|
|
|
*/
|
2020-11-03 12:51:10 +00:00
|
|
|
fallback() external payable {
|
|
|
|
require(false, 'Fallback not allowed');
|
2020-10-28 14:41:31 +00:00
|
|
|
}
|
|
|
|
}
|