services
Get a key

library / How to use Solana / HS0045

Same-slot multi-account snapshot

One TypeScript file reads two mints and a busy token account in a single getMultipleAccounts call, then reads the same keys one by one. It prints every context slot and says whether the slots differ.

By Carbium · Accounts and balances · HS0045 · updated 2026-11-22

01

One call, one slot

A single getMultipleAccounts request is answered by one node from one bank, the node's state at one slot. Every account in the value array comes from that bank, so the balances of two vaults in one answer were true at the same instant. The reply names the instant: context.slot sits once beside the array and applies to every entry in it.

That number is the proof. Accounts read in separate calls each carry their own context.slot, and a pool whose reserves moved between the calls hands you a pair that never existed together. Say a swap raises vault A and lowers vault B between your two reads. You see the old A with the new B, a reserve pair, and so a price, that the pool never had. One call to getMultipleAccounts removes that case.

The tear lives between accounts. Inside one account the lamports, owner and data always come from a single version of it, so the only gap to close is the one between the keys you ask for.

02

The code

Install @solana/kit at 8.6.0, export CARBIUM_KEY and run the file with Node 24. It takes no argument. The keys are the USDC mint, the wrapped SOL mint and a busy USDC token account that the file finds itself: it lists the newest signatures on the USDC mint with getSignaturesForAddress, fetches transactions with getTransaction in jsonParsed form and takes the first USDC token account in the post-balances. The lookup is one getSignaturesForAddress call plus up to five getTransaction calls, each at 50 cells.

Every call runs at confirmed. The kit fills in that level when a call leaves commitment out, while the JSON-RPC server alone would answer at finalized, so the file passes it on every call and both sides of the comparison read at one level. The getTransaction call carries maxSupportedTransactionVersion: 1, since a v1 transaction answers -32015 without it.

main.ts
// HS0045 · Same-slot multi-account snapshot// https://carbium.sh/library/howsolana/same-slot-multi-account-snapshot// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MITimport { address, createDefaultRpcTransport, createSolanaRpcFromTransport, type Address } from "@solana/kit";const key = process.env.CARBIUM_KEY;if (!key) {  console.error("Set CARBIUM_KEY to your Carbium API key.");  process.exit(1);}const url = process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh";const rpc = createSolanaRpcFromTransport(  createDefaultRpcTransport({ url, headers: { "x-api-key": key } }),);const commitment = "confirmed";const USDC = address("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");const WSOL = address("So11111111111111111111111111111111111111112");type Parsed = {  transaction: { message: { accountKeys: { pubkey: string }[] } };  meta: { postTokenBalances?: { accountIndex: number; mint: string }[] } | null;};// A token account that holds USDC, taken from the newest transactions on the mint.async function busyTokenAccount(): Promise<Address | undefined> {  const sigs = await rpc.getSignaturesForAddress(USDC, { commitment, limit: 5 }).send();  for (const { signature } of sigs) {    const tx = await rpc      .getTransaction(signature, { commitment, encoding: "jsonParsed", maxSupportedTransactionVersion: 1 })      .send();    if (!tx) continue; // the address index can list signatures the node no longer holds    const { transaction, meta } = tx as unknown as Parsed;    const hit = meta?.postTokenBalances?.find((b) => b.mint === USDC);    const pubkey = hit && transaction.message.accountKeys[hit.accountIndex]?.pubkey;    if (pubkey) return address(pubkey);  }  return undefined;}const found = await busyTokenAccount().catch((e) => {  console.error(`busy token account lookup failed: ${e}`);  return undefined;});const keys = found ? [USDC, WSOL, found] : [USDC, WSOL];const config = { commitment, encoding: "base64" } as const;const [[multi], singles] = await Promise.all([  Promise.allSettled([rpc.getMultipleAccounts(keys, config).send()]),  Promise.allSettled(keys.map((k) => rpc.getAccountInfo(k, config).send())),]);const slots: bigint[] = [];if (multi.status === "fulfilled") {  const { context, value } = multi.value;  slots.push(context.slot);  console.log(`getMultipleAccounts: ${keys.length} keys, one context slot ${context.slot}`);  value.forEach((a, i) => console.log(`  ${keys[i]}  ${a ? `${a.lamports} lamports` : "missing"}`));} else {  console.error(`getMultipleAccounts failed: ${multi.reason}`);}singles.forEach((r, i) => {  if (r.status === "fulfilled") {    slots.push(r.value.context.slot);    console.log(`getAccountInfo ${keys[i]}: context slot ${r.value.context.slot}`);  } else {    console.error(`getAccountInfo ${keys[i]} failed: ${r.reason}`);  }});const distinct = Array.from(new Set(slots));console.log(  distinct.length > 1    ? `context slots differ: ${distinct.join(", ")}`    : `all ${slots.length} answers share slot ${distinct[0]}`,);

run: node main.ts

The file sends one getMultipleAccounts call for all the keys and, in the same moment, one getAccountInfo per key. Each result is settled on its own, so a failure prints and the others still report. The last line collects every context slot and says whether they differ. The multi call shows one slot for all its keys. The separate calls leave their slots to chance, and with a slot lasting about 200 ms they often split across two or three.

03

Past 100 keys

On Carbium a getMultipleAccounts call costs 10 cells per pubkey, so 100 keys cost 1000 cells, or 1001 when the request body goes past 4 KiB, which a 100-key body usually does. A call takes up to 300 pubkeys on Pro. Below Pro the 1024-cell burst caps a call at 100, so code written for another plan sends at most 100 keys a call. Account reads run at 15 req/s on Dev, 50 on Business and 400 on Pro.

Beyond the cap you send chunks, and each chunk is its own call with its own slot. The same-slot guarantee covers one chunk. A set of 250 keys on Pro is one call and one slot. The same set on Dev is three chunks and up to three slots. Two ways keep the numbers that matter together:

  • Put the keys that must agree, such as both vaults of one pool, in the same chunk, and let the rest fall where they land.
  • When keys that must agree cannot share a chunk, send every chunk with the same minContextSlot, set to the highest context.slot seen so far. A node behind that slot refuses with -32016 and answers nothing from an older view. When all chunks are back, compare their context slots: all equal means the set is one snapshot, otherwise raise the floor to the highest slot and re-read the chunks that came back lower.

A floor only bounds a read from below, so a re-read can land above the slot you wanted and leave the other chunks behind. Cap the rounds at a few and treat a miss as a failed read. Each round costs 10 cells for every pubkey you read again. For the retry side of a pinned read, see Consistent reads with minContextSlot.

04

What one slot does not promise

A shared slot makes the accounts agree with each other. It says nothing about whether that slot survives. At processed, a slot can still be dropped when its fork loses, and an answer from it describes a history that may never become final.

Use finalized when the numbers go into a ledger or a decision you cannot take back, and confirmed for a display. See Solana commitment levels for the ladder.

The keys also have to be known before the read. A wallet can own more than one token account for one mint, so a lookup such as getTokenAccountsByOwner is its own call at its own slot. Find the accounts first, then read them together, and expect that an account opened in between is missing from your set.

$The reply's context.slot is the check. Compare it against the slots of any other reads before you add numbers from different calls.
05

Where it ends

The snapshot covers the accounts of one call and nothing else. Transactions that landed in the slot, signatures and the order of writes inside a slot come from other calls and carry their own slots.

The file prints the slots and stops there. It does not retry, pin a floor or chunk past 100 keys, so it shows the property and leaves the loop from the section above to your code. It also prints raw lamports. Token balances need the account data decoded first, which Token account layout decoder walks through.

The busy token account is found at run time and changes on every run. getSignaturesForAddress pages come from an address index that keeps up to 30 days, so a signature it lists can answer null from getTransaction. The file moves to the next signature in that case and falls back to the two mints when none yields a token account.

A snapshot that is true at a slot is already in the past by the time it reaches you. For a vault watched continuously, a protocol vault drain alert reads it repeatedly and compares each answer with the last.

06

When it goes wrong

what goes wrongwhat to do
The context slots of the separate calls differ.That is expected. Each call picks its own bank. Read values that must agree in one getMultipleAccounts call.
A read with minContextSlot fails with -32016.The node has not reached the floor yet. Wait about one slot and try again, and stop after a few tries.
A call with more than 100 keys fails below Pro.The 1024-cell burst caps a call at 100 pubkeys on other plans. Send chunks of 100, or move to Pro for up to 300 a call.
getTransaction answers -32015 for the transaction the file picked.The transaction is v1. Pass maxSupportedTransactionVersion: 1 with an encoding, as the file does.
getTransaction answers null for a signature the list returned.The signature index reaches below the validator's ledger floor. Move to the next signature, as the file does.
The file stops with Set CARBIUM_KEY to your Carbium API key.Export CARBIUM_KEY in the shell that runs node main.ts. The file reads it from the environment and never prints it.
07

Questions

Does getMultipleAccounts guarantee no torn reads?

Within one call, yes: every account comes from the same bank, and the reply names its slot in context.slot. Across calls or chunks there is no such promise, so compare the slots.

Does a JSON-RPC batch give the same guarantee?

A batch holds several calls in one request, and each answer carries its own context.slot. Check them instead of assuming they match. Of these, only getMultipleAccounts reads a fixed list of keys from one bank.

Does one call cost less than separate reads?

No. A getAccountInfo call costs 10 cells and getMultipleAccounts costs 10 cells per pubkey, so the same keys cost about the same. The gain is one slot and fewer round trips.

Which commitment should a same-slot read use?

Use confirmed for a display and finalized for anything final. Pass it on every call, since the server and the kit fill in different levels when it is left out.