From 3dfe7f84f60bcc609ff6501eed37892bb59185ec Mon Sep 17 00:00:00 2001 From: Thoralf-M <46689931+Thoralf-M@users.noreply.github.com> Date: Thu, 5 Dec 2024 13:02:46 +0100 Subject: [PATCH] feat(iota, iota-sdk): add IotaEnv::faucet, add/update gas url consts (#4286) Co-authored-by: Thibault Martinez --- crates/iota-sdk/src/iota_client_config.rs | 25 +++++++++++++++-- crates/iota-sdk/src/lib.rs | 4 ++- crates/iota/src/client_commands.rs | 34 +++++++++++++++-------- 3 files changed, 47 insertions(+), 16 deletions(-) diff --git a/crates/iota-sdk/src/iota_client_config.rs b/crates/iota-sdk/src/iota_client_config.rs index 1af4d479611..2cecb9cc899 100644 --- a/crates/iota-sdk/src/iota_client_config.rs +++ b/crates/iota-sdk/src/iota_client_config.rs @@ -13,7 +13,8 @@ use serde::{Deserialize, Serialize}; use serde_with::serde_as; use crate::{ - IOTA_DEVNET_URL, IOTA_LOCAL_NETWORK_URL, IOTA_TESTNET_URL, IotaClient, IotaClientBuilder, + IOTA_DEVNET_GAS_URL, IOTA_DEVNET_URL, IOTA_LOCAL_NETWORK_GAS_URL, IOTA_LOCAL_NETWORK_URL, + IOTA_TESTNET_GAS_URL, IOTA_TESTNET_URL, IotaClient, IotaClientBuilder, }; #[serde_as] @@ -100,6 +101,7 @@ pub struct IotaEnv { /// Basic HTTP access authentication in the format of username:password, if /// needed. pub(crate) basic_auth: Option, + pub(crate) faucet: Option, } impl IotaEnv { @@ -109,6 +111,7 @@ impl IotaEnv { rpc: rpc.into(), ws: None, basic_auth: None, + faucet: None, } } @@ -130,6 +133,15 @@ impl IotaEnv { self.basic_auth = basic_auth.into(); } + pub fn with_faucet(mut self, faucet: impl Into>) -> Self { + self.set_faucet(faucet); + self + } + + pub fn set_faucet(&mut self, faucet: impl Into>) { + self.faucet = faucet.into(); + } + pub async fn create_rpc_client( &self, request_timeout: impl Into>, @@ -167,6 +179,7 @@ impl IotaEnv { rpc: IOTA_DEVNET_URL.into(), ws: None, basic_auth: None, + faucet: Some(IOTA_DEVNET_GAS_URL.into()), } } pub fn testnet() -> Self { @@ -175,6 +188,7 @@ impl IotaEnv { rpc: IOTA_TESTNET_URL.into(), ws: None, basic_auth: None, + faucet: Some(IOTA_TESTNET_GAS_URL.into()), } } @@ -184,6 +198,7 @@ impl IotaEnv { rpc: IOTA_LOCAL_NETWORK_URL.into(), ws: None, basic_auth: None, + faucet: Some(IOTA_LOCAL_NETWORK_GAS_URL.into()), } } } @@ -199,9 +214,13 @@ impl Display for IotaEnv { } if let Some(basic_auth) = &self.basic_auth { writeln!(writer)?; - write!(writer, "Basic Auth: {}", basic_auth)?; + write!(writer, "Basic Auth: {basic_auth}")?; } - write!(f, "{}", writer) + if let Some(faucet) = &self.faucet { + writeln!(writer)?; + write!(writer, "Faucet URL: {faucet}")?; + } + write!(f, "{writer}") } } diff --git a/crates/iota-sdk/src/lib.rs b/crates/iota-sdk/src/lib.rs index dcaa87d612d..bf2227e19ee 100644 --- a/crates/iota-sdk/src/lib.rs +++ b/crates/iota-sdk/src/lib.rs @@ -117,9 +117,11 @@ use crate::{ pub const IOTA_COIN_TYPE: &str = "0x2::iota::IOTA"; pub const IOTA_LOCAL_NETWORK_URL: &str = "http://127.0.0.1:9000"; pub const IOTA_LOCAL_NETWORK_URL_0: &str = "http://0.0.0.0:9000"; -pub const IOTA_LOCAL_NETWORK_GAS_URL: &str = "http://127.0.0.1:5003/gas"; +pub const IOTA_LOCAL_NETWORK_GAS_URL: &str = "http://127.0.0.1:9123/v1/gas"; pub const IOTA_DEVNET_URL: &str = "https://api.devnet.iota.cafe"; +pub const IOTA_DEVNET_GAS_URL: &str = "https://faucet.devnet.iota.cafe/v1/gas"; pub const IOTA_TESTNET_URL: &str = "https://api.testnet.iota.cafe"; +pub const IOTA_TESTNET_GAS_URL: &str = "https://faucet.testnet.iota.cafe/v1/gas"; /// Builder for creating an [IotaClient] for connecting to the Iota network. /// diff --git a/crates/iota/src/client_commands.rs b/crates/iota/src/client_commands.rs index 3ddd783aac7..dfb34bb8808 100644 --- a/crates/iota/src/client_commands.rs +++ b/crates/iota/src/client_commands.rs @@ -38,8 +38,9 @@ use iota_package_management::{LockCommand, PublishedAtError}; use iota_protocol_config::{Chain, ProtocolConfig, ProtocolVersion}; use iota_replay::ReplayToolCommand; use iota_sdk::{ - IOTA_COIN_TYPE, IOTA_DEVNET_URL, IOTA_LOCAL_NETWORK_URL, IOTA_LOCAL_NETWORK_URL_0, - IOTA_TESTNET_URL, IotaClient, + IOTA_COIN_TYPE, IOTA_DEVNET_GAS_URL, IOTA_DEVNET_URL, IOTA_LOCAL_NETWORK_GAS_URL, + IOTA_LOCAL_NETWORK_URL, IOTA_LOCAL_NETWORK_URL_0, IOTA_TESTNET_GAS_URL, IOTA_TESTNET_URL, + IotaClient, apis::ReadApi, iota_client_config::{IotaClientConfig, IotaEnv}, wallet_context::WalletContext, @@ -272,6 +273,9 @@ pub enum IotaClientCommands { ws: Option, #[clap(long, help = "Basic auth in the format of username:password")] basic_auth: Option, + /// Optional faucet Url, for example http://127.0.0.1:9123/v1/gas. + #[clap(long, value_hint = ValueHint::Url)] + faucet: Option, }, /// Get object info @@ -1540,17 +1544,21 @@ impl IotaClientCommands { let active_env = context.config().get_active_env(); if let Ok(env) = active_env { - let network = match env.rpc().as_str() { - IOTA_DEVNET_URL => "https://faucet.devnet.iota.cafe/v1/gas", - IOTA_TESTNET_URL => "https://faucet.testnet.iota.cafe/v1/gas", - IOTA_LOCAL_NETWORK_URL | IOTA_LOCAL_NETWORK_URL_0 => { - "http://127.0.0.1:9123/gas" + let faucet_url = if let Some(faucet_url) = env.faucet() { + faucet_url + } else { + match env.rpc().as_str() { + IOTA_DEVNET_URL => IOTA_DEVNET_GAS_URL, + IOTA_TESTNET_URL => IOTA_TESTNET_GAS_URL, + IOTA_LOCAL_NETWORK_URL | IOTA_LOCAL_NETWORK_URL_0 => { + IOTA_LOCAL_NETWORK_GAS_URL + } + _ => bail!( + "Cannot recognize the active network. Please provide the gas faucet full URL." + ), } - _ => bail!( - "Cannot recognize the active network. Please provide the gas faucet full URL." - ), }; - network.to_string() + faucet_url.to_string() } else { bail!("No URL for faucet was provided and there is no active network.") } @@ -1679,6 +1687,7 @@ impl IotaClientCommands { rpc, ws, basic_auth, + faucet, } => { if context .config() @@ -1692,7 +1701,8 @@ impl IotaClientCommands { } let env = IotaEnv::new(alias, rpc) .with_ws(ws) - .with_basic_auth(basic_auth); + .with_basic_auth(basic_auth) + .with_faucet(faucet); // Check urls are valid and server is reachable env.create_rpc_client(None, None).await?;