wasm_client_solana 0.10.0

A wasm compatible solana rpc and pubsub client
Documentation

wasm_client_solana

A WebAssembly (WASM) compatible client for interacting with the Solana RPC and PubSub APIs. It allows for sending transactions, fetching account data, subscribing to account changes, and more, from within a WASM compaitible environment like the web and serverless functions.

Crate Docs Status Unlicense codecov

Installation

Add the following to your Cargo.toml:

[dependencies]
wasm_client_solana = "0.10.0"

Or use cargo add:

cargo add wasm_client_solana

Building for WASM

When building your crate for the wasm32-unknown-unknown target, you need to set a specific RUSTFLAGS environment variable. This is required by the getrandom crate, a dependency used for generating random numbers, to correctly function in a WASM environment.

export RUSTFLAGS='--cfg getrandom_backend="wasm_js"'

Alternatively you can add this to your ./.cargo/config.toml file.

[target.wasm32-unknown-unknown]
rustflags = [
	"--cfg",
	"get_random_backend=\"wasm_js\"",
]

Without this flag, you may encounter compilation errors related to getrandom.

Features

This crate provides the following features:

  • js: Enables the use of the wasm-bindgen crate for the js target. This is useful for using the crate in a browser environment.
  • ssr: Enables the use of the reqwest and tokio crates for the ssr target. This is useful for using the crate in a server or non-browser environment.
  • zstd: Enables the use of the zstd as an encoding format and automatically activates the ssr target.

Usage

The SolanaRpcClient provides a wasm compatible client for the solana rpc and pubsub methods.

use solana_native_token::sol_str_to_lamports;
use solana_pubkey::pubkey;
use wasm_client_solana::ClientResult;
use wasm_client_solana::DEVNET;
use wasm_client_solana::SolanaRpcClient;

async fn run() -> ClientResult<()> {
	let client = SolanaRpcClient::new(DEVNET);
	let address = pubkey!("99P8ZgtJYe1buSK8JXkvpLh8xPsCFuLYhz9hQFNw93WJ");

	client
		.request_airdrop(&address, sol_str_to_lamports("1.0").unwrap())
		.await?;
	let account = client.get_account(&address).await?;

	log::info!("account: {account:#?}");

	Ok(())
}