services
Get a key

library / AI and blockchain / Agent payments / SI0104

Late payment check by block time

A read-only script that takes a payment signature, or the newest successful transaction of an address, and reports whether its block time beat a deadline, with the gap in seconds and the slot it landed in.

By Carbium · Agent payments · SI0104 · updated 2026-11-23

01

Why the chain's clock

An agent that accepts payment against an invoice holds a deadline in wall-clock seconds. The payer's wallet, your server and the validator each have their own idea of now, and they drift apart by seconds. The one timestamp all sides can read back later is the one the cluster stamped on the block that holds the transfer. Comparing that stamp with the deadline gives a verdict you can repeat tomorrow and get the same answer.

The script below takes a signature, or an address whose newest successful transaction it picks, reads the slot and blockTime from getTransaction, asks getBlockTime for the same slot, and prints on time or late with the gap in seconds. A slot is about 200 ms long, so a one-second timestamp covers roughly five slots.

02

The check

main.ts
// SI0104 · Late payment check by block time// https://carbium.sh/library/si/late-payment-check-by-block-time// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MITimport { address, createDefaultRpcTransport, createSolanaRpcFromTransport, 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 target = process.argv[2] ?? process.env.PAY_TARGET;if (!target) {  console.error("Pass a signature or an address, or set PAY_TARGET.");  process.exit(1);}const deadline = Number(process.argv[3] ?? Math.floor(Date.now() / 1000) - 3600);async function paymentSignature(t: string) {  if (t.length > 60) return signature(t);  const page = await rpc.getSignaturesForAddress(address(t), { limit: 10 }).send();  const hit = page.find((s) => s.err === null);  if (!hit) throw new Error("no successful transaction in the newest 10");  return hit.signature;}try {  const sig = await paymentSignature(target);  const tx = await rpc    .getTransaction(sig, { commitment: "confirmed", encoding: "json", maxSupportedTransactionVersion: 1 })    .send();  if (!tx) throw new Error(`getTransaction answered null for ${sig}`);  const [second, head] = await Promise.allSettled([    rpc.getBlockTime(tx.slot).send(),    rpc.getSlot({ commitment: "confirmed" }).send(),  ]);  if (second.status === "rejected") console.log("getBlockTime failed:", String(second.reason));  if (head.status === "rejected") console.log("getSlot failed:", String(head.reason));  const stamp = tx.blockTime == null ? null : Number(tx.blockTime);  const check = second.status === "fulfilled" && second.value != null ? Number(second.value) : null;  console.log(`signature ${sig}`);  console.log(`slot ${tx.slot}, blockTime ${stamp}, getBlockTime ${check}`);  if (head.status === "fulfilled") console.log(`slots behind head: ${head.value - tx.slot}`);  if (stamp === null) console.log("verdict: no block time for this block, retry");  else if (check !== null && check !== stamp) console.log("verdict: the two reads disagree, retry both");  else {    const gap = stamp - deadline;    console.log(gap <= 0 ? `verdict: on time, ${-gap} s before the deadline` : `verdict: late by ${gap} s`);  }} catch (err) {  console.error("check failed:", err instanceof Error ? err.message : String(err));  process.exit(1);}

run: node main.ts TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA

  • getSignaturesForAddress runs only when you pass an address. It costs 50 cells, and the script asks for the newest 10 and takes the first one whose err is null.
  • getTransaction costs 50 cells. The script sets commitment: "confirmed" itself and passes maxSupportedTransactionVersion: 1, so a v1 transaction reads like any other.
  • getBlockTime and getSlot run together under Promise.allSettled. The first is a second opinion on the stamp, the second tells you how old the payment is. getSlot is one of the cheap cached reads at 1 cell.

Run it with a signature and your own deadline: node main.ts <signature> 1796000000. With no deadline it falls back to one hour before now, a demo value that makes any fresh transfer late and shows the late branch. In real use, pass the deadline stored with the invoice.

03

Reading the result

OutputMeaningWhat the agent does
on time, gap negative or zeroBlock time is at or before the deadlineMark the invoice paid
late, gap positiveBlock time is after the deadlineHold for review or apply the late policy
stamp nullThe node has no time for that blockRetry once, then fall back to the slot ordering
stamp and getBlockTime differThe two reads disagreeTrust neither, retry both

The gap is computed as block time minus deadline, so a positive number is seconds late. Because the stamp has one-second resolution, a transfer within a second of the deadline is a judgment call. Settle it in your invoice terms, for example with a grace window, instead of in code at the moment of the check.

04

Setting the deadline

Store the deadline as unix seconds when the invoice is created, next to the signature or the reference address you expect payment on. Never compute it at check time from the local clock. If the check runs an hour after the invoice expired, a deadline built from Date.now() moves with it and every late payment looks on time.

A grace window belongs in the stored deadline too. Add the seconds when you write the invoice and the check stays a plain comparison. When an agent pays on behalf of a user, the same stored value lets you explain the verdict later: the invoice said this second, the block said that one.

Keep the verdict with the invoice. Write the signature, slot, block time, deadline and gap into the same record the first time the check succeeds. A later rerun then has something to compare against, and a disagreement between two runs points at a changed input, such as a different deadline, because a confirmed block keeps its stamp.

$The script reads at confirmed commitment. A transaction seen at processed can still be dropped with its fork, so a block time read there is not yet a fact worth billing on.

To check a batch, list the signatures first with a page from the address and loop the same two reads. Each getTransaction costs 50 cells and runs at 0.24, 1.2, 5 and 25 req/s on Free, Dev, Business and Pro, so a batch of 100 is 5,000 cells and takes about seven minutes on Free. Pace the loop to your plan's rate for that method.

05

Where it ends

The check proves when one transaction landed. It does not prove who paid, how much, or that the transfer went to the right account. Pair it with a balance or token amount check before you release anything.

The script reads one transaction. An invoice paid in two transfers needs one check per signature, and you sum the amounts yourself before deciding the invoice is settled.

getSignaturesForAddress comes from an address index that keeps up to 30 days, so a payment older than 30 days does not appear in its pages, and an empty page past 30 days marks the index's age limit, not an address with no history. Its pages reach below the validator's ledger floor, so getTransaction can answer null for an older signature the list still shows. The script throws on null instead of guessing.

A block time is a seconds-level stamp. It can be null, and it is not a receipt from the payer's side: a wallet that signed before the deadline and landed after it is late by this measure.

06

When it goes wrong

what goes wrongwhat to do
getTransaction answers -32015 for a signature that exists.Pass maxSupportedTransactionVersion: 1 with an encoding. Absent or 0, a v1 transaction or a block holding one is refused.
getTransaction answers null for a signature listed a moment ago by getSignaturesForAddress.The index reaches below the ledger floor for old entries. Check the age of the signature; for a fresh one, retry once at the same confirmed commitment.
Every payment reads as on time when the check runs late.The deadline was built from the clock at check time. Read it from the stored invoice, in unix seconds.
The verdict flips between two runs on the same signature.The deadline changed between runs. With no third argument the script uses one hour before now, so pass the stored invoice deadline on every run.
07

Questions

Is block time the same as slot time?

A slot has a number and a start; block time is the unix second stamped on the block produced in it. A skipped slot has no block and so no stamp, which is why the script reads the stamp from the transaction's own slot.

Why call getBlockTime when getTransaction already returns blockTime?

It reads the same slot's stamp through a second method. If the two differ, retry both and refuse to decide until they agree.

Can I get a time for a slot that holds no transaction of mine?

Yes. Call getBlockTime with the slot number. A skipped slot, or one the node no longer holds, answers with an error, and a block with no stamp answers null, so handle both before you subtract.

How do I handle an address with thousands of payments?

Page through getSignaturesForAddress with the before cursor inside the 30-day index, and check each match against its own stored invoice deadline.