The margin calculation utilities provide functions for determining margin requirements, calculating collateral values, and computing liquidation prices for positions.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/drift-labs/protocol-v2/llms.txt
Use this file to discover all available pages before exploring further.
Margin Weight Functions
calculateSizePremiumLiabilityWeight
Calculates the size-adjusted liability weight for a position.Position size in AMM_RESERVE_PRECISION
Initial margin fraction factor
Base liability weight
Precision to use for calculations
Whether to bound the result to the base liability weight
Size-adjusted liability weight
Usage Example
calculateSizeDiscountAssetWeight
Calculates the size-adjusted asset weight for a position.Position size in AMM_RESERVE_PRECISION
Initial margin fraction factor
Base asset weight
Size-adjusted asset weight (minimum of base weight and calculated discount weight)
Oracle Price Functions
calculateOraclePriceForPerpMargin
Calculates the oracle price adjusted for margin calculations with spread and confidence intervals.The perpetual position
The perpetual market account
Oracle price data including confidence interval
Adjusted oracle price for margin calculations
Usage Example
Base Asset Value Functions
calculateBaseAssetValueWithOracle
Calculates the base asset value using oracle price. For prediction markets, this differs from liability value.The perpetual market account
The perpetual position
Oracle price data
Whether to include open orders in calculation
Base asset value in quote precision
Usage Example
calculateWorstCaseBaseAssetAmount
Calculates the worst-case base asset amount including open orders.The perpetual position
The perpetual market account
Oracle price
Worst-case base asset amount
calculateWorstCasePerpLiabilityValue
Calculates the worst-case liability value for a position.The perpetual position
The perpetual market account
Oracle price
Whether to include open orders
Object containing worst-case base asset amount and liability value
calculatePerpLiabilityValue
Calculates the liability value for a given base asset amount.Base asset amount
Oracle price
Whether this is a prediction market
Liability value. For prediction markets, shorts use (1 - price) * base
Margin Requirement Functions
calculateMarginUSDCRequiredForTrade
Calculates the margin required to open a trade in USDC.The Drift client instance
Market index for the trade
Size of the trade
User’s maximum margin ratio
Whether user is in high leverage mode
Expected entry price (uses oracle price if not provided)
Margin required in USDC
Usage Example
calculateCollateralDepositRequiredForTrade
Calculates the collateral deposit required for a trade in a specific collateral asset.The Drift client instance
Market index for the trade
Size of the trade
Spot market index for the collateral asset
User’s maximum margin ratio
Whether user is in high leverage mode
Estimated entry price
Collateral required in the precision of the target collateral market
calculateCollateralValueOfDeposit
Calculates the collateral value of a deposit.The Drift client instance
Spot market index for the collateral
Amount to deposit in base units
Collateral value in QUOTE_PRECISION
Liquidation Price Functions
calculateLiquidationPrice
Calculates the liquidation price for a position.Current free collateral
Change in free collateral per price change
Current oracle price
Liquidation price. Returns -1 if calculated price is negative
Usage Example
User Position Functions
calculateUserMaxPerpOrderSize
Calculates the maximum order size a user can place.The Drift client instance
User account public key
User account data
Market index for the trade
Direction of the trade (LONG or SHORT)
Object containing max trade size for requested side and opposite side
calcHighLeverageModeInitialMarginRatioFromSize
Calculates the initial margin ratio for high leverage mode based on position size.Margin ratio before size adjustment
Size-adjusted margin ratio
Default margin ratio for the market
Calculated initial margin ratio for high leverage mode