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 / solana-programming-basics

Solana program fundamentals

Solana programs are on-chain services that receive instructions, validate the accounts supplied with each instruction, and change account state or ask another program to act. These examples build that model from simple messages to stored data, derived addresses, and composed program behavior.

Program model

Program entry point

A Solana program starts when the runtime calls its entry point with a program address, an ordered account list, and instruction bytes. The small basics/…/lib.rs example shows the smallest useful program: it receives a call and reports the program address without changing state.

The program itself does not hold mutable state between calls. Persistent state lives in accounts. An instruction therefore describes both the requested action and the accounts that the action may inspect or change.

References: basics/…/lib.rs• L6-19 basics/…/lib.rs• L1-9

Account boundaries

An account is the durable data and balance resource that a program uses to remember facts. The account-data example creates an account owned by the example program, sizes it for serialized AddressInfo data, and stores that value after the system program creates the account.

Account checks define the safety boundary for each instruction. The checking-accounts example teaches the core checks: use the expected program address, require the expected number and order of accounts, distinguish new accounts from initialized accounts, require a writable account for changes, and confirm that the system program is the real system program.

References: basics/…/create.rs• L12-33 basics/…/processor.rs• L11-17 basics/…/lib.rs• L11-61

Instruction processing

Instruction data

Instruction data carries the values that make one call different from another. The processing-instructions example decodes a name and height, then applies a simple rule to decide whether the visitor may ride. This shows the separation between the request data and the accounts used by a program.

Native programs must agree with their clients on a byte format. The examples use Borsh-compatible encoders for structured values. A program rejects bytes that do not match the instruction it expects instead of guessing at their meaning.

References: basics/…/lib.rs• L7-29 basics/…/README.md• L1-9 basics/…/processor.rs• L11-17

Instruction selection

A program can expose more than one action through the same entry point. The favorites example decodes a typed instruction and routes it to either PDA creation or PDA reading. The counter example uses a leading instruction value to select an increment action. It lets any key create a counter and increment any counter. The repository-layout example routes one decoded request to a ride, game, or food action.

This gives each on-chain action a clear contract: the action defines its input data, its required accounts, and the state change that it may perform. The account list remains part of that contract even when the instruction data is valid.

References: basics/…/processor.rs• L7-21 basics/…/lib.rs• L20-45 basics/…/README.md• L1-5 basics/…/processor.rs• L16-48

Instruction boundaries

An instruction may also select a behavior that reads fixed program data instead of changing account state. The repository-layout example keeps the shared request shape at the entry point, then passes only the fields needed by the selected attraction. An unknown attraction or attraction name returns invalid instruction data.

This pattern separates request decoding from action rules. It lets one program present several actions while keeping each action's input checks and result clear.

References: basics/…/processor.rs• L25-48 basics/…/eat_food.rs• L16-34 basics/…/get_on_ride.rs• L17-45 basics/…/play_game.rs• L16-34

Program-derived addresses

Deterministic account identity

A program-derived address (PDA) is a repeatable account address calculated from a program address and a set of seeds. It gives a program a known place for state without asking a user to create or remember a random key. The favorites example derives one record from the favorite seed and the user address.

The same seeds must be used when an account is created and when it is later read or changed. The program checks the supplied account against the derived address before it trusts the account as the intended record.

References: basics/…/create_pda.rs• L12-24 basics/…/get_pda.rs• L10-23

Program authority

A PDA has no private key. A program can act for it only when the runtime verifies the seed set and bump supplied by that program. The create example uses this signed invocation to let the program create an account at its derived address, then store the initial state in that account.

The PDA can also hold lamports that fund a system-program account creation. The pda-rent-payer example shows this authority flow, but its current example instruction does not check who may spend the rent-vault PDA. A real program must add an authority rule before it exposes this pattern.

After creation, a later instruction finds the same PDA and reads its stored value. This joins deterministic identity with durable account state.

References: basics/…/create_pda.rs• L26-48 basics/…/get_pda.rs• L15-23 basics/…/processor.rs• L13-19 basics/…/create_new_account.rs• L9-45

On-chain behavior

State transition

An on-chain behavior is a state transition: an instruction identifies the action, account checks establish which state may change, and the program writes the new value. Account creation adds a new owned state record. A later instruction reads that record and applies a small change such as incrementing a counter.

The foundational examples compose these parts into a predictable lifecycle:

  1. A client builds instruction data and supplies the required accounts.
  2. The program decodes the request and selects the action.
  3. The program checks account identity, ownership, signer status, and write access.
  4. The program creates or reads the account and applies the requested state change.
  5. The updated account can be read by a later instruction or by a client.

This diagram shows how a request becomes persistent program state.

flowchart LR
    Client[Client request] --> Instruction[Instruction data and accounts]
    Instruction --> Entry[Program entry point]
    Entry --> Decode[Decode and select action]
    Decode --> Check[Check account boundaries]
    Check --> Create[Create owned account]
    Check --> Update[Read and update account]
    Create --> State[Persistent account state]
    Update --> State
    State --> Read[Later reads or instructions]

References: basics/…/create.rs• L12-33 basics/…/processor.rs• L11-17 basics/…/lib.rs• L34-45 basics/…/create_pda.rs• L26-48

Program composition

One program can call another program to complete part of a larger action. A cross-program invocation (CPI) is this call between on-chain programs. In the lever and hand examples, hand asks lever to switch shared power state, so the caller coordinates the action while the owning program keeps control of its state rules.

Account state and PDAs make this composition addressable. The caller passes the state account and the target program, while the target program validates and updates the account under its own rules. For the wider CPI flow, see Instruction and CPI patterns.

References: basics/…/test_cpi.rs• L25-31 basics/…/test_cpi.rs• L43-63 basics/…/Cargo.toml• L12-14 basics/…/Cargo.toml• L1-14

Learning paths

The examples are best read as three connected topics rather than as separate program styles:

References: basics/…/lib.rs• L10-35 basics/…/test_program_derived_addresses.rs• L38-69 basics/…/test_cpi.rs• L33-63

Change checklist

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

  • Keep the client instruction encoding aligned with the program's decoding and action selection.
  • Keep account order, signer status, writable status, ownership, and expected program addresses aligned across every instruction contract.
  • Keep PDA seed bytes and bump handling identical in client address derivation, account creation, and later account access.
  • Keep the account data size and serialized state shape aligned when a program creates or updates an account.
  • Keep the selected attraction names and their action routing aligned across the shared request and action modules.
  • When a CPI target or its account contract changes, update the caller's passed accounts and target-program contract together.
  • When a PDA funds account creation, keep its caller-authorization rule aligned with the accounts that can spend the PDA's lamports.

Important failure modes

TriggerConsequenceGuard
Instruction bytes do not match the expected formatThe program cannot select or run the requested actionDecode a defined instruction type and return an invalid-data error for unknown bytes
An attraction value or name is not supportedThe repository-layout action returns invalid instruction dataRoute only known attraction types and match the requested name before running the action
An account is missing, in the wrong order, or has the wrong ownerThe program may reject the request or risk changing the wrong stateCheck account count and identity before reading or writing account data
A client uses different PDA seeds from the programThe supplied account is not the intended state recordDerive the address from one shared seed contract and verify it on chain
A PDA creation call uses invalid signing seedsAccount creation fails and no durable state is madeUse the exact seeds and bump used to derive the PDA
A rent-vault instruction has no caller authority checkAny caller can spend the PDA's lamports to fund an arbitrary accountRequire an authorized signer or stored authority before the PDA signs the system-program call
A state account is read with a different data shapeState decoding fails or produces invalid behaviorKeep serialized state definitions and account space in sync
A CPI passes the wrong target program or state accountThe composed action fails or reaches the wrong authority boundaryRequire the expected target program and let the owning program validate its accounts

Key files

FileWhy to read it
basics/…/lib.rsSmallest entry-point example and the basic runtime call shape
basics/…/lib.rsCore account and program identity checks
basics/…/create.rsAccount creation, funding, ownership, and stored data in one flow
basics/…/processor.rsInstruction decoding for the account-data example
basics/…/lib.rsStructured instruction data and a data-driven action
basics/…/README.mdClient and program agreement on custom instruction encoding
basics/…/processor.rsShared request decoding and routing to several actions
basics/…/get_on_ride.rsAction-level checks based on decoded request data
basics/…/processor.rsPDA instruction selection and dispatch
basics/…/create_pda.rsPDA validation, creation, and initial state storage
basics/…/get_pda.rsDeterministic lookup and state reading
basics/…/create_new_account.rsPDA-funded account creation and its required authority boundary
basics/…/lib.rsSmall instruction selector and persistent counter update
basics/…/README.mdCounter behavior and its in-process test and deploy flow
basics/…/test_cpi.rsCaller side and observed result of a cross-program action
basics/…/Cargo.tomlNative caller dependency on the target program's CPI interface
basics/…/Cargo.tomlTarget program package and CPI feature contract