mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-22 23:08:00 +00:00
3807b29f36
* rpc: stabilize ChainHeadBackend * remove noise from example * add missing features * make tests compile * make tests compile v2 * revert stop event * feature-gate runtime * Update subxt/Cargo.toml * add docsrs feature stuff * Update subxt/src/backend/chain_head/mod.rs * Update subxt/src/backend/chain_head/mod.rs * Update subxt/src/backend/chain_head/mod.rs
36 lines
1.2 KiB
Rust
36 lines
1.2 KiB
Rust
//! Example to utilize the ChainHeadBackend rpc backend to subscribe to finalized blocks.
|
|
|
|
#![allow(missing_docs)]
|
|
|
|
use futures::StreamExt;
|
|
use subxt::backend::chain_head::{ChainHeadBackend, ChainHeadBackendBuilder};
|
|
use subxt::backend::rpc::RpcClient;
|
|
use subxt::{OnlineClient, PolkadotConfig};
|
|
|
|
// Generate an interface that we can use from the node's metadata.
|
|
#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata_small.scale")]
|
|
pub mod polkadot {}
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
tracing_subscriber::fmt::init();
|
|
|
|
let rpc = RpcClient::from_url("ws://localhost:9944".to_string()).await?;
|
|
let backend: ChainHeadBackend<PolkadotConfig> =
|
|
ChainHeadBackendBuilder::default().build_with_background_driver(rpc.clone());
|
|
let api = OnlineClient::from_backend(std::sync::Arc::new(backend)).await?;
|
|
|
|
let mut blocks_sub = api.blocks().subscribe_finalized().await?.take(100);
|
|
|
|
while let Some(block) = blocks_sub.next().await {
|
|
let block = block?;
|
|
|
|
let block_number = block.number();
|
|
let block_hash = block.hash();
|
|
|
|
println!("Block #{block_number} ({block_hash})");
|
|
}
|
|
|
|
Ok(())
|
|
}
|