subxt/book/
mod.rs

1// Copyright 2019-2025 Parity Technologies (UK) Ltd.
2// This file is dual-licensed as Apache-2.0 or GPL-3.0.
3// see LICENSE for license details.
4
5// Dev note; I used the following command to normalize and wrap comments:
6// rustfmt +nightly --config wrap_comments=true,comment_width=100,normalize_comments=true subxt/src/book/custom_values
7// It messed up comments in code blocks though, so be prepared to go and fix those.
8
9//! # The Subxt Guide
10//!
11//! Subxt is a library for interacting with Substrate based nodes. It has a focus on **sub**mitting
12//! e**xt**rinsics, hence the name, however it's also capable of reading blocks, storage, events and
13//! constants from a node. The aim of this guide is to explain key concepts and get you started with
14//! using Subxt.
15//!
16//! 1. [Features](#features-at-a-glance)
17//! 2. [Limitations](#limitations)
18//! 3. [Quick start](#quick-start)
19//! 4. [Usage](#usage)
20//!
21//! ## Features at a glance
22//!
23//! Here's a quick overview of the features that Subxt has to offer:
24//!
25//! - Subxt allows you to generate a static, type safe interface to a node given some metadata; this
26//!   allows you to catch many errors at compile time rather than runtime.
27//! - Subxt also makes heavy use of node metadata to encode/decode the data sent to/from it. This
28//!   allows it to target almost any node which can output the correct metadata, and allows it some
29//!   flexibility in encoding and decoding things to account for cross-node differences.
30//! - Subxt has a pallet-oriented interface, meaning that code you write to talk to some pallet on
31//!   one node will often "Just Work" when pointed at different nodes that use the same pallet.
32//! - Subxt can work offline; you can generate and sign transactions, access constants from node
33//!   metadata and more, without a network connection. This is all checked at compile time, so you
34//!   can be certain it won't try to establish a network connection if you don't want it to.
35//! - Subxt can forego the statically generated interface and build transactions, storage queries
36//!   and constant queries using data provided at runtime, rather than queries constructed
37//!   statically.
38//! - Subxt can be compiled to WASM to run in the browser, allowing it to back Rust based browser
39//!   apps, or even bind to JS apps.
40//!
41//! ## Limitations
42//!
43//! In various places, you can provide a block hash to access data at a particular block, for
44//! instance:
45//!
46//! - [`crate::storage::StorageClient::at`]
47//! - [`crate::events::EventsClient::at`]
48//! - [`crate::blocks::BlocksClient::at`]
49//! - [`crate::runtime_api::RuntimeApiClient::at`]
50//!
51//! However, Subxt is (by default) only capable of properly working with blocks that were produced
52//! after the most recent runtime update. This is because it uses the most recent metadata given
53//! back by a node to encode and decode things. It's possible to decode older blocks produced by a
54//! runtime that emits compatible (currently, V14) metadata by manually setting the metadata used by
55//! the client using [`crate::client::OnlineClient::set_metadata()`].
56//!
57//! Subxt does not support working with blocks produced prior to the runtime update that introduces
58//! V14 metadata. It may have some success decoding older blocks using newer metadata, but may also
59//! completely fail to do so.
60//!
61//! ## Quick start
62//!
63//! Here is a simple but complete example of using Subxt to transfer some tokens from the example
64//! accounts, Alice to Bob:
65//!
66//! ```rust,ignore
67#![doc = include_str!("../../examples/tx_basic.rs")]
68//! ```
69//!
70//! This example assumes that a Polkadot node is running locally (Subxt endeavors to support all
71//! recent releases). Typically, to use Subxt to talk to some custom Substrate node (for example a
72//! parachain node), you'll want to:
73//!
74//! 1. [Generate an interface](setup::codegen)
75//! 2. [Create a config](setup::config)
76//! 3. [Use the config to instantiate the client](setup::client)
77//!
78//! Follow the above links to learn more about each step.
79//!
80//! ## Usage
81//!
82//! Once Subxt is configured, the next step is interacting with a node. Follow the links
83//! below to learn more about how to use Subxt for each of the following things:
84//!
85//! - [Transactions](usage::transactions): Subxt can build and submit transactions, wait until they are in
86//!   blocks, and retrieve the associated events.
87//! - [Storage](usage::storage): Subxt can query the node storage.
88//! - [Events](usage::events): Subxt can read the events emitted for recent blocks.
89//! - [Constants](usage::constants): Subxt can access the constant values stored in a node, which
90//!   remain the same for a given runtime version.
91//! - [Blocks](usage::blocks): Subxt can load recent blocks or subscribe to new/finalized blocks,
92//!   reading the extrinsics, events and storage at these blocks.
93//! - [Runtime APIs](usage::runtime_apis): Subxt can make calls into pallet runtime APIs to retrieve
94//!   data.
95//! - [Custom values](usage::custom_values): Subxt can access "custom values" stored in the metadata.
96//! - [Raw RPC calls](usage::rpc): Subxt can be used to make raw RPC requests to compatible nodes.
97//!
98//! ## Examples
99//!
100//! Some complete, self contained examples which are not a part of this guide:
101//!
102//! - [`parachain-example`](https://github.com/paritytech/subxt/tree/master/examples/parachain-example) is an example
103//!   which uses Zombienet to spawn a parachain locally, and then connects to it using Subxt.
104//! - [`wasm-example`](https://github.com/paritytech/subxt/tree/master/examples/wasm-example) is an example of writing
105//!   a Rust app that contains a Yew based UI, uses Subxt to interact with a chain, and compiles to WASM in order to
106//!   run entirely in the browser.
107pub mod setup;
108pub mod usage;