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. Account size: 2,360 bytes total (8-byte Anchor discriminator + 2,352-byte PortfolioAccount body).

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.

Use the exported IDL with @coral-xyz/anchor — this is safer than hand-parsing offsets.

Python integration

For Python developers, use anchorpy to interact with the V2 program.

Manual offset map

Use this only for custom parsers or debugging. Layout matches contracts/programs/quantdesk-perp-dex/src/state/portfolio.rs (verify against your deployed program version).

Nested struct sizes

[!TIP] Prefer the latest exported IDL from the QuantDesk SDK repository over manual offsets. Regenerate clients after program upgrades.