Skip to content

Commit

Permalink
fix(DeltaNeutralBasisTradingStrategyExtension): Fix for accounting fo…
Browse files Browse the repository at this point in the history
…r 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]>
  • Loading branch information
bweick and 0xSachinK authored May 20, 2022
1 parent 6f8a0bb commit 8ad1000
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,9 @@ contract DeltaNeutralBasisTradingStrategyExtension is BaseExtension {

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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@setprotocol/set-v2-strategies",
"version": "0.0.12",
"version": "0.0.13",
"description": "",
"main": "dist",
"types": "dist/types",
Expand Down
29 changes: 29 additions & 0 deletions test/extensions/deltaNeutralBasisTradingStrategyExtension.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,35 @@ describe("DeltaNeutralBasisTradingStrategyExtension", () => {
});
});

describe("when collateral balance is non-zero but less than a position unit (must account for PERP decimal adjustment)", async () => {
beforeEach(async () => {

await subject();

// set funding rate to non-zero and accrue funding; allows us to shift the net collateral balance such that
// it no longer is an exact multiple of the total supply
await perpV2Setup.clearingHouseConfig.setMaxFundingRate(BigNumber.from(0.1e6));
await perpV2Setup.setBaseTokenOraclePrice(perpV2Setup.vETH, usdc(990));
await perpV2PriceFeedMock.setPrice(BigNumber.from(990).mul(10 ** 8));
await increaseTimeAsync(ONE_DAY_IN_SECONDS);

await leverageStrategyExtension.disengage();

const totalSupply = await setToken.totalSupply();
const accountInfo = await perpBasisTradingModule.getAccountInfo(setToken.address);
const netCollateral = accountInfo.collateralBalance.add(accountInfo.owedRealizedPnl);
const collateralUnits = toUSDCDecimals(preciseDiv(netCollateral, totalSupply));

await leverageStrategyExtension.withdraw(collateralUnits);
// Left out collateral balance (in USDC decimals) = 60; Total supply = 100
// 60e12/1e12 = 60, 60*1e18/100e18 < 1 (rounds to 0); Hence the re-engage should not revert!
});

it("should not revert", async () => {
await expect(subject()).to.not.be.reverted;
});
});

describe("when collateral balance is non-zero", async () => {
beforeEach(async () => {
await leverageStrategyExtension.deposit(usdc(1));
Expand Down

0 comments on commit 8ad1000

Please sign in to comment.