Crate wasm_client_anchor

Crate wasm_client_anchor 

Source
Expand description

§wasm_client_anchor


A Wasm-compatible, type-safe client for interacting with Anchor programs.


Crate Docs Status Unlicense codecov

This crate provides a client for Anchor programs that can be compiled to WebAssembly. It uses a macro-based system to generate type-safe client structs with a builder pattern for each instruction, making it easy and safe to interact with your on-chain programs from any Rust environment, including browsers.

§Installation

Add the following to your Cargo.toml:

[dependencies]
wasm_client_anchor = "0.9"

§Features

  • js: Enables wasm-bindgen support for use in browser environments.
  • ssr: Enables reqwest and tokio support for use in server-side or native environments.

§Compatibility

This crate is compatible with Anchor v0.32.1.

§How It Works

The core of this library is a set of macros that you use to generate a client for your specific Anchor program. This process involves two main steps:

  1. Code Generation: You create a small client crate (or module) where you use the provided macros to generate the program client struct and request builders for each instruction.
  2. Usage: In your application (e.g., a Yew/Leptos component or a server-side process), you use the generated client to build and send transactions.

§1. Code Generation

First, define a client for your Anchor program. Let’s assume your Anchor program is in a crate named my_anchor_program. You would create a new lib.rs file for your client like this:

// In your client crate (e.g., `my_anchor_program_client/src/lib.rs`)

use wasm_client_anchor::create_program_client;
use wasm_client_anchor::create_program_client_macro;

// 1. Generate the main client struct, pointing to your program's ID
create_program_client!(my_anchor_program::ID, MyProgramClient);

// 2. Create a macro that will generate request builders
create_program_client_macro!(my_anchor_program, MyProgramClient);

// 3. For each instruction in your program, generate a request builder.
//    - Use "optional:args" if the instruction struct has no fields.
//    - Omit it if the instruction struct has fields.
my_program_client_request_builder!(Initialize, "optional:args");
my_program_client_request_builder!(DoSomething);

§2. Usage

Once the client is generated, you can use it in your application to interact with the program.

use memory_wallet::MemoryWallet;
use my_anchor_program_client::IntoMyProgramClient;
use my_anchor_program_client::MyProgramClient;
use solana_sdk::signature::Keypair;
use wasm_client_solana::DEVNET;
use wasm_client_solana::SolanaRpcClient;

async fn run() -> anyhow::Result<()> {
	// Setup your RPC client and a wallet
	let rpc = SolanaRpcClient::new(DEVNET);
	let keypair = Keypair::new();
	let mut wallet = MemoryWallet::new(rpc.clone(), &[keypair]);
	wallet.connect().await?;

	// Instantiate your generated program client
	let program_client: MyProgramClient<_> = MyProgramClient::builder()
		.wallet(wallet.clone())
		.rpc(rpc.clone())
		.build()
		.into(); // Convert from the base AnchorProgram

	// --- Example 1: Call a single instruction ---
	let initialize_request = program_client
		.initialize() // The generated method for the `Initialize` instruction
		.accounts(my_anchor_program::accounts::Initialize {
			user: wallet.pubkey(),
			// ... other accounts
		})
		.build();

	let signature = initialize_request.sign_and_send_transaction().await?;
	println!("Initialize Signature: {}", signature);

	// --- Example 2: Compose multiple instructions ---
	let signer_keypair = Keypair::new();
	let composition_request = program_client
		.do_something() // First instruction
		.args(42) // Set instruction arguments
		.accounts(my_anchor_program::accounts::DoSomething {
			signer: signer_keypair.pubkey(),
			// ...
		})
		.signer(&signer_keypair) // Add extra signers
		.build()
		.compose() // Chain to the next instruction
		.initialize()
		.accounts(my_anchor_program::accounts::Initialize {
			user: wallet.pubkey(),
			// ...
		})
		.build();

	let composed_signature = composition_request.sign_and_send_transaction().await?;
	println!("Composed Signature: {}", composed_signature);

	Ok(())
}

Modules§

macros
prelude
utils

Macros§

base_create_request_builder
create_program_client
Create a program client struct with the provided name.
create_program_client_macro
create_request_builder

Structs§

AnchorEventContext
AnchorProgram
Use this struct to interact with anchor programs.
AnchorRequest
A custom anchor request with the async signer as the payer.
EmptyAnchorRequest
A custom anchor request with the anchor wallet as the payer.
EventSubscription
The events stream for anchor logs from programs.
ParsedLogEntry
ProgramLogIterator

Enums§

AnchorClientError

Traits§

AnchorProgramClient
AnchorRequestMethods
AnchorRpcClient
WalletAnchor

Functions§

handle_program_log
handle_system_log

Type Aliases§

AnchorClientResult
AnchorProgramPartialBuilder
Create a partially typed AnchorProgramBuilder with the program_id defined.
AnchorRequestBuilderPartial
Create a partially typed AnchorRequestBuilder with the rpc, program_id and wallet defined.
EmptyAnchorRequestBuilderPartial