services
Get a key

library / Use cases / UC0031

Fee sponsor budget monitor

Sponsoring gas for users turns one wallet balance into your product's uptime. Measure its spendable lamports, its recent fee burn and the hours of runway left, with a read-only Node script and the call costs of checking.

By Carbium · Wallet products · UC0031 · updated 2026-11-22

02

Three numbers to watch

  • Spendable balance: getBalance minus getMinimumBalanceForRentExemption for a zero-data account.
  • Burn rate: lamports of fees per hour, taken from the newest transactions the wallet paid for. The script drops the oldest fee from the sum, because that transaction happened before the span it measures.
  • Runway: spendable divided by burn rate, in hours. The script alerts below 24 hours by default and reads RUNWAY_HOURS if you set it.

Only transactions where the sponsor is the first account key count. A transaction that lists the wallet as a second signer takes nothing from it in fees, and an address list from getSignaturesForAddress includes those as well as the ones it paid for.

Runway in hours is a coarse number on purpose. A top-up job and a human both need time to act, so the useful alert is a runway of a day or two, and an hour-level value of that size changes slowly enough to poll every few minutes.

03

The monitor in one file

The script takes the sponsor address as its first argument. With no argument it reads the newest few signatures of a busy program and uses the first fee payer it can look up, so it runs anywhere. It builds one client on https://services.carbium.sh with the key in the x-api-key header (endpoints, auth), reads the balance, the rent floor and the newest signatures in parallel, then looks up each transaction.

main.ts
// UC0031 · Fee sponsor budget monitor// https://carbium.sh/library/usecase/fee-sponsor-budget-monitor// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MIT// Read-only: how many hours a fee-sponsor wallet can keep paying fees at its recent burn rate.import { 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 },}));const SAMPLE = Math.min(Number(process.env.SAMPLE ?? 10), 15);const MIN_HOURS = Number(process.env.RUNWAY_HOURS ?? 24);const BUSY_PROGRAM = "JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4";const cfg = { maxSupportedTransactionVersion: 1, encoding: "json" } as const;// Fee payer (first account key) and total fee of one transaction; null when the ledger no longer has it.const payerOf = async (signature: Signature) => {  const tx = await rpc.getTransaction(signature, cfg).send();  return tx ? { payer: String(tx.transaction.message.accountKeys[0]), fee: Number(tx.meta?.fee ?? 0) } : null;};let sponsor = process.argv[2] ?? "";if (!sponsor) {  const recent = await rpc.getSignaturesForAddress(address(BUSY_PROGRAM), { limit: 5 }).send().catch(() => []);  const found = await Promise.allSettled(recent.map((s) => payerOf(s.signature)));  sponsor = found.flatMap((f) => (f.status === "fulfilled" && f.value ? [f.value.payer] : []))[0] ?? "";  if (!sponsor) { console.error("No fee payer found; pass an address."); process.exit(1); }  console.log(`no address given, using a recent fee payer: ${sponsor}`);}const [bal, floor, sigs] = await Promise.allSettled([  rpc.getBalance(address(sponsor)).send(),  rpc.getMinimumBalanceForRentExemption(0n).send(),  rpc.getSignaturesForAddress(address(sponsor), { limit: SAMPLE }).send(),]);for (const r of [bal, floor, sigs]) if (r.status === "rejected") console.error("call failed:", String(r.reason));if (bal.status !== "fulfilled") process.exit(1);const spendable = Number(bal.value.value) - (floor.status === "fulfilled" ? Number(floor.value) : 0);const list = sigs.status === "fulfilled" ? sigs.value : [];const done = await Promise.allSettled(list.map((s) =>  payerOf(s.signature).then((p) => (p && p.payer === sponsor && s.blockTime != null ? { t: Number(s.blockTime), fee: p.fee } : null))));const paid = done.flatMap((d) => (d.status === "fulfilled" && d.value ? [d.value] : [])).sort((a, b) => b.t - a.t);const failed = done.filter((d) => d.status === "rejected").length;console.log(`sponsor ${sponsor}`);console.log(`balance ${bal.value.value} lamports, spendable ${spendable}`);console.log(`sampled ${list.length} signatures, ${paid.length} paid by the sponsor, ${failed} lookups failed`);const hours = paid.length > 1 ? (paid[0]!.t - paid[paid.length - 1]!.t) / 3600 : 0;if (hours <= 0) {  console.log("Need two fee-paying transactions at different times for a burn rate.");} else {  const perHour = (paid.reduce((n, p) => n + p.fee, 0) - paid[paid.length - 1]!.fee) / hours;  const runway = spendable / perHour;  console.log(`burn ${perHour.toFixed(0)} lamports/hour, runway ${runway.toFixed(1)} hours`);  if (runway < MIN_HOURS) console.log(`ALERT runway under ${MIN_HOURS} hours for ${sponsor}`);}

run: CARBIUM_KEY=$CARBIUM_KEY node main.ts <sponsor-address>

Each call is settled on its own, so one failed lookup shows up as a count and the rest of the sample still produces a number. getTransaction passes maxSupportedTransactionVersion: 1, which keeps v1 transactions from answering -32015. Calls leave commitment out, and @solana/kit fills in confirmed, so the balance and the transactions are read at confirmed.

The output is four lines: the address, the balance with its spendable part, how many signatures were sampled and how many the sponsor paid for, and the burn rate with the runway. The last line starts with ALERT when the runway is under the threshold. Wire that line to your pager or to the job that tops the wallet up.

04

What a check costs

The sample is the expensive part. Every transaction lookup is a 50-cell call, and the address listing is another.

CallCellsFree req/sDev req/sBusiness req/sPro req/s
getSignaturesForAddress500.241.2100500
getTransaction500.241.2525

A default run is one listing and ten lookups, 550 cells across those two classes. A class absorbs bursts up to 1,024 cells or one second of its rate, whichever is larger, and a poll of 15 lookups is 750 cells in the lookup class, which fits inside one burst. Credits meter this RPC work as well, and an account's keys share both the rates and the credits.

Run it from a timer every few minutes. To cut the cost further, keep the signatures you have already priced in your own store and pass the newest as until, so each poll prices only new transactions and keeps a running list for the burn window.

05

Reacting sooner with a log subscription

Polling is enough for a runway that is measured in hours. If you want a reaction per sponsored transaction, subscribe over the WebSocket. A sponsor is a plain SOL wallet, and accountSubscribe sends nothing for a plain SOL wallet, so use logsSubscribe with a mentions filter on its address and call getBalance after each notification.

A mentions subscription opens at 25 credits and then costs 20 credits per MiB sent. A busy sponsor streams every transaction it touches, so the bill follows your traffic. Keep the subscription for the days you run close to the floor, and poll the rest of the time.

06

Where it ends

  • The burn rate comes from a sample of at most 15 transactions, so a short burst of traffic inflates it and a quiet hour deflates it. Compare two windows and alert on the worse one.
  • getSignaturesForAddress reads from an address index that keeps up to 30 days, and its pages reach below the validator's ledger floor, so getTransaction can answer null for an older signature it lists. The script skips those and samples the newest.
  • The monitor measures fees. A sponsor that also funds rent for accounts it creates, or pays fees in a token, spends more than the number shows. Add those flows from your own records.
  • It reads one wallet. A product with a pool of fee payers runs it once per wallet and alerts on the pool total as well as on each member.
  • It signs and sends nothing. Topping the wallet up is a separate job with its own key and its own approval path.
  • A balance read and a transaction read can sit a slot or two apart at confirmed. For a runway in hours that gap does not matter.
07

When it goes wrong

what goes wrongwhat to do
The output says it needs two fee-paying transactions and prints no runway.The sample held fewer than two transactions the sponsor paid for, so there is no span to divide by. Raise SAMPLE up to 15 or poll later. An idle sponsor has no burn to project.
getTransaction answers null for a signature the listing returned.The listing reaches below the validator's ledger floor, so an older signature can have no transaction to read. Skip it and sample the newest signatures.
getTransaction answers -32015 for a recent signature.Pass maxSupportedTransactionVersion: 1 in the call config. Left out or 0, the call refuses a v1 transaction. See Transaction version error -32015.
The alert fires on every poll while the wallet sits below the threshold.Keep the last alert time and stay quiet for a set period, then page again if the runway keeps falling.
The spendable figure is lower than the balance in a wallet app.The script subtracts the rent-exempt minimum for a zero-data account. That amount stays on the account and cannot pay fees.
Several lookups fail at once during a poll.The sample went over a rate. Lower SAMPLE, poll less often, or cache priced signatures and look up only new ones.
08

Questions

Does a failed sponsored transaction cost the wallet?

Yes. A transaction that lands and fails still charges its fee to the fee payer. The script counts failed transactions in the burn rate for that reason.

How often should the monitor run?

Runway is measured in hours, so every few minutes is enough. Each run with the default sample is 550 cells over the two lookup classes, and a tight loop would spend rate you need elsewhere.

How do I watch a pool of fee payers?

Run the script once per wallet with each address as the argument, then add up the spendable balances and the burn rates for a pool runway. Alert on the pool and on each wallet, because one drained payer fails the sends routed to it.

Can it tell me when a user hits a limit on my sponsorship?

No. Per-user caps live in your own service, and this script watches only the shared wallet's balance and burn. Pair it with a counter per user in your database, as in RPC rate-limit alerts.