Introducing a new way to review your PRs.

Read the announcement

solana-foundation / program-examples

A repository of Solana program examples

View on GitHub

19 documents · Updated Nov 23, 2026

docs / instruction-and-cpi-patterns

Instruction processing and cross-program invocation

Solana programs receive instruction data and an ordered list of accounts. They decode the request, check that the accounts and authorities match the intended action, then either update their own state or call another program. The examples cover native Rust, Anchor, and Pinocchio patterns for dispatch, account checks, cross-program invocation (CPI), and SOL transfers. For the wider account model, see Solana program fundamentals, Account creation, storage, resizing, and closure, and Program-derived address design.

Instruction dispatch

Program entry points

The program entry point is the boundary between the Solana runtime and application logic. It receives the program address, the supplied accounts, and raw instruction bytes, then sends the request to the matching operation. Native Rust and Pinocchio expose this boundary directly. Anchor maps an instruction name to a program method.

The dispatch layer should reject data that does not describe a supported operation. The processing-instructions example also shows that instruction fields can be decoded before the program applies its business rule. Native Rust uses Borsh for this data, while Pinocchio uses a compact byte layout in its example. Anchor performs the data mapping from the method call.

References: basics/…/lib.rs• L1-23 basics/…/lib.rs• L5-24 basics/…/lib.rs• L1-20

Instruction data

Instruction data carries values that change from one request to the next. The program must use the same encoding and field order as the client. A mismatch can turn valid bytes into invalid data or produce the wrong values before account checks or state changes begin.

The examples use two styles:

  • A serialized structure or enum names the operation and carries its values.
  • A leading byte selects the operation, followed by fixed-width or variable-length fields.

Client builders also define the account roles that travel with the encoded data. They mark accounts as writable, read-only, or signer accounts so the transaction presents the contract that the program expects.

References: basics/…/lib.rs• L6-28 basics/…/processor.rs• L7-18 basics/…/lib.rs• L14-40 basics/…/create.ts• L14-40 basics/…/transfer.ts• L12-33

Account validation

Account roles and ownership

Accounts are inputs to an instruction, not trusted arguments. Their order, signer state, address, initialization state, and owner determine whether the program may use them for the requested action. The checking-accounts examples make these checks explicit before a program creates an account or changes account data.

Ownership is the boundary for data and lamport changes. A program can change data in an account that it owns. A signer proves that a transaction authority approved the instruction, but signer status alone does not make an account writable by the program. Fixed program addresses, such as the System Program address, also need checks when the instruction depends on a specific program.

References: basics/…/lib.rs• L12-62 basics/…/lib.rs• L9-53 basics/…/lib.rs• L14-30

Framework account contracts

The three program styles express the same account contract in different ways. Native Rust reads accounts in the required order and returns an error when a check fails. Pinocchio matches the complete account slice and checks account properties directly. Anchor places the contract in an accounts structure, where types and constraints require signer, owner, mutability, or program identity before the handler runs.

These checks protect the relationship between an instruction and the state it can affect. For example, the Anchor checking example marks the payer as a signer, the account to change as owned by the program, and the System Program as the required executable program.

References: basics/…/lib.rs• L19-60 basics/…/lib.rs• L13-50 basics/…/lib.rs• L16-30

Signer handling

Transaction authorities

A signer is an account whose key approved the transaction. Programs use signer status to require consent from a payer or other authority before an action. The transfer examples require the payer to sign for a direct program transfer, and the Anchor account contract represents that requirement with Signer.

The runtime supplies signer status with the account input. A program must still check it when the operation depends on user approval. Missing accounts, missing signatures, and wrong account roles are rejected before the state change is accepted.

References: basics/…/instruction.rs• L23-36 basics/…/lib.rs• L33-49 basics/…/lib.rs• L33-50

Program authorities

A program can also act through a program-derived address (PDA). The PDA has no private key, so the owning program must authorize its use through the runtime's signed invocation rules. The examples use ordinary transaction signers for payer actions and use the related PDA patterns for program-controlled authorities; see Program-derived address design for seed and authority design.

Signer handling therefore has two distinct questions: did a transaction signer approve the request, and is the program allowed to act for a program-controlled address? CPI code must pass the accounts and signer context required by the called program rather than treating an account address as proof of authority.

References: basics/…/lib.rs• L16-30 basics/…/lib.rs• L17-30

Cross-program invocation

Dependent program calls

A cross-program invocation calls another Solana program during the current instruction. It is useful when one operation depends on another operation completing in the same transaction path. The hand-and-lever example makes this relationship visible: the hand program forwards a request to the lever program, and the lever program changes the shared power state.

The caller builds the callee's instruction with the callee's program address, instruction data, and the accounts that the callee needs. The callee remains responsible for its own dispatch and account rules. This keeps the dependent work on chain instead of requiring a client to split the sequence between separate calls.

This diagram shows how the outer request becomes a nested program call and then a state update.

flowchart LR
    Client[Client instruction] --> Hand[Hand program]
    Hand -->|CPI: switch power| Lever[Lever program]
    Lever --> Power[Power state account]

References: basics/…/lib.rs• L14-32 basics/…/lib.rs• L16-30 basics/…/lib.rs• L12-23 basics/…/test_cpi.rs• L25-63

CPI interface forms

The CPI boundary is the same across the examples, but each framework gives it a different shape. Native Rust constructs an instruction and invokes it with account inputs. Anchor builds a typed CpiContext from the target program and its account set. Pinocchio builds a compact instruction view and forwards the selected accounts.

The target program must be available as an executable account or as the framework's typed program account. In the native workspace, the imported program disables its own entry point so the caller remains the only program entry point in the built program. This is a build boundary rather than a change to the on-chain CPI contract.

References: basics/…/lib.rs• L1-31 basics/…/lib.rs• L5-30 basics/…/lib.rs• L22-41 basics/…/Cargo.toml• L6-25 basics/…/Cargo.toml• L11-24 basics/…/README.md• L20-56

Native SOL transfers

System Program transfers

The System Program is the standard authority for moving SOL between accounts. A program can create a System Program transfer instruction and invoke it with the payer, recipient, and System Program accounts. The transfer then follows the System Program's rules, including the payer's signature requirement.

The Anchor and native examples expose this as a CPI operation. The Pinocchio example uses the same system transfer path with a compact instruction form. A recipient can be a new keypair account; receiving SOL initializes it as a basic system account in the example flow. The LiteSVM integration test also checks this client-facing account contract by submitting a signed transfer and comparing balances before and after execution.

References: basics/…/instruction.rs• L8-20 basics/…/lib.rs• L10-23 basics/…/lib.rs• L20-30 basics/…/transfer.ts• L19-34 basics/…/test_transfer_sol.rs• L46-64 basics/…/lib.rs• L10-50 basics/…/README.md• L1-5

Program-owned lamport changes

An owning program can update the lamport balances of accounts that it owns without calling the System Program. The direct-transfer example shows this as a separate path from a System Program CPI. It requires the source account to be a signer in the example and changes both balances as one program operation.

This path is not a general replacement for a System Program transfer. The program must have the ownership authority required for the accounts it changes, and it must preserve balance and account invariants. Use the System Program path when the transfer belongs to normal system-account behavior.

References: basics/…/instruction.rs• L22-36 basics/…/lib.rs• L25-50 basics/…/lib.rs• L33-49 basics/…/transfer.ts• L37-52 basics/…/test_transfer_sol.rs• L66-107

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the client instruction encoding aligned with every native or Pinocchio dispatch discriminator and field layout.
  • Keep account order and mutability aligned between the caller's instruction, the CPI account metas, and the callee's account contract.
  • When a CPI target changes its instruction or account interface, update the Anchor generated program interface and the native caller dependency together.
  • Preserve the authority model when changing a transfer path: a System Program transfer needs the payer's signer context, while direct lamport changes need ownership authority.
  • Keep imported native CPI programs free of a second compiled entry point through the no-entrypoint feature boundary.

Important failure modes

TriggerConsequenceGuard
Unsupported or malformed instruction bytesDispatch returns an invalid-data error and no operation runs.Use one shared instruction encoding and reject unknown operation selectors.
Wrong account order, count, or program addressThe instruction fails or could address the wrong state if checks are missing.Match the account contract and validate fixed program addresses before use.
Missing signer on a payer-dependent actionThe System Program or the program rejects the transfer.Mark the payer as a signer and check signer status where the program owns dispatch.
CPI omits an account or passes the wrong mutabilityThe nested instruction fails account validation or cannot update state.Derive CPI account metas from the callee's required account contract.
Native CPI dependency includes its entry pointThe caller build contains conflicting program entry points.Compile the imported program with its no-entrypoint feature.

Key files

FileWhy to read it
basics/…/lib.rsShows a native entry point that decodes custom data and applies a rule.
basics/…/lib.rsShows explicit checks for account count, order, initialization, ownership, and program identity.
basics/…/lib.rsShows how Anchor expresses the same account contract with types and constraints.
basics/…/lib.rsShows a caller that builds and invokes another program's instruction.
basics/…/lib.rsShows the callee that dispatches the nested request and updates shared state.
basics/…/lib.rsShows a typed Anchor CPI using the target program's account interface.
basics/…/test_cpi.rsShows the caller and callee running together in one local transaction flow.
basics/…/instruction.rsCompares System Program SOL transfer with direct program transfer.
basics/…/transfer.tsShows the client account roles and data variants for both transfer paths.
basics/…/lib.rsShows both SOL transfer paths with Anchor account contracts.
basics/…/lib.rsShows an end-to-end local check of a signed System Program transfer.