Skip to content
Solana programs written in Rust have minimal structural requirements, allowing for flexibility in how code is organized. The only requirement is that a program must have an entrypoint, which defines where the execution of a program begins.

​Program Structure

While there are no strict rules for file structure, Solana programs typically follow a common pattern:
  • entrypoint.rs: Defines the entrypoint that routes incoming instructions.
  • state.rs: Defines program state (account data).
  • instructions.rs: Defines the instructions that the program can execute.
  • processor.rs: Defines the instruction handlers (functions) that implement the business logic for each instruction.
  • error.rs: Defines custom errors that the program can return.
For example, see the Token Program.

​Example Program

To demonstrate how to build a native Rust program with multiple instructions, we’ll walk through a simple counter program that implements two instructions:
  1. InitializeCounter: Creates and initializes a new account with an initial value.
  2. IncrementCounter: Increments the value stored in an existing account.
For simplicity, the program will be implemented in a single lib.rs file, though in practice you may want to split larger programs into multiple files.

Full Program Code

​Part 1: Writing the Program

Let’s start by building the counter program. We’ll create a program that can initialize a counter with a starting value and increment it.
1

Create a new program

First, let’s create a new Rust project for our Solana program.
Terminal
You should see the default src/lib.rs and Cargo.toml files.
Update the edition field in Cargo.toml to 2021. Otherwise, you might encounter an error when building the program.
Cargo.toml
lib.rs
2

Add dependencies

Now let’s add the necessary dependencies for building a Solana program. We need solana-program for the core SDK and borsh for serialization.
Terminal
Cargo.toml
There is no requirement to use Borsh. However, it is a commonly used serialization library for Solana programs.
3

Configure crate-type

Solana programs must be compiled as dynamic libraries. Add the [lib] section to configure how Cargo builds the program.
Cargo.toml
Cargo.toml
If you don’t include this config, the target/deploy directory will not be generated when you build the program.
4

Setup program entrypoint

Every Solana program has an entrypoint, which is the function that gets called when the program is invoked. Let’s start with adding the imports we’ll need for the program and setting up the entrypoint.Add the following code to lib.rs:
lib.rs
The entrypoint macro handles the deserialization of the input data into the parameters of the process_instruction function.A Solana program entrypoint has the following function signature. Developers are free to create their own implementation of the entrypoint function.
lib.rs
5

Define program state

Now let’s define the data structure that will be stored in our counter accounts. This is the data that will be stored in the data field of the account.Add the following code to lib.rs:
lib.rs
lib.rs
6

Define instruction enum

Let’s define the instructions our program can execute. We’ll use an enum where each variant represents a different instruction.Add the following code to lib.rs:
lib.rs
lib.rs
7

Implement instruction deserialization

Now we need to deserialize the instruction_data (raw bytes) into one of our CounterInstruction enum variants. The Borsh try_from_slice method handles this conversion automatically.Update the process_instruction function to use Borsh deserialization:
lib.rs
lib.rs
8

Route instructions to handlers

Now let’s update the main process_instruction function to route instructions to their appropriate handler functions.This routing pattern is common in Solana programs. The instruction_data is deserialized into a variant of an enum representing the instruction, then the appropriate handler function is called. Each handler function includes the implementation for that instruction.Add the following code to lib.rs updating the process_instruction function and adding the handlers for the InitializeCounter and IncrementCounter instructions:
lib.rs
lib.rs
9

Implement initialize handler

Let’s implement the handler to create and initialize a new counter account. Since only the System Program can create accounts on Solana, we’ll use a Cross Program Invocation (CPI), essentially calling another program from our program.Our program makes a CPI to call the System Program’s create_account instruction. The new account is created with our program as the owner, giving our program the ability to write to the account and initialize the data.Add the following code to lib.rs updating the process_initialize_counter function:
lib.rs
This instruction is for demonstration purposes only. It does not include security and validation checks that are required for production programs.
lib.rs
10

Implement increment handler

Now let’s implement the handler that increments an existing counter. This instruction:
  • Reads the account data field for the counter_account
  • Deserializes it into a CounterAccount struct
  • Increments the count field by 1
  • Serializes the CounterAccount struct back into the account’s data field
Add the following code to lib.rs updating the process_increment_counter function:
lib.rs
This instruction is for demonstration purposes only. It does not include security and validation checks that are required for production programs.
lib.rs
11

Completed Program

You’ve built a complete Solana program that demonstrates the basic structure shared by all Solana programs:
  • Entrypoint: Defines where program execution begins and routes all incoming requests to appropriate instruction handlers
  • Instruction Handling: Defines instructions and their associated handlers functions
  • State Management: Defines account data structures and manages their state in program owned accounts
  • Cross-program invocation (CPI): Calls the System Program to create new program owned accounts
The next step is to test the program to ensure everything works correctly.
lib.rs

​Part 2: Testing the Program

Now let’s test our counter program. We’ll use LiteSVM, a testing framework that lets us test programs without deploying to a cluster.
1

Add test dependencies

First, let’s add the dependencies needed for testing. We’ll use litesvm for testing and solana-sdk.
Terminal
Cargo.toml
2

Create test module

Now let’s add a test module to our program. We’ll start with the basic scaffold and imports.Add the following code to lib.rs, directly below the program code:
lib.rs
lib.rs
The #[cfg(test)] attribute ensures this code is only compiled when running tests.
3

Initialize test environment

Let’s set up the test environment with LiteSVM and fund a payer account.LiteSVM simulates the Solana runtime environment, allowing us to test our program without deploying to a real cluster.Add the following code to lib.rs updating the test_counter_program function:
lib.rs
lib.rs
4

Load the program

Now we need to build and load our program into the test environment. Run the cargo build-sbf command to build the program. This will generate the counter_program.so file in the target/deploy directory.
Terminal
Build for the SBPF v0 architecture with --arch v0. Solana CLI 4.3 and later build SBPF v3 programs by default, and LiteSVM 0.6.1 can’t load them: without the flag, add_program_from_file fails with Instruction(InvalidAccountData). Older Solana CLI releases already build v0 by default and accept the flag.
Ensure the edition in Cargo.toml is set to 2021.
After building, we can load the program.Update the test_counter_program function to load the program into the test environment.
lib.rs
You must run cargo build-sbf --arch v0 before running tests to generate the .so file. The test loads the compiled program.
lib.rs
5

Test initialization instruction

Let’s test the initialization instruction by creating a new counter account with a starting value.Add the following code to lib.rs updating the test_counter_program function:
lib.rs
lib.rs
6

Verify initialization

After initialization, let’s verify the counter account was created correctly with the expected value.Add the following code to lib.rs updating the test_counter_program function:
lib.rs
lib.rs
7

Test increment instruction

Now let’s test the increment instruction to ensure it properly updates the counter value.Add the following code to lib.rs updating the test_counter_program function:
lib.rs
lib.rs
8

Verify final results

Finally, let’s verify that the increment worked correctly by checking the updated counter value.Add the following code to lib.rs updating the test_counter_program function:
lib.rs
lib.rs
Run the tests with the following command. The --nocapture flag prints the output of the test.
Terminal
Expected output:

​Part 3: Invoking the Program

Now let’s add a client script to invoke the program.
1

Create client example

Let’s create a Rust client to interact with our deployed program.
Terminal
Add the following configuration to Cargo.toml:
Cargo.toml
Cargo.toml
Install the client dependencies:
Terminal
2

Implement client code

Now let’s implement the client that will invoke our deployed program.Run the following command to get your program ID from the keypair file:
Terminal
Add the client code to examples/client.rs and replace the program_id with the output of the previous command:
examples/client.rs
examples/client.rs

​Part 4: Deploying the Program

Now that we have our program and client ready, let’s build, deploy, and invoke the program.
1

Build the program

First, let’s build our program with the default architecture. Part 2 built it with --arch v0 for LiteSVM, but solana-test-validator 4.1 and later rejects SBPF v0 programs, so rebuild before you deploy:
Terminal
The default build is SBPF v3 with Solana CLI 4.3 and later, and v0 with Solana CLI 3.x, whose test validator accepts v0 programs. Solana CLI 4.1 and 4.2 also build v0 by default, so with those releases run cargo build-sbf --arch v3 instead.
This command compiles your program and generates two important files in target/deploy/:
You can view your program’s ID by running the following command:
Terminal
Example output:
2

Start local validator

For development, we’ll use a local test validator.First, configure the Solana CLI to use localhost:
Terminal
Example output:
Now start the test validator in a separate terminal:
Terminal
3

Deploy the program

With the validator running, deploy your program to the local cluster:
Terminal
Example output:
You can verify the deployment using the solana program show command with your program ID:
Terminal
Example output:
4

Run the client

With the local validator still running, execute the client:
Terminal
Expected output:
With the local validator running, you can view the transactions on Solana Explorer using the output transaction signatures. Note the cluster on Solana Explorer must be set to “Custom RPC URL”, which defaults to http://localhost:8899 that the solana-test-validator is running on.