Skip to content

Commit b73e6bb

Browse files
authored
Merge pull request #9 from SetProtocol/sachin/perp-manager-fixes
Perp Manager post testing fixes
2 parents fc12bfc + ed36183 commit b73e6bb

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

contracts/extensions/PerpV2LeverageStrategyExtension.sol

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ contract PerpV2LeverageStrategyExtension is BaseExtension {
136136
);
137137
event RebalanceIterated(
138138
int256 _currentLeverageRatio,
139-
int256 _newLeverageRatio,
139+
int256 _newTwapLeverageRatio,
140140
int256 _chunkRebalanceNotional,
141141
int256 _totalRebalanceNotional
142142
);
@@ -233,7 +233,9 @@ contract PerpV2LeverageStrategyExtension is BaseExtension {
233233
function engage() external onlyOperator {
234234
ActionInfo memory engageInfo = _createActionInfo();
235235

236-
require(engageInfo.baseBalance == 0, "Must not have existing base token position");
236+
// Assert currentLeverageRatio is 0. Since currentLeverageRatio = baseBalance * basePrice / accountValue,
237+
// asserting baseBalance is 0 is equivalent to asserting currentLeverageRatio is 0.
238+
require(engageInfo.baseBalance == 0, "Current leverage ratio must be 0");
237239
require(engageInfo.accountInfo.collateralBalance > 0, "Collateral balance must be > 0");
238240

239241
LeverageInfo memory leverageInfo = LeverageInfo({
@@ -707,15 +709,14 @@ contract PerpV2LeverageStrategyExtension is BaseExtension {
707709
ActionInfo memory actionInfo = _createActionInfo();
708710

709711
require(actionInfo.setTotalSupply > 0, "SetToken must have > 0 supply");
710-
711-
// This function is called during rebalance, iterateRebalance, ripcord and disengage.
712-
// |baseBalance| > 0, shows the position exists, and the Set has been engaged. We should not
713-
// check for |quoteBalance| > 0, as it is redundant.
714-
require(actionInfo.baseBalance.absUint256() > 0, "Base asset balance must be > 0");
715712

716713
// Get current leverage ratio
717714
int256 currentLeverageRatio = _calculateCurrentLeverageRatio(actionInfo);
718715

716+
// This function is called during rebalance, iterateRebalance, ripcord and disengage.
717+
// Assert currentLeverageRatio is 0 as the set should be engaged before this function is called.
718+
require(currentLeverageRatio.absUint256() > 0, "Current leverage ratio must NOT be 0");
719+
719720
return LeverageInfo({
720721
action: actionInfo,
721722
currentLeverageRatio: currentLeverageRatio,
@@ -973,6 +974,7 @@ contract PerpV2LeverageStrategyExtension is BaseExtension {
973974
* Calculate total notional rebalance quantity and chunked rebalance quantity in base asset units for engaging the SetToken. Used in engage().
974975
* Leverage ratio (for the base asset) is zero before engage. We open a new base asset position with size equals to (collateralBalance * targetLeverageRatio / baseAssetPrice)
975976
* to gain (targetLeverageRatio * collateralBalance) worth of exposure to the base asset.
977+
* Note: We can't use `_calculateChunkRebalanceNotional` function because CLR is 0 during engage and it would lead to a divison by zero error.
976978
*
977979
* return int256 Chunked rebalance notional in base asset units
978980
* return int256 Total rebalance notional in base asset units

test/extensions/perpV2LeverageStrategyExtension.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ describe("PerpV2LeverageStrategyExtension", () => {
658658
});
659659

660660
it("should revert", async () => {
661-
await expect(subject()).to.be.revertedWith("Must not have existing base token position");
661+
await expect(subject()).to.be.revertedWith("Current leverage ratio must be 0");
662662
});
663663
});
664664

@@ -1737,7 +1737,7 @@ describe("PerpV2LeverageStrategyExtension", () => {
17371737

17381738
describe("when not engaged", async () => {
17391739
it("should revert", async () => {
1740-
await expect(subject()).to.be.revertedWith("Base asset balance must be > 0");
1740+
await expect(subject()).to.be.revertedWith("Current leverage ratio must NOT be 0");
17411741
});
17421742
});
17431743
});
@@ -2547,7 +2547,7 @@ describe("PerpV2LeverageStrategyExtension", () => {
25472547

25482548
describe("when not engaged", async () => {
25492549
it("should revert", async () => {
2550-
await expect(subject()).to.be.revertedWith("Base asset balance must be > 0");
2550+
await expect(subject()).to.be.revertedWith("Current leverage ratio must NOT be 0");
25512551
});
25522552
});
25532553
});
@@ -3064,7 +3064,7 @@ describe("PerpV2LeverageStrategyExtension", () => {
30643064

30653065
describe("when not engaged", async () => {
30663066
it("should revert", async () => {
3067-
await expect(subject()).to.be.revertedWith("Base asset balance must be > 0");
3067+
await expect(subject()).to.be.revertedWith("Current leverage ratio must NOT be 0");
30683068
});
30693069
});
30703070
});
@@ -3145,7 +3145,7 @@ describe("PerpV2LeverageStrategyExtension", () => {
31453145
});
31463146

31473147
it("should revert", async () => {
3148-
await expect(subject()).to.be.revertedWith("Base asset balance must be > 0");
3148+
await expect(subject()).to.be.revertedWith("Current leverage ratio must NOT be 0");
31493149
});
31503150
});
31513151
});
@@ -3228,7 +3228,7 @@ describe("PerpV2LeverageStrategyExtension", () => {
32283228
});
32293229

32303230
it("should revert", async () => {
3231-
await expect(subject()).to.be.revertedWith("Base asset balance must be > 0");
3231+
await expect(subject()).to.be.revertedWith("Current leverage ratio must NOT be 0");
32323232
});
32333233
});
32343234
});

0 commit comments

Comments
 (0)