diff --git a/examples/examples/rpc_call_subscribe_blocks.rs b/examples/examples/rpc_call_subscribe_blocks.rs new file mode 100644 index 0000000000..f28821b409 --- /dev/null +++ b/examples/examples/rpc_call_subscribe_blocks.rs @@ -0,0 +1,65 @@ +// Copyright 2019-2022 Parity Technologies (UK) Ltd. +// This file is part of subxt. +// +// subxt is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// subxt is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with subxt. If not, see . + +//! To run this example, a local polkadot node should be running. Example verified against polkadot 0.9.18-f6d6ab005d-aarch64-macos. +//! +//! E.g. +//! ```bash +//! curl "https://github.com/paritytech/polkadot/releases/download/v0.9.13/polkadot" --output /usr/local/bin/polkadot --location +//! polkadot --dev --tmp +//! ``` + +use subxt::{ + rpc::Subscription, + sp_runtime::{ + generic::Header, + traits::BlakeTwo256, + }, + ClientBuilder, + DefaultConfig, + PolkadotExtrinsicParams, +}; + +#[subxt::subxt(runtime_metadata_path = "../artifacts/polkadot_metadata.scale")] +pub mod polkadot {} + +#[tokio::main] +async fn main() -> Result<(), Box> { + tracing_subscriber::fmt::init(); + + let api = ClientBuilder::new() + .set_url("wss://rpc.polkadot.io:443") + .build() + .await? + .to_runtime_api::>>(); + + // For non-finalised blocks use `.subscribe_blocks()` + let mut blocks: Subscription> = + api.client.rpc().subscribe_finalized_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 + ); + } + + Ok(()) +}