#solana #tools

Replaying a failed Solana transaction locally

When a transaction fails on mainnet, the explorer gives you an error code and a wall of logs. I wanted to run the exact same transaction again on my laptop, with the same state, and see what went wrong. This is the setup.

fig. 1 — from a signature to a root cause

get the transaction

I use a Helius RPC endpoint and a small TypeScript script, run with npx tsx. It takes a signature and prints the status, compute units, logs and which programs were called.

fetch-tx.tstypescript
import { Connection } from "@solana/web3.js";

const conn = new Connection(process.env.RPC_URL!);
const sig = process.argv[2];

const tx = await conn.getTransaction(sig, {
  maxSupportedTransactionVersion: 0,
});

console.log(tx?.meta?.err);
console.log(tx?.meta?.computeUnitsConsumed);
console.log(tx?.meta?.logMessages);

fork and replay

Surfpool runs a local validator that pulls account state from mainnet the first time it is needed. I run it in Docker, point the script at it instead of the public RPC, and send the same transaction again. Now it fails on my machine, where I can add logs and retry as many times as I want.

read the error

My test case failed with Custom(57005) after only 30 compute units, almost at the very start. 57005 in hex is 0xdead. A custom error is just a number the program decided to return, so the meaning lives in the program, not in Solana. If the program has an Anchor IDL, its error list maps codes to names. That lookup is the step my debugger does for you.

next: turning these three steps into one command that prints the root cause.