Reading data#
The canonical v1 clients use generated account readers. Fetch finalized bytes
from your RPC provider, derive the expected PDA, and decode through
@seesaw/core; malformed or mismatched accounts fail closed.
Derive PDAs#
If you already know the complete market identity, derive its PDA directly. A timestamp and duration alone are insufficient: the creator is part of the native-market seed tuple. Discover candidate accounts first when that tuple is unknown.
// docs-check: semantic
import type { Address } from '@solana/addresses';
import {
deriveConfigPda,
deriveMarketPda,
deriveOrderbookPda,
derivePositionPda,
deriveTraderLedgerPda,
} from '@seesaw/core';
export async function deriveAccounts(
feedId: Uint8Array,
durationSeconds: bigint,
marketId: bigint,
creator: Address,
currencyRecord: Address,
wallet: Address
) {
const [config] = await deriveConfigPda();
const [market] = await deriveMarketPda(
feedId,
durationSeconds,
marketId,
creator,
currencyRecord
);
const [orderbook] = await deriveOrderbookPda(market);
const [position] = await derivePositionPda(market, wallet);
const [ledger] = await deriveTraderLedgerPda(market);
return { config, market, orderbook, position, ledger };
}
The complete seed and layout tables live in the account reference
and spec/ACCOUNTS.md. Never accept a caller-supplied account merely because
it is owned by the program; verify discriminator, exact size/version, PDA, and
related-market fields.
Discover native markets by creator#
This read-only recipe calls Solana getProgramAccounts, filters by the generated
market discriminator and exact market account size, and optionally filters the
creator field at byte offset 284. The response is still validated after filtering.
Market accounts have one fixed size; it is their orderbooks, not their market
accounts, that have per-capacity sizes.
// docs-check: semantic
import { createSolanaRpc } from '@solana/rpc';
import { getAddressEncoder, type Address } from '@solana/addresses';
import { getBase64Decoder, getBase64Encoder } from '@solana/codecs-strings';
import type { Base64EncodedBytes, GetProgramAccountsMemcmpFilter } from '@solana/rpc-types';
import {
MARKET_ACCOUNT_DISCRIMINATOR,
MARKET_ACCOUNT_SIZE,
decodeMarketAccount,
deriveCurrencyRecordPda,
deriveMarketPda,
} from '@seesaw/core';
const TOKEN_PROGRAM_ADDRESS = 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA' as Address;
const TOKEN_2022_PROGRAM_ADDRESS = 'TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb' as Address;
export async function discoverNativeMarkets(rpcUrl: string, program: Address, creator?: Address) {
const toBase64 = (bytes: Uint8Array): Base64EncodedBytes =>
getBase64Decoder().decode(bytes) as Base64EncodedBytes;
const creatorFilter: GetProgramAccountsMemcmpFilter[] = creator
? [
{
memcmp: {
offset: 284n,
bytes: toBase64(new Uint8Array(getAddressEncoder().encode(creator))),
encoding: 'base64',
},
},
]
: [];
const response = await createSolanaRpc(rpcUrl)
.getProgramAccounts(program, {
commitment: 'finalized',
encoding: 'base64',
withContext: true,
filters: [
{ dataSize: BigInt(MARKET_ACCOUNT_SIZE) },
{
memcmp: { offset: 0n, bytes: toBase64(MARKET_ACCOUNT_DISCRIMINATOR), encoding: 'base64' },
},
...creatorFilter,
],
})
.send();
const markets = [];
for (const { pubkey, account } of response.value) {
if (account.owner !== program) throw new Error('Unexpected account owner');
const market = decodeMarketAccount(new Uint8Array(getBase64Encoder().encode(account.data[0])));
if (market.marketKind !== 0) continue; // External identity uses MarketMeta, not native seeds.
const [currencyRecord] = await deriveCurrencyRecordPda(
market.settlementMint,
market.settlementTokenStandard === 1 ? TOKEN_2022_PROGRAM_ADDRESS : TOKEN_PROGRAM_ADDRESS,
program
);
const [expected, bump] = await deriveMarketPda(
market.pythFeedId,
market.durationSeconds,
market.marketId,
market.creator,
currencyRecord,
program
);
if (pubkey !== expected || market.bump !== bump || (creator && market.creator !== creator)) {
throw new Error('Market identity mismatch');
}
markets.push({ address: pubkey, market, slot: response.context.slot });
}
return markets;
}
RPC operators may limit program-wide scans; surface that failure rather than
returning an empty catalog. Persist discovered identities and refresh them with
account subscriptions or bounded scans. External markets require the
market_ext and market_meta identity checks in PDAs.
Find positions, orders, and free funds#
For positions, use POSITION_ACCOUNT_DISCRIMINATOR and POSITION_ACCOUNT_SIZE
with the same query pattern, plus a memcmp filter at owner offset 40 and,
optionally, market offset 8. Then decode each position and verify its canonical
PDA against both stored keys. These filters select candidates; they do not
replace ownership, layout, or PDA validation.
For an individual market, deriving the orderbook and ledger PDAs is cheaper than
a program-wide scan. Decode the entire supported book and filter bids and
asks by the order's full owner key. Read remaining quantity, orderId,
status, and maxAgeSeconds; an expired claimable record is not an active quote.
Order records are embedded in the book, so an account-level memcmp is not a
general query for all orders belonging to a wallet.
When enumerating orderbook accounts themselves, issue one dataSize query for
the single canonical DEEP_ORDERBOOK_ACCOUNT_SIZE, alongside
DEEP_ORDERBOOK_ACCOUNT_DISCRIMINATOR. A single market-account size
filter will not find those books. Validate the decoded parent market and derived
book address before using any order.
Decode the trader ledger and find the slot whose owner equals the wallet. Its free quote/YES/NO buckets belong only to that market. See Monitoring for custody reconciliation and why ledger funds must not be double-counted with the position's accounting fields.
Decode generated accounts#
// docs-check: semantic
import type { Address } from '@solana/addresses';
import {
decodeConfigAccount,
decodeMarketAccount,
decodeDeepOrderbookAccount,
decodePositionAccount,
decodeTraderLedgerAccount,
} from '@seesaw/core';
export async function decodeAccounts(
accounts: {
config: Address;
market: Address;
orderbook: Address;
position: Address;
ledger: Address;
},
fetchFinalizedBytes: (address: Address) => Promise<Uint8Array>
) {
return {
config: decodeConfigAccount(await fetchFinalizedBytes(accounts.config)),
market: decodeMarketAccount(await fetchFinalizedBytes(accounts.market)),
orderbook: decodeDeepOrderbookAccount(await fetchFinalizedBytes(accounts.orderbook)),
position: decodePositionAccount(await fetchFinalizedBytes(accounts.position)),
ledger: decodeTraderLedgerAccount(await fetchFinalizedBytes(accounts.ledger)),
};
}
Pass the addresses returned by deriveAccounts into decodeAccounts.
fetchFinalizedBytes is an application adapter, not an SDK export: it must
reject missing accounts and unexpected owners before returning binary data.
Decoders validate bytes; callers must verify address and related-account identity.
Deep orderbooks use only the supported capacities 64, 128, 256, 512, 1024, 2048, 4096; the requested capacity must match both sides. Generated readers
also enforce reserved-byte zeroes, occupied-slot counts, and compatibility
version rules.
Signed-source markets#
For an external market, read the canonical definition and revision from its
SourceState account alongside the market and the relevant source policy.
Keep the source reference and accepted evidence digests with the Seesaw market
address. See signed-source integration for requesting and
submitting an observation.
Commitment and freshness#
Use finalized commitment for user-facing balances and settlement decisions.
Whatever fetch helper you wrap, pass the commitment explicitly rather than
relying on a provider default:
// docs-check: semantic
import { createSolanaRpc } from '@solana/rpc';
import type { Address } from '@solana/addresses';
export async function fetchFinalizedAccount(rpcUrl: string, market: Address) {
const rpc = createSolanaRpc(rpcUrl);
const { value } = await rpc
.getAccountInfo(market, { encoding: 'base64', commitment: 'finalized' })
.send();
return value; // Check owner and decode value.data before passing bytes to a decoder.
}
For live orderbook displays a lower commitment may be used, but only with an explicit slot and staleness indicator. Never mix data from different slots without recording the slot identity in the projection.
Estimating fills and slippage#
Use the generated deep-orderbook reader to obtain the current bid/ask ladder, then apply the same checked arithmetic as the on-chain matcher. Treat the result as a quote, not a settlement guarantee: a later slot can change the book before a signed transaction lands.
API responses and stream freshness#
Use the response schema for the selected endpoint. Exchange list responses use markets; detail responses use market.
exchangeMarketsResponseSchema checks the exchange list envelope at runtime.
The read-only agent reports missing and malformed inputs without inventing a price.
The colon-channel stream has no resume-from-sequence contract.
After each onSession, fetch a fresh authoritative snapshot before exposing current state.
Treat stream messages as invalidations when the message cannot prove a complete, ordered update.
Do not apply a guessed sequence protocol to these messages.
packages/core/examples/13-stream-snapshot.ts uses each update to request a full validated snapshot.
It discards responses from older requests and older sessions.
It returns null after expiry, a failed refresh, malformed data, or subscription rejection.
Connect its handlers to createSeesawStreams for one channel.
The default freshness limit is 5,000 milliseconds. Select a limit for the application's risk requirements.
The server timestamp check requires a synchronized host clock. Neither timestamp proves chain authenticity.
Run packages/core/src/__tests__/documentation-examples.test.ts for reconnect races, stale responses, and malformed envelopes.
Run packages/core/src/__tests__/streams.test.ts for transport session and subscription behavior.
API and direct-RPC fallback#
Use createResilientDataSource with a configured RpcDataSource for supported fallback reads.
The source validates API responses and uses direct RPC for supported failures.
readMarket, readPosition, scanPositionsByOwner, and scanOrdersByOwner validate their account relationships.
Retain source and slot metadata. Do not combine observations from different slots as an atomic snapshot.
Account decoding alone does not validate owner or PDA. Call the validated read helper or perform those checks explicitly.
Run packages/core/src/__tests__/resilientData.test.ts and packages/core/src/__tests__/rpcDataSource.test.ts for unavailable API and invalid-account fixtures.
These fixtures do not establish provider independence, live discovery completeness, or deployed service availability.
Cross-language vectors#
The Rust and Python SDKs consume the same account and instruction vectors in
packages/test-vectors. Run bun --filter @seesaw/test-vectors test and the
language-specific test suites before publishing a client.