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

anchor-client

An RPC client to interact with Anchor programs

46 releases (29 breaking)

0.30.0 Apr 15, 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

#1060 in Magic Beans

Download history 1714/week @ 2024-02-14 1257/week @ 2024-02-21 1964/week @ 2024-02-28 1733/week @ 2024-03-06 2045/week @ 2024-03-13 2047/week @ 2024-03-20 1847/week @ 2024-03-27 1746/week @ 2024-04-03 1979/week @ 2024-04-10 2028/week @ 2024-04-17 2130/week @ 2024-04-24 1669/week @ 2024-05-01 1588/week @ 2024-05-08 1779/week @ 2024-05-15 1843/week @ 2024-05-22 2045/week @ 2024-05-29

7,530 downloads per month
Used in 54 crates (46 directly)

Apache-2.0

46KB
987 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.0 ", features = ["async"] }

Dependencies

~75MB
~1.5M SLoC