Skip to main content
The margin module provides utilities for calculating margin requirements, collateral, and leverage for user positions.

MarginContext

A struct that holds the context for margin calculations.
pub struct MarginContext {
    pub margin_type: MarginRequirementType,
    pub mode: MarginCalculationMode,
    pub strict: bool,
    pub margin_buffer: u128,
    pub fuel_bonus_numerator: i64,
    pub fuel_bonus: u64,
    pub fuel_perp_delta: Option<(u16, i64)>,
    pub fuel_spot_deltas: [(u16, i128); 2],
}

Constructor Methods

standard

Create a standard margin context.
pub fn standard(margin_type: MarginRequirementType) -> Self
Parameters:
  • margin_type - The type of margin requirement (Initial or Maintenance)
Returns: A new MarginContext with standard settings Example:
let ctx = MarginContext::standard(MarginRequirementType::Initial);

liquidation

Create a liquidation margin context.
pub fn liquidation(margin_buffer: u32) -> Self
Parameters:
  • margin_buffer - Additional margin buffer in basis points
Returns: A new MarginContext configured for liquidation calculations

Builder Methods

strict

Set strict margin checking mode.
pub fn strict(mut self, strict: bool) -> Self

margin_buffer

Set a margin buffer for the calculation.
pub fn margin_buffer(mut self, margin_buffer: u32) -> Self
Parameters:
  • margin_buffer - Additional margin buffer in basis points

fuel_perp_delta

Set the perp position delta for fuel calculations.
pub fn fuel_perp_delta(mut self, market_index: u16, delta: i64) -> Self
Parameters:
  • market_index - The perp market index
  • delta - The position delta to apply

fuel_spot_delta

Set a single spot position delta for fuel calculations.
pub fn fuel_spot_delta(mut self, market_index: u16, delta: i128) -> Self

fuel_spot_deltas

Set multiple spot position deltas for fuel calculations.
pub fn fuel_spot_deltas(mut self, deltas: [(u16, i128); 2]) -> Self

Functions

get_liquidation_fee

Calculate the liquidation fee based on how long a position has been inactive.
pub fn get_liquidation_fee(
    base_liquidation_fee: u32,
    max_liquidation_fee: u32,
    last_active_user_slot: u64,
    current_slot: u64,
) -> SdkResult<u32>
Parameters:
  • base_liquidation_fee - Base liquidation fee in basis points
  • max_liquidation_fee - Maximum liquidation fee in basis points
  • last_active_user_slot - The last slot the user was active
  • current_slot - The current blockchain slot
Returns: The calculated liquidation fee, which increases with time elapsed since last activity Notes:
  • Fee starts increasing after LIQUIDATION_FEE_ADJUST_GRACE_PERIOD_SLOTS
  • Fee increases by LIQUIDATION_FEE_INCREASE_PER_SLOT for each slot elapsed
  • Fee is capped at max_liquidation_fee

calculate_base_asset_amount_to_cover_margin_shortage

Calculate how much base asset needs to be liquidated to cover a margin shortage.
pub fn calculate_base_asset_amount_to_cover_margin_shortage(
    margin_shortage: u128,
    margin_ratio: u32,
    liquidation_fee: u32,
    if_liquidation_fee: u32,
    oracle_price: i64,
    quote_oracle_price: i64,
) -> SdkResult<u64>
Parameters:
  • margin_shortage - The amount of margin shortage (QUOTE_PRECISION)
  • margin_ratio - The margin ratio for the position
  • liquidation_fee - The liquidation fee in basis points
  • if_liquidation_fee - The insurance fund liquidation fee
  • oracle_price - Current oracle price (PRICE_PRECISION)
  • quote_oracle_price - Quote asset oracle price (PRICE_PRECISION)
Returns: The amount of base asset to liquidate (BASE_PRECISION), or u64::MAX if the position cannot be liquidated profitably

calculate_perp_if_fee

Calculate the insurance fund fee for perp liquidations.
pub fn calculate_perp_if_fee(
    margin_shortage: u128,
    user_base_asset_amount: u64,
    margin_ratio: u32,
    liquidator_fee: u32,
    oracle_price: i64,
    quote_oracle_price: i64,
    max_if_liquidation_fee: u32,
) -> u32
Parameters:
  • margin_shortage - The margin shortage amount
  • user_base_asset_amount - User’s base asset amount
  • margin_ratio - Margin ratio for the market
  • liquidator_fee - Fee paid to liquidator
  • oracle_price - Current oracle price
  • quote_oracle_price - Quote asset price
  • max_if_liquidation_fee - Maximum IF fee allowed
Returns: The calculated insurance fund fee (scaled by 95%)

Utility Functions

standardize_price

Round a price to the nearest valid tick size.
pub fn standardize_price(
    price: u64,
    tick_size: u64,
    direction: PositionDirection
) -> u64
Parameters:
  • price - The price to standardize (PRICE_PRECISION)
  • tick_size - The market’s tick size
  • direction - Long rounds down, Short rounds up
Returns: The standardized price Panics: If tick_size is 0

standardize_price_i64

Round a signed price to the nearest valid tick size.
pub fn standardize_price_i64(
    price: i64,
    tick_size: u64,
    direction: PositionDirection
) -> i64

standardize_base_asset_amount

Round a base asset amount down to the nearest step size.
pub fn standardize_base_asset_amount(
    base_asset_amount: u64,
    step_size: u64
) -> u64
Parameters:
  • base_asset_amount - The amount to standardize (BASE_PRECISION)
  • step_size - The market’s step size
Returns: The standardized amount (rounded down) Panics: If step_size is 0

standardize_base_asset_amount_ceil

Round a base asset amount up to the nearest step size.
pub fn standardize_base_asset_amount_ceil(
    base_asset_amount: u64,
    step_size: u64
) -> u64
Returns: The standardized amount (rounded up) Panics: If step_size is 0

Build docs developers (and LLMs) love