library / How to use Solana / Confirmation and retries / HS0093
Duplicate-safe retry check
One TypeScript file takes a landed signature from a busy address plus a random never-sent one, reads their status, the block height and a fresh blockhash window, and prints whether to resend the same bytes, stop, or sign again. It sends nothing.
Why one signature lands once
A signed transaction is a message plus one or more Ed25519 signatures, and the first signature doubles as the transaction's id. The cluster remembers the signatures it has processed. A second copy of the same bytes is refused with an AlreadyProcessed error or dropped before it runs, so sending identical bytes ten times moves the funds at most once.
Ed25519 signing is deterministic: the same key over the same message gives the same signature. Rebuilding a message that is identical down to the blockhash is therefore a resend too. Anything that changes the message makes a different transaction with a different signature, and the cluster treats it as unrelated to the first. A new blockhash does that. So does a changed priority fee or one extra instruction.
Where duplicates come from
A retry loop has two moves: send the same thing again, or build a new thing. The first is harmless. The second is where a payment doubles. A send that timed out, or a status read that came back empty, tells you nothing about whether the first transaction is still on its way. It stays valid until the chain passes the last valid block height of its blockhash, and it can land at any point before that. If your code signs a fresh transaction with a new blockhash in the meantime, two different transactions carry one transfer and both can land.
- A client timeout on the send call, read as a failure when the node had already accepted the transaction.
- A status call that answered null, read as dropped when the transaction was only not visible yet.
- A fee bump that rebuilds the message while the original is still in flight.
The decision table
Three inputs settle every case: the status of the signature, the current block height, and the lastValidBlockHeight that getLatestBlockhash returned when you picked the blockhash. Write that height next to the signature before the first send, because after a restart it is the only thing that tells you how long the first transaction can still land.
| Case | What you read | Action |
|---|---|---|
| Landed | A status object with err null at confirmed or finalized | Stop. Record the payment as made and never sign again. |
| Landed with an error | A status object with err set | The transfer did not happen and the signature is spent. Build a new transaction. |
| Seen at processed only | A status at processed | Keep polling. Resending the same bytes is safe, and a processed slot can still be dropped with its fork. |
| Unknown inside the window | null, block height at or below the last valid height | Resend the same bytes. Do not sign again. |
| Unknown past the window | null, block height above the last valid height | Check once more after a few blocks, then sign again with a fresh blockhash. |
The status call runs with searchTransactionHistory on, so the node looks in its ledger as well as in its cache of recent slots. Read the block height and the status from the same endpoint, and compare the confirmationStatus of the status with the commitment the height read used. A node that is behind reports a low height and keeps you inside a window that has already closed.
The run
Install @solana/kit at 8.6.0, export CARBIUM_KEY and run node main.ts <address> with Node 24, which strips the types on load. Give it any busy address. The file takes that address's newest signature as the landed case and builds a random 64-byte signature that no one ever sent as the unknown case.
// HS0093 · Duplicate-safe retry check// https://carbium.sh/library/howsolana/duplicate-safe-retry-check// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MITimport { createDefaultRpcTransport, createSolanaRpcFromTransport, getBase58Decoder, isAddress, signature, 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 url = process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh";const rpc = createSolanaRpcFromTransport( createDefaultRpcTransport({ url, headers: { "x-api-key": key } }),);const address = process.argv[2];if (!address || !isAddress(address)) { console.error("Usage: node main.ts <busy address>"); process.exit(1);}type Status = { err: unknown; confirmationStatus: string | null } | null;// Identical bytes inside the window, a new signature only after it closes.function decide(s: Status, height: bigint, lastValid: bigint): string { if (s && s.err !== null) return "landed with an error: signature spent, build a new transaction"; if (s?.confirmationStatus === "processed") return "seen at processed: keep polling, resend the same bytes"; if (s) return `landed at ${s.confirmationStatus}: stop, never sign again`; if (height <= lastValid) return `unknown, ${lastValid - height} blocks left: resend the same bytes`; return "unknown past the window: re-sign with a new blockhash";}let landed: Signature | undefined;try { const page = await rpc .getSignaturesForAddress(address, { limit: 1, commitment: "confirmed" }) .send(); landed = page[0]?.signature;} catch (e) { console.error("getSignaturesForAddress failed:", e instanceof Error ? e.message : e);}const never = signature(getBase58Decoder().decode(crypto.getRandomValues(new Uint8Array(64))));const sigs = landed ? [landed, never] : [never];const [st, h, bh] = await Promise.allSettled([ rpc.getSignatureStatuses(sigs, { searchTransactionHistory: true }).send(), rpc.getBlockHeight({ commitment: "confirmed" }).send(), rpc.getLatestBlockhash({ commitment: "confirmed" }).send(),]);for (const [name, r] of [["getSignatureStatuses", st], ["getBlockHeight", h], ["getLatestBlockhash", bh]] as const) { if (r.status === "rejected") console.error(`${name} failed:`, r.reason?.message ?? r.reason);}if (st.status !== "fulfilled" || h.status !== "fulfilled") process.exit(1);const height = h.value;const windows: [string, bigint][] = [["window already closed", height - 1n]];if (bh.status === "fulfilled") { windows.unshift(["window of a send made now", bh.value.value.lastValidBlockHeight]);}console.log(`block height ${height} at confirmed`);st.value.value.forEach((s, i) => { console.log(`${sigs[i] === landed ? "landed" : "never sent"}: ${sigs[i]}`); for (const [name, last] of windows) console.log(` ${name}: ${decide(s, height, last)}`);});
run: node main.ts <address>
The three reads run together and each settles on its own. A failure prints its method name and the run exits only when the status or the height is missing, because the decision needs both. The never-sent signature is judged against two windows: the one a send made right now would have, from getLatestBlockhash, and one built as the current height minus one, so the closed branch shows up in a single run. Your own code reads the saved height instead.
Read the output in pairs. The landed signature gets the same answer under both windows, because a status always wins over the height. The never-sent signature gets "resend the same bytes" under the open window and "re-sign" under the closed one.
Every call goes to https://services.carbium.sh, or to whatever CARBIUM_RPC_URL names, with the key in the x-api-key header. The address lookup is one getSignaturesForAddress, which costs 50 cells and is rate-limited per plan. Each read that takes a commitment asks for confirmed. getSignatureStatuses takes no commitment and returns the level reached as confirmationStatus.
In a real retry loop the resend is a sendTransaction, which costs 20 cells without preflight and 70 with it. A resend loop pays that on every pass. sendTransaction without preflight is rate-limited at 0.25, 1.5, 5 and 25 req/s on Free, Dev, Business and Pro, so a Free loop resends once every 4 seconds. Whatever the plan, stop at the last valid height. For the status side of the loop, see Transaction status check.
Where it ends
A null after the window closes is strong evidence and short of proof. A transaction included in one of the last blocks before the height passed can show in the status a moment later, and a node that has not yet processed that block cannot report it. Wait a few blocks past the last valid height, read the status once more, and only then sign again.
A send can also reach a node that drops it. The status then stays null for the whole window and the resend of the same bytes is what gets it in. The check cannot tell a dropped transaction from a slow one, and it does not need to, since both get the same action.
Durable nonce transactions have no block height window. They stay valid until the nonce account's stored value changes, so the rule moves to that account: the transaction is spent once the nonce advances. A retry that reuses the same nonce value can land once, because the first one to land advances it and voids the rest. The file does not read nonce accounts.
The only guard that survives a wrong guess is one in the program: a payment id that maps to an account address, so a second creation of that account fails. This check lowers the odds of a double payment to a lost height or a lagging node. It does not replace that guard when the amounts are large. The file reads and never signs, so the send side, the saved bytes and the saved height stay in your code.
When it goes wrong
| what goes wrong | what to do |
|---|---|
| Every status is null right after a send. | A null status means the node has no record of the signature yet, so the outcome is still unknown. Poll again and resend the same bytes until the block height passes the last valid height, then decide with the table. |
| Two transfers landed for one payment. | The second was signed with a new blockhash or fee while the first was still valid. Compare the two signatures to confirm, and from now on save the signed bytes and lastValidBlockHeight before the first send. Add a payment id guard in the program. |
| A resend answers that the transaction was already processed. | The first copy landed. Read its status, record the payment and stop resending. |
| After a restart the loop has no last valid height. | Write the signature, the signed bytes and the height to disk before the send call. Without them the loop cannot tell an open window from a closed one, so hold the payment for review and make no guess. |
| A status at processed turns into null on the next read. | The slot was dropped with its fork. Inside the window, resend the same bytes. Past it, sign again. |
Questions
Is resending the same bytes safe?
Yes. The signature is the same, so at most one copy runs. The cost is the cells for each send call, so space the resends out.
Can I raise the priority fee on a retry?
Only past the window. A changed fee changes the message, so it gets a new signature and the original can still land beside it.
How long is the window?
It ends at the lastValidBlockHeight that getLatestBlockhash returned for the blockhash you signed with. Save it with the signature and compare it to getBlockHeight.
Do I need a keypair to run the check?
No. The file reads statuses and heights, signs nothing and sends nothing. The Carbium key only identifies your account.