Crate subxt

Source
Expand description

Subxt is a library for interacting with Substrate based nodes. Using it looks something like this:

#![allow(missing_docs)]
use subxt::{OnlineClient, PolkadotConfig};
use subxt_signer::sr25519::dev;

// Generate an interface that we can use from the node's metadata.
#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata_small.scale")]
pub mod polkadot {}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a new API client, configured to talk to Polkadot nodes.
    let api = OnlineClient::<PolkadotConfig>::new().await?;

    // Build a balance transfer extrinsic.
    let dest = dev::bob().public_key().into();
    let balance_transfer_tx = polkadot::tx().balances().transfer_allow_death(dest, 10_000);

    // Submit the balance transfer extrinsic from Alice, and wait for it to be successful
    // and in a finalized block. We get back the extrinsic events if all is well.
    let from = dev::alice();
    let events = api
        .tx()
        .sign_and_submit_then_watch_default(&balance_transfer_tx, &from)
        .await?
        .wait_for_finalized_success()
        .await?;

    // Find a Transfer event and print it.
    let transfer_event = events.find_first::<polkadot::balances::events::Transfer>()?;
    if let Some(event) = transfer_event {
        println!("Balance transfer success: {event:?}");
    }

    Ok(())
}

Take a look at the Subxt guide to learn more about how to use Subxt.

Re-exports§

pub use crate::client::OfflineClient;
pub use crate::client::OnlineClient;
pub use crate::error::Error;
pub use subxt_lightclient as lightclient;unstable-light-client

Modules§

backend
This module exposes a backend trait for Subxt which allows us to get and set the necessary information (probably from a JSON-RPC API, but that’s up to the implementation).
blocks
This module exposes the necessary functionality for working with events.
book
The Subxt Guide
client
This module provides two clients that can be used to work with transactions, storage and events. The OfflineClient works entirely offline and can be passed to any function that doesn’t require network access. The OnlineClient requires network access.
config
This module provides a Config type, which is used to define various types that are important in order to speak to a particular chain. SubstrateConfig provides a default set of these types suitable for the default Substrate node implementation, and PolkadotConfig for a Polkadot node.
constants
Types associated with accessing constants.
custom_values
Types associated with accessing custom types
dynamic
Submit dynamic transactions.
error
Types representing the errors that can be returned.
events
This module exposes the types and such necessary for working with events. The two main entry points into events are crate::OnlineClient::events() and calls like crate::tx::TxProgress::wait_for_finalized_success().
ext
Re-export external crates that are made use of in the subxt API.
metadata
Types representing the metadata obtained from a node.
runtime_api
Types associated with executing runtime API calls.
storage
Types associated with accessing and working with storage items.
tx
Create and submit extrinsics.
utils
Miscellaneous utility helpers.
view_functions
Types associated with executing View Function calls.

Structs§

Metadata
A cheaply clone-able representation of the runtime metadata received from a node.

Enums§

PolkadotConfig
Default set of commonly used types by Polkadot nodes.
SubstrateConfig
Default set of commonly used types by Substrate runtimes.

Traits§

Config
Runtime types.

Attribute Macros§

subxt
Generate a strongly typed API for interacting with a Substrate runtime from its metadata of WASM.