On-chain program development
On-chain program development turns Solana product rules into instructions that check accounts, change durable state, and work with other programs. This guidance uses Anchor for most programs and Pinocchio when a program needs tighter control of its size, memory, or compute use. See Solana development practices for the wider development model and Application and client development for the client boundary.
Framework selection
Framework choice sets the balance between delivery speed and direct control. Start with the program's safety and resource needs, then choose the smallest framework that meets them.
| Need | Direction | Reason |
|---|---|---|
| Fast delivery and clear account rules | Anchor | It provides typed account handling, common checks, custom errors, workspace tools, and an interface description for clients. |
| Small binary or few dependencies | Pinocchio | It keeps the program close to the Solana runtime and supports direct data and memory control. |
| Mature client and test workflow | Anchor | Program definitions connect to interface and client tooling. |
| Byte-level control or a constrained runtime | Pinocchio | The author controls parsing, validation, layout, and allocation. |
| A performance-sensitive path in an Anchor product | Measure first, then target the bottleneck | A focused Pinocchio component can improve one path without moving the whole product. |
Anchor is the normal choice for a new program. The current guidance uses Anchor 1.2.x with a pinned toolchain. Pinocchio 0.11+ is a deliberate choice for compute, binary-size, dependency, or allocation constraints. Its lower level of automation also makes the author responsible for checks that Anchor normally supplies. skills/…/SKILL.md defines these default stack choices.
Program design
Program design makes valid state, account relationships, and instruction outcomes clear before implementation begins. These choices also define the contract that clients, indexers, and later program versions must preserve.
State and account boundaries
State accounts hold the durable facts that later instructions need. Keep state and authority rules explicit so that an instruction cannot depend on scattered or implied checks.
- Model phases as named states with explicit transitions.
- Put fixed-size fields before variable-size fields when readers or filters need stable offsets.
- Reserve space for likely future fields because account layout is a long-lived contract.
- Store canonical PDA bumps when repeated address checks are on a hot path.
- Separate the payer from the authority when a service or other signer may fund an action.
- Check the state invariant after each transition. Reject the change if the resulting state is not valid.
An operating state such as normal, halted, or withdrawals-only gives the product a controlled response to incidents. A close action must also clear the account's usable state and complete the supported ownership and balance lifecycle; moving only lamports is not a complete close.
References: skills/…/design-patterns.md• L30-59 skills/…/design-patterns.md• L88-93 skills/…/design-patterns.md• L120-124 skills/…/concepts.md• L18-24
PDA and instruction contracts
Program-derived addresses (PDAs) are program-controlled addresses made from stable seeds and the program ID. Their seeds, account layout, instruction data, and serialized values form a public contract for clients and future program versions.
Use stable seed prefixes with canonical identifiers such as public keys or fixed-width numbers. Keep variable-length strings at the end of the seed set. Before a state change or cross-program invocation (CPI), validate the expected owner, signer, writable status, program ID, and PDA address.
Give each instruction a clear subject, action, and target. Use domain types for values that share a machine type but have different meaning, such as lamports and token amounts. Each instruction should end with valid state and the expected ownership and balance relationships.
Clients use an interface description such as an Anchor IDL to encode instructions and decode accounts. Indexers also depend on stable account and event data, so a program contract extends beyond the deployed program itself. See Application and client development for this boundary.
References: skills/…/design-patterns.md• L18-35 skills/…/design-patterns.md• L88-103 skills/…/anchor.md• L42-57 skills/…/pinocchio.md• L203-223
Shared-account and transaction limits
Solana can run transactions in parallel when they only read the same account. It serializes transactions that write it. A design that sends every operation through one writable vault or counter can therefore limit throughput.
Use deterministic shards such as per-user or per-pool state when the workload needs parallel writes. Reconcile those shards at a higher level when the product needs an aggregate view. Design each composed workflow around the transaction's finite resources.
| Resource | Design effect |
|---|---|
| Account locks | Shared writable accounts reduce parallel work. |
| Compute units | Validation, PDA work, CPIs, and batching share one transaction budget. |
| Account growth | Large state changes may need several steps. |
| CPI depth and instruction trace | Deep composition may need fewer layers or a different split. |
| Transaction size and version | Account-heavy flows must fit the selected transaction format and client read support. |
| Transaction v1 resource limits | The message carries compute and loaded-account limits. A client must estimate or set them before sending. |
The compute budget applies to the whole transaction. A batch can fail even when each instruction works alone. Measure the real product path before changing frameworks because a smaller binary does not by itself prove a faster or safer product.
References: skills/…/design-patterns.md• L94-118 skills/…/design-patterns.md• L176-179 skills/…/concepts.md• L48-60 skills/…/transactions-v1.md• L117-133 skills/…/transactions-v1.md• L305-321
Token program boundaries
Token operations must identify which token program owns the mint. SPL Token and Token-2022 are separate programs, so a mint, its token accounts, its instructions, and its associated token account derivation must use the same program.
Token-2022 extensions make account size and behavior part of the program contract. Size accounts from the selected extension set instead of using fixed SPL Token sizes. Initialize most mint extensions before InitializeMint; initialize metadata and group data after it. Use checked token instructions by default and account for extension behavior such as transfer fees, hooks, pauses, permanent delegates, or confidential balances.
For program code that supports both token programs, validate the selected program and use the matching interface path. See Secure token and payment features for value-moving safety guidance.
References: skills/…/tokens.md• L10-21 skills/…/tokens.md• L65-102 skills/…/tokens.md• L147-180 skills/…/token-2022.md• L18-75
Operational controls
Programs can separate privileges across program boundaries and can let permissionless callers advance work such as settlement or liquidation. These patterns need an explicit authority model so that a caller can make progress without gaining control of protected state.
Use a shared authority such as a multisig for sensitive actions when one signer would create a single point of failure. For permissionless cranks, define what a hostile caller can change and whether the reward supports timely progress. Review CPI depth and account privileges when the design splits work across programs.
References: skills/…/design-patterns.md• L132-143
Anchor authoring
Anchor gives a program a structured boundary for entrypoints, instruction accounts, state, errors, and client interfaces. Its main benefit is that common account checks become part of the instruction contract instead of separate hand-written logic.
Account validation and instruction flow
An Anchor instruction receives a typed context that describes its accounts. Anchor checks common properties such as signatures, ownership, PDA seeds, relationships, initialization, and closure before the business action runs.
Prefer typed accounts and explicit constraints. Use separate validation logic and distinct errors for complex checks. Use raw or unchecked accounts only when the instruction performs all required validation. Represent known CPI targets with validated program types instead of accepting arbitrary executable accounts.
When a client submits an Anchor instruction, the program follows this path:
- The client encodes the instruction and supplies its accounts.
- Anchor validates the context and account relationships.
- The handler applies the business transition.
- The program returns an error or commits the new account state and events.
This diagram shows how the typed instruction boundary separates account checks from the business transition.
flowchart LR
C[Client instruction] --> A[Anchor context]
A --> V[Account and constraint validation]
V -->|valid| B[Business transition]
V -->|invalid| E[Program error]
B --> S[Updated accounts and events]
References: skills/…/anchor.md• L30-40 skills/…/anchor.md• L47-67 skills/…/anchor.md• L69-117 skills/…/anchor.md• L294-310 skills/…/design-patterns.md• L10-16
CPI and interface artifacts
Cross-program invocation (CPI) lets one program ask another program to perform an operation. Anchor provides typed CPI calls and lets a program sign for a PDA that it controls.
Validate the target program and pass only the privileges that the callee needs. Refresh any cached account view after a CPI changes its data. Treat the IDL as a product interface because clients and tools depend on its instruction, account, error, and event shape. Anchor 1.0+ publishes and manages this interface through Program Metadata rather than relying on the old on-chain IDL account.
Anchor can support both SPL Token and Token-2022 through interface account types. Use this form when the feature must work with either program. External token account types are not automatically included in the generated IDL, so clients must use the token programs' own interfaces.
References: skills/…/anchor.md• L160-178 skills/…/anchor.md• L214-242 skills/…/anchor.md• L321-326 skills/…/design-patterns.md• L105-118 skills/…/migrating-v0.32-to-v1.md• L199-253 skills/…/migrating-v0.32-to-v1.md• L323-357 skills/…/migrating-v0.32-to-v1.md• L441-452
Pinocchio authoring
Pinocchio is a small Rust framework for programs that need direct control over parsing, account views, data layout, and allocation. It removes much of Anchor's automatic structure, so its resource benefits come with greater responsibility for validation and compatibility.
Entrypoint and validation model
Pinocchio routes raw instruction data from one entrypoint to instruction-specific account and data types. Each instruction should parse its inputs and validate them before it changes state or performs a CPI.
Keep validation at the conversion boundary. Account conversion should check account count, signer and writable status, owner, expected program IDs, and PDA relationships. Data conversion should check length, discriminator, version, and value rules. The processor can then operate on trusted shapes without hiding which checks exist.
Pinocchio does not provide implicit validation for passed sysvar accounts. Read required runtime values through the supported sysvar path or validate the address before use. Use checked arithmetic and reject malformed or short input before parsing it.
References: skills/…/pinocchio.md• L69-107 skills/…/pinocchio.md• L199-300 skills/…/pinocchio.md• L350-413 skills/…/pinocchio.md• L825-848
Data layout and resource control
Pinocchio makes account bytes and memory use visible design choices. A versioned account prefix can use a discriminator and version byte before a fixed layout that the program reads without a general serializer.
Reserve bytes for future state and assert the intended layout. Avoid unaligned casts or packed fields that can read invalid memory. Keep hot paths free of needless allocation. Batch work only when the larger instruction remains easy to validate and fits transaction limits.
Events need the same care. String logs help with debugging but can be truncated. If indexers must read an event reliably, carry its data through a non-truncated instruction-data path and validate the authority for that path.
References: skills/…/pinocchio.md• L203-245 skills/…/pinocchio.md• L477-547 skills/…/pinocchio.md• L624-639 skills/…/pinocchio.md• L698-782
Framework migration
Migration changes more than dependency versions. It can change account bytes, instruction encoding, CPI call shapes, generated interfaces, and the tools that build or test the program.
Anchor version changes
Keep Anchor crates on compatible versions and align the Anchor and Solana toolchains in local work and CI. New projects should use the current 1.2.x line with a pinned toolchain. Its default Solana crate line is v3; Solana v4 crates require an explicit feature on every Anchor dependency. Audit external CPI crates and direct SPL dependencies because older types can make valid calls incompatible.
The Anchor 0.32 to 1.x path also changes CPI context construction, duplicate mutable-account handling, declare_program! parser names, interface-instruction declarations, serialization APIs, selected Solana SDK APIs, and TypeScript package names. Move external CPI and SPL dependencies to compatible release lines or interface crates before relying on the new SDK types.
Treat the change as a staged interface and deployment process:
- Pin the Anchor CLI, Rust, Solana CLI, platform tools, and dependency versions. Audit external CPI and SPL dependencies before fixing local compile errors.
- While the old program is live, publish the new IDL through Program Metadata and update clients that read it.
- Use the v0.32 CLI to close legacy IDL accounts on every cluster before deploying the v1 binary. v1 tooling cannot manage those legacy accounts.
- Update program and client boundaries together. Review account layouts, duplicate-account behavior, CPI targets, serialization, and generated interfaces.
- Build and run local simulations and client flows before deployment. Anchor 1.0+ uses Surfpool by default for
anchor test; LiteSVM and Mollusk provide faster unit-level checks.
See Testing and local simulation for the environments that exercise these boundaries.
References: skills/…/anchor.md• L273-291 skills/…/anchor.md• L321-355 skills/…/migrating-v0.32-to-v1.md• L37-57 skills/…/migrating-v0.32-to-v1.md• L100-151 skills/…/migrating-v0.32-to-v1.md• L199-321 skills/…/migrating-v0.32-to-v1.md• L325-359 skills/…/migrating-v0.32-to-v1.md• L731-863 skills/…/testing.md• L20-30 skills/…/compatibility-matrix.md• L101-145
Anchor and Pinocchio boundaries
Moving a program between frameworks is a wire-format migration when either side changes account prefixes, discriminators, instruction tags, serialization, or events. Anchor uses an eight-byte account discriminator by default. The Pinocchio pattern described here uses a discriminator byte plus a version byte, so the layouts cannot be mixed by assumption.
Choose the migration shape that matches the data contract:
- Keep the existing program and change only its internal implementation when public bytes and instruction behavior remain stable.
- Add a compatibility path that reads old state and writes a new version when the data contract must evolve.
- Deploy a new program or module when the old contract cannot safely support both forms.
Clients and indexers must know the selected contract before traffic moves. A framework change is complete only when the deployed program, interface metadata, clients, tests, and monitoring agree on the same bytes and meanings.
References: skills/…/pinocchio.md• L203-223 skills/…/pinocchio.md• L302-348 skills/…/anchor.md• L119-132 skills/…/anchor.md• L328-338
Change checklist
When these files or sections are changed, remember to consider these:
- Account layout, discriminator, version, or PDA seed changes: review every reader, writer, filter, client codec, and indexer that uses the old bytes.
- Anchor or Pinocchio dependency changes: align the Rust toolchain, Solana CLI, build mode, and generated interface tooling.
- CPI target or account privilege changes: update caller validation and the top-level transaction account list.
- Token program or Token-2022 extension changes: review mint sizing, ATA derivation, initialization order, checked instructions, and extension authorities.
- Event or error changes: update IDL consumers and monitoring that treats event or error values as stable identifiers.
- A new writable shared account: review transaction parallelism and decide whether the state needs sharding.
- A framework migration: audit external CPI and direct SPL dependencies. Close legacy Anchor IDL accounts with the old CLI before deploying v1, then verify old-state compatibility.
- A new permissionless crank or authority boundary: review who can trigger the transition and which privileges cross each CPI boundary.
- A transaction v1 integration: verify that the client sets message resource limits and that readers opt in to version 1 rather than treating it as v0.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| An unchecked or wrongly owned account reaches business logic | An attacker can redirect state changes or funds | Use typed Anchor accounts or complete Pinocchio conversion checks. |
| A CPI accepts an arbitrary executable account | The program may call an unintended program with its privileges | Validate the expected program ID and pass only required privileges. |
| A PDA uses unstable seeds or an unverified bump | The program reads or signs for a different account than the client expects | Define canonical seeds and validate the stored or derived bump. |
| A token flow derives an ATA with the wrong token program | The instruction targets an address with no matching token account | Read the mint owner and use the same token program for every operation and derivation. |
| A Token-2022 account uses a fixed size or wrong initialization order | Account creation or mint initialization fails | Calculate size from extensions and follow the required initialization order. |
| A Pinocchio parser reads short or unaligned data | The instruction fails unpredictably or corrupts state | Check lengths and layout before parsing. Avoid unsafe unaligned casts. |
| A closed account keeps usable data or ownership | The account can be revived or treated as valid later | Use the complete close path that clears state and returns ownership. |
| An Anchor-to-Pinocchio move assumes matching discriminators | Existing accounts or clients become unreadable | Treat account prefixes and instruction bytes as explicit migration contracts. |
| A hot writable account is shared by every operation | Transactions serialize and throughput falls | Shard state by a deterministic product key when the workload needs parallel writes. |
| A state transition skips its invariant check | Invalid state becomes durable and affects later instructions | Model transitions explicitly and validate the resulting state before commit. |
| Anchor CLI, framework crates, and Solana crates use different release lines | Builds fail through type conflicts or interface warnings | Pin compatible versions and commit the lockfile. Use the compatibility matrix before upgrading. |
| Legacy IDL accounts remain when the v1 binary is deployed | The old IDL rent cannot be recovered through the v1 program | Close them with the v0.32 CLI before deployment, then publish through Program Metadata. |
| An external CPI or direct SPL crate uses an older type line | The workspace gets trait or type conflicts at compile time | Upgrade the crate or use a compatible interface or vendored CPI crate before migration. |
Key files
| File | Why to read it |
|---|---|
| skills/…/SKILL.md | Defines the default Anchor and Pinocchio choices and the current Solana program workflow. |
| skills/…/anchor.md | Primary guidance for Anchor account rules, CPIs, IDLs, token interfaces, testing, and version migration. |
| skills/…/pinocchio.md | Guidance for low-overhead programs, manual validation, account layout, resource control, and events. |
| skills/…/design-patterns.md | Cross-framework patterns for state machines, PDA seeds, parallel writes, account lifecycle, events, cranks, and transaction limits. |
| skills/…/tokens.md | Token program ownership, account sizing, extension behavior, authorities, and initialization rules. |
| skills/…/token-2022.md | Token-2022 client boundary for sizing, ATA derivation, initialization order, and extension reads. |
| skills/…/migrating-v0.32-to-v1.md | Ordered migration guidance for toolchains, dependencies, CPI context, legacy IDL closure, and client changes. |
| skills/…/compatibility-matrix.md | Version pairings for Anchor, Solana tooling, Rust, Node.js, testing tools, and transaction v1 support. |
| skills/…/testing.md | Testing strategy for LiteSVM, Mollusk, Surfpool, forked state, and CI. |
| docs/application-client-development.md | Related page for clients and IDL consumers at the program boundary. |
| docs/testing-and-local-simulation.md | Related page for local execution and tests that validate program and client contracts. |
| docs/secure-token-and-payment-features.md | Related page for token, payment, CPI, and value-moving safety concerns. |