#anchor #programs #account #solana #interact #signer #rpc-client

anchor-client

An RPC client to interact with Anchor programs

48 releases (30 breaking)

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

#750 in Magic Beans

Download history 3082/week @ 2024-11-19 3610/week @ 2024-11-26 4014/week @ 2024-12-03 4759/week @ 2024-12-10 3238/week @ 2024-12-17 1854/week @ 2024-12-24 2654/week @ 2024-12-31 2888/week @ 2025-01-07 4171/week @ 2025-01-14 3755/week @ 2025-01-21 4232/week @ 2025-01-28 4336/week @ 2025-02-04 5013/week @ 2025-02-11 5651/week @ 2025-02-18 5260/week @ 2025-02-25 4976/week @ 2025-03-04

21,705 downloads per month
Used in 61 crates (54 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.0 ", 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

~74MB
~1.5M SLoC