Skip to content

Commit

Permalink
add cooldown() external (#25)
Browse files Browse the repository at this point in the history
* add cooldown() external

* simplifying claimRewards call

* adding tests on the strategies

* fixing lint

* adding sweep tests

* renaming some elements

* fixing some strategies

* fixing failing tests

Co-authored-by: Pablo Veyrat <[email protected]>
  • Loading branch information
teddav and sogipec authored May 12, 2022
1 parent a8604a3 commit 4130d25
Show file tree
Hide file tree
Showing 12 changed files with 794 additions and 591 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ contract AaveFlashloanStrategy is BaseStrategyUpgradeable, IERC3156FlashBorrower
IAToken private _aToken;
IVariableDebtToken private _debtToken;

// ================================== Errors ===================================

error ErrorSwap();
error InvalidSender();
error InvalidSetOfParameters();
error InvalidWithdrawCheck();
error TooSmallAmountOut();
error TooHighParameterValue();

// ============================ Initializer ====================================

/// @notice Constructor of the `Strategy`
Expand Down Expand Up @@ -363,7 +372,7 @@ contract AaveFlashloanStrategy is BaseStrategyUpgradeable, IERC3156FlashBorrower
}

if (boolParams.withdrawCheck) {
require(_amountNeeded == _liquidatedAmount + _loss, "54"); // dev: withdraw safety check
if (_amountNeeded != _liquidatedAmount + _loss) revert InvalidWithdrawCheck(); // dev: withdraw safety check
}
}

Expand All @@ -386,14 +395,13 @@ contract AaveFlashloanStrategy is BaseStrategyUpgradeable, IERC3156FlashBorrower
) external onlyRole(GUARDIAN_ROLE) {
(uint256 ltv, uint256 liquidationThreshold) = _getProtocolCollatRatios(address(want));
(uint256 daiLtv, ) = _getProtocolCollatRatios(_dai);
require(
_targetCollatRatio < liquidationThreshold &&
_maxCollatRatio < liquidationThreshold &&
_targetCollatRatio < _maxCollatRatio &&
_maxBorrowCollatRatio < ltv &&
_daiBorrowCollatRatio < daiLtv,
"8"
);
if (
_targetCollatRatio >= liquidationThreshold ||
_maxCollatRatio >= liquidationThreshold ||
_targetCollatRatio >= _maxCollatRatio ||
_maxBorrowCollatRatio >= ltv ||
_daiBorrowCollatRatio >= daiLtv
) revert InvalidSetOfParameters();

targetCollatRatio = _targetCollatRatio;
maxCollatRatio = _maxCollatRatio;
Expand All @@ -407,7 +415,8 @@ contract AaveFlashloanStrategy is BaseStrategyUpgradeable, IERC3156FlashBorrower
uint256 _minRatio,
uint8 _maxIterations
) external onlyRole(GUARDIAN_ROLE) {
require(_minRatio < maxBorrowCollatRatio && _maxIterations > 0 && _maxIterations < 16, "8");
if (_minRatio >= maxBorrowCollatRatio || _maxIterations == 0 || _maxIterations >= 16)
revert InvalidSetOfParameters();
minWant = _minWant;
minRatio = _minRatio;
maxIterations = _maxIterations;
Expand All @@ -420,7 +429,7 @@ contract AaveFlashloanStrategy is BaseStrategyUpgradeable, IERC3156FlashBorrower

/// @notice Sets the discount factor for the StkAAVE price
function setDiscountFactor(uint256 _discountFactor) external onlyRole(GUARDIAN_ROLE) {
require(_discountFactor < 10000, "4");
if (_discountFactor > 10000) revert TooHighParameterValue();
discountFactor = _discountFactor;
}

Expand Down Expand Up @@ -471,7 +480,7 @@ contract AaveFlashloanStrategy is BaseStrategyUpgradeable, IERC3156FlashBorrower
if (!success) _revertBytes(result);

uint256 amountOut = abi.decode(result, (uint256));
require(amountOut >= minAmountOut, "15");
if (amountOut < minAmountOut) revert TooSmallAmountOut();
}

/// @notice Flashload callback, as defined by EIP-3156
Expand All @@ -484,7 +493,7 @@ contract AaveFlashloanStrategy is BaseStrategyUpgradeable, IERC3156FlashBorrower
uint256,
bytes calldata data
) external override returns (bytes32) {
require(msg.sender == FlashMintLib.LENDER && initiator == address(this), "1");
if (msg.sender != FlashMintLib.LENDER || initiator != address(this)) revert InvalidSender();
(bool deficit, uint256 amountWant) = abi.decode(data, (bool, uint256));

return FlashMintLib.loanLogic(deficit, amountWant, amount, address(want));
Expand All @@ -496,13 +505,8 @@ contract AaveFlashloanStrategy is BaseStrategyUpgradeable, IERC3156FlashBorrower
/// @dev stkAAVE require a "cooldown" period of 10 days before being claimed
function _claimRewards() internal returns (uint256 stkAaveBalance) {
stkAaveBalance = _balanceOfStkAave();
uint256 cooldownStatus;
if (stkAaveBalance > 0) {
cooldownStatus = _checkCooldown(); // don't check status if we have no stkAave
}

// If it's the claim period claim
if (stkAaveBalance > 0 && cooldownStatus == 1) {
if (stkAaveBalance > 0 && _checkCooldown() == 1) {
// redeem AAVE from stkAave
_stkAave.claimRewards(address(this), type(uint256).max);
_stkAave.redeem(address(this), stkAaveBalance);
Expand All @@ -514,7 +518,7 @@ contract AaveFlashloanStrategy is BaseStrategyUpgradeable, IERC3156FlashBorrower
stkAaveBalance = _balanceOfStkAave();

// request start of cooldown period, if there's no cooldown in progress
if (boolParams.cooldownStkAave && stkAaveBalance > 0 && cooldownStatus == 0) {
if (boolParams.cooldownStkAave && stkAaveBalance > 0 && _checkCooldown() == 0) {
_stkAave.cooldown();
}
}
Expand All @@ -523,6 +527,10 @@ contract AaveFlashloanStrategy is BaseStrategyUpgradeable, IERC3156FlashBorrower
_claimRewards();
}

function cooldown() external onlyRole(KEEPER_ROLE) {
_stkAave.cooldown();
}

/// @notice Reduce exposure by withdrawing funds and repaying debt
/// @param amountToFree Amount of `want` to withdraw/repay
/// @return balance Current balance of `want`
Expand Down Expand Up @@ -994,6 +1002,6 @@ contract AaveFlashloanStrategy is BaseStrategyUpgradeable, IERC3156FlashBorrower
revert(add(32, errMsg), mload(errMsg))
}
}
revert("117");
revert ErrorSwap();
}
}
13 changes: 9 additions & 4 deletions contracts/strategies/BaseStrategyUpgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ abstract contract BaseStrategyUpgradeable is BaseStrategyEvents, AccessControlUp
/// @notice See note on `setEmergencyExit()`
bool public emergencyExit;

// ============================ Errors =========================================

error InvalidToken();
error ZeroAddress();

// ============================ Constructor ====================================

/// @custom:oz-upgrades-unsafe-allow constructor
Expand All @@ -62,7 +67,7 @@ abstract contract BaseStrategyUpgradeable is BaseStrategyEvents, AccessControlUp
poolManager = IPoolManager(_poolManager);
want = IERC20(poolManager.token());
wantBase = 10**(IERC20Metadata(address(want)).decimals());
require(guardian != address(0) && governor != address(0) && governor != guardian, "0");
if (guardian == address(0) || governor == address(0) || governor == guardian) revert ZeroAddress();
// AccessControl
// Governor is guardian so no need for a governor role
_setupRole(GUARDIAN_ROLE, guardian);
Expand All @@ -73,7 +78,7 @@ abstract contract BaseStrategyUpgradeable is BaseStrategyEvents, AccessControlUp

// Initializing roles first
for (uint256 i = 0; i < keepers.length; i++) {
require(keepers[i] != address(0), "0");
if (keepers[i] == address(0)) revert ZeroAddress();
_setupRole(KEEPER_ROLE, keepers[i]);
}
_setRoleAdmin(KEEPER_ROLE, GUARDIAN_ROLE);
Expand Down Expand Up @@ -304,13 +309,13 @@ abstract contract BaseStrategyUpgradeable is BaseStrategyEvents, AccessControlUp
/// Implement `_protectedTokens()` to specify any additional tokens that
/// should be protected from sweeping in addition to `want`.
function sweep(address _token, address to) external onlyRole(GUARDIAN_ROLE) {
require(_token != address(want), "93");
if (_token == address(want)) revert InvalidToken();

address[] memory __protectedTokens = _protectedTokens();
for (uint256 i = 0; i < __protectedTokens.length; i++)
// In the strategy we use so far, the only protectedToken is the want token
// and this has been checked above
require(_token != __protectedTokens[i], "93");
if (_token == __protectedTokens[i]) revert InvalidToken();

IERC20(_token).safeTransfer(to, IERC20(_token).balanceOf(address(this)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ contract GenericAaveFraxStaker is GenericAaveUpgradeable {
// ================================ Parameters =================================

/// @notice Minimum amount of aFRAX to stake
uint256 private constant minStakingAmount = 1000 * 1e18; // 100 aFrax
uint256 private constant minStakingAmount = 1000 * 1e18; // 1000 aFrax
/// @notice Staking duration
uint256 public stakingPeriod;

Expand Down
2 changes: 1 addition & 1 deletion test/optimizerApr/lenderAave.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
} from '../../typechain';
import { gwei } from '../../utils/bignumber';
import { deploy, deployUpgradeable, latestTime, impersonate } from '../test-utils';
import hre, { ethers, network } from 'hardhat';
import { ethers, network } from 'hardhat';
import { expect } from '../test-utils/chai-setup';
import { BASE_TOKENS } from '../utils';
import { parseUnits, parseEther } from 'ethers/lib/utils';
Expand Down
Loading

0 comments on commit 4130d25

Please sign in to comment.