Program upgrade authority watch
Watch the upgrade authority and last deploy slot of Solana programs. You get a script that decodes the loader accounts, remembers each program between runs in SQLite, and flags an authority change or a redeploy, plus rules for what to do next.
Why so many programs can change
Most Solana programs are upgradeable because the default deploy makes them so. A program deployed with the standard command lives under the upgradeable loader, which splits it into two accounts and records one address, the upgrade authority, that may replace the code later. A team that wants to fix a bug, add an instruction or grow the program does it with an upgrade at the same program address, so every integrator, frontend and other program keeps calling the address it already knows. Making a program immutable takes a deliberate step, setting the authority to none, and it cannot be undone.
That convenience is also the exposure. Whoever holds the authority can replace the code that guards user funds, and transactions in later slots run the new code. Counterparties, treasuries and risk teams ask the same questions of every protocol they touch: who holds the authority, is it one key or a multisig, and has the code changed since we last looked. The answers sit in the first bytes of two accounts, and a few batched reads get them for a whole list.
What the loader keeps
An upgradeable program has a program account and a program data account. The program account is 36 bytes and points at the program data account. The program data account holds the deploy slot, the authority and then the compiled program. The watch needs only the first 45 bytes of it, so it asks for that slice and the response stays small even when the program is hundreds of kilobytes.
| Account | Offset | Size | Reading |
|---|---|---|---|
| Program | 0 | 4 | Tag, 2 for a program |
| Program | 4 | 32 | Address of its program data account |
| Program data | 0 | 4 | Tag, 3 |
| Program data | 4 | 8 | Slot of the last deploy, little-endian |
| Program data | 12 | 1 | 1 when an authority is set |
| Program data | 13 | 32 | The upgrade authority |
The program account must be owned by the upgradeable loader, BPFLoaderUpgradeab1e11111111111111111111111. A program under an older loader has no record in this layout, so the script prints its owner and moves on. When the authority option is 0 the program is immutable and no instruction can set an authority again: the Token program shows this, with authority none and deploy slot 419472000. The slot is the slot of the last deploy, so a higher number between two runs means the code changed even when the authority stayed put.
The script
Run it with program addresses as arguments, or with none to watch four busy programs, one of them immutable. It makes three getMultipleAccounts reads at finalized, so an alert never fires on state a dropped fork could undo: the program accounts, then the program data accounts they name, then the accounts that hold the authorities. Each read is settled on its own, so a failed read prints which one failed and the run still prints what it has, with owner unknown when the authority read is the one that failed. What it saw goes into a watch.db SQLite file in the folder where you run it.
// UC0044 · Program upgrade authority watch// https://carbium.sh/library/usecase/program-upgrade-authority-watch// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MIT// Read-only watch: reports each program's upgrade authority and last deploy slot, and what changed since the previous run.import { DatabaseSync } from "node:sqlite";import { createDefaultRpcTransport, createSolanaRpcFromTransport, getAddressDecoder, isAddress, type Address } 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 LOADER = "BPFLoaderUpgradeab1e11111111111111111111111";const SYSTEM = "11111111111111111111111111111111";const DEFAULTS = [ "JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4", // Jupiter v6 "whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc", // Orca Whirlpools "LBUZKhRxPF3XUpBCjp4YzTKgLccjZhTSDM9YuVaPwxo", // Meteora DLMM "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", // Token program: immutable, authority none];const args = process.argv.slice(2);const list = args.length ? args : DEFAULTS;list.filter((p) => !isAddress(p)).forEach((p) => console.log(`${p} not an address`));const valid = list.filter(isAddress);if (valid.length > 34) { console.error("At most 34 programs a run: three reads stay inside the 1,024-cell burst."); process.exit(1); }const rpc = createSolanaRpcFromTransport(createDefaultRpcTransport({ url, headers: { "x-api-key": key } }));const decoder = getAddressDecoder();type Acc = { owner: string; bytes: Buffer } | null;async function read(addrs: Address[], dataSlice?: { offset: number; length: number }): Promise<Acc[]> { if (!addrs.length) return []; const { value } = await rpc.getMultipleAccounts(addrs, { encoding: "base64", commitment: "finalized", dataSlice }).send(); return value.map((a) => (a ? { owner: a.owner, bytes: Buffer.from(a.data[0], "base64") } : null));}// One failed read is printed and the run goes on with what it has.async function attempt(what: string, addrs: Address[], dataSlice?: { offset: number; length: number }): Promise<Acc[] | null> { try { return await read(addrs, dataSlice); } catch (e) { console.error(`${what} read failed:`, e instanceof Error ? e.message : e); process.exitCode = 1; return null; }}type Row = { program: Address; note?: string; closed?: boolean; slot?: bigint; authority?: Address | null };const db = new DatabaseSync("watch.db");db.exec("CREATE TABLE IF NOT EXISTS seen (program TEXT PRIMARY KEY, authority TEXT, slot TEXT)");const last = db.prepare("SELECT authority, slot FROM seen WHERE program = ?");const save = db.prepare("INSERT OR REPLACE INTO seen VALUES (?, ?, ?)");const drop = db.prepare("DELETE FROM seen WHERE program = ?");const accounts = await attempt("program account", valid);if (!accounts) process.exit(1);// A program account under the upgradeable loader: tag 2, then the address of its program data account.const links = accounts.map((a) => a && a.owner === LOADER && a.bytes.length === 36 && a.bytes.readUInt32LE(0) === 2 ? decoder.decode(a.bytes.subarray(4)) : null);// Program data: tag 3, deploy slot (u64), authority option (1 byte), key. The first 45 bytes are enough.const data = await attempt("program data", links.filter((l): l is Address => l !== null), { offset: 0, length: 45 });let next = 0;const rows: Row[] = valid.map((program, i) => { const acc = accounts[i]; if (!acc) return { program, note: "missing at finalized" }; if (!links[i]) return { program, note: `no upgrade record, owner ${acc.owner}` }; if (!data) return { program, note: "program data read failed" }; const d = data[next++]; if (!d) return { program, note: "program data account closed", closed: true }; const b = d.bytes; if (b.length < 45 || b.readUInt32LE(0) !== 3) return { program, note: "program data account unreadable" }; return { program, slot: b.readBigUInt64LE(4), authority: b[12] === 1 ? decoder.decode(b.subarray(13, 45)) : null };});const holders = [...new Set(rows.flatMap((r) => (r.authority ? [r.authority] : [])))];const held = await attempt("authority", holders);const kind = (h: Address) => { if (!held) return "owner unknown"; const owner = held[holders.indexOf(h)]?.owner; return owner === undefined ? "no account" : owner === SYSTEM ? "system-owned" : `owned by ${owner}`;};let changed = false;for (const r of rows) { const before = last.get(r.program) as { authority: string | null; slot: string } | undefined; if (r.slot === undefined || r.authority === undefined) { const gone = r.closed && before; if (gone) { changed = true; drop.run(r.program); } console.log(`${r.program} ${r.note}${gone ? `; CLOSED, last slot ${before.slot}` : ""}`); continue; } const status = !before ? "new" : [ before.authority !== r.authority && `AUTHORITY CHANGED from ${before.authority ?? "none"}`, before.slot !== String(r.slot) && `REDEPLOYED, last slot ${before.slot}`, ].filter(Boolean).join("; ") || "same"; if (before && status !== "same") changed = true; save.run(r.program, r.authority, String(r.slot)); const who = r.authority ? `${r.authority} (${kind(r.authority)})` : "none, immutable"; console.log(`${r.program} deploy-slot=${r.slot} authority=${who} ${status}`);}if (changed) process.exitCode = 2;
run: CARBIUM_KEY=$CARBIUM_KEY node main.ts <program1> <program2>
Each line shows the program, the slot of its last deploy, the authority with the kind of account that holds it, and a status: new on first sight, same, AUTHORITY CHANGED from <old>, or REDEPLOYED, last slot <old>, or both. A run that sees a change exits with 2, so cron or a CI step can raise it. A program that is missing, sits under another loader or cannot be read prints a plain line. A program saved in an earlier run that now has no program data account prints CLOSED, last slot <old>, counts as a change and exits 2, and its row is dropped so the next run is quiet.
What a run costs
Account reads cost 10 cells per pubkey. A run of N programs makes three reads of at most N pubkeys, so it costs at most 30N cells. The script takes 34 programs a run, which is 340 cells a read and 1,020 for all three. That fits inside a burst of at least 1,024 cells on every plan, so no read in a run has to wait for a refill.
| Programs in a run | Cells for one read | Cells for three reads |
|---|---|---|
| 4 | 40 | 120 |
| 12 | 120 | 360 |
| 34 | 340 | 1,020 |
The third read covers only distinct authorities, so it is usually smaller. For a longer list, split it across runs: the database is keyed by program address and shared by all of them. Limits are per account, so a watch on a schedule shares one budget with every other key on the account.
What a change means
| Finding | What it allows | Suggested response |
|---|---|---|
| Authority set, system-owned | A single key or a multisig vault address; the owner alone does not say which | Ask who holds it before adding exposure |
| Authority owned by a program | A multisig or governance program decides | Read that account to find members and threshold |
| Authority none | No instruction can change the authority or the code. A cluster feature gate can still replace a core program in place: the Token program once sat under the non-upgradeable loader, and its deploy slot 419472000 falls on an epoch boundary | Keep comparing the slot, as the script does |
| Authority changed | Control moved to a new holder | Alert at once; compare with the protocol's own notice |
| Slot higher, authority same | New code at the same address | Review the deploy before moving more funds |
| Program data account gone | Program closed | Treat as retired and stop calling it |
The owner field gives the account type and nothing about who signs for it. A multisig vault is often a system-owned address, so treat the table as a first filter and read the protocol's governance notes for the rest.
To learn of an upgrade between runs, open a logsSubscribe with a mention of the program data account and run the script when a line arrives. A subscription with a mention opens at 25 credits and then costs 20 credits per MiB sent. Upgrade and set-authority instructions name that account, and a quiet account sends almost nothing. The same screen for token mints is in Token authority screening.
Where it ends
- Only the loader's authority is read. A program's own admin keys, pause switches and fee authorities live in its own accounts and need a decoder written for that program.
- The script reports that the code changed; it never reads the compiled program, so comparing builds is a separate job.
- Two deploys between runs show as one change, with the later slot.
- Upgrade history comes from getSignaturesForAddress on the program data address. It costs 50 cells and reaches back 30 days at most, because its pages come from an address index. An empty page past that is the index's age limit, and the address may have older deploys.
- getProgramAccountsV2 is not served and answers -32601, so a scan for upgradeable programs through it is out and the watch list is one you name.
When it goes wrong
| what goes wrong | what to do |
|---|---|
| Every program prints no upgrade record. | The address passed is not a program account under the upgradeable loader. Pass the program id; a program data account, an IDL account or a token mint has no upgrade record. |
| Every run prints new for every program. | watch.db is created in the current folder, so a job started from another folder gets a fresh file each time. Give the job a fixed working directory. |
| A run of 34 programs or fewer gets 429 on one of its reads. | Other keys on the account have drained the shared burst, because limits are per account. Run the watch when those jobs are idle, or split the list so each run asks for fewer cells, and retry after the hint the 429 carries. |
| The program data read comes back with the whole program in it. | The read went out without a slice. Ask for dataSlice with offset 0 and length 45, which is all the header needs. |
| Authority prints none and an alert is still expected. | None is final for the authority: it can never be set again. A feature-gated runtime replacement still shows as REDEPLOYED, so leave the program on the list. |
| A hunt for upgradeable programs with getProgramAccountsV2 answers -32601. | The method is not served, so do not retry it. Build the watch list from the programs you depend on and pass it as arguments. |
Questions
Is an upgradeable program unsafe?
No. Most live programs are upgradeable. The risk depends on who holds the authority and how they act, and a multisig with review is a different case from one hot key.
Can an immutable program become upgradeable again?
No. Once the authority is set to none, nothing remains that could set it again, so a finalized read of none can be relied on.
How do I find who deployed a change?
List the signatures of the program data address with getSignaturesForAddress, which reaches back 30 days at most, and read the signer of the upgrade transaction with getTransaction. Pass maxSupportedTransactionVersion: 1, because mainnet carries v1 transactions and the call answers -32015 without it. It can answer null for an older signature the address index lists, so handle null.
How many programs can one run watch?
The script takes 34. Each read costs 10 cells per pubkey, so three reads of 34 cost 1,020 cells, inside a burst of at least 1,024 cells on every plan.