Getting Started with Anchor
The Anchor framework is a tool that simplifies the process of building Solana programs. Whether you're new to blockchain development or an experienced programmer, Anchor simplifies the process of writing, testing, and deploying Solana programs.
In this section, we'll walk through:
- Creating a new Anchor project
- Building and testing your program
- Deploying to Solana clusters
- Understanding the project file structure
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.
- Anchor CLI: Command-line tool for the Anchor framework.
To verify Anchor CLI installation, open your terminal and run:
Expected output:
Getting Started
This section covers the basic steps to create, build, and test your first local Anchor program.
Create a new Project
To start a new project, use the anchor init
command followed by your project's
name. This command creates a new directory with the specified name and sets up a
default program and test file.
Navigate to the new project directory and open it in your code editor.
The default Anchor program is located at programs/my-project/src/lib.rs
.
The default Typescript test file is located at /tests/my-project.ts
.
If you prefer Rust for testing, initialize your project with the
--test-template rust
flag.
The Rust test file will be at /tests/src/test_initialize.rs
.
Build the Program
Build the program by running anchor build
.
The compiled program will be at /target/deploy/my_project.so
. The content of
this file is what gets stored on the Solana network (as an executable account)
when you deploy your program.
Test the Program
To test the program, run anchor test
.
By default, the Anchor.toml
config file specifies the localnet
cluster. When
developing on localnet
, anchor test
will automatically:
- Start a local Solana validator
- Build and deploy your program to the local cluster
- Run the tests in the
tests
folder - Stop the local Solana validator
Alternatively, you can manually start a local Solana validator and run tests against it. This is useful if you want to keep the validator running while you iterate on your program. It allows you to inspect accounts and transaction logs on the Solana Explorer while developing locally.
Open a new terminal and start a local Solana validator by running the
solana-test-validator
command.
In a separate terminal, run the tests against the local cluster. Use the
--skip-local-validator
flag to skip starting the local validator since it's
already running.
Deploy to Devnet
By default, the Anchor.toml
config file in an Anchor project specifies the
localnet cluster.
To deploy your program to devnet, change the cluster
value to Devnet
. Note
that this requires your wallet to have enough SOL on Devnet to cover deployment
cost.
Now when you run anchor deploy
, your program will be deployed to the devnet
cluster. The anchor test
command will also use the cluster specified in the
Anchor.toml
file.
To deploy to mainnet, simply update the Anchor.toml
file to specify the
mainnet cluster.
Update the Program
Solana programs can be updated by redeploying the program to the same program ID.
To update a program, simply make changes to your program's code and run the
anchor build
command to generated an updated .so
file.
Then run the anchor deploy
command to redeploy the updated program.
Close the Program
To reclaim the SOL allocated to a program account, you can close your Solana program.
To close a program, use the solana program close <PROGRAM_ID>
command. For
example:
Note that once a program is closed, the program ID cannot be reused to deploy a new program.
Project File Structure
Below is an overview of default file structure in an Anchor workspace:
Programs Folder
The /programs
folder contains your project's Anchor programs. A single
workspace can contain multiple programs.
Tests Folder
The /tests
folder contains test files for your project. A default test file is
created for you when you create your project.
Target Folder
The /target
folder contains build outputs. The main subfolders include:
/deploy
: Contains the keypair and program binary for your programs./idl
: Contains the JSON IDL for your programs./types
: Contains the TypeScript type for the IDL.
Anchor.toml File
The Anchor.toml
file configures workspace settings for your project.
.anchor Folder
Includes a program-logs
file that contains transaction logs from the last run
of test files.
App Folder
The /app
folder is an empty folder that can be optionally used for your
frontend code.