---
id: UC0141
title: "Campaign window spend qualifier"
url: https://carbium.sh/library/usecase/campaign-window-spend-qualifier
collection: Use cases
category: Loyalty, rewards and memberships
revision: 1
updated: 2026-11-23
author: Carbium
code: MIT (LICENSE in the zip); text: all rights reserved, Carbium Corporation
---

# Campaign window spend qualifier

A rewards campaign pays wallets that spent inside a set window. The check reads each wallet's signatures in that window, reads the SOL it spent in the newest five, and reports whether it qualifies, with a read-only Node script.

## What counts as a spend

A rewards window needs a rule the campaign owner can state in one sentence. This one has two parts. The wallet must have a successful transaction whose block time falls inside the window, and that transaction must lower the wallet's SOL balance by at least a threshold. The threshold is in lamports and defaults to 10,000,000, which is 0.01 SOL. The fee is part of the drop, so a transaction that moves nothing does not clear a threshold of that size.

Block time is in Unix seconds. A campaign runs on a calendar, so the window is set in those seconds, and the script works back from the current time by a number of hours.

| Step | Method | Cells per call | Free req/s | Dev req/s |
|---|---|---|---|---|
| List signatures | getSignaturesForAddress | 50 | 0.24 | 1.2 |
| Read one transaction | getTransaction | 50 | 0.24 | 1.2 |

A check on one wallet costs one list and up to five reads, which is 300 cells at most. A sweep over many wallets multiplies that, and the request rates in the table set how fast the sweep can run on each plan.

## Reading the window from signatures

getSignaturesForAddress returns one entry per transaction, newest first, each with an err field and a block time. The script keeps the entries whose err is null and whose block time is at or after the window start. The call leaves commitment out, so @solana/kit sends confirmed, and the newest entries are read at that level.

The script asks for one page of 100 entries. A full page means the window may hold more than the count shows, so the script prints that the count is a floor. Paging further back means passing the last signature as the before cursor, which this script leaves out to keep the check small.

The address index keeps up to 30 days of history for an address. A window that starts earlier than that cannot be counted from signatures, and an empty page past the 30-day mark is the index's age limit, so it says nothing about the wallet's older activity.

## Reading the spend from the transaction

For each of the newest five in-window entries, the script calls getTransaction with encoding json, commitment confirmed and maxSupportedTransactionVersion set to 1. Mainnet carries v1 transactions. Without that value the call answers -32015 for a v1 transaction, so the script sets it on every read.

The reply lists the account keys and the pre and post lamport balances. The script finds the wallet's position in the key list and takes the difference. A drop at or above the threshold counts as a spend.

A null answer is a normal result for an older signature that the index still lists, because the index reaches below the validator's ledger floor. The script prints that entry and moves on, and a null never adds to the spend count.

The sample holds five entries. A wallet with more in-window activity can qualify on an entry the script never reads, so a yes holds for the wallet, and a no on a busy wallet means only that the five newest entries did not qualify.

## One read-only script

The script reads CARBIUM_KEY and sends every call through the RPC endpoint, https://services.carbium.sh, with the key in the [x-api-key header](https://carbium.sh/docs/auth). It signs nothing and sends nothing. A wallet passed on the command line is checked directly. Without one, the script takes the fee payer of the newest successful transaction on the token program that reads back, so a run needs no address from you.

`main.ts` (run: `CARBIUM_KEY=$CARBIUM_KEY node main.ts`)

```ts
// UC0141 · Campaign window spend qualifier
// https://carbium.sh/library/usecase/campaign-window-spend-qualifier
// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.
// SPDX-License-Identifier: MIT

// Read-only: does a wallet hold a successful spend inside a campaign window?
import { 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 hours = Math.min(Number(process.env.CAMPAIGN_HOURS ?? 24), 720);
const minSpend = Number(process.env.MIN_SPEND_LAMPORTS ?? 10_000_000);
const TOKEN_PROGRAM = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";

const rpc = createSolanaRpcFromTransport(
  createDefaultRpcTransport({ url, headers: { "x-api-key": key } }),
);

type Sig = { signature: string; err: unknown; blockTime: number | bigint | null };
type Tx = {
  meta: { preBalances: (number | bigint)[]; postBalances: (number | bigint)[] } | null;
  transaction: { message: { accountKeys: string[] } };
} | null;

const listSigs = async (addr: string, limit: number) =>
  (await rpc.getSignaturesForAddress(address(addr), { limit }).send()) as unknown as Sig[];

const readTx = async (sig: string) =>
  (await rpc.getTransaction(signature(sig), {
    encoding: "json", maxSupportedTransactionVersion: 1, commitment: "confirmed",
  }).send()) as unknown as Tx;

async function busyWallet(): Promise<string> {
  for (const s of await listSigs(TOKEN_PROGRAM, 10)) {
    if (s.err !== null) continue;
    const payer = (await readTx(s.signature))?.transaction.message.accountKeys[0];
    if (payer) return payer;
  }
  throw new Error("no readable transaction among the newest token program signatures");
}

async function main() {
  const wallet = process.argv[2] ?? (await busyWallet());
  const start = Math.floor(Date.now() / 1000) - hours * 3600;
  const sigs = await listSigs(wallet, 100);
  const inWindow = sigs.filter((s) => s.err === null && s.blockTime !== null && Number(s.blockTime) >= start);
  console.log(`wallet ${wallet}: ${sigs.length} signatures listed, ${inWindow.length} successful in the last ${hours} h`);
  if (sigs.length === 100) console.log("page full: the window may hold more, so the count is a floor");

  const sample = inWindow.slice(0, 5);
  const reads = await Promise.allSettled(sample.map((s) => readTx(s.signature)));
  let spends = 0;
  reads.forEach((r, i) => {
    const id = sample[i]!.signature.slice(0, 12);
    if (r.status === "rejected") return console.log(`read failed ${id}: ${String(r.reason)}`);
    const tx = r.value;
    const meta = tx?.meta;
    const idx = tx ? tx.transaction.message.accountKeys.indexOf(wallet) : -1;
    if (!tx || !meta || idx < 0) return console.log(`${id}: null answer or wallet not in the message`);
    const drop = Number(meta.preBalances[idx]) - Number(meta.postBalances[idx]);
    if (drop >= minSpend) spends++;
    console.log(`${id}: wallet SOL change ${-drop} lamports`);
  });
  console.log(`sampled ${sample.length}, spends of at least ${minSpend} lamports: ${spends}, qualifies: ${spends > 0 ? "yes" : "no"}`);
}

main().catch((e) => console.error("failed:", e instanceof Error ? e.message : e));
```

Set CAMPAIGN_HOURS to choose the window, up to 720 hours, and MIN_SPEND_LAMPORTS for the threshold. The run prints one line per sampled entry and then a final verdict.

## Where it ends

- The count covers the newest 100 signatures for one address. A busy wallet can hold more in the window than the script lists, and the script says so when the page fills.
- The index holds 30 days per address. A campaign that opens earlier than that needs its own record of past spends. For a wider date range, see the [date-range transaction query](https://carbium.sh/library/usecase/date-range-transaction-query).
- A spend here is a balance drop in the wallet's SOL. A token spend, or a payment in another mint, is outside this check.
- A qualifier names wallets that paid a cost. It does not show who owns a wallet or whether a person signed the transaction.
- The script keeps no state and pays nothing. Who has already been rewarded, and the payout itself, belong to the campaign's own service.
- Free keys read 0.24 req/s on both calls, and a key over its limit gets a [429](https://carbium.sh/docs/limits) with a retry-after hint. A sweep needs a plan with more rate.

## When it goes wrong

| What goes wrong | What to do |
|---|---|
| The window starts hours off, so the count includes or drops whole days. | Block time is in Unix seconds. Build the start as Date.now() divided by 1000, minus the window in seconds, as the script does, and print the start when the campaign is set up. |
| A wallet reads zero spends because its busy page filled before the window began. | Treat a full page as incomplete. Page back with the before cursor, or narrow the window and run the check again. |
| getTransaction answers -32015 on a block that holds a v1 transaction. | Keep maxSupportedTransactionVersion set to 1 with an encoding, as the script sends it. The [errors page](https://carbium.sh/docs/errors) lists the codes. |
| A null read is counted as a spend, or as a failure of the whole campaign. | Count a null separately as unreadable and move on. It is an older entry the index still lists, and it adds nothing to the spend count. |
| A sweep over many wallets gets 429 from its first minute. | A call over its rate waits once, then gets 429 with a retry-after hint. Space the sweep to the request rate of your plan, read the hint, and wait that long before the next call. |
| The key ends up in a log line or a shared script. | Keep the key in CARBIUM_KEY and the x-api-key header. The script never prints it, so a log holds only the wallet and the counts. |

## Questions

### Does the script move funds or sign anything?

No. It lists signatures and reads transactions, and it never signs, sends or requests funds. Paying a reward is a separate step in the campaign's own service.

### Why does the count stop at 100?

The script asks for one page of 100 signatures. A full page means the window may hold more, and the script prints that the count is a floor.

### Can it check a window that opened more than 30 days ago?

No. The address index keeps up to 30 days, and an empty page past that is the index's age limit, so older spends need a record you keep yourself.

### Which commitment does it read at?

The transaction reads set confirmed. The signature list leaves commitment out, so @solana/kit sends confirmed there too.

## Related

- [Date-range transaction query](https://carbium.sh/library/usecase/date-range-transaction-query) (Use cases, UC0123)
- [Signature history pagination](https://carbium.sh/library/howsolana/signature-history-pagination) (How to use Solana, HS0034)
- [carbium.sh/docs/limits](https://carbium.sh/docs/limits)
- [carbium.sh/docs/rpc](https://carbium.sh/docs/rpc)
- [carbium.sh/docs/auth](https://carbium.sh/docs/auth)
- [carbium.sh/docs/errors](https://carbium.sh/docs/errors)
- [carbium.sh/docs/endpoints](https://carbium.sh/docs/endpoints)
