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

184 lines
6.3 KiB
Solidity
Raw Normal View History

2021-02-08 13:58:28 +00:00
pragma solidity ^0.7.0;
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.
* @param token token address to deposit.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt token amount to deposit.
* @param getId Get token amount at this ID from `InstaMemory` Contract.
* @param setId Set token amount at this ID in `InstaMemory` Contract.
*/
function deposit(
address token,
uint amt,
uint getId,
uint setId
) 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.
* @param token token address to withdraw.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt token amount to withdraw.
* @param getId Get token amount at this ID from `InstaMemory` Contract.
* @param setId Set token amount at this ID in `InstaMemory` Contract.
*/
function withdraw(
address token,
uint amt,
uint getId,
uint setId
) 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.
* @param token token address to borrow.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt token amount to borrow.
2021-02-08 14:31:55 +00:00
* @param rateMode type of borrow debt.(For Stable: 1, Variable: 2)
2021-02-08 13:58:28 +00:00
* @param getId Get token amount at this ID from `InstaMemory` Contract.
* @param setId Set token amount at this ID in `InstaMemory` Contract.
*/
function borrow(
address token,
uint amt,
2021-02-08 14:31:55 +00:00
uint rateMode,
2021-02-08 13:58:28 +00:00
uint getId,
uint setId
) 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.
* @param token token address to payback.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param amt token amount to payback.
2021-02-08 14:31:55 +00:00
* @param rateMode type of borrow debt.(For Stable: 1, Variable: 2)
2021-02-08 13:58:28 +00:00
* @param getId Get token amount at this ID from `InstaMemory` Contract.
* @param setId Set token amount at this ID in `InstaMemory` Contract.
*/
function payback(
address token,
uint amt,
2021-02-08 14:31:55 +00:00
uint rateMode,
2021-02-08 13:58:28 +00:00
uint getId,
uint setId
) 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
* @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);
}
}
_eventName = "LogEnableCollateral(address[]);";
_eventParam = abi.encode(tokens);
}
}
2021-03-15 12:26:22 +00:00
contract ConnectV2AaveV2 is AaveResolver {
2021-03-15 12:56:34 +00:00
string public name = "AaveV2-v1";
2021-02-08 13:58:28 +00:00
}