Skip to main content

Liquidation

Liquidation Mechanism

Traders whose netValue falls below the maintenance margin may have their positions force-closed, called "liquidation".

During the liquidation, the system tries to sell the position at a discount based on the mark price. Anyone interested in this deal can join liquidation and is known as a "liquidator".

Liquidators are permitted to take over the position of the liquidated account, entirely or partially, without consuming the liquidity from the order book. The liquidation process will last until no position is left or the netValue is greater than the maintenance margin.

As the liquidation is open to the public, there is no guarantee that your request will be executed. It will not be executed or partially executed if:

  • Someone else submitted a liquidation request before you, or
  • The trader deposited enough margin in time, or
  • The mark price moved beyond your price protection.

Trigger a Liquidation

You can trigger a liquidation by calling IPerpetual#liquidate():

/// @param liquidator who will be the liquidator
/// @param liquidatedTrader is the trader you want to liquidate.
/// @param requestPaper is the size of position you want to take .
/// @param expectCredit is the amount of credit you want to pay (when liquidating a short position)
/// or receive (when liquidating a long position)
/// @return liqtorPaperChange is the final executed change of liquidator's paper amount
/// @return liqtorCreditChange is the final executed change of liquidator's credit amount
function liquidate(
address liquidator,
address liquidatedTrader,
int256 requestPaper,
int256 expectCredit
) external returns (int256 liqtorPaperChange, int256 liqtorCreditChange);

Liquidation is just like a regular trade. Liquidators can customize the amounts and set price protection to avoid losses caused by markPrice changes.

The parameter expectCredit is the amount of credit you want to pay (when liquidating a short position) or receive (when liquidating a long position). You can set the expectCredit by yourself.

By setting requestPaper and expectCredit, you're indicating the "expected price":

expectedPrice=1expectCreditrequestPaperexpectedPrice = -1 * \frac{expectCredit}{requestPaper}

The smart contract will not execute with a higher(lower) price than the expected price if you're liquidating a long(short) position.

Auto Scaled Amount

Your liquidation will be limited to the position size. For example, if the position remains 10ETH and you request a 15ETH liquidation. Only 10ETH will be executed. The other 5ETH requests will be canceled.

Liquidation Price

Here we show how to calcuate the liquidation price of an account.

To avoid liquidation, we need:

netValuemaintenanceMarginnetValue \geq maintenanceMargin
  • We first calculate the maintenanceMargin for all other markets' positions. Let's call it maintenanceMargin'. Then we have netValue of the account. Let's call it netValue'. So we have:
netValue+paperAmountprice+creditAmountmaintenanceMargin+paperAmountpriceliquidationThresholdnetValue' + paperAmount * price + creditAmount \geq maintenanceMargin' + \lvert paperAmount \rvert * price * liquidationThreshold
  • if paperAmount > 0
paperAmountprice(1liquidationThreshold)maintenanceMarginnetValuecreditAmountpaperAmount * price * (1-liquidationThreshold) \geq maintenanceMargin' - netValue' - creditAmount
price(maintenanceMarginnetValuecreditAmount)paperAmount(1liquidationThreshold)price \geq \frac{(maintenanceMargin' - netValue' - creditAmount)}{paperAmount * (1-liquidationThreshold)}
liqPrice=(maintenanceMarginnetValuecreditAmount)paperAmount(1liquidationThreshold)liqPrice = \frac{(maintenanceMargin' - netValue' - creditAmount)}{paperAmount * (1-liquidationThreshold)}
  • if paperAmount < 0
paperAmountprice(1+liquidationThreshold)maintenanceMarginnetValuecreditAmountpaperAmount * price * (1+liquidationThreshold) \geq maintenanceMargin' - netValue' - creditAmount
price(maintenanceMarginnetValuecreditAmount)paperAmount(1+liquidationThreshold)price \leq \frac{(maintenanceMargin' - netValue' - creditAmount)}{paperAmount * (1+liquidationThreshold)}
liqPrice=(maintenanceMarginnetValuecreditAmount)paperAmount(1+liquidationThreshold)liqPrice = \frac{(maintenanceMargin' - netValue' - creditAmount)}{paperAmount * (1+liquidationThreshold)}
  • Let's call 1±liquidationThreshold "multiplier", then:
liqPrice=(maintenanceMarginnetValuecreditAmount)paperAmountmultiplierliqPrice = \frac{(maintenanceMargin' - netValue' - creditAmount)}{paperAmount * multiplier}
  • If liqPrice<0, it should be considered as the position can never be liquidated (absolutely safe).