anchor-client crate provides a Rust client library for interacting with Anchor programs. It offers a type-safe, ergonomic API for building transactions, sending them to the cluster, and fetching program accounts.
Installation
Addanchor-client to your Cargo.toml:
The client is blocking by default. Enable the
async feature for asynchronous operations.Core Concepts
The Anchor Rust client is built around these key components:Client
Configures cluster connection and payer for transactions
Program
Provides methods to interact with a specific on-chain program
RequestBuilder
Builder pattern for constructing and sending transactions
Client Setup
Create aClient to manage your cluster connection and transaction payer.
API Signature
Basic Example
Using Dynamic Signers
If you need to work withBox<dyn Signer>, use the DynSigner wrapper:
Cluster Configuration
TheCluster enum defines which Solana cluster to connect to:
Usage Examples
Program Instance
Create aProgram instance to interact with a specific on-chain program.
API
Creating a Program
Using declare_program!
Thedeclare_program! macro generates type-safe client code from your program’s IDL.
Place IDL in idls/ directory
Create an
idls/ folder in your client project root and place your program’s IDL JSON file there:Generated Modules
Thedeclare_program! macro generates:
client::accounts- Structs matching instruction account requirementsclient::args- Structs for instruction arguments- Account types - Deserializable account data structures
ID- The program’s public key constant
Building Transactions
Use theRequestBuilder to construct transactions with the builder pattern.
RequestBuilder API
Single Instruction
Multiple Instructions
Combine multiple instructions into a single transaction:Remaining Accounts
For instructions that accept remaining accounts:Fetching Accounts
Fetch and deserialize program accounts using type-safe methods.Single Account
Multiple Accounts with Filters
Event Listeners
Subscribe to program events emitted via logs:- Async
- Blocking
Complete Example
Here’s a full example demonstrating the Rust client in action:Error Handling
The client uses theClientError enum for error handling:
Features
Theanchor-client crate supports optional features:
async
Enables asynchronous client operations:Futures and require an async runtime like Tokio.
mock
Allows passing custom RPC clients for testing:RpcClient::new_mock.
API Reference
Client
| Method | Description |
|---|---|
new(cluster, payer) | Create new client |
new_with_options(cluster, payer, options) | Create client with custom commitment |
program(program_id) | Create program instance |
Program
| Method | Description |
|---|---|
request() | Create new request builder |
account<T>(address) | Fetch and deserialize account |
accounts<T>(filters) | Fetch multiple accounts with filters |
payer() | Get payer public key |
id() | Get program ID |
RequestBuilder
| Method | Description |
|---|---|
accounts(accounts) | Set instruction accounts |
args(args) | Set instruction arguments |
signer(signer) | Add signer |
instruction(ix) | Add instruction |
send() | Build, sign, and send transaction |
transaction() | Build transaction without sending |
instructions() | Get list of instructions |
Next Steps
TypeScript Client
Learn about the TypeScript client library
Testing Programs
Write tests for your Anchor programs