Home › Solana / Anchor › Solana Anchor programs CLAUDE.md

Solana Anchor programs CLAUDE.md

CLAUDE.md for Solana programs built with Anchor: account validation, PDAs and bumps, checked math, CPI safety, and local-validator tests.

Claude Code (CLAUDE.md)Solana / Anchor

How to use

Save this as CLAUDE.md in your repository root, commit it, and your agent will pick it up automatically. Edit the commands to match your project.

Note: Review and test before use. Provided as is, without warranty. See Terms.

CLAUDE.mdDownload  
# CLAUDE.md: Solana program (Anchor)

## Commands
- `anchor build` · `anchor test` (spins up a local validator) · `cargo clippy --all-targets -- -D warnings` · `cargo fmt --check`
- Check `Anchor.toml` and `Cargo.toml` for the Anchor and Solana versions and use matching APIs; Anchor's macros changed between majors.
- Deploy only to localnet/devnet unless explicitly told otherwise. Never use or print a mainnet keypair.

## Code map
`programs/<name>/src/lib.rs` (instruction entrypoints), `instructions/` (one module per instruction with its `Accounts` struct), `state/` (account structs), `errors.rs`. Clients/tests in `tests/` (TypeScript) or `tests/*.rs`.

## Security rules
- Validate every account through Anchor constraints: `has_one`, `seeds` + `bump`, `constraint = ...`, `address = ...`, and `Signer<'info>` for authorities. Never trust an account just because it was passed in.
- Use typed `Account<'info, T>` / `Program<'info, T>` / `Interface` types; `UncheckedAccount` only with a `/// CHECK:` comment explaining why it is safe.
- PDAs: store the canonical bump in the account on init and reuse it (`bump = state.bump`); don't recompute with arbitrary bumps.
- All arithmetic uses `checked_add/sub/mul/div` and returns a custom error on overflow. No `as` casts that can truncate.
- `init` accounts get explicit `space` (`8 + T::INIT_SPACE` with `#[derive(InitSpace)]`); close accounts with `close = destination` to reclaim rent.
- CPIs: verify the target program ID (use `Program<'info, Token>` etc.), pass signer seeds only for PDAs you own, and re-load accounts after a CPI if you read them again.
- Token work via `anchor_spl` (token or token-2022 interface); check mint and owner of every token account.
- Emit events for state changes; return descriptive `#[error_code]` errors.

## Tests
Cover the happy path plus wrong signer, wrong PDA, wrong mint, and overflow cases for every instruction.

## Don't
Don't add `unsafe` code, disable overflow checks in release profile, or commit keypair JSON files.

More: all Solana / Anchor rules · all Claude Code (CLAUDE.md) rules

Related rules

ESP32 ESP-IDF firmware CLAUDE.md

CLAUDE.md for ESP32 firmware on ESP-IDF 5.x in C: components, FreeRTOS tasks, error handling, and logging.

Claude Code (CLAUDE.md)Embedded / firmware

ROS 2 robotics workspace CLAUDE.md

CLAUDE.md for ROS 2 workspaces: colcon builds, rclcpp/rclpy nodes, launch files, parameters, QoS, interfaces packages, and launch_testing.

Claude Code (CLAUDE.md)ROS 2 (robotics)

Solidity smart contract security CLAUDE.md

CLAUDE.md for reviewing and writing Solidity contracts safely: threat checklist, Foundry tests, and no-deploy rules.

Claude Code (CLAUDE.md)Solidity / smart contracts

Modern C++ with CMake rules

Cursor rules for modern C++ (C++20/23) projects built with CMake presets: RAII, ownership, warnings, and tests.

CursorC++

CUDA C++ kernels Cursor rules

Cursor rules for CUDA C++: error checking every API call, coalesced memory access, launch configuration, streams, and correctness tests before tuning.

CursorCUDA / GPU

Embedded C (bare metal / RTOS) AGENTS.md

AGENTS.md for embedded C firmware (bare metal, Zephyr, FreeRTOS): safety-minded C rules, memory, and hardware access.

AGENTS.mdEmbedded / firmware

Get the best new AI prompts every week

One short email a week: the best new prompts and rules for ChatGPT, Claude, Gemini, Midjourney, Cursor and more, plus what changed in the tools. Free, unsubscribe anytime.

Finishes on our newsletter page at beehiiv (opens in a new tab). Or subscribe directly.