# Liquidations
When using leverage on positions, traders naturally become exposed to liquidation risks. For example, when the underlying value of a trader’s perp declines, the derivative asset will approach the value of its margin, putting the exchange at risk. To prevent the position from falling below the value of the margin that backs it, the protocol will proactively liquidate the position. Liquidations are triggered by liquidations bots that earn a small percentage of the remaining position.
Liquidate
is a function which closes a position and distributes assets based on a liquidation fee that goes to the liquidator and Ecosystem Fund. Liquidations prevent traders' accounts from falling into negative equity.
A liquidation happens when a trader can no longer meet the margin requirement of their leveraged position. In Nibiru, meeting the margin requirement means maintaining a margin ratio on the position that exceeds the maintenance margin ratio (6.25%), which is the minimum margin ratio that a position can have before being liquidated.
When a liquidator address sends a message to liquidate a position, the protocol keeper first computes the margin ratio of the position using the mark price. The notional is taken to be that maximum of the markSpot
(mark at an instance in time) notional and markTWAP
notional. Similarly, the unrealized PnL is taken to be the max of the markSpot
PnL and markTWAP
PnL. This computation realizes any outstanding funding payments on the position, tells us whether or not the position is underwater, and tells us if the position has "bad debt" (margin owed in excess of the collateral backing the position).
If this margin ratio is below the maintenance margin ratio, the liquidation message will close the position. This consists of opening a reverse position with a size equivalent to the one that is currently open, which brings the size to zero. A liquidation fee is taken out of the margin and distributed in some split (currently 50:50) between the Nibi-Perps Ecosystem Fund (Perp EF) and the liquidator. If any margin remains in the position after the liquidation fee is taken out, this remaining margin is sent back to the owner of the position. And if bad debt is created by the liquidation fee, it is payed by the Perp EF.
# Bad Debt
Bad debt is when the margin owed exceeds the value of the collateral backing a position. There are two broad types of bad debt: positional bad debt and liquidation bad debt.
Positional bad debt accrues when the realized PnL of a position results in a negative margin value. Liquidation bad debt accrues when the remaining margin of the position isn't enough to cover the liquidator's service fee.
If a position has some remaining margin after realizing its PnL but not enough to cover the liquidator fee, there is only liquidation bad debt, and all of the remaining margin is used to help cover the liquidator fee.
If instead the remaining margin on a position becomes negative due to the realized PnL, there is both positional and liquidation bad debt because a position with nonpositive value can't possibly cover a liquidation fee.
Whenever a trader owes bad debt, the Perp EF covers the payment.