Skip to content

Commit

Permalink
feat(iota, iota-sdk): add IotaEnv::faucet, add/update gas url consts (#…
Browse files Browse the repository at this point in the history
…4286)

Co-authored-by: Thibault Martinez <[email protected]>
  • Loading branch information
Thoralf-M and thibault-martinez authored Dec 5, 2024
1 parent 2c7de01 commit 3dfe7f8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 16 deletions.
25 changes: 22 additions & 3 deletions crates/iota-sdk/src/iota_client_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -100,6 +101,7 @@ pub struct IotaEnv {
/// Basic HTTP access authentication in the format of username:password, if
/// needed.
pub(crate) basic_auth: Option<String>,
pub(crate) faucet: Option<String>,
}

impl IotaEnv {
Expand All @@ -109,6 +111,7 @@ impl IotaEnv {
rpc: rpc.into(),
ws: None,
basic_auth: None,
faucet: None,
}
}

Expand All @@ -130,6 +133,15 @@ impl IotaEnv {
self.basic_auth = basic_auth.into();
}

pub fn with_faucet(mut self, faucet: impl Into<Option<String>>) -> Self {
self.set_faucet(faucet);
self
}

pub fn set_faucet(&mut self, faucet: impl Into<Option<String>>) {
self.faucet = faucet.into();
}

pub async fn create_rpc_client(
&self,
request_timeout: impl Into<Option<std::time::Duration>>,
Expand Down Expand Up @@ -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 {
Expand All @@ -175,6 +188,7 @@ impl IotaEnv {
rpc: IOTA_TESTNET_URL.into(),
ws: None,
basic_auth: None,
faucet: Some(IOTA_TESTNET_GAS_URL.into()),
}
}

Expand All @@ -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()),
}
}
}
Expand All @@ -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}")
}
}

Expand Down
4 changes: 3 additions & 1 deletion crates/iota-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
34 changes: 22 additions & 12 deletions crates/iota/src/client_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -272,6 +273,9 @@ pub enum IotaClientCommands {
ws: Option<String>,
#[clap(long, help = "Basic auth in the format of username:password")]
basic_auth: Option<String>,
/// Optional faucet Url, for example http://127.0.0.1:9123/v1/gas.
#[clap(long, value_hint = ValueHint::Url)]
faucet: Option<String>,
},

/// Get object info
Expand Down Expand Up @@ -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.")
}
Expand Down Expand Up @@ -1679,6 +1687,7 @@ impl IotaClientCommands {
rpc,
ws,
basic_auth,
faucet,
} => {
if context
.config()
Expand All @@ -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?;
Expand Down

0 comments on commit 3dfe7f8

Please sign in to comment.