chore: internals inside helpers, add events.

This commit is contained in:
Shivva 2021-08-21 11:34:40 +02:00
parent fc2c597497
commit cd46f4fae8
3 changed files with 137 additions and 96 deletions

View File

@ -0,0 +1,20 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.7.6;
contract Events {
event LogSubmitProtection(
address indexed dsa,
address indexed action,
uint256 wantedHealthFactor,
uint256 minimumHealthFactor,
bool isPermanent
);
event LogUpdateProtection(
address indexed dsa,
address indexed action,
uint256 wantedHealthFactor,
uint256 minimumHealthFactor,
bool isPermanent
);
event LogCancelProtection(address indexed dsa, address indexed action);
}

View File

@ -1,7 +1,12 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.7.6;
pragma abicoder v2;
import {LendingPoolInterface, AaveServicesInterface} from "./interface.sol";
import {
LendingPoolInterface,
AaveServicesInterface,
IERC20
} from "./interface.sol";
abstract contract Helpers {
// solhint-disable-next-line const-name-snakecase
@ -15,4 +20,94 @@ abstract contract Helpers {
// solhint-disable-next-line const-name-snakecase
address internal constant _protectionAction =
0xc38b6dbd0F84777AA4fae2d36FE1506428A22b9B;
function _cancelProtection() internal {
_aaveServices.cancelTask(_protectionAction);
}
function _submitProtection(
uint256 _wantedHealthFactor,
uint256 _minimumHealthFactor,
bool _isPermanent
) internal {
_giveAllowance();
_aaveServices.submitTask(
_protectionAction,
abi.encode(
_wantedHealthFactor,
_minimumHealthFactor,
address(this)
),
_isPermanent
);
}
function _updateProtection(
uint256 _wantedHealthFactor,
uint256 _minimumHealthFactor,
bool _isPermanent
) internal {
_giveAllowance();
_aaveServices.updateTask(
_protectionAction,
abi.encode(
_wantedHealthFactor,
_minimumHealthFactor,
address(this)
),
_isPermanent
);
}
function _giveAllowance() internal {
address[] memory aTokenList = _getATokenList();
for (uint256 i = 0; i < aTokenList.length; i++) {
if (
!(IERC20(aTokenList[i]).allowance(
address(this),
_protectionAction
) == type(uint256).max)
) {
IERC20(aTokenList[i]).approve(
_protectionAction,
type(uint256).max
);
}
}
}
function _revokeAllowance() internal {
address[] memory aTokenList = _getATokenList();
for (uint256 i = 0; i < aTokenList.length; i++) {
if (
!(IERC20(aTokenList[i]).allowance(
address(this),
_protectionAction
) == 0)
) {
IERC20(aTokenList[i]).approve(_protectionAction, 0);
}
}
}
function _getATokenList()
internal
view
returns (address[] memory aTokenList)
{
address[] memory underlyingsList = _lendingPool.getReservesList();
aTokenList = new address[](underlyingsList.length);
for (uint256 i = 0; i < underlyingsList.length; i++) {
aTokenList[i] = (_lendingPool.getReserveData(underlyingsList[i]))
.aTokenAddress;
}
}
function _dsaHasProtection() internal view returns (bool) {
return
_aaveServices.taskByUsersAction(address(this), _protectionAction) !=
bytes32(0);
}
}

View File

@ -1,17 +1,15 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.7.6;
pragma abicoder v2;
/**
* @title Aave Protection.
* @dev Protect DSA against Liquidation risk on Aave with Gelato.
*/
import {IERC20} from "./interface.sol";
import {Events} from "./events.sol";
import {Helpers} from "./helpers.sol";
abstract contract GAaveProtectionResolver is Helpers {
abstract contract GAaveProtectionResolver is Events, Helpers {
/// @dev Function for submitting a protection task
/// @param _wantedHealthFactor targeted health after protection.
/// @param _minimumHealthFactor trigger protection when current health
@ -27,6 +25,13 @@ abstract contract GAaveProtectionResolver is Helpers {
_minimumHealthFactor,
_isPermanent
);
emit LogSubmitProtection(
address(this),
_protectionAction,
_wantedHealthFactor,
_minimumHealthFactor,
_isPermanent
);
}
/// @dev Function for modifying a protection task
@ -44,109 +49,30 @@ abstract contract GAaveProtectionResolver is Helpers {
_minimumHealthFactor,
_isPermanent
);
emit LogUpdateProtection(
address(this),
_protectionAction,
_wantedHealthFactor,
_minimumHealthFactor,
_isPermanent
);
}
/// @dev Function for cancelling a protection task
function cancelProtection() external payable {
_cancelProtection();
emit LogCancelProtection(address(this), _protectionAction);
}
/// @dev Function for cancelling and removing allowance
/// of aToken to _protectionAction
function cancelAndRevoke() external payable {
if (_dsaHasProtection()) _cancelProtection();
if (_dsaHasProtection()) {
_cancelProtection();
emit LogCancelProtection(address(this), _protectionAction);
}
_revokeAllowance();
}
function _submitProtection(
uint256 _wantedHealthFactor,
uint256 _minimumHealthFactor,
bool _isPermanent
) internal {
_giveAllowance();
_aaveServices.submitTask(
_protectionAction,
abi.encode(
_wantedHealthFactor,
_minimumHealthFactor,
address(this)
),
_isPermanent
);
}
function _updateProtection(
uint256 _wantedHealthFactor,
uint256 _minimumHealthFactor,
bool _isPermanent
) internal {
_giveAllowance();
_aaveServices.updateTask(
_protectionAction,
abi.encode(
_wantedHealthFactor,
_minimumHealthFactor,
address(this)
),
_isPermanent
);
}
function _cancelProtection() internal {
_aaveServices.cancelTask(_protectionAction);
}
function _giveAllowance() internal {
address[] memory aTokenList = _getATokenList();
for (uint256 i = 0; i < aTokenList.length; i++) {
if (
!(IERC20(aTokenList[i]).allowance(
address(this),
_protectionAction
) == type(uint256).max)
) {
IERC20(aTokenList[i]).approve(
_protectionAction,
type(uint256).max
);
}
}
}
function _revokeAllowance() internal {
address[] memory aTokenList = _getATokenList();
for (uint256 i = 0; i < aTokenList.length; i++) {
if (
!(IERC20(aTokenList[i]).allowance(
address(this),
_protectionAction
) == 0)
) {
IERC20(aTokenList[i]).approve(_protectionAction, 0);
}
}
}
function _getATokenList()
internal
view
returns (address[] memory aTokenList)
{
address[] memory underlyingsList = _lendingPool.getReservesList();
aTokenList = new address[](underlyingsList.length);
for (uint256 i = 0; i < underlyingsList.length; i++) {
aTokenList[i] = (_lendingPool.getReserveData(underlyingsList[i]))
.aTokenAddress;
}
}
function _dsaHasProtection() internal view returns (bool) {
return
_aaveServices.taskByUsersAction(address(this), _protectionAction) !=
bytes32(0);
}
}
contract GAaveProtectionPolygonConnector is GAaveProtectionResolver {