diff --git a/bin/archive-demo/src/main.rs b/bin/archive-demo/src/main.rs index 4d1c6bb..2a7ed81 100644 --- a/bin/archive-demo/src/main.rs +++ b/bin/archive-demo/src/main.rs @@ -1,51 +1,55 @@ +use desub::{Chain, Decoder}; use subxt::{ - config::PolkadotConfig, - backend::{ legacy::{ LegacyRpcMethods, rpc_methods::{ NumberOrHex, Bytes } }, rpc::{ RpcClient, rpc_params } }, ext::codec::Decode + backend::{ + legacy::{ + rpc_methods::{Bytes, NumberOrHex}, + LegacyRpcMethods, + }, + rpc::{rpc_params, RpcClient}, + }, + config::PolkadotConfig, }; -use desub::{ Decoder, Chain }; #[tokio::main] async fn main() -> Result<(), anyhow::Error> { pretty_env_logger::init(); - // Connect to a node with an RPC client: - let rpc_client = RpcClient::from_url("wss://rpc.polkadot.io").await?; - let methods = LegacyRpcMethods::::new(rpc_client.clone()); + // Connect to a node with an RPC client: + let rpc_client = RpcClient::from_url("wss://rpc.polkadot.io").await?; + let methods = LegacyRpcMethods::::new(rpc_client.clone()); - let mut bn = 1; - let mut decoder = Decoder::new(Chain::Polkadot); + let mut bn = 1; + let mut decoder = Decoder::new(Chain::Polkadot); - loop { - let hash = methods.chain_get_block_hash(Some(NumberOrHex::Number(bn))).await?.unwrap(); - let runtime_version = methods.state_get_runtime_version(Some(hash)).await?; - let spec_version = runtime_version.spec_version; - let block = methods.chain_get_block(Some(hash)).await?.unwrap(); + loop { + let hash = methods.chain_get_block_hash(Some(NumberOrHex::Number(bn))).await?.unwrap(); + let runtime_version = methods.state_get_runtime_version(Some(hash)).await?; + let spec_version = runtime_version.spec_version; + let block = methods.chain_get_block(Some(hash)).await?.unwrap(); - let ext_bytes = make_extrinsic_bytes(block.block.extrinsics); + let ext_bytes = make_extrinsic_bytes(block.block.extrinsics); - if !decoder.has_version(spec_version) { - // download the relevant metadata bytes, since the decoder doesn't have it yet. - let md: Bytes = rpc_client - .request("state_getMetadata", rpc_params![hash]) - .await?; - decoder.register_version(spec_version, &md.0)?; - } + if !decoder.has_version(spec_version) { + // download the relevant metadata bytes, since the decoder doesn't have it yet. + let md: Bytes = rpc_client.request("state_getMetadata", rpc_params![hash]).await?; + decoder.register_version(spec_version, &md.0)?; + } - println!("# Decoding exts for block {bn}"); - let decoded_exts = decoder.decode_extrinsics(spec_version, &ext_bytes)?; + println!("# Decoding exts for block {bn}"); + let decoded_exts = decoder.decode_extrinsics(spec_version, &ext_bytes)?; - println!("{decoded_exts}"); - bn += 10_000; - } + println!("{decoded_exts}"); + bn += 10_000; + } } // A hack because we get the exts back as a vec of bytes and // desub wants the whole block body as bytes. fn make_extrinsic_bytes(exts: Vec) -> Vec { - use subxt::utils::Encoded; - use subxt::ext::codec::Encode; - // The inner `Bytes` are already encoded and contain the compact length etc, - // so don't encode them again by wrapping them in `Encoded`. - let e: Vec = exts.into_iter().map(|ext| Encoded(ext.0)).collect(); - e.encode() -} \ No newline at end of file + use subxt::ext::codec::Encode; + use subxt::utils::Encoded; + // The inner `Bytes` are already encoded and contain the compact length etc, + // so don't encode them again by wrapping them in `Encoded`. + let e: Vec = exts.into_iter().map(|ext| Encoded(ext.0)).collect(); + e.encode() +}