I would like to inspect transactions (extrinsics) in a given block to potentially:
- Find different types of transactions and their parameters
- Look through all transactions included in the block and act on them based on type, sender etc.
So far I am able to query the block using subtx
and see all transactions in raw form. I do not know how can I decode them to Rust types and act on them:
#[subxt::subxt(runtime_metadata_path = "../specs/kusama.spec.scale")]
pub mod kusama_client {}
...
let api = OnlineClient::<PolkadotConfig>::from_url(rpc_url)
.await.unwrap();
let block = api.rpc().block(None).await.unwrap().unwrap();
for tx in block.block.extrinsics {
println!("tx: {:?}", tx);
// TODO: Decode transactions and act on them based on types etc...
// ex. ImOnline etc...
}
I know that the transactions are SCALE encoded, but I do not know what to decode them to Rust types, I do not even know what Rust types to use (I assume those generated from metadata, but how do I find appropriate type to decode to?).
I would also like to do the similar thing with other construct such as offences:
let offences_addr = kusama_client::storage().offences().reports_by_kind_index(offence_kind);
let offences = api
.storage()
.at(None)
.await.unwrap()
.fetch(&offences_addr)
.await.unwrap()
.unwrap();
// TODO: decode and inspect offences
I would appreciate any pointers in the right direction, or examples if such exists.
#[subxt::subxt(runtime_metadata_path = "...")]
doesn't generate code anywhere (ie like other macros, you can usecargo expand
to see the generated output, but it's generated during compilation. You can use the CLI tool to generate code instead if preferred (ie runsubxt codegen
aftercargo install subxt-cli
).