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