---
id: BI0115
title: "Rent-exempt margin audit"
url: https://carbium.sh/library/blockchainideas/rent-exempt-margin-audit
collection: Blockchain ideas
category: Developer tools
revision: 1
updated: 2026-11-23
author: Carbium
code: MIT (LICENSE in the zip); text: all rights reserved, Carbium Corporation
---

# Rent-exempt margin audit

You get an audit that reads any list of accounts, compares each balance with the node's current rent-exempt minimum for its data length, and prints the margin plus the rate multiples at which it would fall short. Spec and main.ts included.

## Who uses it

Rent exemption is a deposit every account holds, sized by its data length. Teams that run programs or treasuries with many accounts keep a lot of that deposit in one place: a program that creates a PDA per user, a payout service that funds a token account per recipient, a game with one save account per player. If the per-byte rate in the cluster's rent settings ever rose, each of those accounts would need a larger minimum, and the ones funded to the exact minimum would sit short.

The audit answers one question for a list of addresses: how much margin does each account carry above today's minimum, and at which multiple of today's rate does it fall short. It reads the accounts and the minimum from the node and does the arithmetic locally. Nothing is signed or sent, so it needs only an API key and runs on a laptop or in a nightly job.

## The margin arithmetic

The minimum for an account is a function of its data length. The node adds 128 bytes of account overhead to the data, multiplies by the rent rate and returns lamports. `getMinimumBalanceForRentExemption` returns that figure for any length, so the code carries no rate constant that could go stale.

The margin is the account's lamports minus that minimum. Coverage is lamports divided by the minimum, as a percentage. Coverage of 100% means the account holds exactly the minimum. Coverage of 250% means the rate could rise to 2.5 times today's before the account is short, provided its data length stays the same. The minimum scales in a straight line with the rate, so one division gives the break-even multiple for every account, and the code never has to guess what a future rate would be.

| Coverage | Reading |
|---|---|
| Under 100% | Already below today's minimum |
| 100% to 149% | Short at 1.5 times today's rate |
| 150% to 199% | Holds at 1.5 times, short at 2 times |
| 200% to 299% | Holds at 2 times, short at 3 times |
| 300% and above | Holds at every multiple the audit checks |

> A higher minimum takes no lamports from an account, and a short account can still be written as long as its size stays the same and it gains no lamports. The runtime rejects a transaction that credits a short account without bringing it up to the minimum, or that reallocates it while it is short. The audit finds which accounts need a top-up to the full new minimum, so the next deposit or realloc on them does not fail.

Accounts that a program creates are often funded to exactly the minimum, because the creating instruction passes that figure to the system program. Wallets and treasuries usually hold far more than their minimum. The audit separates the two groups without you sorting them by hand.

## The build

The audit is one file that needs `@solana/kit` and what Node 24 ships. It is read-only: no signing, no wallet, nothing sent.

1. Read up to 100 addresses from the command line. With none given, check the USDC, wrapped SOL and USDT mints on mainnet and the clock sysvar, which is the small-account case.
2. Fetch all of them in one `getMultipleAccounts` call at base64 encoding and measure each account's data length from the returned bytes.
3. Ask `getMinimumBalanceForRentExemption` once per distinct length. The calls run together and each one is settled on its own, so a failed length prints its error and the other rows still print.
4. Print lamports, minimum, margin and coverage per account, then the multiples (1.5, 2 and 3 times) at which the account would be short.

`main.ts` (run: `CARBIUM_KEY=<YOUR_KEY> node main.ts EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v So11111111111111111111111111111111111111112`)

```ts
// BI0115 · Rent-exempt margin audit
// https://carbium.sh/library/blockchainideas/rent-exempt-margin-audit
// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.
// SPDX-License-Identifier: MIT

import { address, createDefaultRpcTransport, createSolanaRpcFromTransport } 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 MAX_PUBKEYS = 100; // one getMultipleAccounts call below Pro
const SCENARIOS = [150n, 200n, 300n]; // coverage percent needed to survive 1.5x, 2x, 3x the rate
const SAMPLE = [
  "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", // USDC mint
  "So11111111111111111111111111111111111111112", // wrapped SOL mint
  "Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB", // USDT mint
  "SysvarC1ock11111111111111111111111111111111", // clock sysvar
];

const client = createSolanaRpcFromTransport(
  createDefaultRpcTransport({
    url: process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh",
    headers: { "x-api-key": apiKey },
  }),
);

const args = process.argv.slice(2);
const names = (args.length ? args : SAMPLE).slice(0, MAX_PUBKEYS);
const keys = names.map((name) => {
  try {
    return address(name);
  } catch {
    console.error(`Not an address: ${name}`);
    process.exit(1);
  }
});

const sizeOf = (data: string) => Buffer.from(data, "base64").byteLength;

const { value: rows } = await client.getMultipleAccounts(keys, { encoding: "base64" }).send();
const sizes = rows.map((row) => (row ? sizeOf(row.data[0]) : -1));
const distinct = [...new Set(sizes.filter((s) => s >= 0))];

const minimums = new Map<number, bigint>();
const settled = await Promise.allSettled(
  distinct.map((size) => client.getMinimumBalanceForRentExemption(BigInt(size)).send()),
);
for (const [idx, res] of settled.entries()) {
  if (res.status === "fulfilled") minimums.set(distinct[idx], res.value);
  else console.error(`minimum for ${distinct[idx]} bytes failed: ${String(res.reason)}`);
}

console.log("address | data bytes | lamports | minimum | margin | coverage % | short at");
rows.forEach((row, idx) => {
  const who = names[idx];
  if (!row) return console.log(`${who} | missing`);
  const minimum = minimums.get(sizes[idx]);
  if (minimum === undefined) {
    return console.log(`${who} | ${sizes[idx]} | ${row.lamports} | minimum unavailable`);
  }
  const coverage = (row.lamports * 100n) / minimum;
  const short = SCENARIOS.filter((need) => coverage < need).map((need) => `${Number(need) / 100}x`);
  const margin = row.lamports - minimum;
  console.log(
    [who, sizes[idx], row.lamports, minimum, margin, coverage, short.join(",") || "none"].join(" | "),
  );
});
```

Lengths go in as `bigint` because kit types the argument that way, and every lamport figure stays a `bigint`, so the margin is exact and coverage is a whole percent rounded down. Both calls leave commitment out, so kit fills in confirmed.

[`getMultipleAccounts`](https://carbium.sh/docs/rpc) costs 10 cells per pubkey, so a 100-address call costs 1,000 cells. Pro accounts can send 300 pubkeys in one call. Below Pro the 1,024-cell burst caps a call at 100, which is why the code cuts the list at 100. For a larger inventory, run the audit in chunks of 100 and add the rows together.

## Reading the output

Each line holds the address, its data length, its lamports, the current minimum, the margin and the coverage, followed by the scenarios it fails. A line that says `none` means the account holds all three multiples. A missing account prints `missing`: it was closed, or it never existed on this cluster.

Sort the printed lines by coverage and work from the bottom. The accounts at 100% to 110% are the ones whose owners should hear about a rate change first. The ones at 400% and above need nothing. In practice a short list at the low end is what you hand to whoever runs the funding job.

Group by owner program when the list is large. A single program usually creates all its accounts the same way, so one program funded to the exact minimum tends to put the whole group at 100%, and that is one fix in one place. The same group at 1,000% points to a deposit that someone sized with generous headroom.

## What to add next

- Read the list from a file or a database export, so the audit covers an inventory without typing addresses.
- Sum the top-up each short account would need at a scenario: minimum times the multiple, minus lamports, added over the accounts. That is the treasury figure for the whole fleet.
- Run it nightly and keep each run, so a falling coverage on a growing account shows up before it crosses 150%.
- Add a threshold argument and exit with a non-zero code when any account falls under it, so a scheduler or CI job can alert.
- Pair it with the [rent-exempt minimum calculator](https://carbium.sh/library/howsolana/rent-exempt-minimum-calculator) to size new accounts with the same margin you want on the old ones.

## Where it ends

- The multiples are scenarios. No rate change is announced or scheduled by this audit; it only shows your exposure to one.
- Coverage assumes the data length stays fixed. An account that grows by realloc needs its minimum recomputed at the new length.
- Account data comes back in the response, so very large accounts such as program data make slow calls. Audit those by their known length instead.
- The audit sees only the addresses it is given and discovers no accounts on its own.
- Lamports above the minimum may be earmarked by the owning program, as in an escrow or a vault. The audit reads balance only and cannot tell spendable surplus from reserved funds.
- The figure holds at the moment of the call.

## When it goes wrong

| What goes wrong | What to do |
|---|---|
| Every call answers 401. | The key is unknown at the door. Attach the whole key in CARBIUM_KEY, with no prefix cut off and no trailing space. |
| The run stops with a message naming one argument as not an address. | Addresses are base58 strings of 32 to 44 characters. Check the argument for a stray quote, a line break from a pasted list or a signature in place of an address. |
| A row says missing. | The account is closed or the address belongs to another cluster. Drop it from the list, or confirm the owner closed it on purpose. |
| A row says minimum unavailable. | The call for that data length failed and its error printed to stderr. Run again for a transient error, or check the method and key if it repeats. |
| A list past 100 addresses prints only the first 100 rows. | main.ts keeps the first 100 because below Pro the burst caps one call at 100 pubkeys. Split the inventory into chunks of 100 and run each chunk. |
| Coverage looks far higher than expected for a program's accounts. | The account may hold escrowed funds as well as its deposit. Subtract the program's own tracked balance before reading the margin. |

## Questions

### Does a rent increase take lamports out of my accounts?

No. The balance stays as it is, and writes that leave a short account's size and lamports alone keep working. The runtime rejects a transaction that credits a short account without reaching the new minimum, or that reallocates it while it is short.

### How do I find the multiple that breaks a given account?

Divide its lamports by the current minimum for its data length. A result of 2.4 means the minimum can rise to 2.4 times today's before the account is short, at the same data length.

### Why not hard-code the per-byte rate?

The rate is a cluster setting, so a constant in your code goes stale the day it changes. Asking the node for the minimum per length keeps the audit right, and the extra calls are one per distinct length.

### Can I audit accounts I do not own?

Yes. Any account is readable by address. The audit reports its balance and size and changes nothing, so it works on a partner's treasury or a public program's accounts as easily as on your own.

## Related

- [Rent-exempt minimum calculator](https://carbium.sh/library/howsolana/rent-exempt-minimum-calculator) (How to use Solana, HS0020)
- [Account storage cost calculator](https://carbium.sh/library/blockchainideas/account-storage-cost-calculator) (Blockchain ideas, BI0031)
- [carbium.sh/docs/rpc](https://carbium.sh/docs/rpc)
- [carbium.sh/docs/endpoints](https://carbium.sh/docs/endpoints)
- [carbium.sh/docs/auth](https://carbium.sh/docs/auth)
- [carbium.sh/docs/errors](https://carbium.sh/docs/errors)
- [carbium.sh/docs/limits](https://carbium.sh/docs/limits)
