From 3baea1999ef8289b97a39ebd04f0043e656d07fa Mon Sep 17 00:00:00 2001 From: Squirrel Date: Fri, 17 Jun 2022 15:38:40 +0100 Subject: [PATCH] added subscribe blocks example (#521) * added subscribe blocks example * print hash out to match parent hash * Update examples/examples/rpc_call_subscribe_blocks.rs Co-authored-by: Alexandru Vasile <60601340+lexnv@users.noreply.github.com> * fmt + point to moved metadata * tracing_subscriber now Co-authored-by: Alexandru Vasile <60601340+lexnv@users.noreply.github.com> Co-authored-by: James Wilson --- .../examples/rpc_call_subscribe_blocks.rs | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 examples/examples/rpc_call_subscribe_blocks.rs 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(()) +}