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

anchor-client

An RPC client to interact with Anchor programs

47 releases (29 breaking)

0.30.1 Jun 20, 2024
0.29.0 Oct 16, 2023
0.28.0 Jun 9, 2023
0.27.0 Mar 8, 2023
0.3.0 Mar 12, 2021

#13 in #anchor

Download history 2218/week @ 2024-08-26 2215/week @ 2024-09-02 2066/week @ 2024-09-09 1720/week @ 2024-09-16 2151/week @ 2024-09-23 1708/week @ 2024-09-30 1103/week @ 2024-10-07 2281/week @ 2024-10-14 2454/week @ 2024-10-21 3047/week @ 2024-10-28 3796/week @ 2024-11-04 2818/week @ 2024-11-11 3284/week @ 2024-11-18 3433/week @ 2024-11-25 3872/week @ 2024-12-02 4950/week @ 2024-12-09

15,699 downloads per month
Used in 57 crates (50 directly)

Apache-2.0

46KB
983 lines

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 a 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 an 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

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

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

Dependencies

~78MB
~1.5M SLoC