forked from availproject/avail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retrieve_data_subscription.rs
58 lines (47 loc) · 1.47 KB
/
retrieve_data_subscription.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use super::local_connection;
use avail_subxt::{api::data_availability::calls::types::SubmitData, primitives::CheckAppId};
use anyhow::Result;
use test_log::test;
use tracing::trace;
/// This example demonstrates how to listen to blocks (we'll listen to 5 blocks) and list data submissions and their AppIds.
/// By default, no data submission will be made, to see something displayed here, you need to send data blobs with your local node.
#[test(tokio::test)]
async fn test() -> Result<()> {
let client = local_connection().await?;
let total_blocks = 5;
let mut block_count = 0;
let mut blocks_sub = client.blocks().subscribe_finalized().await?;
while let Some(block) = blocks_sub.next().await {
let block = block?;
let block_hash = block.hash();
let extrinsics = block.extrinsics().await?;
let da_submissions = extrinsics.find::<SubmitData>();
let mut found = false;
for da_submission in da_submissions {
let da_submission = da_submission?;
found = true;
let submitted_data = String::from_utf8(da_submission.value.data.0)?;
let app_id = da_submission
.details
.signed_extensions()
.unwrap()
.find::<CheckAppId>()
.unwrap()
.unwrap();
trace!(
"DA submission found in block: {} \nSubmitted Data: {:?} \nApp Id: {:?}",
block_hash,
submitted_data,
app_id
);
}
if !found {
trace!("No DA submission found in block: {}", block_hash);
}
block_count += 1;
if block_count >= total_blocks {
break;
}
}
Ok(())
}