|
| 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 | +} |
0 commit comments