The example here https://github.com/paritytech/subxt/blob/master/subxt/examples/storage_iterating.rs
use subxt::{OnlineClient, PolkadotConfig};
#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata_full.scale")]
pub mod polkadot {}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a new API client, configured to talk to Polkadot nodes.
let api = OnlineClient::<PolkadotConfig>::new().await?;
// Build a storage query to iterate over account information.
let storage_query = polkadot::storage().system().account_iter();
// Get back an iterator of results (here, we are fetching 10 items at
// a time from the node, but we always iterate over one at a time).
let mut results = api.storage().at_latest().await?.iter(storage_query).await?;
while let Some(Ok((key, value))) = results.next().await {
println!("Key: 0x{}", hex::encode(&key));
println!("Value: {:?}", value);
}
Ok(())
}
Shows how to iterate over a storage map and get typed results. However, only the values are typed in this case and I didn't find a way to get a typed key
value as well. Instead I have to decode into AccountId
using a code like:
let account_bytes = key[40..].to_vec();
let array_u8: [u8; 32] = account_bytes.as_slice().try_into().unwrap();
let account = AccountId32::from(array_u8);
Is there a better way?