- Seek granular control over program logic and optimizations
- Want to learn the underlying concepts before moving to higher-level frameworks
Prerequisites
For detailed installation instructions, visit the installation page. Before you begin, ensure you have the following installed:- Rust: The programming language for building Solana programs.
- Solana CLI: Command-line tool for Solana development.
Getting Started
The example below covers the basic steps to create your first Solana program written in Rust. We’ll create a minimal program that prints “Hello, world!” to the program log.1
Create a new program
First, create a new Rust project using the standard Navigate to the project directory. You should see the default
cargo new command with the
--lib flag.Terminal
src/lib.rs and
Cargo.toml filesTerminal
Update the
edition field in Cargo.toml to 2021. Otherwise, you might
encounter an error when building the program.2
Add the solana-program dependency
Next, add the
solana-program dependency. This is the minimum dependency
required to build a Solana program.Terminal
3
Add the crate-type
Next, add the following snippet to Your
Cargo.toml.Cargo.toml
If you don’t include this config, the
target/deploy directory will not be
generated when you build the program.Cargo.toml file should look like the following:4
Add the program code
Next, replace the contents of
src/lib.rs with the following code. This is a
minimal Solana program that prints “Hello, world!” to the program log when the
program is invoked.The msg! macro is used in Solana programs to print a message to the program
log.5
Build the program
Next, build the program using the This command generates a Example output:
cargo build-sbf command. Build for the
SBPF v0 architecture with --arch v0:Terminal
Solana CLI 4.3 and later build SBPF v3 programs by default. LiteSVM 0.6.1,
which the test in this guide uses, can’t load v3 programs: without
--arch v0, cargo test panics at add_program_from_file with
Instruction(InvalidAccountData). Older Solana CLI releases already
build v0 by default and accept the flag. Before you deploy to a local
validator, you rebuild, as the deploy step shows. Tested with Solana CLI
4.3.2 and 3.1.10.target/deploy directory containing two important
files:- A
.sofile (e.g.,hello_world.so): This is the compiled Solana program that will be deployed to the network. - A keypair file (e.g.,
hello_world-keypair.json): The public key of this keypair is used as the program ID when deploying the program.
Terminal
6
Add test dependencies
Next, test the program using the
litesvm crate. Add the following dependencies
to Cargo.toml.Terminal
7
Test the program
Add the following test to Example output:
src/lib.rs, below the program code. This is a test
module that invokes the hello world program.Run the test using the
cargo test command. The program log will display
“Hello, world!”.Terminal
Terminal
8
Deploy the program
Next, deploy the program. When developing locally, we can use the
Example output:Open a new terminal and run the While the test validator is running, rebuild the program with the default
architecture, then run the Example output:You can inspect the program ID and transaction signature on
Solana Explorer.
solana-test-validator.First, configure the Solana CLI to use the local Solana cluster.Terminal
solana-test-validators command to start the
local validator.Terminal
solana program deploy command in a separate
terminal to deploy the program to the local validator.Terminal
solana-test-validator 4.1 and later rejects SBPF v0 programs with
Detected sbpf_version required by the executable which are not enabled,
so deploy the default build, not the --arch v0 build you tested. 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 rebuild with
cargo build-sbf --arch v3 instead. Mainnet, devnet, and testnet accept
both v0 and v3 programs.Note that the cluster on Solana Explorer must also be localhost. The “Custom
RPC URL” option on Solana Explorer defaults to
http://localhost:8899.9
Create example client
Next, we’ll demonstrate how to invoke the program using a Rust client.First create an Add the following to Add
examples directory and a client.rs file.Terminal
Cargo.toml.Cargo.toml
solana-client and tokio dependencies.Terminal
10
Add the client
Add the following code to
examples/client.rs. This is a Rust client script
that funds a new keypair to pay for transaction fees and then invokes the hello
world program.11
Replace the program ID
Before running the client code, replace the program ID in the code snippet with
the one for your program.You can get your program ID by running the following command.
Terminal
12
Invoke the program
Run the client script with the following command.Example output:You can inspect the transaction signature on
Solana Explorer
(local cluster) to see “Hello, world!” in the program log.
Terminal
13
Update the program
Solana programs can be updated by redeploying to the same program ID. Update the
program in Test the updated program by running the You should see “Hello, Solana!” in the program log.
src/lib.rs to print “Hello, Solana!” instead of “Hello, world!”.lib.rs
Run the
cargo build-sbf command to generate an updated .so file.Terminal
cargo test command.Terminal
Terminal
14
Redeploy the program
Rebuild with the default architecture (Run the client code again and inspect the transaction signature on Solana
Explorer to see “Hello, Solana!” in the program log.
--arch v3 with Solana CLI 4.1 or
4.2) and redeploy the program using the same solana program deploy
command.Terminal
Terminal
15
Close the program
You can close your Solana program to reclaim the SOL allocated to the account.
Closing a program is irreversible, so it should be done with caution.To close a program, use the Example output:Note that once a program is closed, its program ID cannot be reused. Attempting
to deploy a program with a previously closed program ID will result in an error.
solana program close <PROGRAM_ID> command. For
example:Terminal
16
Redeploy a closed program
If you need to redeploy a program with the same source code after closing a
program, you must generate a new program ID. To generate a new keypair for the
program, run the following command:
Terminal
Alternatively, you can delete the existing keypair file (e.g.
./target/deploy/hello_world-keypair.json) and run cargo build-sbf again,
which will generate a new keypair file.