Skip to content

Commit

Permalink
StrategyLib, StrategyLib2: remove _splitter arg from .checkWithdrawIm…
Browse files Browse the repository at this point in the history
…pact, update withdraw impact tolerance check, refactor denominator
  • Loading branch information
a17 committed Jul 3, 2024
1 parent 685efde commit 2474aca
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 39 deletions.
3 changes: 1 addition & 2 deletions contracts/strategy/StrategyBaseV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ abstract contract StrategyBaseV2 is IStrategyV2, ControllableV3 {
_asset,
balance,
expectedWithdrewUSD,
assetPrice,
_splitter
assetPrice
);
}

Expand Down
3 changes: 1 addition & 2 deletions contracts/strategy/StrategyBaseV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ abstract contract StrategyBaseV3 is IStrategyV3, ControllableV3 {
_asset,
balance,
expectedWithdrewUSD,
assetPrice,
_splitter
assetPrice
);
}

Expand Down
17 changes: 7 additions & 10 deletions contracts/strategy/StrategyLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ library StrategyLib {
// *************************************************************

/// @dev Denominator for fee calculation.
uint internal constant FEE_DENOMINATOR = 100_000;
uint internal constant DENOMINATOR = 100_000;

uint internal constant CHECK_WITHDRAW_IMPACT_TOLERANCE = 1_000;

// *************************************************************
// EVENTS
Expand All @@ -37,16 +39,14 @@ library StrategyLib {
string internal constant DENIED = "SB: Denied";
string internal constant TOO_HIGH = "SB: Too high";
string internal constant WRONG_VALUE = "SB: Wrong value";
/// @dev Denominator for compound ratio
uint internal constant COMPOUND_DENOMINATOR = 100_000;

// *************************************************************
// CHECKS AND EMITS
// *************************************************************

function _checkCompoundRatioChanged(address controller, uint oldValue, uint newValue) external {
onlyPlatformVoter(controller);
require(newValue <= COMPOUND_DENOMINATOR, TOO_HIGH);
require(newValue <= DENOMINATOR, TOO_HIGH);
emit CompoundRatioChanged(oldValue, newValue);
}

Expand Down Expand Up @@ -110,16 +110,14 @@ library StrategyLib {
address _asset,
uint balanceBefore,
uint expectedWithdrewUSD,
uint assetPrice,
address _splitter
uint assetPrice
) public view returns (uint balance) {
balance = IERC20(_asset).balanceOf(address(this));
if (assetPrice != 0 && expectedWithdrewUSD != 0) {

uint withdrew = balance > balanceBefore ? balance - balanceBefore : 0;
uint withdrewUSD = withdrew * assetPrice / 1e18;
uint difference = expectedWithdrewUSD > withdrewUSD ? expectedWithdrewUSD - withdrewUSD : 0;
require(difference * 1e18 / expectedWithdrewUSD < 1e16, TOO_HIGH);
require(difference * DENOMINATOR / expectedWithdrewUSD < CHECK_WITHDRAW_IMPACT_TOLERANCE, TOO_HIGH);
}
}

Expand Down Expand Up @@ -147,8 +145,7 @@ library StrategyLib {
_asset,
balanceBefore,
expectedWithdrewUSD,
assetPrice,
_splitter
assetPrice
);

if (balance != 0) {
Expand Down
20 changes: 8 additions & 12 deletions contracts/strategy/StrategyLib2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@ library StrategyLib2 {
// *************************************************************

/// @dev Denominator for fee calculation.
uint internal constant FEE_DENOMINATOR = 100_000;
uint internal constant DENOMINATOR = 100_000;
/// @notice 10% of total profit is sent to {performanceReceiver} before compounding
uint internal constant DEFAULT_PERFORMANCE_FEE = 10_000;
address internal constant DEFAULT_PERF_FEE_RECEIVER = 0x9Cc199D4353b5FB3e6C8EEBC99f5139e0d8eA06b;
/// @dev Denominator for compound ratio
uint internal constant COMPOUND_DENOMINATOR = 100_000;
uint internal constant CHECK_WITHDRAW_IMPACT_TOLERANCE = 1_000;

// *************************************************************
// ERRORS
Expand Down Expand Up @@ -63,9 +62,9 @@ library StrategyLib2 {

function _checkSetupPerformanceFee(address controller, uint fee_, address receiver_, uint ratio_) internal {
onlyGovernance(controller);
require(fee_ <= FEE_DENOMINATOR, TOO_HIGH);
require(fee_ <= DENOMINATOR, TOO_HIGH);
require(receiver_ != address(0), WRONG_VALUE);
require(ratio_ <= FEE_DENOMINATOR, TOO_HIGH);
require(ratio_ <= DENOMINATOR, TOO_HIGH);
emit PerformanceFeeChanged(fee_, receiver_, ratio_);
}

Expand All @@ -75,7 +74,7 @@ library StrategyLib2 {

function _changeCompoundRatio(IStrategyV3.BaseState storage baseState, address controller, uint newValue) external {
onlyPlatformVoterOrGov(controller);
require(newValue <= COMPOUND_DENOMINATOR, TOO_HIGH);
require(newValue <= DENOMINATOR, TOO_HIGH);

uint oldValue = baseState.compoundRatio;
baseState.compoundRatio = newValue;
Expand Down Expand Up @@ -146,17 +145,15 @@ library StrategyLib2 {
address _asset,
uint balanceBefore,
uint expectedWithdrewUSD,
uint assetPrice,
address _splitter
uint assetPrice
) public view returns (uint balance) {
balance = IERC20(_asset).balanceOf(address(this));
if (assetPrice != 0 && expectedWithdrewUSD != 0) {

uint withdrew = balance > balanceBefore ? balance - balanceBefore : 0;
uint withdrewUSD = withdrew * assetPrice / 1e18;
uint priceChangeTolerance = ITetuVaultV2(ISplitter(_splitter).vault()).withdrawFee();
uint difference = expectedWithdrewUSD > withdrewUSD ? expectedWithdrewUSD - withdrewUSD : 0;
require(difference * FEE_DENOMINATOR / expectedWithdrewUSD <= priceChangeTolerance, TOO_HIGH);
require(difference * DENOMINATOR / expectedWithdrewUSD < CHECK_WITHDRAW_IMPACT_TOLERANCE, TOO_HIGH);
}
}

Expand Down Expand Up @@ -184,8 +181,7 @@ library StrategyLib2 {
_asset,
balanceBefore,
expectedWithdrewUSD,
assetPrice,
_splitter
assetPrice
);

if (balance != 0) {
Expand Down
7 changes: 3 additions & 4 deletions contracts/test/MockStrategy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ contract MockStrategy is StrategyBaseV2 {
IERC20(asset).transfer(controller(), _slippage);
}
if (lastEarned != 0) {
uint toCompound = lastEarned * compoundRatio / StrategyLib.COMPOUND_DENOMINATOR;
uint toCompound = lastEarned * compoundRatio / StrategyLib.DENOMINATOR;
MockToken(asset).mint(address(this), toCompound);
address forwarder = IController(controller()).forwarder();
if (forwarder != address(0)) {
Expand Down Expand Up @@ -188,9 +188,8 @@ contract MockStrategy is StrategyBaseV2 {
address _asset,
uint balanceBefore,
uint investedAssetsUSD,
uint assetPrice,
address _splitter
uint assetPrice
) external view returns (uint balance) {
return StrategyLib.checkWithdrawImpact(_asset, balanceBefore, investedAssetsUSD, assetPrice, _splitter);
return StrategyLib.checkWithdrawImpact(_asset, balanceBefore, investedAssetsUSD, assetPrice);
}
}
7 changes: 3 additions & 4 deletions contracts/test/MockStrategyV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ contract MockStrategyV3 is StrategyBaseV3 {
IERC20(asset).transfer(controller(), _slippage);
}
if (lastEarned != 0) {
uint toCompound = lastEarned * baseState.compoundRatio / StrategyLib2.COMPOUND_DENOMINATOR;
uint toCompound = lastEarned * baseState.compoundRatio / StrategyLib2.DENOMINATOR;
MockToken(asset).mint(address(this), toCompound);
address forwarder = IController(controller()).forwarder();
if (forwarder != address(0)) {
Expand Down Expand Up @@ -189,9 +189,8 @@ contract MockStrategyV3 is StrategyBaseV3 {
address _asset,
uint balanceBefore,
uint investedAssetsUSD,
uint assetPrice,
address _splitter
uint assetPrice
) external view returns (uint balance) {
return StrategyLib2.checkWithdrawImpact(_asset, balanceBefore, investedAssetsUSD, assetPrice, _splitter);
return StrategyLib2.checkWithdrawImpact(_asset, balanceBefore, investedAssetsUSD, assetPrice);
}
}
3 changes: 1 addition & 2 deletions test/vault/CheckWithdrawImpactTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ describe("Tests for StrategyBaseV2._checkWithdrawImpact", function () {
asset.address,
balanceBeforeWithdraw,
investedAssetsUSD,
assetPrice,
splitter.address
assetPrice
);
}
//endregion Utils
Expand Down
5 changes: 2 additions & 3 deletions test/vault/SplitterForBaseStrategyV3Tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
import {Misc} from "../../scripts/utils/Misc";
import {parseUnits} from "ethers/lib/utils";


const {expect} = chai;
chai.use(chaiAsPromised);

Expand Down Expand Up @@ -854,7 +853,7 @@ describe("SplitterForBaseStrategyV3Tests", function () {
});
});

it("should not withdraw when MockStrategy has UseTrueExpectedWithdraw enabled, but in real slippage exist", async () => {
it("should not withdraw when MockStrategyV3 has UseTrueExpectedWithdraw enabled, but in real slippage exist", async () => {
const insurance = await vault.insurance();

await strategy.init(controller.address, splitter.address);
Expand All @@ -878,7 +877,7 @@ describe("SplitterForBaseStrategyV3Tests", function () {
// console.log('insuranceBefore', insuranceBefore)

await strategy.setUseTrueExpectedWithdraw(true)
await strategy.setSlippage(10);
await strategy.setSlippage(1_000);

await expect(vault.withdrawAll()).revertedWith('SB: Too high')

Expand Down

0 comments on commit 2474aca

Please sign in to comment.