Crate subxt_codegen
source ·Expand description
Library to generate an API for a Substrate runtime from its metadata.
Generated Structure
The API generator logic:
- At the root there is the item_modprovided (iepub mod api {})
- Pallets are represented by a child module (ie pub mod PalletName {}) of the root
- Each pallet exposes as child modules (if applicable):
- Calls (pub mod calls {})
- Events (pub mod events {})
- Storage (pub mod storage {})
- Constants (pub mod constants {})
 
- Calls (
Example
use std::fs;
use codec::Decode;
use subxt_metadata::Metadata;
use subxt_codegen::{CratePath, DerivesRegistry, TypeSubstitutes};
let encoded = fs::read("../artifacts/polkadot_metadata_full.scale").unwrap();
// Runtime metadata obtained from a node.
let metadata = Metadata::decode(&mut &*encoded).unwrap();
// Module under which the API is generated.
let item_mod = syn::parse_quote!(
    pub mod api {}
);
// Default module derivatives.
let mut derives = DerivesRegistry::with_default_derives(&CratePath::default());
// Default type substitutes.
let substs = TypeSubstitutes::with_default_substitutes(&CratePath::default());
// Generate the Runtime API.
let generator = subxt_codegen::RuntimeGenerator::new(metadata);
// Include metadata documentation in the Runtime API.
let generate_docs = true;
let runtime_api = generator.generate_runtime(item_mod, derives, substs, CratePath::default(), generate_docs).unwrap();
println!("{}", runtime_api);Modules
- Utilities to help with fetching and decoding metadata.
Structs
- A newtype wrapper which stores the path to the Subxt crate.
- A struct storing the set of derives and derive attributes that we’ll apply to generated types.
- A struct containing the derives that we’ll be applying to types; a combination of some common derives for all types, plus type specific derives.
- Generate the runtime API for interacting with a substrate runtime.
- Represents a Rustmod, containing generated types and childmods.
- Create the API for interacting with a Substrate runtime.
- Generates a Ruststructorenumdefinition based on the supplied [scale-info::Type].
- Represents the set of generic type parameters for generating a type definition e.g. theTinFoo<T>.
- Generate a Rust module containing all types defined in the suppliedPortableRegistry.
- A map of type substitutes. We match on the paths to generated types in order to figure out when to swap said type with some provided substitute.
Enums
- Error returned when the Codegen cannot generate the runtime API.
- Error attempting to do type substitution.