Crate gear_subxt

Crate gear_subxt 

Source
Expand description

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

use sp_keyring::AccountKeyring;
use subxt::{tx::PairSigner, OnlineClient, PolkadotConfig};

// 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 = AccountKeyring::Bob.to_account_id().into();
    let balance_transfer_tx = polkadot::tx().balances().transfer(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 = PairSigner::new(AccountKeyring::Alice.pair());
    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::config::Config;
pub use crate::config::PolkadotConfig;
pub use crate::config::SubstrateConfig;
pub use crate::error::Error;
pub use crate::metadata::Metadata;

Modules§

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.
dynamic
This module provides the entry points to create dynamic transactions, storage and constant lookups.
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.
rpc
RPC types and client for interacting with a substrate 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.

Macros§

rpc_params
Create some RpcParams to pass to our RpcClient. RpcParams simply enforces that parameters handed to our RpcClient methods are the correct shape.

Attribute Macros§

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