@@ -32,7 +32,7 @@ import {
3232} from "@utils/test/index" ;
3333import { PerpV2Fixture , SystemFixture } from "@utils/fixtures" ;
3434import { BigNumber } from "ethers" ;
35- import { ADDRESS_ZERO , ZERO } from "@utils/constants" ;
35+ import { ADDRESS_ZERO , ZERO , MAX_UINT_256 , ZERO_BYTES } from "@utils/constants" ;
3636
3737const expect = getWaffleExpect ( ) ;
3838
@@ -1120,6 +1120,62 @@ describe("PerpV2LeverageSlippageIssuance", () => {
11201120 expect ( toUSDCDecimals ( finalCollateralBalance ) ) . to . be . closeTo ( expectedCollateralBalance , 2 ) ;
11211121 } ) ;
11221122 } ) ;
1123+
1124+ describe ( "when liquidation results in negative account value" , ( ) => {
1125+ beforeEach ( async ( ) => {
1126+ // Calculated leverage = ~8.5X = 8_654_438_822_995_683_587
1127+ await leverUp (
1128+ setToken ,
1129+ perpLeverageModule ,
1130+ perpSetup ,
1131+ owner ,
1132+ baseToken ,
1133+ 6 ,
1134+ ether ( .02 ) ,
1135+ true
1136+ ) ;
1137+
1138+ // Move oracle price down to 5 USDC to enable liquidation
1139+ await perpSetup . setBaseTokenOraclePrice ( vETH , usdcUnits ( 5.0 ) ) ;
1140+
1141+ // Move price down by maker selling 20k USDC of vETH
1142+ // Post trade spot price rises from ~10 USDC to 6_370_910_537_702_299_856
1143+ await perpSetup . clearingHouse . connect ( maker . wallet ) . openPosition ( {
1144+ baseToken : vETH . address ,
1145+ isBaseToQuote : true , // short
1146+ isExactInput : false , // `amount` is USDC
1147+ amount : ether ( 20000 ) ,
1148+ oppositeAmountBound : ZERO ,
1149+ deadline : MAX_UINT_256 ,
1150+ sqrtPriceLimitX96 : ZERO ,
1151+ referralCode : ZERO_BYTES
1152+ } ) ;
1153+
1154+ await perpSetup
1155+ . clearingHouse
1156+ . connect ( otherTrader . wallet )
1157+ . liquidate ( subjectSetToken , baseToken ) ;
1158+ } ) ;
1159+
1160+ it ( "should be possible to remove the module" , async ( ) => {
1161+ await subject ( ) ;
1162+
1163+ const collateralBalance = await perpSetup . vault . getBalance ( subjectSetToken ) ;
1164+ const freeCollateral = await perpSetup . vault . getFreeCollateral ( subjectSetToken ) ;
1165+ const accountValue = await perpSetup . clearingHouse . getAccountValue ( subjectSetToken ) ;
1166+
1167+ // collateralBalance: 20_100_000 (10^6)
1168+ // accountValue: -43_466_857_276_051_287_954 (10^18)
1169+ expect ( collateralBalance ) . gt ( 1 ) ;
1170+ expect ( freeCollateral ) . eq ( 0 ) ;
1171+ expect ( accountValue ) . lt ( - 1 ) ;
1172+
1173+ /// Remove module
1174+ await setToken . removeModule ( perpLeverageModule . address ) ;
1175+ const finalModules = await setToken . getModules ( ) ;
1176+ expect ( finalModules . includes ( perpLeverageModule . address ) ) . eq ( false ) ;
1177+ } ) ;
1178+ } ) ;
11231179 } ) ;
11241180 } ) ;
11251181} ) ;
0 commit comments