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

subxt: update to version 0.33 #25

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "blockstats"
version = "0.2.0"
version = "0.3.0"
edition = "2021"
rust-version = "1.56.1"
authors = ["Parity Technologies <admin@parity.io>"]
Expand All @@ -14,7 +14,7 @@ include = ["src/**/*", "LICENSE", "README.md"]
[dependencies]
codec = { package = "parity-scale-codec", version = "3" }
futures = "0.3"
subxt = "0.28"
subxt = { version = "0.33", features = ["substrate-compat"] }

[dev-dependencies]
clap = { version = "4", features = ["derive"] }
Expand Down
28 changes: 16 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

use core::ops::Add;
use futures::{TryStream, TryStreamExt};
use std::{boxed::Box, fmt, sync::Arc};
use std::{boxed::Box, fmt};
use subxt::{
backend::{legacy::LegacyRpcMethods, rpc::RpcClient},
error::MetadataError,
ext::{scale_decode, sp_core::H256},
storage::{address::StaticStorageMapKey, address::Yes, Address},
Error, OnlineClient, PolkadotConfig as DefaultConfig,
Expand Down Expand Up @@ -75,28 +77,31 @@ impl fmt::Display for BlockStats {
pub async fn subscribe_stats(
url: &str,
) -> Result<impl TryStream<Ok = BlockStats, Error = Error> + Unpin, Error> {
let client = OnlineClient::<DefaultConfig>::from_url(url).await?;
subscribe_stats_with_client(client).await
let rpc_client = RpcClient::from_url(url).await?;
subscribe_stats_with_client(rpc_client).await
}

/// Connect to the specified node and listen for new blocks using OnlineClient.
pub async fn subscribe_stats_with_client(
client: OnlineClient<DefaultConfig>,
rpc_client: RpcClient,
) -> Result<impl TryStream<Ok = BlockStats, Error = Error> + Unpin, Error> {
let client = Arc::new(client);

let client = OnlineClient::<DefaultConfig>::from_rpc_client(rpc_client.clone()).await?;
let blocks = client.blocks().subscribe_best().await?;

let max_block_weights: BlockWeights = {
let metadata = client.metadata();
let pallet = metadata.pallet("System")?;
let constant = pallet.constant("BlockWeights")?;
codec::Decode::decode(&mut &constant.value[..])?
let pallet = metadata.pallet_by_name_err("System")?;
let constant_name = "BlockWeights";
let constant = pallet
.constant_by_name(constant_name)
.ok_or_else(|| MetadataError::ConstantNameNotFound(constant_name.to_owned()))?;
codec::Decode::decode(&mut &constant.value()[..])?
};

Ok(Box::pin(blocks.map_err(Into::into).and_then(
move |block| {
let client = client.clone();
let rpc_methods = LegacyRpcMethods::<DefaultConfig>::new(rpc_client.clone());

let block_weight_address =
Address::<StaticStorageMapKey, PerDispatchClass<Weight>, Yes, Yes, ()>::new_static(
Expand All @@ -107,9 +112,8 @@ pub async fn subscribe_stats_with_client(
)
.unvalidated();
async move {
let stats = client
.rpc()
.block_stats(block.hash())
let stats = rpc_methods
.dev_get_block_stats(block.hash())
.await?
.ok_or_else(|| Error::Other("Block not available.".to_string()))?;
let weight = client
Expand Down
Loading