Skip to content

Commit 8ad1000

Browse files
bweick0xSachinK
andauthored
fix(DeltaNeutralBasisTradingStrategyExtension): Fix for accounting for precision applied by PERP protocol on their US… (#38)
Fix for accounting for precision applied by PERP protocol on their USDC positions to make correct calculations for allowing engage. Co-authored-by: 0xSachinK <[email protected]>
1 parent 6f8a0bb commit 8ad1000

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

contracts/extensions/DeltaNeutralBasisTradingStrategyExtension.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,9 @@ contract DeltaNeutralBasisTradingStrategyExtension is BaseExtension {
11681168

11691169
// Check that there is zero position unit of USDC of collateral value. Allows to neglect dust amounts.
11701170
require(
1171-
engageInfo.accountInfo.collateralBalance.preciseDiv(engageInfo.setTotalSupply.toInt256()) == 0,
1171+
engageInfo.accountInfo.collateralBalance
1172+
.fromPreciseUnitToDecimals(collateralDecimals) // PERP returns 18 decimal number, must convert for logic to work
1173+
.preciseDiv(engageInfo.setTotalSupply.toInt256()) == 0,
11721174
"PerpV2 collateral balance must be 0"
11731175
);
11741176

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@setprotocol/set-v2-strategies",
3-
"version": "0.0.12",
3+
"version": "0.0.13",
44
"description": "",
55
"main": "dist",
66
"types": "dist/types",

test/extensions/deltaNeutralBasisTradingStrategyExtension.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,35 @@ describe("DeltaNeutralBasisTradingStrategyExtension", () => {
919919
});
920920
});
921921

922+
describe("when collateral balance is non-zero but less than a position unit (must account for PERP decimal adjustment)", async () => {
923+
beforeEach(async () => {
924+
925+
await subject();
926+
927+
// set funding rate to non-zero and accrue funding; allows us to shift the net collateral balance such that
928+
// it no longer is an exact multiple of the total supply
929+
await perpV2Setup.clearingHouseConfig.setMaxFundingRate(BigNumber.from(0.1e6));
930+
await perpV2Setup.setBaseTokenOraclePrice(perpV2Setup.vETH, usdc(990));
931+
await perpV2PriceFeedMock.setPrice(BigNumber.from(990).mul(10 ** 8));
932+
await increaseTimeAsync(ONE_DAY_IN_SECONDS);
933+
934+
await leverageStrategyExtension.disengage();
935+
936+
const totalSupply = await setToken.totalSupply();
937+
const accountInfo = await perpBasisTradingModule.getAccountInfo(setToken.address);
938+
const netCollateral = accountInfo.collateralBalance.add(accountInfo.owedRealizedPnl);
939+
const collateralUnits = toUSDCDecimals(preciseDiv(netCollateral, totalSupply));
940+
941+
await leverageStrategyExtension.withdraw(collateralUnits);
942+
// Left out collateral balance (in USDC decimals) = 60; Total supply = 100
943+
// 60e12/1e12 = 60, 60*1e18/100e18 < 1 (rounds to 0); Hence the re-engage should not revert!
944+
});
945+
946+
it("should not revert", async () => {
947+
await expect(subject()).to.not.be.reverted;
948+
});
949+
});
950+
922951
describe("when collateral balance is non-zero", async () => {
923952
beforeEach(async () => {
924953
await leverageStrategyExtension.deposit(usdc(1));

0 commit comments

Comments
 (0)