Skip to content

Commit

Permalink
Use the struct instead of string tuples.
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando committed Sep 19, 2024
1 parent e7f007e commit e666be2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 42 deletions.
4 changes: 2 additions & 2 deletions cmd/soroban-cli/src/commands/contract/bindings/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 14 additions & 6 deletions cmd/soroban-cli/src/config/locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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))
Expand All @@ -227,10 +233,12 @@ impl Args {
pub fn read_network(&self, name: &str) -> Result<Network, Error> {
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
}
Expand Down
81 changes: 47 additions & 34 deletions cmd/soroban-cli/src/config/network.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<String, Network> {
static HASHMAP: OnceLock<HashMap<String, Network>> = 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
})
}

0 comments on commit e666be2

Please sign in to comment.