diff --git a/cmd/soroban-cli/src/commands/contract/bindings/typescript.rs b/cmd/soroban-cli/src/commands/contract/bindings/typescript.rs index 84e2b17627..8f7a0b7b1a 100644 --- a/cmd/soroban-cli/src/commands/contract/bindings/typescript.rs +++ b/cmd/soroban-cli/src/commands/contract/bindings/typescript.rs @@ -124,10 +124,10 @@ impl NetworkRunnable for Cmd { network_passphrase, .. } = self.network.get(&self.locator).ok().unwrap_or_else(|| { - network::DEFAULTS + network::default_networks() .get("futurenet") .expect("why did we remove the default futurenet network?") - .into() + .to_owned() }); let absolute_path = self.output_dir.canonicalize()?; let file_name = absolute_path diff --git a/cmd/soroban-cli/src/config/locator.rs b/cmd/soroban-cli/src/config/locator.rs index 82ce2eee53..8d05474172 100644 --- a/cmd/soroban-cli/src/config/locator.rs +++ b/cmd/soroban-cli/src/config/locator.rs @@ -191,7 +191,9 @@ impl Args { .into_iter() .flatten() .map(|x| x.0); - let default_networks = network::DEFAULTS.keys().map(ToString::to_string); + let list = network::default_networks(); + let default_networks = list.keys().map(ToString::to_string); + Ok(saved_networks .chain(default_networks) .unique() @@ -211,9 +213,13 @@ impl Args { location.to_string(), )) }); - let default_networks = network::DEFAULTS - .into_iter() - .map(|(name, network)| ((*name).to_string(), network.into(), "Default".to_owned())); + let default_networks = network::default_networks().iter().map(|(name, network)| { + ( + (*name).to_string(), + network.to_owned(), + "Default".to_owned(), + ) + }); Ok(saved_networks .chain(default_networks) .sorted_by(|a, b| Ord::cmp(&a.0, &b.0)) @@ -227,10 +233,12 @@ impl Args { pub fn read_network(&self, name: &str) -> Result { let res = KeyType::Network.read_with_global(name, &self.local_config()?); if let Err(Error::ConfigMissing(_, _)) = &res { - let Some(network) = network::DEFAULTS.get(name) else { + let list = network::default_networks(); + + let Some(network) = list.get(name) else { return res; }; - return Ok(network.into()); + return Ok(network.to_owned()); } res } diff --git a/cmd/soroban-cli/src/config/network.rs b/cmd/soroban-cli/src/config/network.rs index 861c131356..552040d929 100644 --- a/cmd/soroban-cli/src/config/network.rs +++ b/cmd/soroban-cli/src/config/network.rs @@ -1,7 +1,6 @@ -use std::str::FromStr; +use std::{collections::HashMap, str::FromStr, sync::OnceLock}; use clap::arg; -use phf::phf_map; use serde::{Deserialize, Serialize}; use serde_json::Value; use stellar_strkey::ed25519::PublicKey; @@ -144,36 +143,50 @@ impl Network { } } -pub static DEFAULTS: phf::Map<&'static str, (&'static str, &'static str, &'static str)> = phf_map! { - "local" => ( - "local", - "http://localhost:8000/rpc", - passphrase::LOCAL, - ), - "futurenet" => ( - "futurenet", - "https://rpc-futurenet.stellar.org:443", - passphrase::FUTURENET, - ), - "testnet" => ( - "testnet", - "https://soroban-testnet.stellar.org", - passphrase::TESTNET, - ), - "mainnet" => ( - "mainnet", - "Bring Your Own: https://developers.stellar.org/docs/data/rpc/rpc-providers", - passphrase::MAINNET, - ), -}; - -impl From<&(&str, &str, &str)> for Network { - /// Convert the return value of `DEFAULTS.get()` into a Network - fn from(n: &(&str, &str, &str)) -> Self { - Self { - name: n.0.to_string(), - rpc_url: n.1.to_string(), - network_passphrase: n.2.to_string(), - } - } +pub fn default_networks() -> &'static HashMap { + static HASHMAP: OnceLock> = OnceLock::new(); + + HASHMAP.get_or_init(|| { + let mut map = HashMap::new(); + + map.insert( + "local".to_string(), + Network { + name: "local".to_string(), + rpc_url: "http://localhost:8000/rpc".to_string(), + network_passphrase: passphrase::LOCAL.to_string(), + }, + ); + + map.insert( + "futurenet".to_string(), + Network { + name: "futurenet".to_string(), + rpc_url: "https://rpc-futurenet.stellar.org:443".to_string(), + network_passphrase: passphrase::FUTURENET.to_string(), + }, + ); + + map.insert( + "testnet".to_string(), + Network { + name: "testnet".to_string(), + rpc_url: "https://soroban-testnet.stellar.org".to_string(), + network_passphrase: passphrase::TESTNET.to_string(), + }, + ); + + map.insert( + "mainnet".to_string(), + Network { + name: "mainnet".to_string(), + rpc_url: + "Bring Your Own: https://developers.stellar.org/docs/data/rpc/rpc-providers" + .to_string(), + network_passphrase: passphrase::MAINNET.to_string(), + }, + ); + + map + }) }