|
| 1 | +// Copyright (C) 2019-2025 Provable Inc. |
| 2 | +// This file is part of the Provable SDK library. |
| 3 | + |
| 4 | +// The Provable SDK library 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 | +// The Provable SDK library 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 the Provable SDK library. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +use crate::types::native::CurrentNetwork; |
| 18 | + |
| 19 | +use snarkvm_console::network::Network; |
| 20 | + |
| 21 | +/// Get the current network name. |
| 22 | +pub fn get_network() -> &'static str { |
| 23 | + match CurrentNetwork::ID { |
| 24 | + snarkvm_console::network::MainnetV0::ID => "mainnet", |
| 25 | + snarkvm_console::network::TestnetV0::ID => "testnet", |
| 26 | + snarkvm_console::network::CanaryV0::ID => "canary", |
| 27 | + _ => panic!("Invalid network"), |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/// Get the latest block height. |
| 32 | +pub async fn latest_block_height(base_url: &str) -> Result<u32, String> { |
| 33 | + let url = format!("{base_url}/{}/block/height/latest", get_network()); |
| 34 | + let res = get(&url).await?; |
| 35 | + Ok(res) |
| 36 | +} |
| 37 | + |
| 38 | +/// Make a GET request to the service. |
| 39 | +pub async fn get<T>(url: &str) -> Result<T, String> |
| 40 | +where |
| 41 | + T: serde::de::DeserializeOwned, |
| 42 | +{ |
| 43 | + let client = reqwest::Client::new(); |
| 44 | + let res = client.get(url).send().await.map_err(|e| e.to_string())?.json().await.map_err(|e| e.to_string())?; |
| 45 | + Ok(res) |
| 46 | +} |
0 commit comments