Incorrect marginBalanceUsdX18 is used for the liquidatable checking during market order creation, protocol may wrongly prevent an order from creating.
When a user calls to create a market order, protocol will simulate the settlement costs and validity of a given order. During the simulation, protocol checks if the current trading account is liquidatable and prevents liquidatable accounts from trading.
As we can see from above, the account's current required margin maintenance is compared to marginBalanceUsdX18 to determine if the account is liquidatable.
The marginBalanceUsdX18 is calculated based on ctx.accountTotalUnrealizedPnlUsdX18, which is calculated by getAccountMarginRequirementUsdAndUnrealizedPnlUsd() in TradingAccount lib.
getAccountMarginRequirementUsdAndUnrealizedPnlUsd() is called with marketId and ctx.sizeDeltaX18 arguments, in this function, ctx.sizeDeltaX18 is used for calculating markPrice which in turn is used to get accountTotalUnrealizedPnlUsdX18.
By doing that, the accountTotalUnrealizedPnlUsdX18 value returned is essentially the value after the trade, hence the marginBalanceUsdX18 is not the current margin balance but the margin balance after the trade. It's obviously wrong that protocol compares the margin balance after the trade with current required maintenance margin to determine if the trading account is currently liquidatable.
In fact, when a trading account is actually being liquidated, protocol compares the trading account's current margin balance with current required margin maintenance to see if the account is liquidatable, and that is the correct checking.
If the skew is to be decresed after the trade, mark price becomes lower hence lower the accountTotalUnrealizedPnlUsdX18, leads to a lower margin balance than the current margin balance, a trader might be wrongly prevented from creating the order, under certain circumstances, the impact could be critical as the position continues to deteriorate and goes below the maintenance margin requirement which would cause their position to be liquidated
Manual Review
Should use the trading account's current margin balance to check against **current required margin maintenance **to see if the account is liquidatable.
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.