14 breaking releases

0.28.0 Apr 12, 2023
0.27.1 Feb 15, 2023
0.26.0 Jan 24, 2023
0.25.0 Nov 16, 2022
0.2.0 Nov 15, 2021

#2259 in Magic Beans

Download history 2710/week @ 2023-02-03 3046/week @ 2023-02-10 2548/week @ 2023-02-17 2600/week @ 2023-02-24 3179/week @ 2023-03-03 3105/week @ 2023-03-10 3483/week @ 2023-03-17 3660/week @ 2023-03-24 3160/week @ 2023-03-31 2570/week @ 2023-04-07 3957/week @ 2023-04-14 3867/week @ 2023-04-21 3158/week @ 2023-04-28 3584/week @ 2023-05-05 4194/week @ 2023-05-12 2923/week @ 2023-05-19

14,514 downloads per month
Used in 15 crates (3 directly)

Apache-2.0 OR GPL-3.0

200KB
4.5K SLoC

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_mod provided (ie pub 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 {})

Example

use std::fs;
use codec::Decode;
use frame_metadata::RuntimeMetadataPrefixed;
use subxt_codegen::{CratePath, DerivesRegistry, TypeSubstitutes};

let encoded = fs::read("../artifacts/polkadot_metadata.scale").unwrap();

// Runtime metadata obtained from a node.
let metadata = <RuntimeMetadataPrefixed as Decode>::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::new(&CratePath::default());
// Default type substitutes.
let substs = TypeSubstitutes::new(&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);

Dependencies

~18–27MB
~529K SLoC