Module constants

Module constants 

Source
Expand description

Access constants from metadata.

Use get to retrieve a constant from some metadata, or validate to check that a static constant address lines up with the value seen in the metadata.

§Example

use subxt_macro::subxt;
use subxt_core::constants;
use subxt_core::metadata;

// If we generate types without `subxt`, we need to point to `::subxt_core`:
#[subxt(
    crate = "::subxt_core",
    runtime_metadata_path = "../artifacts/polkadot_metadata_small.scale",
)]
pub mod polkadot {}

// Some metadata we'd like to access constants in:
let metadata_bytes = include_bytes!("../../../artifacts/polkadot_metadata_small.scale");
let metadata = metadata::decode_from(&metadata_bytes[..]).unwrap();

// We can use a static address to obtain some constant:
let address = polkadot::constants().balances().existential_deposit();

// This validates that the address given is in line with the metadata
// we're trying to access the constant in:
constants::validate(&address, &metadata).expect("is valid");

// This acquires the constant (and internally also validates it):
let ed = constants::get(&address, &metadata).expect("can decode constant");

assert_eq!(ed, 33_333_333);

Modules§

address
Construct addresses to access constants with.

Functions§

get
Fetch a constant out of the metadata given a constant address. If the address has been statically generated, this will validate that the constant shape is as expected, too.
validate
When the provided address is statically generated via the #[subxt] macro, this validates that the shape of the constant value is the same as the shape expected by the static address.