Module storage

Module storage 

Source
Expand description

Encode storage keys, decode storage values, and validate static storage addresses.

§Example

use subxt_signer::sr25519::dev;
use subxt_macro::subxt;
use subxt_core::storage;
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 account = dev::alice().public_key().into();
let address = polkadot::storage().system().account(account);

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

// Encode the address to bytes. These can be sent to a node to query the value.
storage::get_address_bytes(&address, &metadata).unwrap();

// If we were to obtain a value back from the node at that address, we could
// then decode it using the same address and metadata like so:
let value_bytes = hex::decode("00000000000000000100000000000000000064a7b3b6e00d0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080").unwrap();
let value = storage::decode_value(&mut &*value_bytes, &address, &metadata).unwrap();

println!("Alice's account info: {value:?}");

Modules§

address
Construct addresses to access storage entries with.

Functions§

decode_value
Given some storage value that we’ve retrieved from a node, the address used to retrieve it, and metadata from the node, this function attempts to decode the bytes into the target value specified by the address.
default_value
Return the default value at a given storage address if one is available, or an error otherwise.
get_address_bytes
Given a storage address and some metadata, this encodes the address into bytes which can be handed to a node to retrieve the corresponding value.
get_address_root_bytes
Given a storage address and some metadata, this encodes the root of the address (ie the pallet and storage entry part) into bytes. If the entry being addressed is inside a map, this returns the bytes needed to iterate over all of the entries within it.
validate
When the provided address is statically generated via the #[subxt] macro, this validates that the shape of the storage value is the same as the shape expected by the static address.