Skip to main content

build_account_from_balance

Function build_account_from_balance 

Source
pub fn build_account_from_balance(
    account_id: &WalletAddress,
    balance: &PortfolioBalance,
    address: Option<WalletAddress>,
    spot_prices: &HashMap<String, f64>,
) -> Result<Account, BuildAccountError>
Expand description

Build a types::Account from PortfolioService’s internal state.

This converts executed positions (PortfolioBalance) into the format that SPAN/margin code expects (types::Account).

§Arguments

  • account_id - The wallet address / account identifier
  • balance - The internal PortfolioBalance from PortfolioService
  • address - Optional on-chain address (for HyperCore lookups)
  • spot_prices - Map of underlying -> current spot price (e.g., {“BTC”: 100000.0})

§Returns

  • Ok(Account) - A complete, trustworthy Account ready for SPAN margin calculations
  • Err(BuildAccountError) - If any position cannot be safely converted

§Fail-Closed Behavior

This function returns an error (rather than skipping) if:

  • A symbol cannot be parsed
  • A spot price is missing for an underlying with positions
  • An instrument type is not supported