From 8ad1000f36d30a1eb6b01060736822a82d145543 Mon Sep 17 00:00:00 2001 From: bweick Date: Fri, 20 May 2022 09:22:24 -0500 Subject: [PATCH] =?UTF-8?q?fix(DeltaNeutralBasisTradingStrategyExtension):?= =?UTF-8?q?=20Fix=20for=20accounting=20for=20precision=20applied=20by=20PE?= =?UTF-8?q?RP=20protocol=20on=20their=20US=E2=80=A6=20(#38)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix for accounting for precision applied by PERP protocol on their USDC positions to make correct calculations for allowing engage. Co-authored-by: 0xSachinK --- ...taNeutralBasisTradingStrategyExtension.sol | 4 ++- package.json | 2 +- ...utralBasisTradingStrategyExtension.spec.ts | 29 +++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/contracts/extensions/DeltaNeutralBasisTradingStrategyExtension.sol b/contracts/extensions/DeltaNeutralBasisTradingStrategyExtension.sol index 502c327..cbcb14f 100644 --- a/contracts/extensions/DeltaNeutralBasisTradingStrategyExtension.sol +++ b/contracts/extensions/DeltaNeutralBasisTradingStrategyExtension.sol @@ -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" ); diff --git a/package.json b/package.json index bdbc518..49137c9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@setprotocol/set-v2-strategies", - "version": "0.0.12", + "version": "0.0.13", "description": "", "main": "dist", "types": "dist/types", diff --git a/test/extensions/deltaNeutralBasisTradingStrategyExtension.spec.ts b/test/extensions/deltaNeutralBasisTradingStrategyExtension.spec.ts index 1127acb..457794e 100644 --- a/test/extensions/deltaNeutralBasisTradingStrategyExtension.spec.ts +++ b/test/extensions/deltaNeutralBasisTradingStrategyExtension.spec.ts @@ -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));