Skip to main content

parse_derive_subaccount_to_balances

Function parse_derive_subaccount_to_balances 

Source
pub fn parse_derive_subaccount_to_balances(
    subaccount: &DeriveSubaccount,
) -> Result<(Vec<AccountBalance>, Vec<MarginBalance>, Params)>
Expand description

Derives balances, margins, and supplemental info from a DeriveSubaccount snapshot.

Each collateral row becomes one [AccountBalance] in the collateral’s own units, rounded to its registered currency precision, with total = amount and locked = 0. A subset of each collateral row remains in [Params] with decimal strings exact within Decimal range and scale. The venue’s collaterals[].initial_margin is USD credit, not locked funds. Pending vault-deposit holds remain metadata because their inclusion in collateral amounts is not specified by the venue.

Portfolio requirements form one account-wide [MarginBalance] in USD: initial = positions_value - positions_initial_margin - open_orders_margin and maintenance = positions_value - positions_maintenance_margin. The source margin fields are signed health contributions; position value removes their mark-to-market credit, and open-order margin is a signed reservation. These aggregates preserve the venue’s portfolio offsets and round to native USD precision. Source margin components and signed net health remain decimal strings in [Params] within Decimal range and scale. Metadata also includes the manager, risk universe, margin model, and liquidation status.

§Errors

Returns an error if the venue marks the snapshot failed_to_fetch, an embedded position cannot be represented exactly, a balance exceeds the native range, or aggregate requirements overflow.