dsa-connectors/contracts/mainnet/connectors/aave/v2/main.sol

216 lines
7.4 KiB
Solidity
Raw Normal View History

2021-02-08 13:58:28 +00:00
pragma solidity ^0.7.0;
/**
* @title Aave v2.
* @dev Lending & Borrowing.
*/
2021-02-08 14:31:55 +00:00
import { TokenInterface } from "../../../common/interfaces.sol";
import { Stores } from "../../../common/stores.sol";
2021-02-08 13:58:28 +00:00
import { Helpers } from "./helpers.sol";
import { Events } from "./events.sol";
2021-02-08 14:31:55 +00:00
import { AaveInterface } from "./interface.sol";
2021-02-08 13:58:28 +00:00
2021-02-08 14:58:48 +00:00
abstract contract AaveResolver is Events, Helpers {
2021-02-08 13:58:28 +00:00
/**
* @dev Deposit ETH/ERC20_Token.
2021-03-24 12:23:36 +00:00
* @notice Deposit a token to Aave v2 for lending / collaterization.
* @param token The address of the token to deposit.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of the token to deposit. (For max: `uint256(-1)`)
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens deposited.
2021-02-08 13:58:28 +00:00
*/
function deposit(
address token,
2021-03-20 20:23:45 +00:00
uint256 amt,
uint256 getId,
uint256 setId
2021-02-08 13:58:28 +00:00
) external payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amt);
2021-02-08 14:31:55 +00:00
2021-02-08 13:58:28 +00:00
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
2021-02-08 14:31:55 +00:00
bool isEth = token == ethAddr;
address _token = isEth ? wethAddr : token;
TokenInterface tokenContract = TokenInterface(_token);
if (isEth) {
2021-02-08 13:58:28 +00:00
_amt = _amt == uint(-1) ? address(this).balance : _amt;
2021-02-08 14:31:55 +00:00
convertEthToWeth(isEth, tokenContract, _amt);
2021-02-08 13:58:28 +00:00
} else {
_amt = _amt == uint(-1) ? tokenContract.balanceOf(address(this)) : _amt;
}
2021-02-08 14:31:55 +00:00
tokenContract.approve(address(aave), _amt);
2021-02-08 13:58:28 +00:00
2021-02-08 14:31:55 +00:00
aave.deposit(_token, _amt, address(this), referralCode);
if (!getIsColl(_token)) {
aave.setUserUseReserveAsCollateral(_token, true);
}
2021-02-08 13:58:28 +00:00
setUint(setId, _amt);
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, getId, setId);
}
/**
* @dev Withdraw ETH/ERC20_Token.
2021-03-24 12:23:36 +00:00
* @notice Withdraw deposited token from Aave v2
* @param token The address of the token to withdraw.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of the token to withdraw. (For max: `uint256(-1)`)
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens withdrawn.
2021-02-08 13:58:28 +00:00
*/
function withdraw(
address token,
2021-03-20 20:23:45 +00:00
uint256 amt,
uint256 getId,
uint256 setId
2021-02-08 13:58:28 +00:00
) external payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amt);
2021-02-08 14:31:55 +00:00
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
bool isEth = token == ethAddr;
address _token = isEth ? wethAddr : token;
TokenInterface tokenContract = TokenInterface(_token);
uint initialBal = tokenContract.balanceOf(address(this));
aave.withdraw(_token, _amt, address(this));
uint finalBal = tokenContract.balanceOf(address(this));
2021-02-08 13:58:28 +00:00
_amt = sub(finalBal, initialBal);
2021-02-08 14:31:55 +00:00
convertWethToEth(isEth, tokenContract, _amt);
2021-02-08 13:58:28 +00:00
setUint(setId, _amt);
_eventName = "LogWithdraw(address,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, getId, setId);
}
/**
* @dev Borrow ETH/ERC20_Token.
2021-03-24 12:23:36 +00:00
* @notice Borrow a token using Aave v2
* @param token The address of the token to borrow.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of the token to borrow.
* @param rateMode The type of borrow debt. (For Stable: 1, Variable: 2)
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens borrowed.
2021-02-08 13:58:28 +00:00
*/
function borrow(
address token,
2021-03-20 20:23:45 +00:00
uint256 amt,
uint256 rateMode,
uint256 getId,
uint256 setId
2021-02-08 13:58:28 +00:00
) external payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amt);
2021-02-08 14:31:55 +00:00
2021-02-08 13:58:28 +00:00
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
2021-02-08 14:31:55 +00:00
bool isEth = token == ethAddr;
address _token = isEth ? wethAddr : token;
aave.borrow(_token, _amt, rateMode, referralCode, address(this));
convertWethToEth(isEth, TokenInterface(_token), _amt);
2021-02-08 13:58:28 +00:00
setUint(setId, _amt);
2021-02-08 14:31:55 +00:00
_eventName = "LogBorrow(address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, rateMode, getId, setId);
2021-02-08 13:58:28 +00:00
}
/**
* @dev Payback borrowed ETH/ERC20_Token.
2021-03-24 12:23:36 +00:00
* @notice Payback debt owed.
* @param token The address of the token to payback.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt The amount of the token to payback. (For max: `uint256(-1)`)
* @param rateMode The type of debt paying back. (For Stable: 1, Variable: 2)
* @param getId ID to retrieve amt.
* @param setId ID stores the amount of tokens paid back.
2021-02-08 13:58:28 +00:00
*/
function payback(
address token,
2021-03-20 20:23:45 +00:00
uint256 amt,
uint256 rateMode,
uint256 getId,
uint256 setId
2021-02-08 13:58:28 +00:00
) external payable returns (string memory _eventName, bytes memory _eventParam) {
uint _amt = getUint(getId, amt);
2021-02-08 14:31:55 +00:00
2021-02-08 13:58:28 +00:00
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
2021-02-08 14:31:55 +00:00
bool isEth = token == ethAddr;
address _token = isEth ? wethAddr : token;
2021-02-08 13:58:28 +00:00
2021-02-08 14:31:55 +00:00
TokenInterface tokenContract = TokenInterface(_token);
_amt = _amt == uint(-1) ? getPaybackBalance(_token, rateMode) : _amt;
if (isEth) convertEthToWeth(isEth, tokenContract, _amt);
tokenContract.approve(address(aave), _amt);
aave.repay(_token, _amt, rateMode, address(this));
2021-02-08 13:58:28 +00:00
setUint(setId, _amt);
2021-02-08 14:31:55 +00:00
_eventName = "LogPayback(address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(token, _amt, rateMode, getId, setId);
2021-02-08 13:58:28 +00:00
}
/**
* @dev Enable collateral
2021-03-24 12:23:36 +00:00
* @notice Enable an array of tokens as collateral
2021-02-08 13:58:28 +00:00
* @param tokens Array of tokens to enable collateral
*/
function enableCollateral(
address[] calldata tokens
) external payable returns (string memory _eventName, bytes memory _eventParam) {
uint _length = tokens.length;
require(_length > 0, "0-tokens-not-allowed");
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
for (uint i = 0; i < _length; i++) {
address token = tokens[i];
2021-02-08 14:31:55 +00:00
if (getCollateralBalance(token) > 0 && !getIsColl(token)) {
2021-02-08 13:58:28 +00:00
aave.setUserUseReserveAsCollateral(token, true);
}
}
2021-03-28 13:46:20 +00:00
_eventName = "LogEnableCollateral(address[])";
2021-02-08 13:58:28 +00:00
_eventParam = abi.encode(tokens);
}
2021-03-28 13:46:20 +00:00
/**
* @dev Swap borrow rate mode
* @notice Swaps user borrow rate mode between variable and stable
* @param token The address of the token to swap borrow rate.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param rateMode Desired borrow rate mode. (Stable = 1, Variable = 2)
*/
function swapBorrowRateMode(
address token,
uint rateMode
) external payable returns (string memory _eventName, bytes memory _eventParam) {
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
uint currentRateMode = rateMode == 1 ? 2 : 1;
if (getPaybackBalance(token, currentRateMode) > 0) {
aave.swapBorrowRateMode(token, rateMode);
}
_eventName = "LogSwapRateMode(address,uint256)";
_eventParam = abi.encode(token, rateMode);
}
2021-02-08 13:58:28 +00:00
}
2021-03-15 12:26:22 +00:00
contract ConnectV2AaveV2 is AaveResolver {
2021-03-20 20:23:45 +00:00
string constant public name = "AaveV2-v1";
2021-02-08 13:58:28 +00:00
}