Module runtime_api

Source
Expand description

Encode runtime API payloads, decode the associated values returned from them, and validate static runtime API payloads.

§Example

use subxt_macro::subxt;
use subxt_core::runtime_api;
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'll use to work with storage entries:
let metadata_bytes = include_bytes!("../../../artifacts/polkadot_metadata_small.scale");
let metadata = metadata::decode_from(&metadata_bytes[..]).unwrap();

// Build a storage query to access account information.
let payload = polkadot::apis().metadata().metadata_versions();

// We can validate that the payload is compatible with the given metadata.
runtime_api::validate(&payload, &metadata).unwrap();

// Encode the payload name and arguments to hand to a node:
let _call_name = runtime_api::call_name(&payload);
let _call_args = runtime_api::call_args(&payload, &metadata).unwrap();

// If we were to obtain a value back from the node, we could
// then decode it using the same payload and metadata like so:
let value_bytes = hex::decode("080e0000000f000000").unwrap();
let value = runtime_api::decode_value(&mut &*value_bytes, &payload, &metadata).unwrap();

println!("Available metadata versions: {value:?}");

Modules§

payload
This module contains the trait and types used to represent runtime API calls that can be made.

Functions§

call_args
Return the encoded call args given a runtime API payload.
call_name
Return the name of the runtime API call from the payload.
decode_value
Decode the value bytes at the location given by the provided runtime API payload.
validate
Run the validation logic against some runtime API payload you’d like to use. Returns Ok(()) if the payload is valid (or if it’s not possible to check since the payload has no validation hash). Return an error if the payload was not valid or something went wrong trying to validate it (ie the runtime API in question do not exist at all)