Account Model Basics
10 examples to get you started with the Solana account model - 7 basic and 3 intermediate.
Search across all documentation pages
10 examples to get you started with the Solana account model - 7 basic and 3 intermediate.
solana config set --url devnet
npm install @solana/kit@7.0.0 @coral-xyz/anchor@0.32.1Anchor 0.32.1 / anchor-lang 0.32.1 for on-chain examples. Agave 4.1.1 validator semantics apply throughout.
Every account exposes the same five core fields over RPC.
import { createSolanaRpc, address } from "@solana/kit";
const rpc = createSolanaRpc("https://api.devnet.solana.com");
const { value } = await rpc.getAccountInfo(address("YOUR_PUBKEY"), { encoding: "base64" }).send();
console.log({
lamports: value?.lamports,
owner: value?.owner,
executable: value?.executable,
dataLength: value?.data.length,
rentEpoch: value?.rentEpoch,
});lamports is the SOL balance; owner is the program allowed to write dataexecutable: true means the account is a deployed programnull - no account exists yetRelated: Account Anatomy - field-by-field breakdown
Account data size drives the rent-exempt deposit.
solana rent 165Related: Rent & Rent-Exemption - calculation details
The owner field determines who can write data.
const SYSTEM_PROGRAM = "11111111111111111111111111111111";
const TOKEN_PROGRAM = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA";
// System-owned: only System Program can mutate (e.g., wallet accounts)
// Token-owned: SPL Token program manages token account datainitRelated: System Accounts vs. Program Accounts
Account creation allocates space and transfers ownership.
solana create-account --help
# Typically done via program init or CLI allocate commands// Anchor handles this via #[account(init, payer = ..., space = N)]space is the data buffer size in bytesRelated: Creating & Closing Accounts
Programs store application state in separate accounts.
#[account]
pub struct Counter {
pub count: u64,
}
#[derive(Accounts)]
pub struct Increment<'info> {
#[account(mut)]
pub counter: Account<'info, Counter>,
}Counter account holds data; the program binary holds logicAccount<'info, Counter> deserializes the byte buffer via BorshRelated: Program-Owned State
Only the owner program can write; your program must validate signers.
#[derive(Accounts)]
pub struct Update<'info> {
#[account(mut, has_one = authority)]
pub data: Account<'info, MyData>,
pub authority: Signer<'info>,
}has_one = authority checks a stored pubkey matches the signerRelated: Account Ownership & Permissions
Size the data buffer before init.
#[account]
pub struct Profile {
pub authority: Pubkey, // 32
pub name: String, // 4 + len
}
// space = 8 (discriminator) + 32 + (4 + 32) for max name lengthString and Vec need 4-byte length prefix plus max content sizeRelated: Data Accounts & Layout
PDAs are accounts with no private key, owned by your program.
#[derive(Accounts)]
pub struct InitVault<'info> {
#[account(
init,
seeds = [b"vault", user.key().as_ref()],
bump,
payer = user,
space = 8 + 8,
)]
pub vault: Account<'info, Vault>,
#[account(mut)]
pub user: Signer<'info>,
pub system_program: Program<'info, System>,
}bump is stored to avoid recomputing the canonical bumpseeds and bumpRelated: Program-Owned State
Closing returns lamports to a destination account.
#[derive(Accounts)]
pub struct CloseAccount<'info> {
#[account(mut, close = receiver)]
pub data: Account<'info, MyData>,
#[account(mut)]
pub receiver: SystemAccount<'info>,
}close constraint transfers all lamports to receiverRelated: Creating & Closing Accounts
Decode on-chain state from a client.
import { createSolanaRpc, address } from "@solana/kit";
const rpc = createSolanaRpc("https://api.devnet.solana.com");
const account = await rpc
.getAccountInfo(address("COUNTER_ACCOUNT_PUBKEY"), { encoding: "base64" })
.send();
if (account.value) {
const data = Buffer.from(account.value.data[0], "base64");
const discriminator = data.subarray(0, 8);
const count = data.readBigUInt64LE(8);
console.log("Count:", count);
}Related: Data Accounts & Layout
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.
Reviewed by Chris St. John·Last updated Jul 16, 2026