Skip to main content

Crate subxt

Crate subxt 

Source
Expand description

Subxt is a library for interacting with Substrate based nodes. Here’s what it looks like:

//! Construct and submit a transaction.
use subxt::{Error, 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")]
mod polkadot {}

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

    // Almost all actions are performed at an explicit block. Here we use
    // the current block at the time of running this.
    let at_block = api.at_current_block().await?;

    // Build a balance transfer extrinsic.
    let dest = dev::bob().public_key().into();
    let balance_transfer_tx = polkadot::transactions()
        .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 = at_block
        .transactions()
        .sign_and_submit_then_watch_default(&balance_transfer_tx, &from)
        .await?
        .wait_for_finalized_success()
        .await?;

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

    Ok(())
}

Take a look at the examples to get off to a quick start, or check out the introduction for a more in depth introduction to Subxt.

In addition to the single-file examples above, we have a few self contained project examples to show off specific use cases:

  • The parachain example is an example which uses Zombienet to spawn a parachain locally, and then connects to it using Subxt.
  • The WASM example is an example of writing a Rust web application which uses Subxt to interact with a chain entirely in the browser.
  • The FFI example shows off how Subxt can be called via FFI from Node.JS and Python.

Re-exports§

pub use transactions as tx;
pub use crate::client::OfflineClient;
pub use crate::client::OfflineClientAtBlock;
pub use crate::client::OnlineClient;
pub use crate::client::OnlineClientAtBlock;
pub use crate::config::Config;
pub use crate::config::PolkadotConfig;
pub use crate::config::SubstrateConfig;
pub use crate::error::Error;
pub use subxt_metadata as metadata;
pub use subxt_rpcs as rpcs;
pub use subxt_lightclient as lightclient;

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).
client
This module exposes the entrypoint to connect and interact with chains.
config
This module provides a Config type, which provides a way to configure Subxt to work with a specific chain.
constants
This module exposes ConstantsClient, which has methods for working with constants. It’s created by calling crate::client::ClientAtBlock::constants().
custom_values
This module exposes CustomValuesClient, which has methods for working with custom values. It’s created by calling crate::client::ClientAtBlock::custom_values().
dynamic
This module ex-exports various helpers for constructing dynamic payloads/queries/addresses.
error
Types representing the errors that can be returned.
events
This module exposes EventsClient, which has methods for working with eventts. It’s created by calling crate::client::ClientAtBlock::events().
ext
Re-export external crates that are made use of in the subxt API.
extrinsics
This module exposes ExtrinsicsClient, which has methods for working with extrinsics. It’s created by calling crate::client::ClientAtBlock::extrinsics().
introduction
Introduction
runtime_apis
This module exposes RuntimeApisClient, which has methods for calling Runtime APIs. It’s created by calling crate::client::ClientAtBlock::runtime_apis().
storage
This module exposes StorageClient, which has methods for fetching and iterating over storage entries. It’s created by calling crate::client::ClientAtBlock::storage().
transactions
This module exposes TransactionsClient, which has methods for constructing and submitting transactions. It’s created by calling crate::client::ClientAtBlock::transactions(), or crate::client::ClientAtBlock::tx() for short.
utils
Miscellaneous utility helpers.
view_functions
This module exposes ViewFunctionsClient, which has methods for calling View Functions. It’s created by calling crate::client::ClientAtBlock::view_functions().

Structs§

Metadata
Node metadata. This can be constructed by providing some compatible frame_metadata which is then decoded into this. We aim to preserve all of the existing information in the incoming metadata while optimizing the format a little for Subxt’s use cases.

Type Aliases§

ArcMetadata
Metadata is often passed around wrapped in an Arc so that it can be cloned.

Attribute Macros§

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