Skip to content

Commit dc2a73d

Browse files
authored
Wrap and export BlockNumber (paritytech#87)
* Add fetch example and wrap BlockNumber impl * Fmt
1 parent 8cf6526 commit dc2a73d

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

examples/fetch_remote.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2020 Parity Technologies (UK) Ltd.
2+
// This file is part of substrate-subxt.
3+
//
4+
// subxt is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// subxt is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
16+
17+
use substrate_subxt::{
18+
system::System,
19+
Error,
20+
KusamaRuntime,
21+
};
22+
23+
fn main() {
24+
async_std::task::block_on(async move {
25+
env_logger::init();
26+
27+
let block_hash = fetch_block_hash(1).await;
28+
match block_hash {
29+
Ok(Some(hash)) => println!("Block hash for block number 1: {}", hash),
30+
Ok(None) => println!("Block number 1 not found."),
31+
Err(_) => eprintln!("Failed to fetch block hash"),
32+
}
33+
});
34+
}
35+
36+
async fn fetch_block_hash(
37+
block_number: u32,
38+
) -> Result<Option<<KusamaRuntime as System>::Hash>, Error> {
39+
substrate_subxt::ClientBuilder::<KusamaRuntime>::new()
40+
.set_url("wss://kusama-rpc.polkadot.io")
41+
.build()
42+
.await?
43+
.block_hash(Some(block_number.into()))
44+
.await
45+
}

src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ pub use self::{
8585
events::RawEvent,
8686
extrinsic::*,
8787
frame::*,
88-
rpc::ExtrinsicSuccess,
88+
rpc::{
89+
BlockNumber,
90+
ExtrinsicSuccess,
91+
},
8992
runtimes::*,
9093
};
9194
use self::{
@@ -104,7 +107,6 @@ use self::{
104107
},
105108
metadata::Metadata,
106109
rpc::{
107-
BlockNumber,
108110
ChainBlock,
109111
Rpc,
110112
},

src/rpc.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use jsonrpsee::{
3838
use num_traits::bounds::Bounded;
3939

4040
use frame_metadata::RuntimeMetadataPrefixed;
41+
use serde::Serialize;
4142
use sp_core::{
4243
storage::{
4344
StorageChangeSet,
@@ -82,7 +83,30 @@ use crate::{
8283

8384
pub type ChainBlock<T> =
8485
SignedBlock<Block<<T as System>::Header, <T as System>::Extrinsic>>;
85-
pub type BlockNumber<T> = NumberOrHex<<T as System>::BlockNumber>;
86+
87+
/// Wrapper for NumberOrHex to allow custom From impls
88+
#[derive(Serialize)]
89+
#[serde(bound = "<T as System>::BlockNumber: Serialize")]
90+
pub struct BlockNumber<T: System>(NumberOrHex<<T as System>::BlockNumber>);
91+
92+
impl<T> From<NumberOrHex<<T as System>::BlockNumber>> for BlockNumber<T>
93+
where
94+
T: System,
95+
{
96+
fn from(x: NumberOrHex<<T as System>::BlockNumber>) -> Self {
97+
BlockNumber(x)
98+
}
99+
}
100+
101+
impl<T> From<u32> for BlockNumber<T>
102+
where
103+
T: System,
104+
<T as System>::BlockNumber: From<u32>,
105+
{
106+
fn from(x: u32) -> Self {
107+
NumberOrHex::Number(x.into()).into()
108+
}
109+
}
86110

87111
/// Client for substrate rpc interfaces
88112
#[derive(Clone)]

0 commit comments

Comments
 (0)