Skip to main content

Parsing the PortfolioAccount buffer

QuantDesk V2 uses a zero-copy architecture for the PortfolioAccount. This maps the raw account data directly into local memory structures, so you get high-performance reads.

Why zero-copy?

Standard Anchor accounts deserialize on every fetch, which is slow and CPU-intensive for large accounts with many positions. Zero-copy lets you:
  • Avoid deserialization: read fields directly from the buffer.
  • Sub-ms latency: critical for high-frequency trading bots.
  • Atomic state: fetch balances, positions, and orders in a single RPC call.

TypeScript integration

Use the @coral-xyz/anchor library along with bytemuck-style offsets to decode the buffer.
import * as anchor from "@coral-xyz/anchor";

const portfolioPda = ...; // Derived PDA
const accountInfo = await connection.getAccountInfo(portfolioPda);

if (accountInfo) {
  // Use the IDL to decode the zero-copy account
  const portfolio = program.account.portfolioAccount.coder.accounts.decode(
    "PortfolioAccount",
    accountInfo.data
  );

  console.log("Balances:", portfolio.balances);
  console.log("Positions:", portfolio.positions);
  console.log("Open Orders:", portfolio.orders);
}

Python integration

For Python developers, use anchorpy to interact with the V2 program.
from anchorpy import Context, Program
from solana.rpc.async_api import AsyncClient

async def get_portfolio(program: Program, pda: PublicKey):
    account_info = await program.provider.connection.get_account_info(pda)
    
    if account_info.value:
        # Decode the zero-copy buffer
        portfolio = await program.account["PortfolioAccount"].fetch(pda)
        
        print(f"Balances: {portfolio.balances}")
        print(f"Positions: {portfolio.positions}")
        print(f"Orders: {portfolio.orders}")

Offset mapping

If you are building a custom high-performance parser without a full IDL client, account for the 8-byte Anchor discriminator and the zero-copy memory layout defined in the quantdesk-v2 crate.
FieldTypeOffset
Discriminator[u8; 8]0
OwnerPubkey8
Balances[Balance; 16]40
Positions[Position; 32]
[!TIP] Always use the latest exported IDL from our GitHub repository to ensure offset alignment.