library / Blockchain ideas / Wallet watchers / BI0090
Failed transaction fee tally
A per-wallet count of landed failed transactions, the lamports their fees cost and the top error names, read from getSignaturesForAddress and getTransaction. A wallet or support tool shows it when a user asks why a failed transaction still took money.
Why a failure costs a fee
A user writes in that a transaction failed, their balance dropped anyway and nothing came back. The balance is right. A validator charges the fee payer before it runs the transaction, so a transaction that lands and then fails has already been paid for. The program's changes revert and the fee stays spent.
Two parts set the amount. The base fee is 5,000 lamports per signature. A priority fee adds the compute unit price times the compute unit limit the transaction asked for, whether or not the run used all of it, so a transaction that asked for a large limit and failed early still pays for the whole request. The sum comes out of account key 0, the first account in the message, even when other wallets also signed.
A transaction that never lands costs nothing. An expired blockhash or a drop before inclusion leaves no record and no charge. Only a landed failure bills, which is why the tally can be built from the ledger: each charge is a transaction with an err value and a meta.fee.
The tally
For one wallet the tally is a few numbers and a list of causes.
| Figure | Where it comes from | What it tells the user |
|---|---|---|
| Failed count | Entries with a non-null err in getSignaturesForAddress | How many landed transactions failed among the newest 50 |
| Lamports lost | Sum of meta.fee from getTransaction, only where the wallet is account key 0 | What the wallet paid for those failures |
| Top errors | The err value, named by its inner error | Why they failed, most common first |
| Failed calls | Lookups that errored or answered null | What the tally could not read |
Each entry in the signature listing carries its own err, so the failures are picked out without fetching a single successful transaction. Only the failed ones cost a getTransaction call. Fees are added in BigInt, because lamports are 64-bit integers and the sum should print exactly.
A program failure arrives as {"InstructionError":[2,{"Custom":6001}]}. The tally keys on the inner error, so the same failure at a different instruction index lands in one bucket. A transaction-level error can be a plain string or an object with one key, and the code names both. A Custom number is the program's own code, and turning it into words needs that program's published error table.
The build
The tally is one file that needs @solana/kit and what Node 24 ships. It is read-only: no private key, nothing sent. Pass a wallet address as the first argument, or pass nothing and let it find one.
- With no argument, call getSignaturesForAddress on a busy program for its newest 100 signatures and take the first entry whose err is set.
- Call getTransaction on that signature. Account key 0 is the fee payer, and that address becomes the wallet.
- Fetch the wallet's newest 50 signatures and keep the ones with an err. Count their error names.
- Run getTransaction on at most 10 of them, one after another, at confirmed commitment with maxSupportedTransactionVersion: 1.
- Add meta.fee in BigInt for each one where the wallet is account key 0, and count the ones someone else paid.
- Print the failed count, the lamports lost, the error names and every call that failed.
// BI0090 · Failed transaction fee tally// https://carbium.sh/library/blockchainideas/failed-transaction-fee-tally// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MITimport { address, createDefaultRpcTransport, createSolanaRpcFromTransport, type Signature } 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 rpc = createSolanaRpcFromTransport( createDefaultRpcTransport({ url: process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh", headers: { "x-api-key": key }, }),);// With no argument, a busy program's newest failure leads to a wallet that paid for one.const BUSY_PROGRAM = "JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4";const MAX_PRICED = 10;const LAMPORTS_PER_SOL = BigInt(1_000_000_000);const problems: string[] = [];const txConfig = { commitment: "confirmed", encoding: "json", maxSupportedTransactionVersion: 1 } as const;const say = (e: unknown) => (e instanceof Error ? e.message : String(e));function errName(err: unknown): string { if (typeof err === "string") return err; const entry = typeof err === "object" && err !== null ? Object.entries(err)[0] : undefined; if (!entry) return "unknown"; const [kind, detail] = entry; if (kind === "InstructionError" && Array.isArray(detail)) return errName(detail[1]); return typeof detail === "number" || typeof detail === "bigint" ? `${kind} ${detail}` : kind;}async function failedEntries(who: string, limit: number) { try { const page = await rpc.getSignaturesForAddress(address(who), { limit, commitment: "confirmed" }).send(); return page.filter((s) => s.err !== null); } catch (e) { problems.push(`getSignaturesForAddress ${who.slice(0, 8)}: ${say(e)}`); return []; }}async function fetchTx(sig: string) { try { const tx = await rpc.getTransaction(sig as Signature, txConfig).send(); if (!tx) problems.push(`getTransaction ${sig.slice(0, 8)}: null (older than the ledger floor)`); return tx; } catch (e) { problems.push(`getTransaction ${sig.slice(0, 8)}: ${say(e)}`); return null; }}async function main() { let wallet: string | undefined = process.argv[2]; if (!wallet) { const [first] = await failedEntries(BUSY_PROGRAM, 100); const tx = first ? await fetchTx(first.signature) : null; wallet = tx?.transaction.message.accountKeys[0]; } if (wallet) { const failed = await failedEntries(wallet, 50); const errors = new Map<string, number>(); for (const f of failed) errors.set(errName(f.err), (errors.get(errName(f.err)) ?? 0) + 1); let lost = BigInt(0); let priced = 0; let paidByOthers = 0; for (const f of failed.slice(0, MAX_PRICED)) { const tx = await fetchTx(f.signature); if (!tx?.meta) continue; if (tx.transaction.message.accountKeys[0] !== wallet) paidByOthers++; else { lost += BigInt(tx.meta.fee); priced++; } } const sol = `${lost / LAMPORTS_PER_SOL}.${String(lost % LAMPORTS_PER_SOL).padStart(9, "0")}`; console.log(`wallet ${wallet}`); console.log(`failed in its newest 50: ${failed.length}, priced ${priced}, paid by another wallet ${paidByOthers}`); console.log(`lamports lost: ${lost} (${sol} SOL)`); for (const [name, n] of [...errors].sort((a, b) => b[1] - a[1])) console.log(` ${n} x ${name}`); } else console.log("no failed transaction found to start from"); for (const p of problems) console.log(`call failed: ${p}`);}await main();
run: CARBIUM_KEY=<YOUR_KEY> node main.ts
Version 1 matters here. With maxSupportedTransactionVersion absent or 0, getTransaction answers -32015 for a v1 transaction, and that fee would be missing from the total. The code passes 1, and a lookup that still fails prints under the failed calls.
The signature listing reads from an address index that keeps up to 30 days, and its pages reach below the validator's ledger floor. For an older signature getTransaction can answer null. The code reports those as failed calls and prices the rest, so the printed count of priced transactions tells the reader how much of the sum is real.
One run makes at most 13 calls, two of them listings and eleven of them lookups. Each costs 50 cells, so a full run is 650 cells, inside the 1,024-cell burst a rate class absorbs, and repeated runs draw on the per-second rates below.
| Plan | getTransaction | getSignaturesForAddress |
|---|---|---|
| Free ($0) | 0.24 req/s | 0.24 req/s |
| Dev ($20) | 1.2 req/s | 1.2 req/s |
| Business ($250) | 5 req/s | 100 req/s |
| Pro ($500) | 25 req/s | 500 req/s |
On Free, a second run straight after the first finds the lookup bucket short, about 474 of 550 cells left, and its last lookups get 429 until cells refill at 0.24 req/s. The code does not retry, so a 429 shows up under the failed calls. Limits are per account, so other tools on the same keys draw on the same budget.
Presenting it to a user
The complaint is usually about one transaction. The tally gives that transaction a context, so the screen should open with the number the user noticed and then explain it.
- Lead with the amount in SOL with the lamports beside it, and say how many transactions it covers: "8 failed transactions among the newest 50 cost 0.000085 SOL".
- Label the window and the sample. When 14 failed and 10 were priced, say "priced 10 of 14" so the sum is not read as the whole.
- Show one row per failed transaction with its signature, its fee and its error, so the user can find the one they came about.
- Give the error in the program's own words. Keep the number next to it for the support agent who searches by code.
- Split a fee into base and priority parts with Transaction fee breakdown. A large priority part on a failed transaction points at an oversized compute unit limit.
- State that fees on a landed failure are not returned. A tool can suggest what lowers the next failure's cost, such as a tighter compute unit limit and a fresh quote.
Run the tally when the user opens the screen and keep the result for the session. A wallet page that refreshes it on every view spends the account's per-second budget on numbers that change only when a new failure lands.
Where it ends
- Only landed failures appear. A dropped transaction or an expired blockhash leaves nothing on the ledger, so there is nothing to count and nothing was charged.
- The failed count covers the newest 50 signatures, and the lamports cover at most 10 of them. The window is never the wallet's lifetime, and the address index stops at 30 days.
- A fee is read from account key 0 only. A transaction the wallet signed but another wallet paid for is counted apart and adds no lamports.
- The sum is fees. A failed swap moved no funds and a failed account creation reverted its rent, so neither adds to it.
- Custom errors stay numbers until the program's own table names them.
- The tally explains a charge and recovers nothing.
When it goes wrong
| what goes wrong | what to do |
|---|---|
| getTransaction answers null for a signature the listing returned. | The signature is older than the validator's ledger floor, which the 30-day address index reaches below. The code prints it under failed calls and prices the others. Read newer pages or accept a lower priced count. |
| getTransaction answers -32015. | The call left out maxSupportedTransactionVersion or sent 0, and the transaction is v1. Send 1 on every getTransaction, as the code does. |
| A call answers 429 partway through the run. | Wait the retry-after seconds and run again, or space the lookups to your plan's rate, 0.24 req/s on Free. The limit is shared by every key on the account. |
| The wallet shows zero failures but the user insists one failed. | The transaction may be older than the newest 50, or it never landed and cost nothing. Pass before with the last signature to read the next page, within the 30-day index. |
| With no argument the run finds no failed entry on the busy program. | The newest 100 signatures held no err. Run it again a moment later, or pass a wallet address as the first argument. |
| The total looks too low for the number of failures. | Only 10 failures are priced and anyone else's paid fees are skipped. Compare the failed count with the priced count and raise the cap on Dev or above. |
Questions
Does Solana refund the fee of a failed transaction?
No. The fee is charged before execution, and a transaction that lands and fails keeps its charge. Only the program's state changes revert.
Why did the failed transaction cost more than 5,000 lamports?
The base fee is 5,000 lamports per signature, and a priority fee is added on top. The priority part is the compute unit price times the limit the transaction requested, whether or not the run used that much.
Can I list failed transactions that never landed?
No. A transaction rejected before inclusion or one whose blockhash expired leaves no record on the ledger, and it was not charged either.
How far back does the tally reach?
The newest 50 signatures of the wallet, within the 30-day address index. Older failures are not listed, and getTransaction can answer null for the oldest entries.
Why does the code check account key 0?
The fee payer is the first account in the message. A wallet that only co-signed paid nothing, so adding that transaction's fee would overstate the loss.