In OrderBranch::createMarketOrder
, there is a check to see if the previously user was liquiduidatable or not. And to do this users margin is calculated. But to this margin, user's PnL is added which includes the new trade impact.
This is the calcuation which is done to calculate the user's current PnL
GitHub: https://github.com/Cyfrin/2024-07-zaros/blob/d687fe96bb7ace8652778797052a38763fbcbb1b/src/perpetuals/branches/OrderBranch.sol#L123C1-L138C1
In the first line, we are making call to getAccountMarginRequirementUsdAndUnrealizedPnlUsd(marketId, ctx.sizeDeltaX18)(...)
by passing also the sizeDeltaX18
. Because of this, the PnL will take into account the new change in position as well. And the new mark price will also be calculated by taking into account this sizeDeltaX18
.
Then this calculated PnL will be added to the users current margin and the call will be made to the isLiquidatable(...)
to check if the previous position of the user was liquidatable. But since this new calculated PnL includes the new position delta, it will be wrong. And there are chances that it might generate loss and might show user's position liquidatable or vice versa.
The calculated PnL for old position will be wrong.
Manual Review
Calculate the user's PnL based on the current positions only. Do not include the trade impact in it.
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.