Skip to content

Commit

Permalink
Add fetch example and wrap BlockNumber impl
Browse files Browse the repository at this point in the history
  • Loading branch information
ascjones committed Apr 14, 2020
1 parent bb46578 commit 62fdcb1
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
42 changes: 42 additions & 0 deletions examples/fetch_remote.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// 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 <http://www.gnu.org/licenses/>.

use substrate_subxt::{
Error,
KusamaRuntime,
system::System,
};

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<Option<<KusamaRuntime as System>::Hash>, Error> {
substrate_subxt::ClientBuilder::<KusamaRuntime>::new()
.set_url("wss://kusama-rpc.polkadot.io")
.build().await?
.block_hash(Some(block_number.into()))
.await
}
9 changes: 4 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub use self::{
error::Error,
events::RawEvent,
frame::*,
rpc::ExtrinsicSuccess,
rpc::{BlockNumber, ExtrinsicSuccess},
runtimes::*,
};
use self::{
Expand All @@ -106,7 +106,6 @@ use self::{
},
metadata::Metadata,
rpc::{
BlockNumber,
ChainBlock,
Rpc,
},
Expand Down Expand Up @@ -616,15 +615,15 @@ mod tests {
#[test]
#[ignore] // requires locally running substrate node
fn test_create_raw_payload() {

let result: Result<_, Error> = async_std::task::block_on(async move {
let signer_pair = Ed25519Keyring::Alice.pair();
let signer_account_id = Ed25519Keyring::Alice.to_account_id();
let dest = AccountKeyring::Bob.to_account_id();

let client = test_client().await;

// create raw payload with AccoundId and sign it
// create raw payload with AccoundId and sign it
let raw_payload = client.create_raw_payload(signer_account_id, balances::transfer::<Runtime>(dest.clone().into(), 10_000)).await?;
let raw_signature = signer_pair.sign(raw_payload.encode().split_off(2).as_slice());
let raw_multisig = MultiSignature::from(raw_signature);
Expand All @@ -637,7 +636,7 @@ mod tests {
assert_eq!(raw_multisig, xt_multi_sig);

Ok(())

});

assert!(result.is_ok())
Expand Down
26 changes: 25 additions & 1 deletion src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use jsonrpsee::{
use num_traits::bounds::Bounded;

use frame_metadata::RuntimeMetadataPrefixed;
use serde::Serialize;
use sp_core::{
storage::{
StorageChangeSet,
Expand Down Expand Up @@ -82,7 +83,30 @@ use crate::{

pub type ChainBlock<T> =
SignedBlock<Block<<T as System>::Header, <T as System>::Extrinsic>>;
pub type BlockNumber<T> = NumberOrHex<<T as System>::BlockNumber>;

/// Wrapper for NumberOrHex to allow custom From impls
#[derive(Serialize)]
#[serde(bound = "<T as System>::BlockNumber: Serialize")]
pub struct BlockNumber<T: System> (NumberOrHex<<T as System>::BlockNumber>);

impl<T> From<NumberOrHex<<T as System>::BlockNumber>> for BlockNumber<T>
where
T: System
{
fn from(x: NumberOrHex<<T as System>::BlockNumber>) -> Self {
BlockNumber(x)
}
}

impl<T> From<u32> for BlockNumber<T>
where
T: System,
<T as System>::BlockNumber: From<u32>,
{
fn from(x: u32) -> Self {
NumberOrHex::Number(x.into()).into()
}
}

/// Client for substrate rpc interfaces
#[derive(Clone)]
Expand Down

0 comments on commit 62fdcb1

Please sign in to comment.