subxt_core/blocks/mod.rs
1// Copyright 2019-2024 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//! Decode and iterate over the extrinsics in block bodies.
6//!
7//! Use the [`decode_from`] function as an entry point to decoding extrinsics, and then
8//! have a look at [`Extrinsics`] and [`ExtrinsicDetails`] to see which methods are available
9//! to work with the extrinsics.
10//!
11//! # Example
12//!
13//! ```rust
14//! extern crate alloc;
15//!
16//! use subxt_macro::subxt;
17//! use subxt_core::blocks;
18//! use subxt_core::metadata;
19//! use subxt_core::config::PolkadotConfig;
20//! use alloc::vec;
21//!
22//! // If we generate types without `subxt`, we need to point to `::subxt_core`:
23//! #[subxt(
24//! crate = "::subxt_core",
25//! runtime_metadata_path = "../artifacts/polkadot_metadata_small.scale",
26//! )]
27//! pub mod polkadot {}
28//!
29//! // Some metadata we'd like to use to help us decode extrinsics:
30//! let metadata_bytes = include_bytes!("../../../artifacts/polkadot_metadata_small.scale");
31//! let metadata = metadata::decode_from(&metadata_bytes[..]).unwrap();
32//!
33//! // Some extrinsics we'd like to decode:
34//! let ext_bytes = vec![
35//! hex::decode("1004020000").unwrap(),
36//! hex::decode("c10184001cbd2d43530a44705ad088af313e18f80b53ef16b36177cd4b77b846f2a5f07c01a27c400241aeafdea1871b32f1f01e92acd272ddfe6b2f8b73b64c606572a530c470a94ef654f7baa5828474754a1fe31b59f91f6bb5c2cd5a07c22d4b8b8387350100000000001448656c6c6f").unwrap(),
37//! hex::decode("550284001cbd2d43530a44705ad088af313e18f80b53ef16b36177cd4b77b846f2a5f07c0144bb92734447c893ab16d520fae0d455257550efa28ee66bf6dc942cb8b00d5d2799b98bc2865d21812278a9a266acd7352f40742ff11a6ce1f400013961598485010000000400008eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a481700505a4f7e9f4eb106").unwrap()
38//! ];
39//!
40//! // Given some chain config and metadata, we know how to decode the bytes.
41//! let exts = blocks::decode_from::<PolkadotConfig>(ext_bytes, metadata).unwrap();
42//!
43//! // We'll see 3 extrinsics:
44//! assert_eq!(exts.len(), 3);
45//!
46//! // We can iterate over them and decode various details out of them.
47//! for ext in exts.iter() {
48//! println!("Pallet: {}", ext.pallet_name().unwrap());
49//! println!("Call: {}", ext.variant_name().unwrap());
50//! }
51//!
52//! # let ext_details: Vec<_> = exts.iter()
53//! # .map(|ext| {
54//! # let pallet = ext.pallet_name().unwrap().to_string();
55//! # let call = ext.variant_name().unwrap().to_string();
56//! # (pallet, call)
57//! # })
58//! # .collect();
59//! #
60//! # assert_eq!(ext_details, vec![
61//! # ("Timestamp".to_owned(), "set".to_owned()),
62//! # ("System".to_owned(), "remark".to_owned()),
63//! # ("Balances".to_owned(), "transfer_allow_death".to_owned()),
64//! # ]);
65//! ```
66
67mod extrinsic_transaction_extensions;
68mod extrinsics;
69mod static_extrinsic;
70
71use crate::Metadata;
72use crate::config::Config;
73use crate::error::Error;
74use alloc::vec::Vec;
75
76pub use crate::error::BlockError;
77pub use extrinsic_transaction_extensions::{
78 ExtrinsicTransactionExtension, ExtrinsicTransactionExtensions,
79};
80pub use extrinsics::{ExtrinsicDetails, ExtrinsicMetadataDetails, Extrinsics, FoundExtrinsic};
81pub use static_extrinsic::StaticExtrinsic;
82
83/// Instantiate a new [`Extrinsics`] object, given a vector containing each extrinsic hash (in the
84/// form of bytes) and some metadata that we'll use to decode them.
85///
86/// This is a shortcut for [`Extrinsics::decode_from`].
87pub fn decode_from<T: Config>(
88 extrinsics: Vec<Vec<u8>>,
89 metadata: Metadata,
90) -> Result<Extrinsics<T>, Error> {
91 Extrinsics::decode_from(extrinsics, metadata)
92}