services
Get a key

library / AI and blockchain / SI0064

Fee-payer lamport delta check

You get a script that reads a program's newest transactions and prints each fee, the lamports the fee payer lost beyond it, and a count of the instructions that list account 0. An agent runs the same check after every send.

By Carbium · Agent wallets and signing safety · SI0064 · updated 2026-11-22

01

What the runtime allows

A transaction names every account it touches in its message. The runtime hands each instruction only the accounts that instruction lists, and a program can neither read nor change an account outside that list. The direct answer is no: no program reaches the lamports of an address that is missing from the transaction.

One instruction raises the payer's charge without listing the payer. The Compute Budget program's SetComputeUnitPrice lists no accounts, yet it raises what the payer is charged, and the formula below hides that rise inside meta.fee. So the agent also compares fee with the fee it expected.

The fee payer is never missing. It is account 0 of every message, a signer, and writable because the fee comes out of it. Any instruction in the transaction can list account 0 among its own accounts, and an instruction that lists it can be given power over those lamports. An ordinary wallet is owned by the system program, so the system program does the debit, on an instruction the payer signed. A program can reach the same debit with a cross-program call to the system program, because the payer's signature on the transaction carries into the inner call.

Four routes take lamports from the payer once an instruction lists it:

  • A system Transfer from account 0 to any writable account.
  • A system CreateAccount that funds a new account from account 0.
  • A cross-program call that makes either of those two on the payer's behalf.
  • An Assign that moves the payer account to a program, after which that program can debit it directly.
$The payer signs the whole message, so every instruction in it is already authorised. So the check compares each debit with what the agent meant to spend.
02

The delta formula

getTransaction returns a meta object with preBalances and postBalances, lamports for every account in the message, in the same order as the account keys. Index 0 is the payer. meta.fee is the whole fee the payer was charged, base fee and priority fee together. What the payer lost beyond the fee is:

excess = preBalances[0] - postBalances[0] - meta.fee

All three values are lamports. Hold them as bigint, which is what @solana/kit returns, and the subtraction stays exact.

excessWhat it says
0The transaction cost the payer its fee and nothing else.
above 0Instructions moved lamports out of the payer: transfers, rent for new accounts, tips.
below 0The payer received lamports, for example from a closed token account.

A transaction that failed has its instruction effects rolled back and still charges the fee, so its excess is 0. The check matters for transactions that succeeded.

03

Reading the result

A positive excess has an ordinary explanation more often than a hostile one, so compare it with a number the agent wrote down before it signed. The usual sources:

  • Rent for accounts the transaction created. The excess equals the rent-exempt minimum for each new account's size, which you can compute with Rent-exempt minimum calculator.
  • A plain SOL transfer the agent asked for.
  • A tip sent to a validator's tip address, which is a transfer like any other and shows as excess.
  • SOL wrapped into a token account, which is a transfer into that account followed by a sync.
  • Lamports forwarded by a program the agent called, which is the case the check exists to catch.

The second half of the output counts the instructions that list account 0. Top-level instructions come from message.instructions. Calls made inside them sit under meta.innerInstructions, and the script counts both, because a transfer from the payer often runs as an inner call. The script prints counts only, so open the transaction to see which program each one calls. Inner instruction tree shows how to lay those out in full.

Read the two numbers together. An excess of 0 with both counts at 0 is a clean transaction. A positive excess with a single top-level instruction listing the payer is a transfer you can open and name. A positive excess with several inner instructions listing the payer needs a person to look.

04

The check script

One file on @solana/kit and Node's own modules. Set CARBIUM_KEY first; the file exits with a message when it is missing. CARBIUM_RPC_URL overrides the endpoint, which defaults to https://services.carbium.sh. Pass an address as the first argument to read its newest transactions. With no argument the file reads the Compute Budget program, which almost every priority-fee transaction names.

main.ts
// SI0064 · Fee-payer lamport delta check// https://carbium.sh/library/si/fee-payer-lamport-delta-check// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MIT// Fee-payer lamport delta check. Run: CARBIUM_KEY=... node main.ts [program or wallet address]import { address, createDefaultRpcTransport, createSolanaRpcFromTransport } 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 url = process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh";const rpc = createSolanaRpcFromTransport(createDefaultRpcTransport({ url, headers: { "x-api-key": key } }));// With no argument: the Compute Budget program, named by almost every priority-fee transaction.const target = address(process.argv[2] ?? "ComputeBudget111111111111111111111111111111");const sigs = await rpc.getSignaturesForAddress(target, { limit: 5 }).send();if (!sigs.length) { console.log("no signatures listed for this address"); process.exit(0); }const reads = await Promise.allSettled(sigs.map((s) =>  rpc.getTransaction(s.signature, { maxSupportedTransactionVersion: 1, encoding: "json" }).send()));reads.forEach((r, i) => {  const id = sigs[i].signature.slice(0, 12);  if (r.status === "rejected") { console.log(`${id}: read failed: ${r.reason}`); return; }  const tx: any = r.value;  if (!tx) { console.log(`${id}: null, older than the node keeps`); return; }  try {    const meta = tx.meta;    const fee = BigInt(meta.fee);    // Lamports the payer (account 0) lost beyond the fee.    const excess = BigInt(meta.preBalances[0]) - BigInt(meta.postBalances[0]) - fee;    const lists0 = (ix: any) => (ix.accounts ?? []).includes(0);    const top = tx.transaction.message.instructions.filter(lists0).length;    const inner = (meta.innerInstructions ?? []).flatMap((g: any) => g.instructions).filter(lists0).length;    const payer = tx.transaction.message.accountKeys[0];    console.log(`${id}: payer ${String(payer).slice(0, 8)} fee ${fee} excess ${excess} top ${top} inner ${inner}${meta.err ? " failed" : ""}`);  } catch (e) {    console.log(`${id}: unreadable: ${(e as Error).message}`);  }});

run: node main.ts

The file lists the five newest signatures with getSignaturesForAddress, then reads all five with getTransaction at once. Both calls leave commitment out, so @solana/kit fills in confirmed, where the JSON-RPC server alone would use finalized. getTransaction passes maxSupportedTransactionVersion: 1, because mainnet carries v1 transactions and a read without it answers -32015 for them. Each read settles by itself with Promise.allSettled, so one failure prints its reason and the other four still print.

getSignaturesForAddress costs 50 cells in its own rate class. The five getTransaction reads cost 50 cells each, 250 cells in the transaction-lookup class. Each class absorbs a burst of at least 1,024 cells, so one run fits. Twenty reads come to 1,000 cells, which still fits, and past twenty a Free key meets the rate limit.

Output fieldMeaning
feemeta.fee in lamports
excesspayer balance drop beyond the fee, in lamports
top and innercount of instructions that list account 0, top-level and inside others
failedthe transaction errored, so the excess should be 0
05

Wiring it after the send

The check fits at the end of an agent's send step. Before signing, the agent records a spend budget in lamports: the transfers it asked for plus the rent of the accounts it creates. After the send it waits for the signature to reach confirmed, reads the transaction, and compares the excess with the budget. Pre-sign transaction guard covers the part before signing; this check covers what the landed transaction did.

A read right after a send can answer null, because the transaction has not reached the node's ledger yet. Poll getSignatureStatuses until confirmationStatus reads confirmed or finalized, because a processed status can still leave this getTransaction answering null. Then read the transaction. An excess above the budget stops the agent's next spend and raises an alert with the signature, the excess and the count of instructions that list account 0.

Run the same code over a window of an agent wallet's history too. Pass the wallet's address, and any transaction where this wallet is the payer and the excess is unexpected shows up in the same lines.

06

Where it ends

  • The check runs after the transaction landed. The lamports are gone by the time it reports, so it supports a stop and an alert, and a pre-send simulation is the place to prevent the spend.
  • It watches account 0 only. A co-signer, or a token account the agent owns, can lose value that this formula never sees.
  • The formula subtracts meta.fee whole. A SetComputeUnitPrice instruction lists no accounts, so it can raise the charge without showing in excess or in the counts, and only a comparison of fee with the fee the agent expected catches it.
  • Token amounts do not appear in lamport balances. A transaction can drain tokens with an excess of 0.
  • The signature listing reaches back 30 days at most, and getTransaction can answer null for an older signature the listing returns. The script prints that case and moves on.
  • Without a budget to compare against, the excess is a number with no verdict. The check is as good as the spend figure the agent records before it signs.
07

When it goes wrong

what goes wrongwhat to do
-32015 on a transaction read.The call left out maxSupportedTransactionVersion, or set it to 0, and the transaction is v1. Pass maxSupportedTransactionVersion: 1 on every getTransaction.
A line reads null, older than the node keeps.getTransaction returned null for an older signature that the 30-day address index still lists. Skip it and check the newest signatures, which the node holds.
Some reads fail with 429 while others print.The run went over its rate. Lower the limit, or retry the failed signatures after the retry-after hint.
Excess is positive on a transaction the agent meant to send.Rent for new accounts and transfers both count. Subtract the budget the agent recorded before signing and alert on the remainder.
Excess is 0 on a transaction that moved SOL.The payer did not fund it. Another signer or a received refund balanced it. Check the instructions that list account 0, and read the other accounts' balances.
401 at the first read.The key is unknown at the door. Attach the whole key in the x-api-key header.
08

Questions

Can a program move the fee payer's lamports if the payer is not in its accounts?

No. An instruction sees only the accounts it lists and a program cannot name an address the message lacks. The payer is always in the message as account 0, so an instruction that lists it is the one to look for.

Does the fee in meta.fee include the priority fee?

Yes. It is the total charged to the payer, so subtracting it leaves only lamports that moved through instructions. A SetComputeUnitPrice instruction lists no accounts, so compare fee with the fee the agent expected.

Why does the script count inner instructions too?

A program can call the system program from inside its own instruction, and that call lists account 0 in the inner list. Counting only the top level would miss it.

What does a negative excess mean?

The payer ended with more than it started with after the fee. A closed token account that sends its lamports to the payer causes this, and it is normal.