diff --git a/examples/fetch_remote.rs b/examples/fetch_remote.rs new file mode 100644 index 0000000000..8454daf3cc --- /dev/null +++ b/examples/fetch_remote.rs @@ -0,0 +1,45 @@ +// Copyright 2020 Parity Technologies (UK) Ltd. +// This file is part of substrate-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 substrate-subxt. If not, see . + +use substrate_subxt::{ + system::System, + Error, + KusamaRuntime, +}; + +fn main() { + async_std::task::block_on(async move { + env_logger::init(); + + let block_hash = fetch_block_hash(1).await; + match block_hash { + Ok(Some(hash)) => println!("Block hash for block number 1: {}", hash), + Ok(None) => println!("Block number 1 not found."), + Err(_) => eprintln!("Failed to fetch block hash"), + } + }); +} + +async fn fetch_block_hash( + block_number: u32, +) -> Result::Hash>, Error> { + substrate_subxt::ClientBuilder::::new() + .set_url("wss://kusama-rpc.polkadot.io") + .build() + .await? + .block_hash(Some(block_number.into())) + .await +} diff --git a/src/lib.rs b/src/lib.rs index 88d015743b..dc4b46285d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -85,7 +85,10 @@ pub use self::{ events::RawEvent, extrinsic::*, frame::*, - rpc::ExtrinsicSuccess, + rpc::{ + BlockNumber, + ExtrinsicSuccess, + }, runtimes::*, }; use self::{ @@ -104,7 +107,6 @@ use self::{ }, metadata::Metadata, rpc::{ - BlockNumber, ChainBlock, Rpc, }, diff --git a/src/rpc.rs b/src/rpc.rs index d4d91853a0..04acb4669a 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -38,6 +38,7 @@ use jsonrpsee::{ use num_traits::bounds::Bounded; use frame_metadata::RuntimeMetadataPrefixed; +use serde::Serialize; use sp_core::{ storage::{ StorageChangeSet, @@ -82,7 +83,30 @@ use crate::{ pub type ChainBlock = SignedBlock::Header, ::Extrinsic>>; -pub type BlockNumber = NumberOrHex<::BlockNumber>; + +/// Wrapper for NumberOrHex to allow custom From impls +#[derive(Serialize)] +#[serde(bound = "::BlockNumber: Serialize")] +pub struct BlockNumber(NumberOrHex<::BlockNumber>); + +impl From::BlockNumber>> for BlockNumber +where + T: System, +{ + fn from(x: NumberOrHex<::BlockNumber>) -> Self { + BlockNumber(x) + } +} + +impl From for BlockNumber +where + T: System, + ::BlockNumber: From, +{ + fn from(x: u32) -> Self { + NumberOrHex::Number(x.into()).into() + } +} /// Client for substrate rpc interfaces #[derive(Clone)]