MarginContext
A struct that holds the context for margin calculations.Constructor Methods
standard
Create a standard margin context.
margin_type- The type of margin requirement (Initial or Maintenance)
MarginContext with standard settings
Example:
liquidation
Create a liquidation margin context.
margin_buffer- Additional margin buffer in basis points
MarginContext configured for liquidation calculations
Builder Methods
strict
Set strict margin checking mode.
margin_buffer
Set a margin buffer for the calculation.
margin_buffer- Additional margin buffer in basis points
fuel_perp_delta
Set the perp position delta for fuel calculations.
market_index- The perp market indexdelta- The position delta to apply
fuel_spot_delta
Set a single spot position delta for fuel calculations.
fuel_spot_deltas
Set multiple spot position deltas for fuel calculations.
Functions
get_liquidation_fee
Calculate the liquidation fee based on how long a position has been inactive.
base_liquidation_fee- Base liquidation fee in basis pointsmax_liquidation_fee- Maximum liquidation fee in basis pointslast_active_user_slot- The last slot the user was activecurrent_slot- The current blockchain slot
- Fee starts increasing after
LIQUIDATION_FEE_ADJUST_GRACE_PERIOD_SLOTS - Fee increases by
LIQUIDATION_FEE_INCREASE_PER_SLOTfor 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.
margin_shortage- The amount of margin shortage (QUOTE_PRECISION)margin_ratio- The margin ratio for the positionliquidation_fee- The liquidation fee in basis pointsif_liquidation_fee- The insurance fund liquidation feeoracle_price- Current oracle price (PRICE_PRECISION)quote_oracle_price- Quote asset oracle price (PRICE_PRECISION)
u64::MAX if the position cannot be liquidated profitably
calculate_perp_if_fee
Calculate the insurance fund fee for perp liquidations.
margin_shortage- The margin shortage amountuser_base_asset_amount- User’s base asset amountmargin_ratio- Margin ratio for the marketliquidator_fee- Fee paid to liquidatororacle_price- Current oracle pricequote_oracle_price- Quote asset pricemax_if_liquidation_fee- Maximum IF fee allowed
Utility Functions
standardize_price
Round a price to the nearest valid tick size.
price- The price to standardize (PRICE_PRECISION)tick_size- The market’s tick sizedirection- Long rounds down, Short rounds up
tick_size is 0
standardize_price_i64
Round a signed price to the nearest valid tick size.
standardize_base_asset_amount
Round a base asset amount down to the nearest step size.
base_asset_amount- The amount to standardize (BASE_PRECISION)step_size- The market’s step size
step_size is 0
standardize_base_asset_amount_ceil
Round a base asset amount up to the nearest step size.
step_size is 0