49 releases (30 breaking)

0.31.1 Apr 20, 2025
0.31.0 Mar 9, 2025
0.30.1 Jun 20, 2024
0.29.0 Oct 16, 2023
0.3.0 Mar 12, 2021

#17 in #signer

Download history 4203/week @ 2025-01-15 3940/week @ 2025-01-22 4161/week @ 2025-01-29 4195/week @ 2025-02-05 5247/week @ 2025-02-12 5543/week @ 2025-02-19 5420/week @ 2025-02-26 5398/week @ 2025-03-05 7273/week @ 2025-03-12 6085/week @ 2025-03-19 4994/week @ 2025-03-26 3786/week @ 2025-04-02 4261/week @ 2025-04-09 4105/week @ 2025-04-16 5702/week @ 2025-04-23 4385/week @ 2025-04-30

19,236 downloads per month
Used in 62 crates (55 directly)

Apache-2.0

50KB
1K SLoC

An RPC client to interact with Solana programs written in anchor_lang.

Examples

A simple example that creates a client, sends a transaction and fetches an account:

use std::rc::Rc;

use anchor_client::{
    solana_sdk::{
        signature::{read_keypair_file, Keypair},
        signer::Signer,
        system_program,
    },
    Client, Cluster,
};
use my_program::{accounts, instruction, MyAccount};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create client
    let payer = read_keypair_file("keypair.json")?;
    let client = Client::new(Cluster::Localnet, Rc::new(payer));

    // Create program
    let program = client.program(my_program::ID)?;

    // Send transaction
    let my_account_kp = Keypair::new();
    program
        .request()
        .accounts(accounts::Initialize {
            my_account: my_account_kp.pubkey(),
            payer: program.payer(),
            system_program: system_program::ID,
        })
        .args(instruction::Initialize { field: 42 })
        .signer(&my_account_kp)
        .send()?;

    // Fetch account
    let my_account: MyAccount = program.account(my_account_kp.pubkey())?;
    assert_eq!(my_account.field, 42);

    Ok(())
}

More examples can be found in here.

Features

async

The client is blocking by default. To enable asynchronous client, add async feature:

anchor-client = { version = "0.31.1 ", features = ["async"] }

mock

This feature allows passing in a custom RPC client when creating program instances, which is useful for mocking RPC responses, e.g. via RpcClient::new_mock.

Dependencies

~55–74MB
~1.5M SLoC