0

How to get all extrinsics executed in a block with subxt?

My question is identical to With subxt how can I subscribe to watch extrinsics?

There's some great examples of how to use subxt to listen to all events.

Polkadot-js events screen seem to show events grouped by the extrinsic that created them. Can I do the same in rust?

but the accepted answer 3 leaves me hanging at

  1. Map the extracted index to the extrinsic, as found in the block

Where do I find this list of extrinsics in the block? Should I fetch the full block with a new rpc call like this?

let mut blocks: Subscription<Header<u32, BlakeTwo256>> =
    api.client.rpc().subscribe_blocks().await?;

while let Some(Ok(block)) = blocks.next().await {

    println!(
        "block number: {:?} hash:{:?} parent:{:?} state root:{:?} extrinsics root:{:?}",
        block.number,
        block.hash(),
        block.parent_hash,
        block.state_root,
        block.extrinsics_root
    ); 
    if let Ok(Some(fullblock)) = api.client.rpc().block(Some(block.hash())).await
        {             
        // block.
        for extr in fullblock.block.extrinsics {
               
            println!("Hello Extrinsics {:?}",extr);
        }
    }
}

if so what should I decode extr:OpaqueExtrinsic with the value 0401000ba5620d6d8201

enter image description here

in to a type and encoded hash 0x8d5c44aba83278ee050e7595ba1e31ae9c0cc44ab4e02d0b55f83f07809556f3 ?

3 Answers 3

1

At the time of writing, the master branch of Subxt now supports asking for (recent) blocks and iterating over the extrinsics and their associated events; check out this example for more:

https://github.com/paritytech/subxt/blob/33a9ec91afd16c459871e24ae18576f7afbb87c1/examples/examples/subscribe_blocks.rs

Essentially something like:

let block = api.blocks().at(block_hash).await?;
let body = block.body().await?;

for ext in body.extrinsics() {
    // print the bytes in each extrinsic:
    println!("0x{}", hex::encode(ext.bytes()));
}

This will give you back 0x prefixed hex encoded bytes for each extrinsic in the block, which you can do as you wish with.

2

OpaqueExtrinsic implements the trait Encode one could then use the BlakeTwo256::hash_of() function to calculate the hash value of the extrinsics.

use codec::Encode;

fn blake_two_256_hash<T>(data: T) -> H256
where
    T: Encode,
{
    let ans: H256 = BlakeTwo256::hash_of(&data);
    ans
}

And then fetch the full blocks as they are finalized:

let mut blocks: Subscription<Header<u32, BlakeTwo256>> =
    api.client.rpc().subscribe_blocks().await?;

while let Some(Ok(block)) = blocks.next().await {
    if let Ok(Some(fullblock)) = api.client.rpc().block(Some(block.hash())).await {
        for extr in fullblock.block.extrinsics {
           /// extr is an OpaqueExtrinsic
            println!("Got extrinsic Hash: {:?}",  blake_two_256_hash(&extr));
        }
    }
}
0

You can use the libuptest crate to extract the extrinsic: code example: https://uptest-sc.github.io/cli/event_sub.html#code-example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.