library / How to use Solana / HS0019
Account data encodings
One TypeScript file reads the same account in five forms, checks that the bytes agree, and decodes a token mint's supply and decimals by hand. Ask for base64 by default, since it works at any account size.
Which encoding to ask for
getAccountInfo answers with the account's lamports, owner program, executable flag, space and data. Everything except data has one shape. The data field changes with the encoding option, and picking the wrong one is the usual reason a first read looks like noise.
| encoding | What data holds | Pick it when |
|---|---|---|
| base64 | A two item array: the base64 text and the word base64 | You decode the bytes yourself. Works for any account size. |
| base64+zstd | The same array, compressed with zstd before the base64 step | The account is large and you want fewer bytes on the wire. |
| base58 | A two item array: base58 text and the word base58 | Only for accounts of 128 bytes or less. Larger ones are refused. |
| jsonParsed | An object with program, parsed and space | The node ships a parser for the owner program, such as the token program. |
jsonParsed is a convenience, and it has a quiet fallback. When the node has no parser for the owner program, it returns the base64 array instead of an object. Code that reads data.parsed without checking the shape fails on exactly those accounts, which tend to be the program-specific ones you cared about.
A dataSlice option with an offset and a length cuts the bytes before they are encoded. It works with base64, base64+zstd and base58. The node refuses a dataSlice sent with jsonParsed and answers with an InvalidRequest error, so you never get a silent full parse. Slicing pays when you need one field out of a large account, such as a single price from an oracle account.
The code
Install @solana/kit at 8.6.0, export CARBIUM_KEY and run the file with Node 24. The address is an optional argument and defaults to the USDC mint, an 82 byte account that every encoding can return.
// HS0019 · Account data encodings// https://carbium.sh/library/howsolana/account-data-encodings// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MITimport { zstdDecompressSync } from "node:zlib";import { address, createDefaultRpcTransport, createSolanaRpcFromTransport, getBase58Encoder,} from "@solana/kit";const key = process.env.CARBIUM_KEY ?? "";if (key === "") { console.error("CARBIUM_KEY is empty: export your Carbium API key first."); process.exit(1);}const rpc = createSolanaRpcFromTransport( createDefaultRpcTransport({ url: process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh", headers: { "x-api-key": key }, }),);const pubkey = address(process.argv[2] ?? "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v");const lines: string[] = [];// Never rejects: a failed or empty read becomes a report line and null.async function attempt<T>(name: string, call: Promise<{ value: T | null }>): Promise<T | null> { try { const { value } = await call; if (value === null) lines.push(`${name}: no account at ${pubkey}`); return value; } catch (err) { lines.push(`${name}: FAILED ${err instanceof Error ? err.message : String(err)}`); return null; }}const [b64, b58, zst, slice, json] = await Promise.all([ attempt("base64", rpc.getAccountInfo(pubkey, { encoding: "base64" }).send()), attempt("base58", rpc.getAccountInfo(pubkey, { encoding: "base58" }).send()), attempt("zstd", rpc.getAccountInfo(pubkey, { encoding: "base64+zstd" }).send()), attempt("slice", rpc.getAccountInfo(pubkey, { encoding: "base64", dataSlice: { offset: 36, length: 8 } }).send()), attempt("parsed", rpc.getAccountInfo(pubkey, { encoding: "jsonParsed" }).send()),]);const raw = b64 ? Buffer.from(b64.data[0], "base64") : undefined;if (b64 && raw) lines.push(`base64: ${raw.length} bytes, ${b64.lamports} lamports, owner ${b64.owner}`);if (b58 && raw) { const same = Buffer.from(getBase58Encoder().encode(b58.data[0])).equals(raw); lines.push(`base58: ${b58.data[0].length} chars, matches base64 ${same}`);}if (zst && raw) { const same = zstdDecompressSync(Buffer.from(zst.data[0], "base64")).equals(raw); lines.push(`zstd: ${zst.data[0].length} chars on the wire, matches base64 ${same}`);}if (slice) { const part = Buffer.from(slice.data[0], "base64"); if (part.length === 8 && raw?.length === 82) { lines.push(`slice: supply ${part.readBigUInt64LE()} from 8 bytes`); } else { lines.push("slice: not an 82-byte mint, nothing to decode"); }}if (raw?.length === 82) { lines.push(`by hand: supply ${raw.readBigUInt64LE(36)}, decimals ${raw[44]}`);}if (json) { const d = json.data as unknown; if (Array.isArray(d)) { lines.push("parsed: no parser for this owner, a base64 array came back"); } else { const { program, parsed } = d as { program: string; parsed: { type: string; info: Record<string, unknown> } }; lines.push(`parsed: ${program} ${parsed.type}, supply ${parsed.info.supply}, decimals ${parsed.info.decimals}`); }}console.log(lines.join("\n"));
run: node main.ts [account-address]
The five reads go out together and are settled one by one, so a failure in one encoding adds a line to the report and leaves the others standing. Left without a commitment, each call uses confirmed, which is the default @solana/kit 8.6.0 fills in. The JSON-RPC server's own default is finalized, so a client that sends the bare request gets that level instead. The base58 bytes come from getBase58Encoder, and the zstd bytes from zstdDecompressSync in node:zlib, so the file needs no other package.
Decoding the bytes by hand
A mint account written by the token program is 82 bytes in a fixed layout. The file reads two fields from it and prints them next to what jsonParsed reports, which is a quick way to confirm the bytes and the parser agree.
| Offset | Size | Field |
|---|---|---|
| 0 | 4 | Mint authority flag, 1 when an authority is set |
| 4 | 32 | Mint authority address |
| 36 | 8 | Supply, unsigned integer, little endian |
| 44 | 1 | Decimals |
| 45 | 1 | Initialized flag |
| 46 | 4 | Freeze authority flag |
| 50 | 32 | Freeze authority address |
The supply is the field to be careful with. Eight bytes hold up to 2^64 - 1, which a JavaScript number cannot carry exactly past 2^53. The file uses readBigUInt64LE and prints a bigint, which matches the supply string in the parsed object digit for digit. The last read asks only for bytes 36 to 43 through dataSlice and decodes the same number from eight bytes of payload. The file does that only when the full read is an 82 byte mint. For a wallet or a short program account it prints a line saying there is nothing to decode and carries on with the other reads.
On Carbium
The file posts to https://services.carbium.sh with the key in the x-api-key header. getAccountInfo is an account read and costs 10 cells, so the five calls cost 50. A rate class absorbs bursts up to 1,024 cells, which is why the five reads fit without a pause on every plan, even on Free at 0.5 account reads per second.
For a loop over many accounts, send them through getMultipleAccounts, which takes the same encoding and dataSlice options and returns an array in the order you sent. It charges 10 cells per pubkey and takes up to 300 pubkeys on Pro. Below Pro, send at most 100 a call, because the burst caps a call there.
Compression pays most when the same large account is polled. A zstd read moves fewer bytes, and the decompression is a call you make on your side. For an 82 byte account the zstd read comes out larger, 124 characters against 112 for plain base64 in the run, which is why the file shows it only to prove the bytes come out identical.
Where it ends
A missing account is not an error. The call succeeds and value is null, in every encoding. A wallet that never received SOL, a closed token account and a mistyped address all look the same, so the file checks for null before it touches data.
Base58 stops at 128 bytes. A mint fits, a token account of 165 bytes does not, and the node answers with an error that tells you to use base64. Treat base58 as a format for short keys and signatures, and use base64 for account data.
The file decodes one layout, the classic token program's mint. A Token-2022 mint carries the same 82 base bytes, then padding up to byte 165, an account type byte and the extension data, and other programs have their own layouts. Reading those means a decoder for that program, such as the Anchor discriminator and field order in Anchor instruction decoder or the extension reads in Token-2022 metadata lookup.
One read is one moment. The lamports, the data and the slot in context belong together, and a second read can land on a later slot with different data. Two encodings of one account fetched in two calls are not a snapshot unless you compare their context.slot values.
When it goes wrong
| what goes wrong | what to do |
|---|---|
| An error from the node on a base58 read, asking for base64. | The account data is over 128 bytes. Use base64 or base64+zstd for anything that is not a short key. |
| value is null and the code throws on value.data. | The address has no account at this commitment. Check the address and the network, then test for null before reading any field. |
| data.parsed is undefined on a jsonParsed read. | The node has no parser for the owner program and sent the base64 array. Test Array.isArray(data) and decode the bytes with the layout of that program. |
| A decoded supply that is off in the last digits. | The u64 went through a JavaScript number. Read it with readBigUInt64LE and keep it as a bigint or a string. |
| An InvalidRequest error on a call that sends a dataSlice with jsonParsed. | The node says sliced data can only be encoded as base58 or base64. Ask for base64 with the slice, and decode the bytes yourself. |
Questions
Which encoding should I use by default?
Use base64. It works for any account size, decodes with one Buffer call and carries the exact bytes. Add jsonParsed only when you want the node to explain a token, stake or vote account for you.
Does the encoding change what a call costs?
No. getAccountInfo is charged as an account read at 10 cells in every encoding. Compression and slicing shrink the bytes that travel and leave the charge at 10 cells.
Can I read the data without any encoding option?
The older default of base58 is deprecated and fails on larger accounts. Always name the encoding, so the shape of data does not depend on the client or the node.
Why does the data come back as a two item array?
The array holds the encoded text first and the name of the encoding second, so a reader can tell the format from the response alone. A parsed read returns an object instead.