Skip to content

Commit

Permalink
feat(iota-sdk): Add build_mainnet fn for convenience (#4738)
Browse files Browse the repository at this point in the history
* feat(iota-sdk): Add `build_mainnet` fn for convenience

* caps lock

* Apply suggestions from code review

Co-authored-by: Thoralf-M <[email protected]>

* oops

---------

Co-authored-by: Thoralf-M <[email protected]>
  • Loading branch information
DaughterOfMars and Thoralf-M authored Jan 10, 2025
1 parent 5026687 commit 8f3da64
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
12 changes: 8 additions & 4 deletions crates/iota-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ use iota_sdk::IotaClientBuilder;

#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
// Iota testnet -- https://api.testnet.iota.cafe
// IOTA testnet -- https://api.testnet.iota.cafe
let iota_testnet = IotaClientBuilder::default().build_testnet().await?;
println!("Iota testnet version: {}", iota_testnet.api_version());
println!("IOTA testnet version: {}", iota_testnet.api_version());

// Iota devnet -- https://api.devnet.iota.cafe
// IOTA devnet -- https://api.devnet.iota.cafe
let iota_devnet = IotaClientBuilder::default().build_devnet().await?;
println!("Iota devnet version: {}", iota_devnet.api_version());
println!("IOTA devnet version: {}", iota_devnet.api_version());

// IOTA mainnet -- https://api.mainnet.iota.cafe
let iota_mainnet = IotaClientBuilder::default().build_mainnet().await?;
println!("IOTA mainnet version: {}", iota_mainnet.api_version());

Ok(())
}
Expand Down
13 changes: 10 additions & 3 deletions crates/iota-sdk/examples/iota_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,21 @@ async fn main() -> Result<(), anyhow::Error> {

// IOTA devnet -- https://api.devnet.iota.cafe
let devnet_client = IotaClientBuilder::default().build_devnet().await?;
println!("Iota devnet version: {}", devnet_client.api_version());
println!("IOTA devnet version: {}", devnet_client.api_version());

// IOTA testnet -- https://api.testnet.iota.cafe
let testnet_client = IotaClientBuilder::default().build_testnet().await?;
println!("IOTA testnet version: {}", testnet_client.api_version());

println!("{:?}", local_client.available_rpc_methods());
println!("{:?}", local_client.available_subscriptions());
// IOTA mainnet -- https://api.mainnet.iota.cafe
let mainnet_client = IotaClientBuilder::default().build_mainnet().await?;
println!("IOTA mainnet version: {}", mainnet_client.api_version());

println!("rpc methods: {:?}", testnet_client.available_rpc_methods());
println!(
"available subscriptions: {:?}",
testnet_client.available_subscriptions()
);

Ok(())
}
23 changes: 23 additions & 0 deletions crates/iota-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ 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";
pub const IOTA_MAINNET_URL: &str = "https://api.mainnet.iota.cafe";

/// Builder for creating an [IotaClient] for connecting to the Iota network.
///
Expand Down Expand Up @@ -354,6 +355,28 @@ impl IotaClientBuilder {
self.build(IOTA_TESTNET_URL).await
}

/// Returns an [IotaClient] object that is ready to interact with the IOTA
/// mainnet.
///
/// For connecting to a custom URI, use the `build` function instead.
///
/// # Examples
///
/// ```rust,no_run
/// use iota_sdk::IotaClientBuilder;
///
/// #[tokio::main]
/// async fn main() -> Result<(), anyhow::Error> {
/// let iota = IotaClientBuilder::default().build_mainnet().await?;
///
/// println!("{:?}", iota.api_version());
/// Ok(())
/// }
/// ```
pub async fn build_mainnet(self) -> IotaRpcResult<IotaClient> {
self.build(IOTA_MAINNET_URL).await
}

/// Return the server information as a `ServerInfo` structure.
///
/// Fails with an error if it cannot call the RPC discover.
Expand Down

0 comments on commit 8f3da64

Please sign in to comment.