library / How to use Solana / Token-2022 / HS0052
Token-2022 mint close check
One TypeScript file reads Token-2022 mints in a single call and prints each mint's supply, its close authority and a verdict on whether the mint can be closed. It finds mints from recent transactions when you pass none, and it only reads.
What closing a mint means
A mint account holds a token's supply, its decimals and its authorities, and it carries a rent deposit in lamports. Closing a mint deletes the account and sends that deposit to a destination account the closer names. Afterwards the address holds nothing, and a read of it returns null.
The legacy Token program has no instruction that closes a mint. Its close instruction works on token accounts, so a classic mint stays on chain for good with its deposit, even when its supply is zero. Token-2022 adds a path: a mint created with the mint close authority extension can be closed by the key that extension names.
That leaves three answers for any mint you meet: it can never be closed, it can be closed once its supply reaches zero, or it can be closed right now. The file below prints one of the three for each mint, next to the supply and authority it based the answer on.
The close authority extension and the zero-supply rule
The extension has to exist when the mint is initialized. A mint created without it can never gain it. Inside the account, the first 82 bytes are the base mint: the mint authority option, the supply as a little-endian u64 at byte 36, the decimals, an initialized flag and the freeze authority option. Zero padding runs to byte 165, and byte 165 holds the account type, 1 for a mint. From byte 166 on come extension records, each with a u16 type, a u16 length and the value.
The mint close authority record is type 3 with a 32-byte value, the authority's address. A value of all zeros means no authority. The authority can be handed to another key, or cleared to zeros, with the set authority instruction for the close mint kind. Once cleared it cannot be set again, so that mint is closable by nobody, ever.
The zero-supply rule decides the rest. The close instruction, the same one that closes token accounts, fails on a mint while its supply is above zero, and the close authority must sign it. Supply falls only when holders burn tokens, so a mint with circulating tokens stays open until they are gone. The lamports go to whichever destination account the authority names.
The code
Add @solana/kit 8.6.0 to the project, set CARBIUM_KEY and start the file on Node 24, which strips the types itself. Pass mint addresses as arguments. With none, the file takes the five newest transactions of the Token-2022 program and collects the mints from their postTokenBalances.
// HS0052 · Token-2022 mint close check// https://carbium.sh/library/howsolana/token-2022-mint-close-check// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MITimport { address, createDefaultRpcTransport, createSolanaRpcFromTransport, getAddressDecoder } from "@solana/kit";const apiKey = process.env.CARBIUM_KEY ?? "";if (apiKey === "") { console.error("Set CARBIUM_KEY to your Carbium API key."); process.exit(1);}const endpoint = process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh";const client = createSolanaRpcFromTransport( createDefaultRpcTransport({ url: endpoint, headers: { "x-api-key": apiKey } }),);const LEGACY_PROGRAM: string = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";const EXT_PROGRAM: string = "TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb";// Byte layout of a mint account, and the one extension record we look for.const LAYOUT = { supplyAt: 36, baseSize: 82, typeAt: 165, recordsAt: 166, multisigSize: 355, closeRecord: 3 };const SAMPLE_TXS = 5;const MAX_MINTS = 100;async function discoverMints(): Promise<string[]> { const listed = await client.getSignaturesForAddress(address(EXT_PROGRAM), { limit: SAMPLE_TXS }).send(); const settled = await Promise.allSettled( listed.map((l) => client.getTransaction(l.signature, { maxSupportedTransactionVersion: 1, encoding: "json" }).send()), ); const seen = new Set<string>(); settled.forEach((res, n) => { if (res.status === "rejected") { console.error(`transaction ${n + 1} failed: ${res.reason instanceof Error ? res.reason.message : String(res.reason)}`); return; } for (const bal of res.value?.meta?.postTokenBalances ?? []) seen.add(bal.mint); }); return Array.from(seen);}function parseMint(raw: Buffer, owner: string): { supply: bigint; authority: string | null } | null { const extended = owner === EXT_PROGRAM; const sized = raw.length === LAYOUT.baseSize; const tagged = extended && raw.length > LAYOUT.typeAt && raw.length !== LAYOUT.multisigSize && raw[LAYOUT.typeAt] === 1; if (!sized && !tagged) return null; let authority: string | null = null; let at = LAYOUT.recordsAt; while (extended && at + 4 <= raw.length) { if (raw.readUInt16LE(at) === LAYOUT.closeRecord) { const value = raw.subarray(at + 4, at + 36); if (value.some((byte) => byte !== 0)) authority = getAddressDecoder().decode(value); break; } at += 4 + raw.readUInt16LE(at + 2); } return { supply: raw.readBigUInt64LE(LAYOUT.supplyAt), authority };}function verdictFor(authority: string | null, supply: bigint): string { if (authority === null) return "never closable"; return supply === 0n ? "closable now" : "closable once supply reaches zero";}const args = process.argv.slice(2);const targets = (args.length ? args : await discoverMints()).slice(0, MAX_MINTS);if (targets.length === 0) { console.log("no mints found"); process.exit(0);}let rows;try { rows = (await client.getMultipleAccounts(targets.map((t) => address(t)), { encoding: "base64" }).send()).value;} catch (err) { console.error(`getMultipleAccounts failed: ${err instanceof Error ? err.message : String(err)}`); process.exit(1);}targets.forEach((mint, n) => { const row = rows[n]; if (!row) return console.log(`${mint} no account`); if (row.owner !== LEGACY_PROGRAM && row.owner !== EXT_PROGRAM) { return console.log(`${mint} owned by ${row.owner}: no mint close there`); } const parsed = parseMint(Buffer.from(row.data[0], "base64"), row.owner); if (!parsed) return console.log(`${mint} an account of ${row.owner} that is no mint`); const { supply, authority } = parsed; console.log(`${mint} supply ${supply} base units authority ${authority ?? "none"} ${verdictFor(authority, supply)}`);});
run: node main.ts [mint-address ...]
Each transaction is read with maxSupportedTransactionVersion: 1. Mainnet carries v1 transactions, and a call with that value absent or 0 answers -32015 for one. A transaction that comes back null is skipped, and a failed read is printed while the others go on. Reads leave commitment out, so kit fills in confirmed.
The mints then go to one getMultipleAccounts call with base64 encoding, and the file decodes the bytes itself. Supply and extension records arrive in the same account, so they come from one slot and cannot disagree. Before reading a mint's layout, the file checks the size. An 82-byte account is a base mint. Beyond 165 bytes, byte 165 must equal 1. A 355-byte Token-2022 account is a multisig whose signer keys fill that position, so byte 165 can happen to be 1 there, and the file rules that size out.
Discovery spends one getSignaturesForAddress and five getTransaction reads at 50 cells a call, 300 cells together. getMultipleAccounts charges 10 cells per pubkey: a call with 100 mints costs 1,000 cells, or 1,001 when its body passes 4 KiB, as a 100-mint body does. The file sends at most 100 mints, because below Pro the 1,024-cell burst caps a call at 100 pubkeys. Pro takes up to 300.
Reading the three verdicts
Each mint prints one line with its address, its supply in base units, its close authority or none, and the verdict.
| Verdict | When it prints | What it means |
|---|---|---|
| never closable | A legacy Token mint, a Token-2022 mint without the extension, or one whose close authority is cleared | No instruction can close it, and its address stays taken |
| closable once supply reaches zero | The extension names an authority and supply is above 0 | Every token must be burned first, then the authority can close it |
| closable now | The extension names an authority and supply is 0 | The authority can close it in one transaction and take the rent deposit |
Two other lines can appear. no account means the address holds nothing, which is also how a closed mint reads. An address owned by another program prints owned by and that program id with no mint close there, and a Token-2022 account that is no mint, such as a token account or a multisig, prints a line saying so.
What closing does to the address
A closed mint address is free again. Whoever can sign the account creation for that address can create a mint there with new decimals, a new supply and new authorities. The tokens, wallets and token accounts that pointed at the old mint then point at a different token, and so does any record you keep keyed by that address, such as an allowlist entry or a price feed mapping.
Who can do it depends on how the address was made. Most mints sit at a generated keypair address, and bringing one back needs that keypair's secret key to sign the creation. If the key was discarded after launch, the address stays empty after a close. A mint at a program-derived address has no secret key at all. The program that derives it can create the account again by signing with its seeds, so a closable mint of that kind is only as stable as the program behind it.
So a verdict of closable now or closable once supply reaches zero is worth reading together with the authority. When the authority is a single wallet, the owner of that wallet decides. When it is a program address, the program's code decides, and that is the code to read.
Where it ends
The file reports the state at one moment. The authority can change hands or be cleared, and supply can move, between your read and any later close. It never sends a close, signs anything or shows who holds the authority key, so a multisig or a program address behind the authority needs its own look.
A mint that was already closed reads as no account, and nothing in that read shows it was ever a mint. The run-time discovery looks at five recent transactions, a sample that shows what is touching the program now and says nothing about the whole set of Token-2022 mints. Extensions other than the close authority are ignored on purpose.
When it goes wrong
| what goes wrong | what to do |
|---|---|
| A discovery read prints -32015. | A transaction in the discovery step is a v1 transaction and the call left the version out. Keep maxSupportedTransactionVersion: 1 on every getTransaction read. |
| The file prints no mints found. | All five recent transactions came back null or held no token balances. Run it again a moment later, or pass mint addresses as arguments. |
| A line says owned by another program. | The address holds an account of a program other than the token programs. Check that you pasted a mint and not a wallet or a program account. |
| A line says no account for a mint you expected. | The address is mistyped, the mint is new and not yet visible at confirmed commitment, or it was closed. Try again after a few seconds, then check the spelling. |
| HTTP 429 with a retry-after hint. | Wait the hinted time and pass fewer mints per run. The largest call the file sends carries 100 mints and costs 1,000 or 1,001 cells. |
Questions
Can I add a close authority to a mint that already exists?
No. The extension is set when the mint is initialized. A mint created without it stays unclosable for its whole life.
Who gets the lamports when a mint is closed?
The destination account named in the close instruction, which the close authority picks. It can be any account that can receive lamports.
Does the legacy Token program ever close a mint?
No. Its close instruction handles token accounts only, so every classic mint stays on chain for good.
Do I need a private key to run this?
No. The file reads accounts and transactions only. The Carbium key identifies your account to the RPC.