Base Asset Liquidation Functions
calculateBaseAssetAmountToCoverMarginShortage
Calculates the amount of base asset that needs to be liquidated to cover a margin shortage.BN
required
The margin shortage amount in QUOTE_PRECISION
number
required
The margin ratio for the position
number
required
The liquidation fee in LIQUIDATION_FEE_PRECISION
number
required
The insurance fund liquidation fee in LIQUIDATION_FEE_PRECISION
BN
required
The oracle price for the base asset
BN
required
The oracle price for the quote asset
BN | undefined
Base asset amount to liquidate, or undefined if unlimited liquidation is allowed
Usage Example
Spot Asset Liquidation Functions
calculateLiabilityTransferToCoverMarginShortage
Calculates the amount of liability to transfer to cover a margin shortage in spot markets.BN
required
The margin shortage amount
number
required
The asset weight in SPOT_MARKET_WEIGHT_PRECISION
number
required
The asset liquidation multiplier
number
required
The liability weight in SPOT_MARKET_WEIGHT_PRECISION
number
required
The liability liquidation multiplier
number
required
The number of decimals for the liability asset
BN
required
The oracle price for the liability asset
number
required
The insurance fund liquidation fee
BN | undefined
Liability amount to transfer, or undefined if unlimited transfer is allowed
Usage Example
calculateAssetTransferForLiabilityTransfer
Calculates the asset transfer amount for a given liability transfer during liquidation.BN
required
Available asset amount
number
required
The asset liquidation multiplier
number
required
The number of decimals for the asset
BN
required
The oracle price for the asset
BN
required
The liability amount to transfer
number
required
The liability liquidation multiplier
number
required
The number of decimals for the liability
BN
required
The oracle price for the liability
BN | undefined
Asset transfer amount. Rounds to available asset amount if the difference is less than $1
Usage Example
Liquidation Timing Functions
calculateMaxPctToLiquidate
Calculates the maximum percentage of a position that can be liquidated based on time elapsed and margin status.BN
required
The slot when the user was last active/liquidated
BN
required
The amount of margin already freed through liquidation
BN
required
The current margin shortage
BN
required
The current slot
BN
required
The initial percentage allowed to liquidate in LIQUIDATION_PCT_PRECISION
BN
required
The duration over which liquidation percentage increases (in slots)
BN
Maximum percentage to liquidate in LIQUIDATION_PCT_PRECISION
Usage Example
Implementation Details
This function implements a progressive liquidation mechanism:- Small margin shortages (< $50) allow immediate full liquidation
- Larger positions liquidate progressively over time
- The percentage allowed increases linearly from
initialPctToLiquidateto 100% overliquidationDurationslots - Accounts for margin already freed in previous liquidation attempts
Margin Shortage Functions
getMarginShortage
Calculates the margin shortage for an account.BN
required
The maintenance margin requirement plus any buffer
BN
required
The total maintenance collateral
BN
The absolute margin shortage amount
Usage Example
Precision Constants
The liquidation functions use various precision constants:Liquidation Process Overview
The liquidation process typically follows these steps:- Determine margin shortage using
getMarginShortage - Calculate max liquidation percentage using
calculateMaxPctToLiquidate - For perp positions: Calculate base asset amount to liquidate using
calculateBaseAssetAmountToCoverMarginShortage - For spot positions:
- Calculate liability transfer using
calculateLiabilityTransferToCoverMarginShortage - Calculate corresponding asset transfer using
calculateAssetTransferForLiabilityTransfer
- Calculate liability transfer using
- Execute liquidation through the Drift Protocol