Rent & Rent-Exemption
Solana accounts must hold enough lamports to be rent-exempt - a one-time deposit based on data size. Without it, accounts cannot persist on-chain.
Recipe
solana rent 200const rent = await rpc.getMinimumBalanceForRentExemption(200n).send();When to reach for this:
- Sizing
initpayer transfers in Anchor programs - Estimating user onboarding cost (how many accounts to create)
- Closing accounts to reclaim rent
- Budgeting protocol treasury for account creation subsidies
Working Example
use anchor_lang::prelude::*;
#[derive(Accounts)]
pub struct InitConfig<'info> {
#[account(
init,
payer = payer,
space = 8 + Config::INIT_SPACE,
)]
pub config: Account<'info, Config>,
#[account(mut)]
pub payer: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[account]
#[derive(InitSpace)]
pub struct Config {
pub admin: Pubkey,
pub fee_bps: u16,
}import { createSolanaRpc } from "@solana/kit";
const rpc = createSolanaRpc("https://api.devnet.solana.com");
const space = 8 + 32 + 2; // discriminator + fields
const minBalance = await rpc.getMinimumBalanceForRentExemption(BigInt(space)).send();
console.log("Rent-exempt minimum:", minBalance, "lamports");What this demonstrates:
inittransfers at least the rent-exempt minimum from payer to new account- Space calculation must include Anchor's 8-byte discriminator
- RPC returns the exact lamport amount for any byte size
Deep Dive
How It Works
- Rent is a function of data size and a network-wide lamports-per-byte-year rate
- Rent-exempt means holding ~2 years of rent upfront - account is never charged ongoing rent
- The lamports sit in the account's balance - reclaimable on close
- System Program enforces minimum balance on allocation
Typical Sizes
| Account Type | Bytes | Approx Rent (lamports) |
|---|---|---|
| Empty wallet | 0 | ~890,880 |
| SPL Token Account | 165 | ~2,039,280 |
| Anchor small struct | 8 + 40 | ~1,500,000+ |
Exact values change with cluster parameters - always query RPC.
Gotchas
- Undersized
spacein Anchor - init fails or truncates data. Fix: useInitSpacederive or manual calculation with max string/vec lengths. - Forgetting discriminator in space - off by 8 bytes. Fix: always add 8 for Anchor accounts.
- Assuming rent is a recurring charge - rent-exempt accounts pay once. Fix: fund to minimum at creation; no ongoing billing.
- Not reclaiming rent on close - user funds locked forever. Fix: implement
closeconstraint. - Hardcoding rent values - cluster params can change. Fix: query
getMinimumBalanceForRentExemptionin clients.
Alternatives
| Alternative | Use When | Don't Use When |
|---|---|---|
solana rent CLI | Quick terminal estimates | On-chain program logic |
| RPC rent query | Dynamic client UI cost display | Inside BPF (use sysvar or fixed buffer) |
| State compression | Millions of small records | Hot mutable state |
FAQs
What is rent-exemption?
Funding an account with enough lamports to cover ~2 years of storage rent upfront. The account then persists without ongoing rent charges.
How do I calculate rent for an Anchor account?
space = 8 + sizeof serialized fields (with max lengths for String/Vec). Query RPC for exact lamports.
Can I get rent back?
Yes. Close the account and lamports return to a destination you specify.
What happens if balance falls below rent-exempt?
Legacy behavior could garbage-collect; modern practice requires rent-exempt at creation. Underfunded inits fail.
Does larger data always mean more rent?
Yes. Rent scales linearly with data size in bytes.
Who pays rent at account creation?
The payer signer in init - typically the user or protocol treasury.
Is program bytecode subject to rent?
Yes. Deployed programs hold lamports for their program data account size.
How does realloc affect rent?
Growing data may require additional lamports transferred to maintain rent-exemption.
What is the rent sysvar?
Rent sysvar holds calculation parameters. Rarely read directly in modern programs.
Should I subsidize user account creation?
Common UX pattern for ATAs and PDAs. Budget treasury accordingly.
Does devnet rent match mainnet?
Same formula; amounts are identical for the same data size.
Can rent calculation differ by account type?
No. Only data size and cluster rent parameters matter.
Related
- Creating & Closing Accounts - reclaim rent
- Account Anatomy - lamports field
- Data Accounts & Layout - sizing layouts
Stack versions: This page was written for Agave 4.1.1, Solana CLI 3.0.10, Anchor 0.32.1, anchor-lang 0.32.1, Rust 1.91.1, @solana/kit 7.0.0, Surfpool 0.12.0, and LiteSVM 0.6.x.