From ac03f8523e20263cdd47135790a3805a243e121f Mon Sep 17 00:00:00 2001 From: tess3rac7 Date: Sun, 6 Mar 2022 19:29:09 -0500 Subject: [PATCH 01/10] Inline claiming of rewards. --- contracts/ReaperAutoCompoundScreamLeverage.sol | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/contracts/ReaperAutoCompoundScreamLeverage.sol b/contracts/ReaperAutoCompoundScreamLeverage.sol index 3f0500e..87f7154 100644 --- a/contracts/ReaperAutoCompoundScreamLeverage.sol +++ b/contracts/ReaperAutoCompoundScreamLeverage.sol @@ -239,7 +239,7 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { */ function retireStrat() external { _onlyStrategistOrOwner(); - _claimRewards(); + comptroller.claimComp(address(this)); _swapRewardsToWftm(); _swapToWant(); @@ -671,24 +671,13 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { * 5. Deposits. */ function _harvestCore() internal override { - _claimRewards(); + comptroller.claimComp(address(this)); _swapRewardsToWftm(); _chargeFees(); _swapToWant(); deposit(); } - /** - * @dev Core harvest function. - * Get rewards from markets entered - */ - function _claimRewards() internal { - CTokenI[] memory tokens = new CTokenI[](1); - tokens[0] = cWant; - - comptroller.claimComp(address(this), tokens); - } - /** * @dev Core harvest function. * Swaps {SCREAM} to {WFTM} From c43697cb0fdbcf2e3b85ce43526c05782fb3fa04 Mon Sep 17 00:00:00 2001 From: tess3rac7 Date: Sun, 6 Mar 2022 19:33:44 -0500 Subject: [PATCH 02/10] Remove minScreamToSell. --- .../ReaperAutoCompoundScreamLeverage.sol | 31 ++++++++----------- 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/contracts/ReaperAutoCompoundScreamLeverage.sol b/contracts/ReaperAutoCompoundScreamLeverage.sol index 87f7154..0230389 100644 --- a/contracts/ReaperAutoCompoundScreamLeverage.sol +++ b/contracts/ReaperAutoCompoundScreamLeverage.sol @@ -65,7 +65,6 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { uint256 public borrowDepth; uint256 public minWantToLeverage; uint256 public maxBorrowDepth; - uint256 public minScreamToSell; uint256 public withdrawSlippageTolerance; /** @@ -92,7 +91,6 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { borrowDepth = 12; minWantToLeverage = 1000; maxBorrowDepth = 15; - minScreamToSell = 1000; withdrawSlippageTolerance = 50; _giveAllowances(); @@ -206,14 +204,6 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { borrowDepth = _borrowDepth; } - /** - * @dev Sets the minimum reward the will be sold (too little causes revert from Uniswap) - */ - function setMinScreamToSell(uint256 _minScreamToSell) external { - _onlyStrategistOrOwner(); - minScreamToSell = _minScreamToSell; - } - /** * @dev Sets the minimum want to leverage/deleverage (loop) for @@ -684,14 +674,19 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { */ function _swapRewardsToWftm() internal { uint256 screamBalance = IERC20Upgradeable(SCREAM).balanceOf(address(this)); - if (screamBalance >= minScreamToSell) { - IUniswapRouter(UNI_ROUTER).swapExactTokensForTokensSupportingFeeOnTransferTokens( - screamBalance, - 0, - screamToWftmRoute, - address(this), - block.timestamp + 600 - ); + if (screamBalance != 0) { + IUniswapRouter router = IUniswapRouter(UNI_ROUTER); + + uint256 wftmOutput = router.getAmountsOut(screamBalance, screamToWftmRoute)[1]; + if (wftmOutput != 0) { + router.swapExactTokensForTokensSupportingFeeOnTransferTokens( + screamBalance, + 0, + screamToWftmRoute, + address(this), + block.timestamp + 600 + ); + } } } From 5dd1a9bc0af1718ca64e4d8020bf4af07214e639 Mon Sep 17 00:00:00 2001 From: tess3rac7 Date: Sun, 6 Mar 2022 19:35:26 -0500 Subject: [PATCH 03/10] Hold reference for wftm in _chargeFees. --- contracts/ReaperAutoCompoundScreamLeverage.sol | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/contracts/ReaperAutoCompoundScreamLeverage.sol b/contracts/ReaperAutoCompoundScreamLeverage.sol index 0230389..8d3c4b6 100644 --- a/contracts/ReaperAutoCompoundScreamLeverage.sol +++ b/contracts/ReaperAutoCompoundScreamLeverage.sol @@ -695,16 +695,17 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { * Charges fees based on the amount of WFTM gained from reward */ function _chargeFees() internal { - uint256 wftmFee = (IERC20Upgradeable(WFTM).balanceOf(address(this)) * totalFee) / PERCENT_DIVISOR; + IERC20Upgradeable wftm = IERC20Upgradeable(WFTM); + uint256 wftmFee = (wftm.balanceOf(address(this)) * totalFee) / PERCENT_DIVISOR; if (wftmFee != 0) { uint256 callFeeToUser = (wftmFee * callFee) / PERCENT_DIVISOR; uint256 treasuryFeeToVault = (wftmFee * treasuryFee) / PERCENT_DIVISOR; uint256 feeToStrategist = (treasuryFeeToVault * strategistFee) / PERCENT_DIVISOR; treasuryFeeToVault -= feeToStrategist; - IERC20Upgradeable(WFTM).safeTransfer(msg.sender, callFeeToUser); - IERC20Upgradeable(WFTM).safeTransfer(treasury, treasuryFeeToVault); - IERC20Upgradeable(WFTM).safeTransfer(strategistRemitter, feeToStrategist); + wftm.safeTransfer(msg.sender, callFeeToUser); + wftm.safeTransfer(treasury, treasuryFeeToVault); + wftm.safeTransfer(strategistRemitter, feeToStrategist); } } From f747fac6a91f07db9c2c9d179ca24f9033ae3a44 Mon Sep 17 00:00:00 2001 From: tess3rac7 Date: Sun, 6 Mar 2022 19:54:03 -0500 Subject: [PATCH 04/10] Remove redundant _calculateLTVAfterWithdraw(). --- .../ReaperAutoCompoundScreamLeverage.sol | 24 +++++-------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/contracts/ReaperAutoCompoundScreamLeverage.sol b/contracts/ReaperAutoCompoundScreamLeverage.sol index 8d3c4b6..9b328b8 100644 --- a/contracts/ReaperAutoCompoundScreamLeverage.sol +++ b/contracts/ReaperAutoCompoundScreamLeverage.sol @@ -106,7 +106,7 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { function withdraw(uint256 _withdrawAmount) external { require(msg.sender == vault); - uint256 _ltv = _calculateLTVAfterWithdraw(_withdrawAmount); + uint256 _ltv = _calculateLTV(_withdrawAmount); if (_shouldLeverage(_ltv)) { // Strategy is underleveraged so can withdraw underlying directly @@ -281,7 +281,7 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { */ function deposit() public whenNotPaused { CErc20I(cWant).mint(balanceOfWant()); - uint256 _ltv = _calculateLTV(); + uint256 _ltv = _calculateLTV(0); if (_shouldLeverage(_ltv)) { _leverMax(); @@ -477,22 +477,10 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { /** * @dev This is the state changing calculation of LTV that is more accurate - * to be used internally. + * to be used internally. It returns what the LTV will be after withdrawing + * {_withdrawAmount}, which may be 0--in which case we get the current LTV. */ - function _calculateLTV() internal returns (uint256 ltv) { - uint256 supplied = cWant.balanceOfUnderlying(address(this)); - uint256 borrowed = cWant.borrowBalanceStored(address(this)); - - if (supplied == 0 || borrowed == 0) { - return 0; - } - ltv = (MANTISSA * borrowed) / supplied; - } - - /** - * @dev Calculates what the LTV will be after withdrawing - */ - function _calculateLTVAfterWithdraw(uint256 _withdrawAmount) internal returns (uint256 ltv) { + function _calculateLTV(uint256 _withdrawAmount) internal returns (uint256 ltv) { uint256 supplied = cWant.balanceOfUnderlying(address(this)); uint256 borrowed = cWant.borrowBalanceStored(address(this)); supplied = supplied - _withdrawAmount; @@ -500,7 +488,7 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { if (supplied == 0 || borrowed == 0) { return 0; } - ltv = (uint256(1e18) * borrowed) / supplied; + ltv = (MANTISSA * borrowed) / supplied; } /** From 3905c848e49cb79a4309dbfd9c4fc5990eadabc9 Mon Sep 17 00:00:00 2001 From: tess3rac7 Date: Sun, 6 Mar 2022 20:02:45 -0500 Subject: [PATCH 05/10] Inline shouldLeverage and Deleverage. --- .../ReaperAutoCompoundScreamLeverage.sol | 32 ++++--------------- 1 file changed, 6 insertions(+), 26 deletions(-) diff --git a/contracts/ReaperAutoCompoundScreamLeverage.sol b/contracts/ReaperAutoCompoundScreamLeverage.sol index 9b328b8..c8106fb 100644 --- a/contracts/ReaperAutoCompoundScreamLeverage.sol +++ b/contracts/ReaperAutoCompoundScreamLeverage.sol @@ -106,13 +106,13 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { function withdraw(uint256 _withdrawAmount) external { require(msg.sender == vault); - uint256 _ltv = _calculateLTV(_withdrawAmount); + uint256 ltv = _calculateLTV(_withdrawAmount); - if (_shouldLeverage(_ltv)) { + if (ltv < targetLTV - allowedLTVDrift) { // Strategy is underleveraged so can withdraw underlying directly _withdrawUnderlyingToVault(_withdrawAmount, true); _leverMax(); - } else if (_shouldDeleverage(_ltv)) { + } else if (ltv > targetLTV + allowedLTVDrift) { _deleverage(_withdrawAmount); // Strategy has deleveraged to the point where it can withdraw underlying @@ -281,11 +281,11 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { */ function deposit() public whenNotPaused { CErc20I(cWant).mint(balanceOfWant()); - uint256 _ltv = _calculateLTV(0); + uint256 ltv = _calculateLTV(0); - if (_shouldLeverage(_ltv)) { + if (ltv < targetLTV - allowedLTVDrift) { _leverMax(); - } else if (_shouldDeleverage(_ltv)) { + } else if (ltv > targetLTV + allowedLTVDrift) { _deleverage(0); } updateBalance(); @@ -455,26 +455,6 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { return ((wantSupplied * collateralFactor) / (MANTISSA - collateralFactor)); } - /** - * @dev Returns if the strategy should leverage with the given ltv level - */ - function _shouldLeverage(uint256 _ltv) internal view returns (bool) { - if (_ltv < targetLTV - allowedLTVDrift) { - return true; - } - return false; - } - - /** - * @dev Returns if the strategy should deleverage with the given ltv level - */ - function _shouldDeleverage(uint256 _ltv) internal view returns (bool) { - if (_ltv > targetLTV + allowedLTVDrift) { - return true; - } - return false; - } - /** * @dev This is the state changing calculation of LTV that is more accurate * to be used internally. It returns what the LTV will be after withdrawing From 3d2b2ad4690753d1071897e39e2f87a516f781e0 Mon Sep 17 00:00:00 2001 From: tess3rac7 Date: Mon, 7 Mar 2022 20:17:34 -0500 Subject: [PATCH 06/10] Inline getMaxBorrowFromSupply. --- contracts/ReaperAutoCompoundScreamLeverage.sol | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/contracts/ReaperAutoCompoundScreamLeverage.sol b/contracts/ReaperAutoCompoundScreamLeverage.sol index c8106fb..d9fa7bf 100644 --- a/contracts/ReaperAutoCompoundScreamLeverage.sol +++ b/contracts/ReaperAutoCompoundScreamLeverage.sol @@ -410,7 +410,7 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { uint256 borrowed = cWant.borrowBalanceStored(address(this)); uint256 realSupply = supplied - borrowed; - uint256 newBorrow = _getMaxBorrowFromSupplied(realSupply, targetLTV); + uint256 newBorrow = (realSupply * targetLTV) / (MANTISSA - targetLTV); uint256 totalAmountToBorrow = newBorrow - borrowed; for (uint8 i = 0; i < borrowDepth && totalAmountToBorrow > minWantToLeverage; i++) { @@ -448,13 +448,6 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { return _withdrawAmount; } - /** - * @dev Gets the maximum amount allowed to be borrowed for a given collateral factor and amount supplied - */ - function _getMaxBorrowFromSupplied(uint256 wantSupplied, uint256 collateralFactor) internal pure returns (uint256) { - return ((wantSupplied * collateralFactor) / (MANTISSA - collateralFactor)); - } - /** * @dev This is the state changing calculation of LTV that is more accurate * to be used internally. It returns what the LTV will be after withdrawing From 07836014e93024f8defda4d0fbe401554f91eb4c Mon Sep 17 00:00:00 2001 From: tess3rac7 Date: Mon, 7 Mar 2022 20:30:19 -0500 Subject: [PATCH 07/10] Simplify leverUpMax. --- contracts/ReaperAutoCompoundScreamLeverage.sol | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contracts/ReaperAutoCompoundScreamLeverage.sol b/contracts/ReaperAutoCompoundScreamLeverage.sol index d9fa7bf..8f7f543 100644 --- a/contracts/ReaperAutoCompoundScreamLeverage.sol +++ b/contracts/ReaperAutoCompoundScreamLeverage.sol @@ -411,10 +411,10 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { uint256 realSupply = supplied - borrowed; uint256 newBorrow = (realSupply * targetLTV) / (MANTISSA - targetLTV); - uint256 totalAmountToBorrow = newBorrow - borrowed; + uint256 amountLeftToBorrow = newBorrow - borrowed; - for (uint8 i = 0; i < borrowDepth && totalAmountToBorrow > minWantToLeverage; i++) { - totalAmountToBorrow = totalAmountToBorrow - _leverUpStep(totalAmountToBorrow); + for (uint256 i = 0; i < borrowDepth && amountLeftToBorrow > minWantToLeverage; i++) { + amountLeftToBorrow -= _leverUpStep(amountLeftToBorrow); } } From 5d39331c3c99f5a88f5cba9a07fb9e52b3e52683 Mon Sep 17 00:00:00 2001 From: tess3rac7 Date: Mon, 7 Mar 2022 21:14:26 -0500 Subject: [PATCH 08/10] Simplify retireStrat and panic. --- .../ReaperAutoCompoundScreamLeverage.sol | 105 +++++++----------- 1 file changed, 38 insertions(+), 67 deletions(-) diff --git a/contracts/ReaperAutoCompoundScreamLeverage.sol b/contracts/ReaperAutoCompoundScreamLeverage.sol index 8f7f543..08c2762 100644 --- a/contracts/ReaperAutoCompoundScreamLeverage.sol +++ b/contracts/ReaperAutoCompoundScreamLeverage.sol @@ -227,29 +227,22 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { * * Note: this is not an emergency withdraw function. For that, see panic(). */ - function retireStrat() external { + function retireStrat() external doUpdateBalance { _onlyStrategistOrOwner(); comptroller.claimComp(address(this)); _swapRewardsToWftm(); _swapToWant(); - uint256 maxAmount = type(uint256).max; - _deleverage(maxAmount); - _withdrawUnderlyingToVault(maxAmount, false); - updateBalance(); + _deleverage(type(uint256).max); + _withdrawUnderlyingToVault(type(uint256).max, false); } /** * @dev Pauses supplied. Withdraws all funds from Scream, leaving rewards behind. */ - function panic() external { + function panic() external doUpdateBalance { _onlyStrategistOrOwner(); - - uint256 maxAmount = type(uint256).max; - _deleverage(maxAmount); - _withdrawUnderlyingToVault(maxAmount, false); - updateBalance(); - + _deleverage(type(uint256).max); pause(); } @@ -259,9 +252,7 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { function unpause() external { _onlyStrategistOrOwner(); _unpause(); - _giveAllowances(); - deposit(); } @@ -279,7 +270,7 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { * It gets called whenever someone supplied in the strategy's vault contract. * It supplies {want} Scream to farm {SCREAM} */ - function deposit() public whenNotPaused { + function deposit() public whenNotPaused doUpdateBalance { CErc20I(cWant).mint(balanceOfWant()); uint256 ltv = _calculateLTV(0); @@ -288,7 +279,6 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { } else if (ltv > targetLTV + allowedLTVDrift) { _deleverage(0); } - updateBalance(); } /** @@ -421,8 +411,8 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { /** * @dev Does one step of leveraging */ - function _leverUpStep(uint256 _withdrawAmount) internal returns (uint256) { - if (_withdrawAmount == 0) { + function _leverUpStep(uint256 _borrowAmount) internal returns (uint256) { + if (_borrowAmount == 0) { return 0; } @@ -433,19 +423,19 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { canBorrow -= borrowed; - if (canBorrow < _withdrawAmount) { - _withdrawAmount = canBorrow; + if (canBorrow < _borrowAmount) { + _borrowAmount = canBorrow; } - if (_withdrawAmount > 10) { + if (_borrowAmount > 10) { // borrow available amount - CErc20I(cWant).borrow(_withdrawAmount); + CErc20I(cWant).borrow(_borrowAmount); // deposit available want as collateral CErc20I(cWant).mint(balanceOfWant()); } - return _withdrawAmount; + return _borrowAmount; } /** @@ -522,38 +512,27 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { } /** - * @dev For a given withdraw amount, figures out the new borrow with the current supply - * that will maintain the target LTV + * @dev For a given withdraw amount, figures out how much we need to reduce borrow by to + * maintain LTV at targerLTV. */ - function _getDesiredBorrow(uint256 _withdrawAmount) internal returns (uint256 position) { - //we want to use statechanging for safety + function _getBorrowDifference(uint256 _withdrawAmount) internal returns (uint256 difference) { uint256 supplied = cWant.balanceOfUnderlying(address(this)); uint256 borrowed = cWant.borrowBalanceStored(address(this)); + uint256 realSupply = supplied - borrowed; - //When we unwind we end up with the difference between borrow and supply - uint256 unwoundSupplied = supplied - borrowed; - - //we want to see how close to collateral target we are. - //So we take our unwound supplied and add or remove the _withdrawAmount we are are adding/removing. - //This gives us our desired future undwoundDeposit (desired supply) - - uint256 desiredSupply = 0; - if (_withdrawAmount > unwoundSupplied) { - _withdrawAmount = unwoundSupplied; + if (_withdrawAmount > realSupply) { + _withdrawAmount = realSupply; } - desiredSupply = unwoundSupplied - _withdrawAmount; + uint256 desiredSupply = realSupply - _withdrawAmount; //(ds *c)/(1-c) - uint256 num = desiredSupply * targetLTV; - uint256 den = MANTISSA - targetLTV; - - uint256 desiredBorrow = num / den; + uint256 desiredBorrow = (desiredSupply * targetLTV) / (MANTISSA - targetLTV); if (desiredBorrow > 1e5) { //stop us going right up to the wire desiredBorrow = desiredBorrow - 1e5; } - position = borrowed - desiredBorrow; + difference = borrowed - desiredBorrow; } /** @@ -561,53 +540,40 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { * that will maintain the target LTV */ function _deleverage(uint256 _withdrawAmount) internal { - uint256 newBorrow = _getDesiredBorrow(_withdrawAmount); - - // //If there is no deficit we dont need to adjust position - // //if the position change is tiny do nothing - if (newBorrow > minWantToLeverage) { - uint256 i = 0; - while (newBorrow > minWantToLeverage + 100) { - newBorrow = newBorrow - _leverDownStep(newBorrow); - i++; - //A limit set so we don't run out of gas - if (i >= borrowDepth) { - break; - } - } + uint256 borrowDifference = _getBorrowDifference(_withdrawAmount); + + for (uint256 i = 0; i < borrowDepth && borrowDifference > minWantToLeverage; i++) { + borrowDifference -= _leverDownStep(borrowDifference); } } /** * @dev Deleverages one step */ - function _leverDownStep(uint256 maxDeleverage) internal returns (uint256 deleveragedAmount) { - uint256 minAllowedSupply = 0; + function _leverDownStep(uint256 _releaseAmount) internal returns (uint256 deleveragedAmount) { uint256 supplied = cWant.balanceOfUnderlying(address(this)); uint256 borrowed = cWant.borrowBalanceStored(address(this)); (, uint256 collateralFactorMantissa, ) = comptroller.markets(address(cWant)); - //collat ration should never be 0. if it is something is very wrong... but just incase - if (collateralFactorMantissa != 0) { - minAllowedSupply = (borrowed * MANTISSA) / collateralFactorMantissa; - } + uint256 minAllowedSupply = (borrowed * MANTISSA) / collateralFactorMantissa; uint256 maxAllowedDeleverageAmount = supplied - minAllowedSupply; deleveragedAmount = maxAllowedDeleverageAmount; - if (deleveragedAmount >= borrowed) { + if (deleveragedAmount > borrowed) { deleveragedAmount = borrowed; } - if (deleveragedAmount >= maxDeleverage) { - deleveragedAmount = maxDeleverage; + if (deleveragedAmount > _releaseAmount) { + deleveragedAmount = _releaseAmount; } + uint256 exchangeRateStored = cWant.exchangeRateStored(); //redeemTokens = redeemAmountIn * 1e18 / exchangeRate. must be more than 0 //a rounding error means we need another small addition if (deleveragedAmount * MANTISSA >= exchangeRateStored && deleveragedAmount > 10) { deleveragedAmount -= 10; // Amount can be slightly off for tokens with less decimals (USDC), so redeem a bit less cWant.redeemUnderlying(deleveragedAmount); - //our borrow has been increased by no more than maxDeleverage + //our borrow has been increased by no more than _releaseAmount cWant.repayBorrow(deleveragedAmount); } } @@ -717,4 +683,9 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { IERC20Upgradeable(WFTM).safeDecreaseAllowance(UNI_ROUTER, IERC20Upgradeable(WFTM).allowance(address(this), UNI_ROUTER)); IERC20Upgradeable(SCREAM).safeDecreaseAllowance(UNI_ROUTER, IERC20Upgradeable(SCREAM).allowance(address(this), UNI_ROUTER)); } + + modifier doUpdateBalance { + _; + updateBalance(); + } } From 4d73d6b5b2cc564cfbca1953144a443010a99061 Mon Sep 17 00:00:00 2001 From: tess3rac7 Date: Mon, 7 Mar 2022 21:20:48 -0500 Subject: [PATCH 09/10] Simplify withdrawUnderlyingToVault. --- .../ReaperAutoCompoundScreamLeverage.sol | 27 +++++++------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/contracts/ReaperAutoCompoundScreamLeverage.sol b/contracts/ReaperAutoCompoundScreamLeverage.sol index 08c2762..32d9e12 100644 --- a/contracts/ReaperAutoCompoundScreamLeverage.sol +++ b/contracts/ReaperAutoCompoundScreamLeverage.sol @@ -471,16 +471,9 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { _withdrawAmount = realSupplied; } - uint256 tempColla = targetLTV + allowedLTVDrift; - - uint256 reservedAmount = 0; - if (tempColla == 0) { - tempColla = 1e15; // 0.001 * 1e18. lower we have issues - } - - reservedAmount = (borrowed * MANTISSA) / tempColla; - if (supplied >= reservedAmount) { - uint256 redeemable = supplied - reservedAmount; + uint256 minAllowedSupply = (borrowed * MANTISSA) / (targetLTV + allowedLTVDrift); + if (supplied >= minAllowedSupply) { + uint256 redeemable = supplied - minAllowedSupply; uint256 balance = cWant.balanceOf(address(this)); if (balance > 1) { if (redeemable < _withdrawAmount) { @@ -489,26 +482,24 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { } } - uint256 withdrawAmount; - if (_useWithdrawFee) { uint256 withdrawFee = (_withdrawAmount * securityFee) / PERCENT_DIVISOR; - withdrawAmount = _withdrawAmount - withdrawFee - 1; + _withdrawAmount -= (withdrawFee + 1); } else { - withdrawAmount = _withdrawAmount - 1; + _withdrawAmount -= 1; } - if(withdrawAmount < initialWithdrawAmount) { + if(_withdrawAmount < initialWithdrawAmount) { require( - withdrawAmount >= + _withdrawAmount >= (initialWithdrawAmount * (PERCENT_DIVISOR - withdrawSlippageTolerance)) / PERCENT_DIVISOR ); } - CErc20I(cWant).redeemUnderlying(withdrawAmount); - IERC20Upgradeable(want).safeTransfer(vault, withdrawAmount); + CErc20I(cWant).redeemUnderlying(_withdrawAmount); + IERC20Upgradeable(want).safeTransfer(vault, _withdrawAmount); } /** From bb921d09d0f431ba4a28f36a1eb6e6d4de0c2908 Mon Sep 17 00:00:00 2001 From: tess3rac7 Date: Mon, 7 Mar 2022 21:23:48 -0500 Subject: [PATCH 10/10] Modifier in withdraw. --- contracts/ReaperAutoCompoundScreamLeverage.sol | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/contracts/ReaperAutoCompoundScreamLeverage.sol b/contracts/ReaperAutoCompoundScreamLeverage.sol index 32d9e12..b47c4e2 100644 --- a/contracts/ReaperAutoCompoundScreamLeverage.sol +++ b/contracts/ReaperAutoCompoundScreamLeverage.sol @@ -103,7 +103,7 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { * It withdraws {want} from Scream * The available {want} minus fees is returned to the vault. */ - function withdraw(uint256 _withdrawAmount) external { + function withdraw(uint256 _withdrawAmount) external doUpdateBalance { require(msg.sender == vault); uint256 ltv = _calculateLTV(_withdrawAmount); @@ -121,7 +121,6 @@ contract ReaperAutoCompoundScreamLeverage is ReaperBaseStrategy { // LTV is in the acceptable range so the underlying can be withdrawn directly _withdrawUnderlyingToVault(_withdrawAmount, true); } - updateBalance(); } /**