How to parse a Solana transaction in Node.js without web3.js
You do not need @solana/web3.js (or any package) to read a Solana transaction. Node 20 has fetch, and the RPC already returns most of the answer as JSON. Here is the one call, the three fields that matter, and the traps that make a quick script wrong.
The one RPC call
getTransaction with two options you almost always want:
encoding: "jsonParsed": the RPC decodes System and SPL Token instructions for you, and gives every account key with itssignerandwritableflags.maxSupportedTransactionVersion: 0: without it, any v0 transaction (most swaps today) is refused with an error instead of returned.
Working code, zero dependencies
Save as parse-tx.mjs and run node parse-tx.mjs. It uses the free public mainnet RPC unless you set SOLANA_RPC_URL.
// parse-tx.mjs: Node 20+, no dependencies
const RPC = process.env.SOLANA_RPC_URL || "https://api.mainnet-beta.solana.com";
const signature = "x2nTgvSFFneAKW5W5mHcfMYhzen5o7Lfb1pBmepDQQDJZ4ud2Z7QpDxdYVVTVMDqG2e5TJ1qAA8zGDPAxdpudKR";
const res = await fetch(RPC, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
jsonrpc: "2.0", id: 1, method: "getTransaction",
params: [signature, { encoding: "jsonParsed", maxSupportedTransactionVersion: 0, commitment: "confirmed" }],
}),
});
const { result: tx, error } = await res.json();
if (error) throw new Error(`RPC error ${error.code}: ${error.message}`);
if (!tx) throw new Error("transaction not found");
const keys = tx.transaction.message.accountKeys.map((k) => k.pubkey);
console.log("fee payer:", keys[0]);
console.log("status: ", tx.meta.err ? JSON.stringify(tx.meta.err) : "success");
console.log("fee: ", tx.meta.fee, "lamports");
// SOL that moved, per address
tx.meta.preBalances.forEach((pre, i) => {
const change = tx.meta.postBalances[i] - pre;
if (change) console.log("SOL ", keys[i], change / 1e9);
});
// Tokens that moved, per owner wallet (not per token account)
const tokens = new Map();
for (const [list, sign] of [[tx.meta.preTokenBalances, -1n], [tx.meta.postTokenBalances, 1n]]) {
for (const b of list) {
const k = `${b.owner} ${b.mint}`;
const e = tokens.get(k) ?? { raw: 0n, decimals: b.uiTokenAmount.decimals };
e.raw += sign * BigInt(b.uiTokenAmount.amount);
tokens.set(k, e);
}
}
for (const [k, { raw, decimals }] of tokens) if (raw) console.log("TOKEN", k, Number(raw) / 10 ** decimals);
Output, for a USDC transfer on mainnet:
fee payer: H8sMJSCQxfKiFTCfDR3DUMLPwcRbM61LGFJ8N4dK3WjS
status: success
fee: 106500 lamports
SOL H8sMJSCQxfKiFTCfDR3DUMLPwcRbM61LGFJ8N4dK3WjS -0.0001065
TOKEN H8sMJSCQxfKiFTCfDR3DUMLPwcRbM61LGFJ8N4dK3WjS EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v -37.742077
TOKEN 3vKWT4RcdQUx63gfU6dyTGf1fDpoJrQ8PSuHUbF2AxbM EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v 37.742077
What each part is doing
The fee payer is account 0
The first entry of accountKeys always signs and always pays the fee. meta.fee is the whole fee in lamports (1 SOL = 1,000,000,000 lamports), base and priority together.
SOL changes come from two parallel arrays
meta.preBalances[i] and meta.postBalances[i] are the lamports of accountKeys[i] before and after. The fee payer's change includes the fee, which is why the sender above lost 0.0001065 SOL in a transaction that moved no SOL at all.
Token changes are per token account, so group by owner
preTokenBalances and postTokenBalances list token accounts, each with its owner (the wallet) and mint. One wallet can hold the same token in several accounts, and an account can be created or closed in the same transaction, so it may appear in only one of the two lists. Summing pre as negative and post as positive per owner and mint handles all of that. Use BigInt on uiTokenAmount.amount: token amounts often exceed what a JavaScript number holds exactly.
Three traps
- v1 transactions. Newer transactions can be version 1. With
maxSupportedTransactionVersion: 0the RPC answers error-32015, "Transaction version (1) is not supported by the requesting client", and names the version to ask for. Retry with that number. - Lookup tables with
jsonencoding. If you fetch withencoding: "json"instead, a v0 transaction's addresses from lookup tables are not inaccountKeys; they are inmeta.loadedAddresses(writable first, then read-only) and must be appended, or every balance index after them points at the wrong address.jsonParsedmerges them for you. - Failed transactions still cost a fee.
meta.erris not null, the instructions were rolled back, but the fee was charged. Do not report the transfers in the instructions as having happened.
The same thing, finished
Once you also want the priority fee split out, errors in words, programs named, SOL and SPL transfers with their wallets, and swaps, the script grows into a few hundred lines. solana-tx-parse is those lines, MIT licensed and still dependency-free:
npm install github:blixvip/solana-tx-parse
import { parseTransaction } from "solana-tx-parse";
const tx = await parseTransaction("x2nTgvSFFneAKW5W5mHcfMYhzen5o7Lfb1pBmepDQQDJZ4ud2Z7QpDxdYVVTVMDqG2e5TJ1qAA8zGDPAxdpudKR");
console.log(tx.status, tx.feePayer);
console.log(tx.fee.lamports, tx.fee.baseLamports, tx.fee.priorityLamports);
for (const t of tx.transfers) console.log(t.fromOwner, "->", t.toOwner, t.amount, t.mint);
console.log(tx.programs.map((p) => p.label || p.programId));
success H8sMJSCQxfKiFTCfDR3DUMLPwcRbM61LGFJ8N4dK3WjS
106500 5000 101500
H8sMJSCQxfKiFTCfDR3DUMLPwcRbM61LGFJ8N4dK3WjS -> 3vKWT4RcdQUx63gfU6dyTGf1fDpoJrQ8PSuHUbF2AxbM 37.742077 EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v
[ 'Token Program', 'Compute Budget' ]
It retries v1 transactions with the right version, merges lookup tables for any encoding, and if you already have the transaction from a websocket, parseFetchedTransaction(tx) parses it with no network call. Or from a terminal: npx github:blixvip/solana-tx-parse <signature>.
Limits to know
- The public RPC rate limits. A few requests in a row and you get HTTP 429. For more, use the free tier of any RPC provider through
SOLANA_RPC_URL. - Instruction arguments are not decoded here. Balance changes tell you what moved, not which Jupiter route or which Raydium pool instruction did it. That needs the program's IDL.