Raj

@rajgoesout

Pinocchio Cookbook

Quick reference for Anchor developers switching to Pinocchio.

Anchor → Pinocchio

Anchor Pinocchio
#[program] entrypoint!(process_instruction)
#[derive(Accounts)] Manual validation / TryFrom
#[account] #[repr(C)] + zero-copy
Context<T> (&Address, &[AccountView], &[u8])
Pubkey Address
AccountInfo AccountView

Cargo.toml

[package]
name = "my-program"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib", "lib"]

[features]
bpf-entrypoint = []
cpi = ["pinocchio/cpi"]

[dependencies]
pinocchio = "0.10"
pinocchio-system = "0.3"
pinocchio-token = "0.4"

Minimal Program

#![no_std]

use pinocchio::{AccountView, Address, ProgramResult, entrypoint, error::ProgramError};

pub const ID: Address = pinocchio::address!("YourProgramId11111111111111111111111111111");

entrypoint!(process_instruction);

pub fn process_instruction(
    program_id: &Address,
    accounts: &[AccountView],
    instruction_data: &[u8],
) -> ProgramResult {
    let [account, ..] = accounts else {
        return Err(ProgramError::NotEnoughAccountKeys);
    };

    // Your logic here
    Ok(())
}

Build

cargo build-sbf

Docs

Topic Description
Accounts AccountView API
Programs Entrypoints & structure
Instructions Parsing instruction data
State Zero-copy state structs
PDAs Program derived addresses
CPI Cross-program invocation
Tokens SPL Token operations
Errors Custom errors
Testing LiteSVM & Mollusk

Examples

  • Counter - Simple state with PDA
  • Vault - Token custody with PDA signing