pezkuwi_subxt/
lib.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//! Subxt is a library for interacting with Bizinikiwi based nodes. Using it looks something like
6//! this:
7//!
8//! ```rust,ignore
9#![doc = include_str!("../examples/tx_basic.rs")]
10//! ```
11//!
12//! Take a look at [the Subxt guide](book) to learn more about how to use Subxt.
13
14#![cfg_attr(docsrs, feature(doc_cfg))]
15
16// Note: When both 'web' and 'native' features are enabled (e.g., --all-features),
17// 'native' takes priority. This allows CI to run with --all-features.
18#[cfg(not(any(feature = "web", feature = "native")))]
19compile_error!("subxt: at least one of the 'web' or 'native' features must be enabled.");
20
21// Internal helper macros
22#[macro_use]
23mod macros;
24
25// The guide is here.
26pub mod book;
27
28// Suppress an unused dependency warning because tokio is
29// only used in example code snippets at the time of writing.
30#[cfg(test)]
31mod only_used_in_docs_or_tests {
32	use pezkuwi_subxt_signer as _;
33	use tokio as _;
34}
35
36// Suppress an unused dependency warning because tracing_subscriber is
37// only used in example code snippets at the time of writing.
38#[cfg(test)]
39use tracing_subscriber as _;
40
41pub mod backend;
42pub mod blocks;
43pub mod client;
44pub mod constants;
45pub mod custom_values;
46pub mod error;
47pub mod events;
48pub mod runtime_api;
49pub mod storage;
50pub mod tx;
51pub mod utils;
52pub mod view_functions;
53
54/// This module provides a [`Config`] type, which is used to define various
55/// types that are important in order to speak to a particular chain.
56/// [`BizinikiwConfig`] provides a default set of these types suitable for the
57/// default Bizinikiwi node implementation, and [`PezkuwiConfig`] for a
58/// Pezkuwi node.
59pub mod config {
60	pub use pezkuwi_subxt_core::{
61		config::{
62			bizinikiwi, pezkuwi, transaction_extensions, BizinikiwConfig,
63			BizinikiwiExtrinsicParams, Config, DefaultExtrinsicParams,
64			DefaultExtrinsicParamsBuilder, ExtrinsicParams, ExtrinsicParamsEncoder, Hash, HashFor,
65			Hasher, Header, PezkuwiConfig, PezkuwiExtrinsicParams, TransactionExtension,
66		},
67		error::ExtrinsicParamsError,
68	};
69}
70
71/// Types representing the metadata obtained from a node.
72pub mod metadata {
73	pub use pezkuwi_subxt_metadata::*;
74}
75
76/// Submit dynamic transactions.
77pub mod dynamic {
78	pub use pezkuwi_subxt_core::dynamic::*;
79}
80
81// Expose light client bits
82cfg_unstable_light_client! {
83	pub use pezkuwi_subxt_lightclient as lightclient;
84}
85
86// Expose a few of the most common types at root,
87// but leave most types behind their respective modules.
88pub use crate::{
89	client::{OfflineClient, OnlineClient},
90	config::{BizinikiwConfig, Config, PezkuwiConfig},
91	error::Error,
92	metadata::Metadata,
93};
94
95/// Re-export external crates that are made use of in the subxt API.
96pub mod ext {
97	pub use codec;
98	pub use frame_metadata;
99	pub use futures;
100	pub use pezkuwi_subxt_core;
101	pub use pezkuwi_subxt_rpcs;
102	pub use scale_bits;
103	pub use scale_decode;
104	pub use scale_encode;
105	pub use scale_value;
106
107	cfg_jsonrpsee! {
108		pub use jsonrpsee;
109	}
110}
111
112/// Generate a strongly typed API for interacting with a Bizinikiwi runtime from its metadata of
113/// WASM.
114///
115/// # Metadata
116///
117/// First, you'll need to get hold of some metadata for the node you'd like to interact with.
118/// One way to do this is by using the `subxt` CLI tool:
119///
120/// ```bash
121/// # Install the CLI tool:
122/// cargo install subxt-cli
123/// # Use it to download metadata (in this case, from a node running locally)
124/// subxt metadata > pezkuwi_metadata.scale
125/// ```
126///
127/// Run `subxt metadata --help` for more options.
128///
129/// # Basic usage
130///
131/// We can generate an interface to a chain given either:
132/// - A locally saved SCALE encoded metadata file (see above) for that chain,
133/// - The Runtime WASM for that chain, or
134/// - A URL pointing at the JSON-RPC interface for a node on that chain.
135///
136/// In each case, the `subxt` macro will use this data to populate the annotated module with
137/// all of the methods and types required for interacting with the chain that the
138/// Runtime/metadata was loaded from.
139///
140/// Let's look at each of these:
141///
142/// ## Using a locally saved metadata file
143///
144/// Annotate a Rust module with the `subxt` attribute referencing a metadata file like so:
145///
146/// ```rust,no_run,standalone_crate
147/// #[pezkuwi_subxt::subxt(
148///     runtime_metadata_path = "../artifacts/pezkuwi_metadata_full.scale",
149/// )]
150/// mod pezkuwi {}
151/// ```
152///
153/// You can use the `$OUT_DIR` placeholder in the path to reference metadata generated at build
154/// time:
155///
156/// ```rust,ignore
157/// #[pezkuwi_subxt::subxt(
158///     runtime_metadata_path = "$OUT_DIR/metadata.scale",
159/// )]
160/// mod pezkuwi {}
161/// ```
162///
163/// ## Using a WASM runtime via `runtime_path = "..."`
164///
165/// This requires the `runtime-wasm-path` feature flag.
166///
167/// Annotate a Rust module with the `subxt` attribute referencing some runtime WASM like so:
168///
169/// ```rust,ignore
170/// #[pezkuwi_subxt::subxt(
171///     runtime_path = "../artifacts/zagros_runtime.wasm",
172/// )]
173/// mod pezkuwi {}
174/// ```
175///
176/// You can also use the `$OUT_DIR` placeholder in the path to reference WASM files generated
177/// at build time:
178///
179/// ```rust,ignore
180/// #[pezkuwi_subxt::subxt(
181///     runtime_path = "$OUT_DIR/runtime.wasm",
182/// )]
183/// mod pezkuwi {}
184/// ```
185///
186/// ## Connecting to a node to download metadata via `runtime_metadata_insecure_url = "..."`
187///
188/// This will, at compile time, connect to the JSON-RPC interface for some node at the URL
189/// given, download the metadata from it, and use that. This can be useful in CI, but is **not
190/// recommended** in production code, because:
191///
192/// - The compilation time is increased since we have to download metadata from a URL each
193///   time. If the node we connect to is unresponsive, this will be slow or could fail.
194/// - The metadata may change from what is expected without notice, causing compilation to fail
195///   if it leads to changes in the generated interfaces that are being used.
196/// - The node that you connect to could be malicious and provide incorrect metadata for the
197///   chain.
198///
199/// ```rust,ignore
200/// #[pezkuwi_subxt::subxt(
201///     runtime_metadata_insecure_url = "wss://rpc.pezkuwi.io:443"
202/// )]
203/// mod pezkuwi {}
204/// ```
205///
206/// # Configuration
207///
208/// This macro supports a number of attributes to configure what is generated:
209///
210/// ## `crate = "..."`
211///
212/// Use this attribute to specify a custom path to the `pezkuwi_subxt_core` crate:
213///
214/// ```rust,standalone_crate
215/// # pub extern crate pezkuwi_subxt_core;
216/// # pub mod path { pub mod to { pub use pezkuwi_subxt_core; } }
217/// # fn main() {}
218/// #[pezkuwi_subxt::subxt(
219///     runtime_metadata_path = "../artifacts/pezkuwi_metadata_full.scale",
220///     crate = "crate::path::to::pezkuwi_subxt_core"
221/// )]
222/// mod pezkuwi {}
223/// ```
224///
225/// This is useful if you write a library which uses this macro, but don't want to force users
226/// to depend on `subxt` at the top level too. By default the path `::subxt` is used.
227///
228/// ## `substitute_type(path = "...", with = "...")`
229///
230/// This attribute replaces any reference to the generated type at the path given by `path`
231/// with a reference to the path given by `with`.
232///
233/// ```rust,standalone_crate
234/// #[pezkuwi_subxt::subxt(
235///     runtime_metadata_path = "../artifacts/pezkuwi_metadata_full.scale",
236///     substitute_type(path = "sp_arithmetic::per_things::Perbill", with = "crate::Foo")
237/// )]
238/// mod pezkuwi {}
239///
240/// # #[derive(
241/// #     scale_encode::EncodeAsType,
242/// #     scale_decode::DecodeAsType,
243/// #     codec::Encode,
244/// #     codec::Decode,
245/// #     Clone,
246/// #     Debug,
247/// # )]
248/// // In reality this needs some traits implementing on
249/// // it to allow it to be used in place of Perbill:
250/// pub struct Foo(u32);
251/// # impl codec::CompactAs for Foo {
252/// #     type As = u32;
253/// #     fn encode_as(&self) -> &Self::As {
254/// #         &self.0
255/// #     }
256/// #     fn decode_from(x: Self::As) -> Result<Self, codec::Error> {
257/// #         Ok(Foo(x))
258/// #     }
259/// # }
260/// # impl From<codec::Compact<Foo>> for Foo {
261/// #     fn from(v: codec::Compact<Foo>) -> Foo {
262/// #         v.0
263/// #     }
264/// # }
265/// # fn main() {}
266/// ```
267///
268/// If the type you're substituting contains generic parameters, you can "pattern match" on
269/// those, and make use of them in the substituted type, like so:
270///
271/// ```rust,no_run,standalone_crate
272/// #[pezkuwi_subxt::subxt(
273///     runtime_metadata_path = "../artifacts/pezkuwi_metadata_full.scale",
274///     substitute_type(
275///         path = "sp_runtime::multiaddress::MultiAddress<A, B>",
276///         with = "::pezkuwi_subxt::utils::Static<sp_runtime::MultiAddress<A, B>>"
277///     )
278/// )]
279/// mod pezkuwi {}
280/// ```
281///
282/// The above is also an example of using the [`crate::utils::Static`] type to wrap some type
283/// which doesn't on it's own implement [`scale_encode::EncodeAsType`] or
284/// [`scale_decode::DecodeAsType`], which are required traits for any substitute type to
285/// implement by default.
286///
287/// ## `derive_for_all_types = "..."`
288///
289/// By default, all generated types derive a small set of traits. This attribute allows you to
290/// derive additional traits on all generated types:
291///
292/// ```rust,no_run,standalone_crate
293/// #[pezkuwi_subxt::subxt(
294///     runtime_metadata_path = "../artifacts/pezkuwi_metadata_full.scale",
295///     derive_for_all_types = "Eq, PartialEq"
296/// )]
297/// mod pezkuwi {}
298/// ```
299///
300/// Any substituted types (including the default substitutes) must also implement these traits
301/// in order to avoid errors here.
302///
303/// ## `derive_for_type(path = "...", derive = "...")`
304///
305/// Unlike the above, which derives some trait on every generated type, this attribute allows
306/// you to derive traits only for specific types. Note that any types which are used inside the
307/// specified type may also need to derive the same traits.
308///
309/// ```rust,no_run,standalone_crate
310/// #[pezkuwi_subxt::subxt(
311///     runtime_metadata_path = "../artifacts/pezkuwi_metadata_full.scale",
312///     derive_for_all_types = "Eq, PartialEq",
313///     derive_for_type(path = "frame_support::PalletId", derive = "Ord, PartialOrd"),
314///     derive_for_type(path = "sp_runtime::ModuleError", derive = "Hash"),
315/// )]
316/// mod pezkuwi {}
317/// ```
318///
319/// ## `generate_docs`
320///
321/// By default, documentation is not generated via the macro, since IDEs do not typically make
322/// use of it. This attribute forces documentation to be generated, too.
323///
324/// ```rust,no_run,standalone_crate
325/// #[pezkuwi_subxt::subxt(
326///     runtime_metadata_path = "../artifacts/pezkuwi_metadata_full.scale",
327///     generate_docs
328/// )]
329/// mod pezkuwi {}
330/// ```
331///
332/// ## `runtime_types_only`
333///
334/// By default, the macro will generate various interfaces to make using Subxt simpler in
335/// addition with any types that need generating to make this possible. This attribute makes
336/// the codegen only generate the types and not the Subxt interface.
337///
338/// ```rust,no_run,standalone_crate
339/// #[pezkuwi_subxt::subxt(
340///     runtime_metadata_path = "../artifacts/pezkuwi_metadata_full.scale",
341///     runtime_types_only
342/// )]
343/// mod pezkuwi {}
344/// ```
345///
346/// ## `no_default_derives`
347///
348/// By default, the macro will add all derives necessary for the generated code to play nicely
349/// with Subxt. Adding this attribute removes all default derives.
350///
351/// ```rust,no_run,standalone_crate
352/// #[pezkuwi_subxt::subxt(
353///     runtime_metadata_path = "../artifacts/pezkuwi_metadata_full.scale",
354///     runtime_types_only,
355///     no_default_derives,
356///     derive_for_all_types="codec::Encode, codec::Decode"
357/// )]
358/// mod pezkuwi {}
359/// ```
360///
361/// **Note**: At the moment, you must derive at least one of `codec::Encode` or `codec::Decode`
362/// or `scale_encode::EncodeAsType` or `scale_decode::DecodeAsType` (because we add
363/// `#[codec(..)]` attributes on some fields/types during codegen), and you must use this
364/// feature in conjunction with `runtime_types_only` (or manually specify a bunch of defaults
365/// to make codegen work properly when generating the subxt interfaces).
366///
367/// ## `unstable_metadata`
368///
369/// This attribute works only in combination with `runtime_metadata_insecure_url`. By default,
370/// the macro will fetch the latest stable version of the metadata from the target node. This
371/// attribute makes the codegen attempt to fetch the unstable version of the metadata first.
372/// This is **not recommended** in production code, since the unstable metadata a node is
373/// providing is likely to be incompatible with Subxt.
374///
375/// ```rust,ignore
376/// #[pezkuwi_subxt::subxt(
377///     runtime_metadata_insecure_url = "wss://rpc.pezkuwi.io:443",
378///     unstable_metadata
379/// )]
380/// mod pezkuwi {}
381/// ```
382pub use pezkuwi_subxt_macro::subxt;