LiquidationBranch.liquidateAccounts()
can liquidate multiple accounts in one call. If account has more than 1 position, all these positions would be liquidated too. When liquidating an account asset price changes because it closes position. In that case when liquidating multiple trading accounts and/or positions mark price should be changed multiple times but it doesn't. It changes only for current position in loop, but doesn't count all other liquidated position sizes.
Incorrect value of markPrice
affects on calculation of funding rate. The more accounts are liquidated, the bigger difference between correct and actual markPrice and consequently funding rate would be. This would be also a problem when the first closed position has a large size and the last has a small size, in that case, funding rate would be calculated for the last position with a small size.
If we take a look at LiquidationBranch.liquidateAccounts() function we will see that it iterates through all liquidatable account ids and if an account is liquidatable it start closing all account positions.
As we see from in this line calculation of price impact is getting from closing current position (ctx.liquidationSizeX18
which is -ctx.oldPositionSizeX18
). But this does not include price impact of previous positions as index price is the same for all closed positions.
Let's take a simple example. Liquidatable accounts have 2 buy positions, first with size 20e18, other with 10e18. indexPrice is MOCK_BTC_USD_PRICE = 100_000e18. When liquidating this account position position with size 20e18 is liquidated first. After closing first position price would be 999_999e17, but after closing second position price would be bigger - 999_9995e16 (price after closing second position is bigger than after closing first).
Here is a simple test example with provided data that "simulates" mark price change while liquidating accounts:
It is necessary to take into account the sizeDelta of closed positions:
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.