Skip to main content

margin_balance_from_user_stats

Function margin_balance_from_user_stats 

Source
pub fn margin_balance_from_user_stats(
    stats: &LighterUserStats,
) -> Result<MarginBalance>
Expand description

Builds the cross-margin [MarginBalance] from a user_stats frame.

Lighter is USDC-collateralized end-to-end. user_stats is the perp-side rollup; we derive:

  • initial = max(collateral - available_balance, 0): collateral currently allocated to open positions/orders
  • maintenance = 0: Lighter does not publish maintenance margin on user_stats. margin_usage looks like a maintenance ratio but is actually the initial-margin-usage percentage ((collateral - available) / collateral * 100), so it carries no information about maintenance. Computing maintenance per-position from the positions stream would be more accurate; until that’s wired we report zero rather than fabricate a value.

instrument_id = None routes this into MarginAccount.account_margins (cross margin keyed by currency), which is what the portfolio reads for venues running unified cross-margin mode.

§Errors

Returns an error if either Money::from_decimal call rejects the value.