Read the Account
The account reads are methods on the client. Each takes a market, orperps.MarketAll for all of them:
Deposits, Withdrawals and Fundings return one perps.Page at a time. Pass Next back as the cursor until it comes back empty; CompletedOrders takes it in its query:
OpenOrders follows the cursor for you and returns every open order.
Subscribe to Streams
perps.WithSubscriptions adds streams beyond the Order Creation Stream when the connection opens; conn.Subscribe and conn.Unsubscribe change the set later. The set survives reconnects.
Handle Events
Everything the connection receives arrives as a typed event onconn.Events(). Read it steadily. The connection never waits for it: what does not fit in the channel’s buffer waits in the SDK, and once that backlog passes 16 times the buffer (16,384 events at the default WithEventBuffer of 1024), market data is dropped until it has been read. A MarketDataDropped event then says how many were dropped. Order events, account streams and connection events are never dropped.
perps.OrderBookDelta is handled below. perps.OrderFilled and perps.OrderRejected are your orders filling or being refused after the command that placed them returned. perps.Connected and perps.Disconnected report the socket coming and going. perps.Unknown is a message this version of the SDK has no type for.
Keep an Order Book
A local book starts from a snapshot and applies each delta after it, as the Order Book guide describes. Deltas carry no sequence number, so take a new snapshot after every reconnect, and afterperps.MarketDataDropped, which follows deltas dropped while events were not read. perps.Connected arrives for the first socket and for each reconnect.
A snapshot is a request on the connection. It is asked for off the event loop, which has to keep reading events while it waits:
Ts, dropping any older delta that is still queued; discard a snapshot that was requested before a later reconnect. The example has that bookkeeping. Snapshot levels (snapshot.Orders) and delta levels are applied the same way: each replaces the level at its price, and a quantity of 0 removes it.
After a Reconnect
perps.SessionBackfill reports how the session was recovered, on the first connection and after each reconnect. When the session could not be resumed, or a market’s backfill expired, rebuild your order state over HTTP:
Run the Example
The complete program isexamples/perps-streams-and-account in the SDK repository. Run it for as long as you want to watch:
rebuilt line is the fresh session reporting that nothing was resumed.