Skip to main content

parse_combined_account_balances_and_margins

Function parse_combined_account_balances_and_margins 

Source
pub fn parse_combined_account_balances_and_margins(
    perp_state: &ClearinghouseState,
    spot_state: &SpotClearinghouseState,
    abstraction: HyperliquidAccountAbstraction,
) -> Result<(Vec<AccountBalance>, Vec<MarginBalance>)>
Expand description

Merges perp clearinghouse balances with spot balances into a unified set.

Unified and portfolio margin accounts report every balance and hold in the spot clearinghouse state, so balances come from spot alone and the spot USDC hold becomes the account-wide margin entry. That hold covers margin on every USDC-collateralized perp dex plus USDC reserved by resting spot orders; margin on dexes collateralized in another token shows up as that token’s locked balance. The perp summary describes only the default dex in these modes: its totalRawUsd goes negative while longs are open and its withdrawable is per-dex.

Otherwise the perp parser already reflects combined USDC when its cross-margin summary carries collateral or margin state, so this parser appends only non-USDC spot tokens in that case. If the perp state has no margin summary, or the summary is present but zeroed, spot USDC is used verbatim.

Account updates keep any currency an update omits, so spot tokens the venue lists at zero are reported at zero, clearing a previous balance, and USDC is always reported, at zero when there is no USDC balance. On accounts without spot collateral this needs a perp summary; with none, no zeroed entries are reported and the previous balances are kept.

§Errors

Returns an error if any balance conversion fails.