In TradingAccountBranch::withdrawMargin
, it is checked that the margin balance is greater than the required initial margin once collateral has been withdrawn:
https://github.com/Cyfrin/2024-07-zaros/blob/69ccf428b745058bea08804b3f3d961d31406ba8/src/perpetuals/branches/TradingAccountBranch.sol#L382-L392
If a user has a position with enough profit (i.e. unrealized PnL is greater than the required maintenance margin), this user can withdraw all collateral. If, later, that position becomes liquidatable, this user has no collateral so none can be deducted.
Let's assume a user opens a long position of 1 contract at 3,000 of ETHUSD. Let's also assume the maintenance margin rate required is 5%:
Position value: 1 * 3,000 = 3,000
Maintenance margin: 3,000 * 5% = 150
As soon as this position is in profit above 150, the user can withdraw all collateral. With zero collateral, if the position`s profit fall below 150, it will get liquidated (position will get closed) but no collateral will be deducted.
In this PoC, a position is created, when enough profit is accrued, collateral is withdrawn. Later, price is updated so the position becomes liquidatable and it is liquidated.
Add this test into liquidateAccounts.t.sol
:
As can be seen in the event emitted, the liquidatedCollateralUsd
is zero
even though the requiredMaintenanceMarginUsd
is 4e21
and liquidationFeeUsd
is 5e18
Foundry
A minimum amount of collateral (e.g. required initial margin) should always remain in the user account as long as he has got open 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.