Solana Programs
Build on-chain Solana programs with Anchor - account constraints, instructions, and cross-program invocation in Rust.
Busca en todas las páginas de la documentación
Build on-chain Solana programs with Anchor - account constraints, instructions, and cross-program invocation in Rust.
use anchor_lang::prelude::*;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[program]
pub mod hello_anchor {
use super::*;
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
ctx.accounts.counter.count = 0;
Ok(())
}
}
#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(init, payer = user, space = 8 + 8)]
pub counter: Account<'info, Counter>,
#[account(mut)]
pub user: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[account]
pub struct Counter {
pub count: u64,
}When to reach for this:
Increment instruction with signer and account constraints:
use anchor_lang::prelude::*;
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
#[program]
pub mod counter {
use super::*;
pub fn increment(ctx: Context<Increment>) -> Result<()> {
ctx.accounts.counter.count = ctx.accounts.counter.count
.checked_add(1)
.ok_or(ProgramError::InvalidArgument)?;
Ok(())
}
}
#[derive(Accounts)]
pub struct Increment<'info> {
#[account(mut, has_one = authority)]
pub counter: Account<'info, Counter>,
pub authority: Signer<'info>,
}
#[account]
pub struct Counter {
pub authority: Pubkey,
pub count: u64,
}What this demonstrates:
has_one ties signer to stored authority pubkey#[account(...)] macros validate owners, seeds, and rent.transfer with invoked signer seeds.| Type | Use |
|---|---|
Account<T> | Anchor-owned deserialized data |
Signer | Must sign transaction |
UncheckedAccount | Manual validation only |
space = 8 + sizeof(T) plus discriminator.Signer and mut correctly in Accounts struct.| Alternative | Use When | Don't Use When |
|---|---|---|
| Native Solana Rust (no Anchor) | Minimal binary size | Team wants IDL and safety macros |
| Ethereum Solidity | EVM liquidity and tooling | Need Solana throughput |
| Substrate pallet | Custom L1 chain | App on existing Solana cluster |
| Off-chain Rust + oracle | Heavy compute | State must be trustlessly on-chain |
solana-test-validator plus anchor test runs migrations and integration tests against local cluster.
Anchor account discriminator - do not forget it in space calculation.
Deploy with upgrade authority, then transfer authority to multisig or burn for immutability when audited.
CPI to token program with PDA signer seeds for vault authority pattern.
anchor build emits IDL; use @coral-xyz/anchor or anchor-client in Rust integration tests.
Accounts need minimum lamports for size; fund on init or reclaim on close.
Define #[error_code] enum for user-visible errors instead of generic failures.
Devnet airdrops are unreliable; use local validator for CI, devnet for manual QA.
Walk Security & Auditing before mainnet deploy.
Deployer keys off hot laptops - see Wallets & Key Management.
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.
Revisado por Chris St. John·Última actualización: 19 jul 2026