Skip to main content

Crate anchor_client

Crate anchor_client 

Source
Expand description

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::{Client, Cluster, Signer};
use my_program::{accounts, instruction, MyAccount};
use solana_keypair::{read_keypair_file, Keypair};
use solana_system_interface::program as system_program;

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 = "1.0.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.

Re-exports§

pub use anchor_lang;

Structs§

Client
Client defines the base configuration for building RPC clients to communicate with Anchor programs running on a Solana cluster. It’s primary use is to build a Program client via the program method.
CommitmentConfig
Config
DynSigner
Auxiliary data structure to align the types of the Solana CLI utils with Anchor client. Client implementation requires <C: Clone + Deref<Target = impl Signer>> which does not comply with Box that’s used when loaded Signer from keypair file. This struct is used to wrap the usage.
EventContext
EventUnsubscriber
Execution
Hash
A hash; the 32-byte output of a hashing algorithm.
Instruction
A directive for a single invocation of a Solana program.
Program
Program is the primary client handle to be used to build and send requests.
ProgramAccountsIterator
Iterator with items of type (Pubkey, T). Used to lazily deserialize account structs. Wrapper type hides the inner type from usages so the implementation can be changed.
RequestBuilder
RequestBuilder provides a builder interface to create and send transactions to a cluster.
RpcSendTransactionConfig
SolanaClientError
Transaction
An atomically-committed sequence of instructions.

Enums§

ClientError
Cluster
PubsubClientError
RpcFilterType
SignerError

Traits§

AsSigner
Signer
The Signer trait declares operations that all digital signature providers must support. It is the primary interface by which signers are specified in Transaction signing interfaces
ThreadSafeSignerasync

Functions§

handle_program_log
handle_system_log