dsa-polygon-migration/contracts/senders/aave-v2-migrator/helpers.sol

108 lines
4.0 KiB
Solidity
Raw Normal View History

pragma solidity >=0.7.0;
2021-04-07 23:45:00 +00:00
pragma experimental ABIEncoderV2;
import { DSMath } from "../../common/math.sol";
2021-04-07 23:45:00 +00:00
import { Stores } from "../../common/stores-mainnet.sol";
2021-04-07 00:22:28 +00:00
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
2021-04-07 23:45:00 +00:00
import { Variables } from "./variables.sol";
import {
AaveLendingPoolProviderInterface,
AaveDataProviderInterface,
AaveInterface,
2021-04-07 23:45:00 +00:00
ATokenInterface,
StateSenderInterface
} from "./interfaces.sol";
2021-04-07 23:45:00 +00:00
abstract contract Helpers is DSMath, Stores, Variables {
function _paybackBehalfOne(AaveInterface aave, address token, uint amt, uint rateMode, address user) private {
aave.repay(token, amt, rateMode, user);
}
function _PaybackStable(
uint _length,
AaveInterface aave,
address[] memory tokens,
uint256[] memory amts,
address user
) internal {
for (uint i = 0; i < _length; i++) {
if (amts[i] > 0) {
_paybackBehalfOne(aave, tokens[i], amts[i], 1, user);
}
}
}
function _PaybackVariable(
uint _length,
AaveInterface aave,
address[] memory tokens,
uint256[] memory amts,
address user
) internal {
for (uint i = 0; i < _length; i++) {
if (amts[i] > 0) {
_paybackBehalfOne(aave, tokens[i], amts[i], 2, user);
}
}
}
2021-04-07 23:45:00 +00:00
function _PaybackCalculate(AaveInterface aave, AaveDataRaw memory _data, address sourceDsa) internal returns (uint[] memory stableBorrow, uint[] memory variableBorrow, uint[] memory totalBorrow) {
2021-04-07 00:22:28 +00:00
for (uint i = 0; i < _data.borrowTokens.length; i++) {
2021-04-07 23:45:00 +00:00
require(isSupportedToken[_data.borrowTokens[i]], "token-not-enabled");
2021-04-07 00:22:28 +00:00
address _token = _data.borrowTokens[i] == ethAddr ? wethAddr : _data.borrowTokens[i];
_data.borrowTokens[i] = _token;
(
,
uint stableDebt,
uint variableDebt,
,,,,,
) = aaveData.getUserReserveData(_token, sourceDsa);
2021-04-07 00:22:28 +00:00
stableBorrow[i] = _data.stableBorrowAmts[i] == uint(-1) ? stableDebt : _data.stableBorrowAmts[i];
variableBorrow[i] = _data.variableBorrowAmts[i] == uint(-1) ? variableDebt : _data.variableBorrowAmts[i];
totalBorrow[i] = add(stableBorrow[i], variableBorrow[i]);
2021-04-07 00:22:28 +00:00
if (totalBorrow[i] > 0) {
2021-04-07 23:45:00 +00:00
IERC20(_token).approve(address(aave), totalBorrow[i]); // TODO: Approval is to Aave address of atokens address?
}
aave.borrow(_token, totalBorrow[i], 2, 3088, address(this)); // TODO: Borrowing debt to payback
}
}
2021-04-07 23:45:00 +00:00
function _getAtokens(address dsa, AaveInterface aave, address[] memory supplyTokens, uint[] memory supplyAmts) internal returns (uint[] memory finalAmts) {
for (uint i = 0; i < supplyTokens.length; i++) {
2021-04-07 23:45:00 +00:00
require(isSupportedToken[supplyTokens[i]], "token-not-enabled");
(address _aToken, ,) = aaveData.getReserveTokensAddresses(supplyTokens[i]);
ATokenInterface aTokenContract = ATokenInterface(_aToken);
2021-04-07 23:45:00 +00:00
uint _finalAmt;
if (supplyAmts[i] == uint(-1)) {
2021-04-07 23:45:00 +00:00
_finalAmt = aTokenContract.balanceOf(dsa);
} else {
2021-04-07 23:45:00 +00:00
_finalAmt = supplyAmts[i];
}
2021-04-07 23:45:00 +00:00
aTokenContract.transferFrom(dsa, address(this), finalAmts[i]);
_finalAmt = wmul(_finalAmt, fee);
finalAmts[i] = _finalAmt;
}
}
2021-04-07 23:45:00 +00:00
function isPositionSafe() internal returns (bool isOk) {
// TODO: Check the final position health
// @mubaris we need to add the function from Aave to check the health should be "safeRatioGap"(currently set to 20%) below liquidation
require(isOk, "position-at-risk");
}
function _checkRatio(AaveData memory data) public returns (bool isOk) {
// TODO: @mubaris Check the debt/collateral ratio should be less than "safeRatioGap" from Liquidation of that particular user assets
}
}