services
Get a key

library / How to use Solana / HS0063

Program return data reader

One TypeScript file pulls recent transactions of the associated token program, prints the program id and bytes from meta.returnData decoded as a little-endian u64, and lists every Program return log line beside it. It only reads, so it needs no keypair.

By Carbium · History and parsing · HS0063 · updated 2026-11-22

01

Where return data lives

A program hands a value back to its caller by calling the set-return-data syscall. The value is a short byte string. It is not an account, and it is not part of the instruction data you sent. After the transaction lands, two places keep a record of it.

PlaceValues keptShape
meta.returnData in the getTransaction resultOne at most, the one in the slot at the endAn object with programId and data, where data is a base64 string and the word base64, or absent when the slot ended empty
meta.logMessagesOne line per call to the syscallA text line, Program return: <program id> <base64>

The two disagree whenever the slot changed after a write. That happens when a transaction sets return data more than once, and when a cross-program call runs after the last write, because every such call empties the slot. The field is a single slot. The log is a history, so it is the only place an earlier value survives.

02

Last write wins

A transaction with several instructions can set return data once per instruction, and programs that call other programs add more writes. The runtime holds one slot. Each new write replaces the one before it. Every cross-program call also empties the slot before the callee runs, so meta.returnData holds only what was set after the last call of that kind. A transaction that creates three token accounts through the associated token program makes system and token calls after each size answer, so it ends with an empty slot: the field is absent and the logs keep one Program return: line per create.

Cross-program calls shape the field in a second way. When program A calls program B, the slot is empty as B starts. B's value fills it as B returns, and A reads it with the matching get syscall. If A then sets its own value, the slot changes again, and if A makes another call, the slot is emptied again. So the programId inside meta.returnData names the last program that wrote, which can differ from the top-level program you invoked. Every log line carries its own program id too, so the history tells you who wrote each value in order.

$Read the logs when you need every value a transaction returned, and read meta.returnData when the final value is enough and no call followed it. Walking the instruction tree gets you the same call order with the accounts attached, and the inner instruction tree shows how.
03

The code

Install @solana/kit at 8.6.0, export CARBIUM_KEY and run the file with Node 24. With no argument it lists recent signatures of the associated token program. Creating a token account makes that program ask the token program how many bytes the account needs, and the token program answers through return data. The calls that follow empty the slot, so these transactions carry the answer only in their Program return: log lines, and the default run prints meta.returnData as absent beside them. Pass another address to look at a program of your own.

main.ts
// HS0063 · Program return data reader// https://carbium.sh/library/howsolana/program-return-data-reader// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MITimport {  address,  createDefaultRpcTransport,  createSolanaRpcFromTransport,} from "@solana/kit";type ReturnData = { programId: string; data: readonly [string, string] };const RETURN_PREFIX = "Program return: ";const MAX_LOOKUPS = 18;function readU64LE(raw: Uint8Array): bigint | null {  if (raw.length !== 8) return null;  return new DataView(raw.buffer, raw.byteOffset, raw.length).getBigUint64(0, true);}function describe(rd: ReturnData): void {  const raw = Buffer.from(rd.data[0], "base64");  const value = readU64LE(raw);  console.log(`  meta.returnData: program ${rd.programId}, ${raw.length} bytes, hex ${raw.toString("hex")}`);  console.log(`  as little-endian u64: ${value === null ? "not a single u64" : value}`);}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 } }),);// Default: the associated token program. Creating a token account makes it ask the// token program for an account size, and the token program answers in return data.// The calls that follow empty the slot, so the answer survives only in the log lines.const target = address(process.argv[2] ?? "ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");const page = await rpc.getSignaturesForAddress(target, { limit: 30 }).send();const okSigs = page.filter((entry) => entry.err === null).map((entry) => entry.signature);console.log(`${okSigs.length} successful signatures for ${target}`);let found = false;for (const sig of okSigs.slice(0, MAX_LOOKUPS)) {  try {    const tx = await rpc      .getTransaction(sig, { encoding: "json", maxSupportedTransactionVersion: 1 })      .send();    if (tx === null) {      console.error(`${sig} not found (older than the ledger)`);      continue;    }    const rd: ReturnData | null | undefined = tx.meta?.returnData;    // The field holds what was set after the last CPI; the logs hold one line per write.    const lines = (tx.meta?.logMessages ?? []).filter((line) => line.startsWith(RETURN_PREFIX));    if (!rd && lines.length === 0) continue;    found = true;    console.log(sig);    if (rd) describe(rd);    else console.log("  meta.returnData: absent");    lines.forEach((line, n) => console.log(`  log write ${n + 1}: ${line}`));    break;  } catch (err) {    console.error(`${sig} failed: ${String(err)}`);  }}if (!found) console.log(`No return data field or Program return line in the first ${MAX_LOOKUPS} lookups; run again for newer ones.`);

run: node main.ts [program-address]

The file takes successful signatures only, looks them up one at a time, and stops at the first transaction that has either a meta.returnData field or a Program return: log line, or after eighteen lookups. It prints the field, or says it is absent, with the log lines under it. A failing fetch is printed and the next signature carries on.

Both calls go to https://services.carbium.sh, or to the URL in CARBIUM_RPC_URL. @solana/kit fills in confirmed for a call that leaves commitment out, so both calls read at confirmed. The getTransaction call sets maxSupportedTransactionVersion: 1 and encoding: "json". Each call costs 50 cells, so the eighteen lookups spend 900 cells in their class, inside the 1,024 cells a class absorbs as a burst.

04

Decoding the bytes

The field gives you base64. Decode that to bytes first, then apply the layout of the program named in programId. Nothing in the bytes says how to read them, and the runtime does not check what a program puts there. The layout lives in the program's source, its IDL or its documentation. The same applies to the base64 in a log line.

For the token program's size answer the layout is one unsigned 64-bit integer, little-endian, so the value is eight bytes. A plain token account comes back as 165. Token-2022 accounts come back larger, because each extension adds space. Programs written with Anchor serialize the handler's return value with borsh, so the bytes there are fields in declaration order.

  • A fixed integer such as a u64 takes exactly its width in bytes. Check the length before reading, because a short buffer is a wrong layout.
  • A borsh struct runs its fields in order, with strings and vectors prefixed by a 4-byte length.
  • Raw text or a hash needs no decoding beyond the bytes.

Keep a u64 as a bigint. Values above 2^53 lose digits as a JavaScript number, and amounts in base units reach that range.

05

Size and truncated logs

One write holds at most 1,024 bytes. A program with more to say writes an account and returns a pointer or a hash. The cap applies to each write, and the log line carries the whole value in base64, so a long value makes a long line.

The log has a limit of its own. The runtime keeps about 10 KB of log text per transaction. Once that fills it appends a Log truncated message and drops everything after it. A transaction that logs heavily can lose its Program return: lines, while meta.returnData can still hold the last value because it is not part of the log. When the field and the lines disagree on a busy transaction, check the end of logMessages for the truncation message, and look for a cross-program call after the last write, before you suspect the decoder.

06

Where it ends

The file reads transactions that already landed and skips failed ones. It cannot show what a program would return for a call you have not sent.

The field holds only what was set after the last cross-program call, and it can be empty. Earlier values exist in the logs and nowhere else, and a truncated log loses them. A transaction whose logs were cut and whose slot was emptied or overwritten afterward cannot be reconstructed from the stored record.

getSignaturesForAddress on Carbium reads from an address index that keeps up to 30 days. Its pages reach below the validator's ledger floor, so getTransaction can answer null for an older signature the list returned. The file prints those and moves on. Run it on the newest signatures, as it does by default.

The bytes mean something only with the program's layout. For a program you did not write and whose layout is not published, you get the length and a hex dump, and no more.

07

When it goes wrong

what goes wrongwhat to do
getTransaction answers -32015.The transaction is version 1 and the request left maxSupportedTransactionVersion out or set it to 0. Pass maxSupportedTransactionVersion: 1. Carbium does not charge for the refused call.
meta.returnData prints as absent, or the file finds no hit in eighteen lookups.A value set before a later cross-program call is cleared, so the field is empty even when the logs show a Program return: line. Read the lines in that case. When no line appears either, none of those transactions set return data: run it again for newer signatures, or pass the address of a program that is known to set it.
The decoded number is huge or the u64 line says the layout is not a single u64.The program uses another layout. Check the program id, then the length. Eight bytes is a candidate for a u64. Other lengths need the program's own definition.
Fewer Program return: lines than writes you expected.The log hit its size limit and the runtime added a Log truncated message. The lines after it are gone. The field still holds the last value when no cross-program call followed it.
A signature from the list prints as not found.The signature is older than the validator's ledger floor. The address index lists it and the node no longer holds the transaction. Use newer signatures.
meta.returnData names a program you did not call.A program you called made a cross-program call, and the callee wrote last. Read the log lines in order to see each writer, or decode with the layout of the program named in the field.
08

Questions

How many bytes can a program return?

At most 1,024 per write. A program that needs more stores the data in an account and returns a key or a hash.

Why is there one value in the field and several log lines?

The field is a single slot that each new write replaces and each cross-program call empties. Each write also appends a line to the log, so the log keeps the whole sequence until it is truncated.

Do I need a keypair to read return data?

No. The file reads public data with getSignaturesForAddress and getTransaction. The Carbium key only identifies your account.

Can I decode a value without the program's source?

Only if you know the layout from an IDL or documentation. Without it you have the length and the raw bytes.