49 releases (30 breaking)

new 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

#12 in #signer

Download history 2124/week @ 2024-12-29 3181/week @ 2025-01-05 4001/week @ 2025-01-12 3805/week @ 2025-01-19 3910/week @ 2025-01-26 4298/week @ 2025-02-02 4781/week @ 2025-02-09 5466/week @ 2025-02-16 6013/week @ 2025-02-23 5057/week @ 2025-03-02 6116/week @ 2025-03-09 6824/week @ 2025-03-16 5747/week @ 2025-03-23 3792/week @ 2025-03-30 4261/week @ 2025-04-06 3875/week @ 2025-04-13

18,017 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

~75MB
~1.5M SLoC