Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend the new api.blocks() to be the primary way to subscribe and fetch blocks/extrinsics/events #691

Merged
merged 14 commits into from
Nov 1, 2022
Merged
1 change: 0 additions & 1 deletion examples/examples/custom_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ impl Config for MyConfig {
type Address = <SubstrateConfig as Config>::Address;
type Header = <SubstrateConfig as Config>::Header;
type Signature = <SubstrateConfig as Config>::Signature;
type Extrinsic = <SubstrateConfig as Config>::Extrinsic;
// ExtrinsicParams makes use of the index type, so we need to adjust it
// too to align with our modified index type, above:
type ExtrinsicParams = SubstrateExtrinsicParams<Self>;
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/rpc_call_subscribe_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

// For non-finalised blocks use `.subscribe_blocks()`
let mut blocks: Subscription<Header<u32, BlakeTwo256>> =
api.rpc().subscribe_finalized_blocks().await?;
api.rpc().subscribe_finalized_block_headers().await?;

while let Some(Ok(block)) = blocks.next().await {
println!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a client to use:
let api = OnlineClient::<PolkadotConfig>::new().await?;

// Subscribe to any events that occur:
let mut event_sub = api.events().subscribe().await?;
// Subscribe to (in this case, finalized) blocks.
let mut block_sub = api.blocks().subscribe_finalized().await?;

// While this subscription is active, balance transfers are made somewhere:
tokio::task::spawn({
Expand All @@ -58,10 +58,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
});

// Our subscription will see the events emitted as a result of this:
while let Some(events) = event_sub.next().await {
let events = events?;
let block_hash = events.block_hash();
// Get each finalized block as it arrives.
while let Some(block) = block_sub.next().await {
let block = block?;

// Ask for the events for this block.
let events = block.events().await?;

let block_hash = block.hash();

// We can dynamically decode events:
println!(" Dynamic event details: {block_hash:?}:");
Expand Down
64 changes: 64 additions & 0 deletions examples/examples/subscribe_blocks.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2019-2022 Parity Technologies (UK) Ltd.
// This file is dual-licensed as Apache-2.0 or GPL-3.0.
// see LICENSE for license details.

//! To run this example, a local polkadot node should be running. Example verified against polkadot 0.9.29-41a9d84b152.
//!
//! E.g.
//! ```bash
//! curl "https://github.com/paritytech/polkadot/releases/download/v0.9.29/polkadot" --output /usr/local/bin/polkadot --location
//! polkadot --dev --tmp
//! ```

use futures::StreamExt;
use subxt::{
OnlineClient,
PolkadotConfig,
};

#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata.scale")]
pub mod polkadot {}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();

// Create a client to use:
let api = OnlineClient::<PolkadotConfig>::new().await?;

// Subscribe to all finalized blocks:
let mut blocks_sub = api.blocks().subscribe_finalized().await?;

while let Some(block) = blocks_sub.next().await {
let block = block?;

let block_number = block.header().number;
let block_hash = block.hash();

println!("Block #{block_number}:");
println!(" Hash: {block_hash}");
println!(" Extrinsics:");

let body = block.body().await?;
for ext in body.extrinsics() {
let idx = ext.index();
let events = ext.events().await?;
let bytes_hex = format!("0x{}", hex::encode(ext.bytes()));

println!(" Extrinsic #{idx}:");
println!(" Bytes: {bytes_hex}");
println!(" Events:");

for evt in events.iter() {
let evt = evt?;

let pallet_name = evt.pallet_name();
let event_name = evt.variant_name();

println!(" {pallet_name}_{event_name}");
}
}
}

Ok(())
}
72 changes: 0 additions & 72 deletions examples/examples/subscribe_one_event.rs

This file was deleted.

87 changes: 0 additions & 87 deletions examples/examples/subscribe_some_events.rs

This file was deleted.

Loading