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/ordersmaintenance = 0: Lighter does not publish maintenance margin onuser_stats.margin_usagelooks 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.