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§

Modules§

  • 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).
  • This module exposes the necessary functionality for working with events.
  • The Subxt Guide
  • 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.
  • 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.
  • Types associated with accessing constants.
  • Types associated with accessing custom types
  • This module provides the entry points to create dynamic transactions, storage and constant lookups.
  • Types representing the errors that can be returned.
  • 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().
  • Re-export external crates that are made use of in the subxt API.
  • Types representing the metadata obtained from a node.
  • Types associated with executing runtime API calls.
  • Types associated with accessing and working with storage items.
  • Create and submit extrinsics.
  • Miscellaneous utility helpers.

Macros§

Attribute Macros§

  • Generate a strongly typed API for interacting with a Substrate runtime from its metadata.