Skill v1.0.0
currentLLM-judged scan90/100version: "1.0.0" name: solana-anchor description: Solana program development with the Anchor framework (Rust). Use when building Solana programs, working with Anchor.toml, accounts, PDAs, CPIs, or SPL tokens, or on requests mentioning Solana, Anchor, SPL, or program. Covers the account model, PDAs, security pitfalls, and Anchor tests. Cross-links rust-pro. allowed-tools: Read, Write, Edit, Glob, Grep, Bash
You build Solana programs with Anchor. Solana's model is fundamentally different from EVM: programs are stateless and operate on accounts passed into each instruction — most Solana bugs are missing account validation.
Use this skill when
- Building/editing Solana programs (Rust, Anchor) or
Anchor.tomlprojects - Working with PDAs, CPIs, SPL tokens
- Reviewing a Solana program for the common account-validation pitfalls
Load rust-pro alongside this for idiomatic Rust.
Mental model
- Programs are stateless. State lives in accounts the caller passes in. The program must validate every account it's given.
- PDAs (Program Derived Addresses): deterministic addresses owned by the program, derived from seeds + bump. Used for program-controlled state and signing (
invoke_signed). - CPI (Cross-Program Invocation): calling another program (e.g., the SPL Token program) from yours.
- Rent: accounts must be rent-exempt (hold enough lamports) or they're purged.
Anchor program skeleton
use anchor_lang::prelude::*;declare_id!("Fg6PaFpoGXkYsidMpWxqSWY...");#[program]pub mod vault {use super::*;pub fn deposit(ctx: Context<Deposit>, amount: u64) -> Result<()> {require!(amount > 0, VaultError::ZeroAmount);let vault = &mut ctx.accounts.vault;vault.total = vault.total.checked_add(amount).ok_or(VaultError::Overflow)?;Ok(())}}#[derive(Accounts)]pub struct Deposit<'info> {#[account(mut, seeds = [b"vault", authority.key().as_ref()], bump)]pub vault: Account<'info, Vault>,#[account(mut)]pub authority: Signer<'info>,pub system_program: Program<'info, System>,}#[account]pub struct Vault { pub authority: Pubkey, pub total: u64 }#[error_code]pub enum VaultError { #[msg("amount is zero")] ZeroAmount, #[msg("overflow")] Overflow }
Security pitfalls (Solana-specific — check every one)
| Pitfall | Fix | |
|---|---|---|
| Missing signer check | Require Signer<'info> on accounts that must authorize | |
| Missing owner check | Use typed Account<'info, T> (Anchor checks program ownership) — don't use raw AccountInfo without manual checks | |
| Account substitution / confused deputy | Constrain accounts with seeds/has_one/constraint; verify relationships | |
| PDA bump not canonical | Use Anchor's bump (canonical) — don't accept a user-supplied bump | |
| Arithmetic overflow | checked_add/checked_sub (Rust release builds wrap silently otherwise) | |
| Unvalidated CPI target | Verify the program id you CPI into (e.g., the real SPL Token program) | |
| Account reinit / type confusion | Use init/init_if_needed carefully; validate discriminator |
SPL tokens
Use anchor_spl::token (or token-2022) for mint/transfer via CPI; never reimplement token logic. Validate the mint and token account owner.
Testing
anchor buildanchor test # spins a local validator, runs TS tests
Write tests for: each instruction's success path, every require!/error, and unauthorized-signer / wrong-account attempts.
Anti-patterns
- ❌ Raw
AccountInfowithout owner/signer validation - ❌ Trusting any account address passed in without constraints
- ❌ Plain
+/-on balances (use checked math) - ❌ Accepting a user-supplied PDA bump
- ❌ CPI into an unvalidated program id