gear_subxt/lib.rs
1// Copyright 2019-2023 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//! Subxt is a library for interacting with Substrate based nodes. Using it looks something like this:
6//!
7//! ```rust,ignore
8#![doc = include_str!("../examples/tx_basic.rs")]
9//! ```
10//!
11//! Take a look at [the Subxt guide](book) to learn more about how to use Subxt.
12
13#![deny(
14 bad_style,
15 improper_ctypes,
16 missing_docs,
17 non_shorthand_field_patterns,
18 no_mangle_generic_items,
19 overflowing_literals,
20 path_statements,
21 patterns_in_fns_without_body,
22 private_in_public,
23 unconditional_recursion,
24 unused_allocation,
25 unused_comparisons,
26 unused_parens,
27 while_true,
28 trivial_casts,
29 trivial_numeric_casts,
30 unused_crate_dependencies,
31 unused_extern_crates,
32 clippy::all
33)]
34#![allow(clippy::type_complexity)]
35
36// The guide is here.
37pub mod book;
38
39// Suppress an unused dependency warning because tokio is
40// only used in example code snippets at the time of writing.
41#[cfg(test)]
42use tokio as _;
43
44// Used to enable the js feature for wasm.
45#[cfg(target_arch = "wasm32")]
46pub use getrandom as _;
47
48#[cfg(all(feature = "jsonrpsee-ws", feature = "jsonrpsee-web"))]
49std::compile_error!(
50 "Both the features `jsonrpsee-ws` and `jsonrpsee-web` are enabled which are mutually exclusive"
51);
52
53pub mod blocks;
54pub mod client;
55pub mod config;
56pub mod constants;
57pub mod dynamic;
58pub mod error;
59pub mod events;
60pub mod metadata;
61pub mod rpc;
62pub mod runtime_api;
63pub mod storage;
64pub mod tx;
65pub mod utils;
66
67// Expose a few of the most common types at root,
68// but leave most types behind their respective modules.
69pub use crate::{
70 client::{OfflineClient, OnlineClient},
71 config::{Config, PolkadotConfig, SubstrateConfig},
72 error::Error,
73 metadata::Metadata,
74};
75
76/// Re-export external crates that are made use of in the subxt API.
77pub mod ext {
78 pub use codec;
79 pub use frame_metadata;
80 pub use scale_bits;
81 pub use scale_decode;
82 pub use scale_encode;
83 pub use scale_value;
84 #[cfg(feature = "substrate-compat")]
85 pub use sp_core;
86 #[cfg(feature = "substrate-compat")]
87 pub use sp_runtime;
88}
89
90/// Generate a strongly typed API for interacting with a Substrate runtime from its metadata.
91///
92/// # Metadata
93///
94/// First, you'll need to get hold of some metadata for the node you'd like to interact with. One
95/// way to do this is by using the `subxt` CLI tool:
96///
97/// ```bash
98/// # Install the CLI tool:
99/// cargo install subxt-cli
100/// # Use it to download metadata (in this case, from a node running locally)
101/// subxt metadata > polkadot_metadata.scale
102/// ```
103///
104/// Run `subxt metadata --help` for more options.
105///
106/// # Basic usage
107///
108/// Annotate a Rust module with the `subxt` attribute referencing the aforementioned metadata file.
109///
110/// ```rust,no_run
111/// #[subxt::subxt(
112/// runtime_metadata_path = "../artifacts/polkadot_metadata_full.scale",
113/// )]
114/// mod polkadot {}
115/// ```
116///
117/// The `subxt` macro will populate the annotated module with all of the methods and types required
118/// for interacting with the runtime that the metadata came from via Subxt.
119///
120/// # Configuration
121///
122/// This macro supports a number of attributes to configure what is generated:
123///
124/// ## `crate = "..."`
125///
126/// Use this attribute to specify a custom path to the `subxt` crate:
127///
128/// ```rust
129/// # pub extern crate subxt;
130/// # pub mod path { pub mod to { pub use subxt; } }
131/// # fn main() {}
132/// #[subxt::subxt(
133/// runtime_metadata_path = "../artifacts/polkadot_metadata_full.scale",
134/// crate = "crate::path::to::subxt"
135/// )]
136/// mod polkadot {}
137/// ```
138///
139/// This is useful if you write a library which uses this macro, but don't want to force users to depend on `subxt`
140/// at the top level too. By default the path `::subxt` is used.
141///
142/// ## `substitute_type(path = "...", with = "...")`
143///
144/// This attribute replaces any reference to the generated type at the path given by `path` with a
145/// reference to the path given by `with`.
146///
147/// ```rust
148/// #[subxt::subxt(
149/// runtime_metadata_path = "../artifacts/polkadot_metadata_full.scale",
150/// substitute_type(path = "sp_arithmetic::per_things::Perbill", with = "crate::Foo")
151/// )]
152/// mod polkadot {}
153///
154/// # #[derive(
155/// # scale_encode::EncodeAsType,
156/// # scale_decode::DecodeAsType,
157/// # codec::Encode,
158/// # codec::Decode,
159/// # Clone,
160/// # Debug,
161/// # )]
162/// // In reality this needs some traits implementing on
163/// // it to allow it to be used in place of Perbill:
164/// pub struct Foo(u32);
165/// # impl codec::CompactAs for Foo {
166/// # type As = u32;
167/// # fn encode_as(&self) -> &Self::As {
168/// # &self.0
169/// # }
170/// # fn decode_from(x: Self::As) -> Result<Self, codec::Error> {
171/// # Ok(Foo(x))
172/// # }
173/// # }
174/// # impl From<codec::Compact<Foo>> for Foo {
175/// # fn from(v: codec::Compact<Foo>) -> Foo {
176/// # v.0
177/// # }
178/// # }
179/// # fn main() {}
180/// ```
181///
182/// If the type you're substituting contains generic parameters, you can "pattern match" on those, and
183/// make use of them in the substituted type, like so:
184///
185/// ```rust,no_run
186/// #[subxt::subxt(
187/// runtime_metadata_path = "../artifacts/polkadot_metadata_full.scale",
188/// substitute_type(
189/// path = "sp_runtime::multiaddress::MultiAddress<A, B>",
190/// with = "::subxt::utils::Static<::sp_runtime::MultiAddress<A, B>>"
191/// )
192/// )]
193/// mod polkadot {}
194/// ```
195///
196/// The above is also an example of using the [`crate::utils::Static`] type to wrap some type which doesn't
197/// on it's own implement [`scale_encode::EncodeAsType`] or [`scale_decode::DecodeAsType`], which are required traits
198/// for any substitute type to implement by default.
199///
200/// ## `derive_for_all_types = "..."`
201///
202/// By default, all generated types derive a small set of traits. This attribute allows you to derive additional
203/// traits on all generated types:
204///
205/// ```rust,no_run
206/// #[subxt::subxt(
207/// runtime_metadata_path = "../artifacts/polkadot_metadata_full.scale",
208/// derive_for_all_types = "Eq, PartialEq"
209/// )]
210/// mod polkadot {}
211/// ```
212///
213/// Any substituted types (including the default substitutes) must also implement these traits in order to avoid errors
214/// here.
215///
216/// ## `derive_for_type(path = "...", derive = "...")`
217///
218/// Unlike the above, which derives some trait on every generated type, this attribute allows you to derive traits only
219/// for specific types. Note that any types which are used inside the specified type may also need to derive the same traits.
220///
221/// ```rust,no_run
222/// #[subxt::subxt(
223/// runtime_metadata_path = "../artifacts/polkadot_metadata_full.scale",
224/// derive_for_all_types = "Eq, PartialEq",
225/// derive_for_type(path = "frame_support::PalletId", derive = "Ord, PartialOrd"),
226/// derive_for_type(path = "sp_runtime::ModuleError", derive = "Hash"),
227/// )]
228/// mod polkadot {}
229/// ```
230///
231/// ## `runtime_metadata_url = "..."`
232///
233/// This attribute can be used instead of `runtime_metadata_path` and will tell the macro to download metadata from a node running
234/// at the provided URL, rather than a node running locally. This can be useful in CI, but is **not recommended** in production code,
235/// since it runs at compile time and will cause compilation to fail if the node at the given address is unavailable or unresponsive.
236///
237/// ```rust,ignore
238/// #[subxt::subxt(
239/// runtime_metadata_url = "wss://rpc.polkadot.io:443"
240/// )]
241/// mod polkadot {}
242/// ```
243///
244/// ## `generate_docs`
245///
246/// By default, documentation is not generated via the macro, since IDEs do not typically make use of it. This attribute
247/// forces documentation to be generated, too.
248///
249/// ```rust,no_run
250/// #[subxt::subxt(
251/// runtime_metadata_path = "../artifacts/polkadot_metadata_full.scale",
252/// generate_docs
253/// )]
254/// mod polkadot {}
255/// ```
256///
257/// ## `runtime_types_only`
258///
259/// By default, the macro will generate various interfaces to make using Subxt simpler in addition with any types that need
260/// generating to make this possible. This attribute makes the codegen only generate the types and not the Subxt interface.
261///
262/// ```rust,no_run
263/// #[subxt::subxt(
264/// runtime_metadata_path = "../artifacts/polkadot_metadata_full.scale",
265/// runtime_types_only
266/// )]
267/// mod polkadot {}
268/// ```
269/// ## `no_default_derives`
270///
271/// By default, the macro will add all derives necessary for the generated code to play nicely with Subxt. Adding this attribute
272/// removes all default derives.
273///
274/// ```rust,no_run
275/// #[subxt::subxt(
276/// runtime_metadata_path = "../artifacts/polkadot_metadata_full.scale",
277/// runtime_types_only,
278/// no_default_derives,
279/// derive_for_all_types="codec::Encode, codec::Decode"
280/// )]
281/// mod polkadot {}
282/// ```
283///
284/// **Note**: At the moment, you must derive at least one of `codec::Encode` or `codec::Decode` or `scale_encode::EncodeAsType` or
285/// `scale_decode::DecodeAsType` (because we add `#[codec(..)]` attributes on some fields/types during codegen), and you must use this
286/// feature in conjunction with `runtime_types_only` (or manually specify a bunch of defaults to make codegen work properly when
287/// generating the subxt interfaces).
288pub use subxt_macro::subxt;