In the SettlementBranch.sol
the auto-deleveraging will be applied twice.
In the _filOrder
function auto-deleveraging is applied when the old position of a trader had a positive pnl:
However in point 1) in the code above we can see that auto deleveraging will first be applied in the getAdjustedProfitForMarketId
:
This will return the amount with the applied deleveraging. This amount will be deposited to the trading account in point 2).
In point 3) we see that the withdrawUsdTokenFromMarket
is called. However the amount that is passed in the function is the deleveraged amount.
When we check out the withdrawUsdTokenFromMarket
we can see that the this amount will be deleveraged once again:
Also the amount that was deleveraged twice will be minted to the engine, while the user's position was updated with the amount that was deleveraged only once.
The withdrawUsdTokenFromMarket
uses the deleveraged amount instead of the original `pnlUsdX18`
As a result the protocol will even be in an insolvent state as there will be more amount recorded(the amount that was deleveraged only once) in the positions of the perp engine than it was minted to it(the amount that was deleveraged twice).
Currently the amount will be deleveraged twice in getAdjustedProfitForMarketId
and then in withdrawUsdTokenFromMarket
. To fix this issue you could use the original amount as an input in the withdrawUsdTokenFromMarket
instead of the deleveraged one(assuming both functions will perform the same deleveraging).
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.