Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Adjust applicationo of "unitilizedLeveragePercentage" in calculation of maxBorrow #192

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 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 @@ -1123,14 +1130,16 @@ contract MorphoLeverageStrategyExtension is BaseExtension {
// Note NetBorrow Limit is already denominated in borrow asset
uint256 netBorrowLimit = _actionInfo.collateralBalance
.mul(_actionInfo.collateralPrice).div(MORPHO_ORACLE_PRICE_SCALE)
.preciseMul(_actionInfo.lltv)
.preciseMul(PreciseUnitMath.preciseUnit().sub(execution.unutilizedLeveragePercentage));
.preciseMul(_actionInfo.lltv);

if (_isLever) {
return netBorrowLimit
.preciseMul(PreciseUnitMath.preciseUnit().sub(execution.unutilizedLeveragePercentage))
.sub(_actionInfo.borrowBalance)
.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
Loading