in-solana by Supanode

What Is Anchor in Solana?

Anchor is a framework for developing Solana programs in Rust. It provides macros for declarative account validation, automatic Borsh serialization, and an IDL (Interface Definition Language) that client libraries use to interact with the program.

What Anchor provides

  • Account validation macros — #[derive(Accounts)] structs with constraints like #[account(mut)], #[account(init)], #[account(has_one = authority)] validate accounts before the instruction handler runs
  • Instruction discriminators — Anchor generates an 8-byte discriminator (from SHA-256 of the instruction name) prepended to instruction data, preventing incorrect dispatching
  • IDL generation — Anchor produces a JSON IDL describing all instructions, accounts, and types, enabling automatic client generation
  • Error codes — Custom error enums with numeric codes (starting at 6000 for custom errors, 2000–2999 for constraint errors)
  • Testing framework — Built-in support for writing integration tests in TypeScript

Anchor program structure

An Anchor program is defined with the #[program] macro. Each public function in the module is an instruction handler. The first argument is always a Context<T> where T is the accounts struct.

Anchor vs native Solana programs

Native programs require manual account deserialization, validation, and error handling. Anchor automates these tasks at the cost of a small binary size increase and slightly higher compute unit usage. Most new Solana programs use Anchor for development speed and safety.

FAQ

Does Anchor work with Token-2022? Yes. Anchor’s token and associated_token constraints support both the original Token Program and Token-2022 via the token_program field.

What is an Anchor IDL? A JSON file describing the program’s interface: instructions (with parameters), account types, custom types, and error codes. Clients use the IDL to serialize instructions and deserialize account data without manual encoding.

What version of Anchor should I use? Anchor 0.30+ is recommended (as of 2026-10-20). Earlier versions have known issues with account validation and compatibility.

Updated: