Skip to content

Commit

Permalink
fix: Add separate unitilizedLeveragePercentage parameter for delevering
Browse files Browse the repository at this point in the history
  • Loading branch information
ckoopmann committed Nov 11, 2024
1 parent 0825e0d commit 39ba214
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
8 changes: 8 additions & 0 deletions contracts/adapters/MorphoLeverageStrategyExtension.sol
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ contract MorphoLeverageStrategyExtension is BaseExtension {

struct ExecutionSettings {
uint256 unutilizedLeveragePercentage; // Percent of max borrow left unutilized in precise units (1% = 10e16)
uint256 unutilizedLeveragePercentageDelever; // Percent of max borrow left unutilized when delevering
uint256 slippageTolerance; // % in precise units to price min token receive amount from trade quantities
uint256 twapCooldownPeriod; // Cooldown period required since last trade timestamp in seconds
}
Expand Down Expand Up @@ -161,6 +162,7 @@ contract MorphoLeverageStrategyExtension is BaseExtension {
);
event ExecutionSettingsUpdated(
uint256 _unutilizedLeveragePercentage,
uint256 _unutilizedLeveragePercentageDelever,
uint256 _twapCooldownPeriod,
uint256 _slippageTolerance
);
Expand Down Expand Up @@ -490,6 +492,7 @@ contract MorphoLeverageStrategyExtension is BaseExtension {

emit ExecutionSettingsUpdated(
execution.unutilizedLeveragePercentage,
execution.unutilizedLeveragePercentageDelever,
execution.twapCooldownPeriod,
execution.slippageTolerance
);
Expand Down Expand Up @@ -960,6 +963,10 @@ contract MorphoLeverageStrategyExtension is BaseExtension {
_execution.unutilizedLeveragePercentage <= PreciseUnitMath.preciseUnit(),
"Unutilized leverage must be <100%"
);
require (
_execution.unutilizedLeveragePercentageDelever <= PreciseUnitMath.preciseUnit(),
"Unutilized leverage on delever must be <100%"
);
require (
_execution.slippageTolerance <= PreciseUnitMath.preciseUnit(),
"Slippage tolerance must be <100%"
Expand Down Expand Up @@ -1132,6 +1139,7 @@ contract MorphoLeverageStrategyExtension is BaseExtension {
.mul(MORPHO_ORACLE_PRICE_SCALE).div(_actionInfo.collateralPrice);
} else {
return _actionInfo.collateralBalance
.preciseMul(PreciseUnitMath.preciseUnit().sub(execution.unutilizedLeveragePercentageDelever))
.preciseMul(netBorrowLimit.sub(_actionInfo.borrowBalance))
.preciseDiv(netBorrowLimit);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ if (process.env.INTEGRATIONTEST) {
const rebalanceInterval = BigNumber.from(86400);

const unutilizedLeveragePercentage = ether(0.01);
const unutilizedLeveragePercentageDelever = ZERO;
const twapMaxTradeSize = ether(0.5);
const twapCooldownPeriod = BigNumber.from(3000);
const slippageTolerance = ether(0.01);
Expand Down Expand Up @@ -412,6 +413,7 @@ if (process.env.INTEGRATIONTEST) {
};
execution = {
unutilizedLeveragePercentage: unutilizedLeveragePercentage,
unutilizedLeveragePercentageDelever: unutilizedLeveragePercentageDelever,
twapCooldownPeriod: twapCooldownPeriod,
slippageTolerance: slippageTolerance,
};
Expand Down Expand Up @@ -473,6 +475,7 @@ if (process.env.INTEGRATIONTEST) {
};
subjectExecutionSettings = {
unutilizedLeveragePercentage: ether(0.01),
unutilizedLeveragePercentageDelever: ZERO,
twapCooldownPeriod: BigNumber.from(120),
slippageTolerance: ether(0.01),
};
Expand Down Expand Up @@ -553,6 +556,9 @@ if (process.env.INTEGRATIONTEST) {
expect(execution.unutilizedLeveragePercentage).to.eq(
subjectExecutionSettings.unutilizedLeveragePercentage,
);
expect(execution.unutilizedLeveragePercentageDelever).to.eq(
subjectExecutionSettings.unutilizedLeveragePercentageDelever
);
expect(execution.twapCooldownPeriod).to.eq(subjectExecutionSettings.twapCooldownPeriod);
expect(execution.slippageTolerance).to.eq(subjectExecutionSettings.slippageTolerance);
});
Expand Down Expand Up @@ -650,6 +656,16 @@ if (process.env.INTEGRATIONTEST) {
});
});

describe("when unutilizedLeveragePercentageDelever is >100%", async () => {
beforeEach(async () => {
subjectExecutionSettings.unutilizedLeveragePercentageDelever = ether(1.1);
});

it("should revert", async () => {
await expect(subject()).to.be.revertedWith("Unutilized leverage on delever must be <100%");
});
});

describe("when slippage tolerance is >100%", async () => {
beforeEach(async () => {
subjectExecutionSettings.slippageTolerance = ether(1.1);
Expand Down Expand Up @@ -2967,7 +2983,6 @@ if (process.env.INTEGRATIONTEST) {
);

const expectedFirstPositionUnit = initialPositions[0].unit.sub(maxRedeemCollateral);
console.log("expectedFirstPositionUnit", expectedFirstPositionUnit.toString());

expect(initialPositions.length).to.eq(2);
expect(currentPositions.length).to.eq(2);
Expand Down Expand Up @@ -3430,6 +3445,7 @@ if (process.env.INTEGRATIONTEST) {
const oldExecution = await leverageStrategyExtension.getExecution();
const newExecution: ExecutionSettings = {
unutilizedLeveragePercentage: oldExecution.unutilizedLeveragePercentage,
unutilizedLeveragePercentageDelever: ZERO,
twapCooldownPeriod: oldExecution.twapCooldownPeriod,
slippageTolerance: ether(0.05),
};
Expand Down Expand Up @@ -3851,6 +3867,7 @@ if (process.env.INTEGRATIONTEST) {
const initializeSubjectVariables = () => {
subjectExecutionSettings = {
unutilizedLeveragePercentage: ether(0.05),
unutilizedLeveragePercentageDelever: ZERO,
twapCooldownPeriod: BigNumber.from(360),
slippageTolerance: ether(0.02),
};
Expand All @@ -3872,6 +3889,9 @@ if (process.env.INTEGRATIONTEST) {
expect(execution.unutilizedLeveragePercentage).to.eq(
subjectExecutionSettings.unutilizedLeveragePercentage,
);
expect(execution.unutilizedLeveragePercentageDelever).to.eq(
subjectExecutionSettings.unutilizedLeveragePercentageDelever,
);
expect(execution.twapCooldownPeriod).to.eq(subjectExecutionSettings.twapCooldownPeriod);
expect(execution.slippageTolerance).to.eq(subjectExecutionSettings.slippageTolerance);
});
Expand All @@ -3881,6 +3901,7 @@ if (process.env.INTEGRATIONTEST) {
.to.emit(leverageStrategyExtension, "ExecutionSettingsUpdated")
.withArgs(
subjectExecutionSettings.unutilizedLeveragePercentage,
subjectExecutionSettings.unutilizedLeveragePercentageDelever,
subjectExecutionSettings.twapCooldownPeriod,
subjectExecutionSettings.slippageTolerance,
);
Expand Down
1 change: 1 addition & 0 deletions utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export interface MethodologySettings {

export interface ExecutionSettings {
unutilizedLeveragePercentage: BigNumber;
unutilizedLeveragePercentageDelever: BigNumber;
twapCooldownPeriod: BigNumber;
slippageTolerance: BigNumber;
}
Expand Down

0 comments on commit 39ba214

Please sign in to comment.