Rust
Learn how to use Anchor's Rust client library to interact with Solana programs
The anchor-client
crate
is the Rust client library for interacting with Anchor programs. You can find
the source code here.
Example
The example below demonstrates how to use the anchor-client
crate to interact
with a simple Anchor program. The program client can be automatically generated
from the program's IDL using the declare_program!
macro. This macro generates
dependency free modules that enable you to interact with the program's
instructions and accounts.
The program has two instructions:
initialize
– Creates and initializes a counter account to store a valueincrement
– Increments the value stored on the counter account
Below is an example folder structure for a Rust client that interacts with the Anchor program:
The program IDL must be in a /idls
folder. The declare_program!
macro
searches for the IDL in the /idls
folder to generate the client modules.
Below is the src/main.rs
file for interacting with the program:
-
The
declare_program!
macro - Generates client modules for the program using the IDL file -
The
anchor_client
crate - Provides utilities for interacting with the program, including:- Building program instructions
- Sending transactions
- Fetching program accounts
Below are the dependencies for the Cargo.toml
file: