The problem is that when a position is liquidated both skew and OI are set to 0.
Add the folllowing test to test/integration/external/chainlink/keepers/liquidation/performUpkeep/performUpkeep.t.sol file.
function test_LiquidationClears_SkewAndOpenInterest(
)
external
givenInitializeContract
{
bool isLong = true;
uint256 marketId = 1;
uint256 amountOfTradingAccounts = 1 ;
MarketConfig memory fuzzMarketConfig = getFuzzMarketConfig(marketId);
uint256 marginValueUsd = 1_000e18;
uint256 initialMarginRate = fuzzMarketConfig.imr;
deal({ token: address(usdz), to: users.naruto.account, give: marginValueUsd });
uint128[] memory accountsIds = new uint128[](2);
uint128 tradingAccountId = createAccountAndDeposit(marginValueUsd, address(usdz));
openPosition(fuzzMarketConfig, tradingAccountId, initialMarginRate, marginValueUsd, isLong);
accountsIds[0] = tradingAccountId;
deal({ token: address(usdz), to: users.naruto.account, give: marginValueUsd *2 });
uint128 nonLiquidatableTradingAccountId = createAccountAndDeposit(marginValueUsd *2, address(usdz));
openPosition(fuzzMarketConfig, nonLiquidatableTradingAccountId, initialMarginRate, marginValueUsd, isLong);
accountsIds[amountOfTradingAccounts] = nonLiquidatableTradingAccountId;
setAccountsAsLiquidatable(fuzzMarketConfig, isLong);
changePrank({ msgSender: users.owner.account });
LiquidationKeeper(liquidationKeeper).setForwarder(users.keepersForwarder.account);
changePrank({ msgSender: users.keepersForwarder.account });
bytes memory performData = abi.encode(accountsIds);
for (uint256 i; i < accountsIds.length; i++) {
if (accountsIds[i] == nonLiquidatableTradingAccountId) {
continue;
}
vm.expectEmit({
checkTopic1: true,
checkTopic2: true,
checkTopic3: false,
checkData: false,
emitter: address(perpsEngine)
});
emit LiquidationBranch.LogLiquidateAccount({
keeper: liquidationKeeper,
tradingAccountId: accountsIds[i],
amountOfOpenPositions: 0,
requiredMaintenanceMarginUsd: 0,
marginBalanceUsd: 0,
liquidatedCollateralUsd: 0,
liquidationFeeUsd: 0
});
}
LiquidationKeeper(liquidationKeeper).performUpkeep(performData);
assertEq(GlobalConfigurationHarness(address(perpsEngine)).workaround_getAccountsIdsWithActivePositionsLength(),
1,
"Active Positions count is not 1");
assertEq(GlobalConfigurationHarness(address(perpsEngine)).workaround_getAccountIdWithActivePositions(0),
nonLiquidatableTradingAccountId,
"Non-liquidatable trading account id is not correct");
SD59x18 skew = perpsEngine.getSkew(uint128(marketId));
assertTrue(skew.intoInt256() == 0, "skew is Not 0");
(UD60x18 longsOpenInterest, UD60x18 shortsOpenInterest, UD60x18 totalOpenInterest) = perpsEngine.getOpenInterest(uint128(marketId));
assertEq(longsOpenInterest.intoUint256(), 0, "longsOpenInterest is not 0");
assertEq(shortsOpenInterest.intoUint256(), 0, "shortsOpenInterest is not 0");
assertEq(totalOpenInterest.intoUint256(), 0, "totalOpenInterest is not 0");
}