Skip to content

Commit

Permalink
Revert "refactor network to use cli::network::Args"
Browse files Browse the repository at this point in the history
This reverts commit 237cdd5.
  • Loading branch information
BlaineHeffron committed Jun 26, 2024
1 parent 6740864 commit 2beed2d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 56 deletions.
63 changes: 44 additions & 19 deletions crates/loam-cli/src/commands/build/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
use crate::commands::build::env_toml;
use stellar_xdr::curr::Error as xdrError;
use soroban_cli::commands::NetworkRunnable;
use soroban_cli::commands::network::Args as Network;
use soroban_cli::utils::contract_hash;
use soroban_cli::{commands as cli, CommandParser};
use std::collections::BTreeMap as Map;
use std::fmt::Debug;
use std::hash::Hash;
use serde_json;

use super::env_toml::Network;

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, clap::ValueEnum)]
pub enum LoamEnv {
Expand Down Expand Up @@ -72,7 +72,7 @@ impl Args {
return Ok(());
};

Self::add_network_to_env(current_env.network.clone())?;
Self::add_network_to_env(&current_env.network)?;
Self::handle_accounts(current_env.accounts.as_deref()).await?;
self.handle_contracts(workspace_root, current_env.contracts.as_ref(), &current_env.network)
.await?;
Expand All @@ -90,26 +90,51 @@ impl Args {
/// We could set `STELLAR_NETWORK` instead, but when importing contracts, we want to hard-code
/// the network passphrase. So if given a network name, we use soroban-cli to fetch the RPC url
/// & passphrase for that named network, and still set the environment variables.
fn add_network_to_env(network: cli::network::Args) -> Result<(), Error> {
let cli::network::Network {
rpc_url,
network_passphrase,
} = network.get(&cli::config::locator::Args {
global: false,
config_dir: None,
})?;

std::env::set_var("STELLAR_RPC_URL", &rpc_url);
std::env::set_var("STELLAR_NETWORK_PASSPHRASE", &network_passphrase);

match network.network {
Some(name) => eprintln!("🌐 using {name} network"),
None => eprintln!("🌐 using network at {rpc_url}"),
fn add_network_to_env(network: &env_toml::Network) -> Result<(), Error> {
match &network {
Network {
name: Some(name), ..
} => {
let cli::network::Network {
rpc_url,
network_passphrase,
} = (cli::network::Args {
network: Some(name.clone()),
rpc_url: None,
network_passphrase: None,
})
.get(&cli::config::locator::Args {
global: false,
config_dir: None,
})?;
eprintln!("🌐 using {name} network");
std::env::set_var("STELLAR_RPC_URL", rpc_url);
std::env::set_var("STELLAR_NETWORK_PASSPHRASE", network_passphrase);
}
Network {
rpc_url: Some(rpc_url),
network_passphrase: Some(passphrase),
..
} => {
std::env::set_var("STELLAR_RPC_URL", rpc_url);
std::env::set_var("STELLAR_NETWORK_PASSPHRASE", passphrase);
eprintln!("🌐 using network at {rpc_url}");
}
_ => return Err(Error::MalformedNetwork),
}

Ok(())
}


fn get_network_args(network: &Network) -> cli::network::Args {
cli::network::Args {
rpc_url: network.rpc_url.clone(),
network_passphrase: network.network_passphrase.clone(),
network: network.name.clone(),
}
}

fn get_config_locator() -> cli::config::locator::Args {
cli::config::locator::Args {
global: false,
Expand All @@ -121,7 +146,7 @@ impl Args {
let account = std::env::var("STELLAR_ACCOUNT")
.expect("No STELLAR_ACCOUNT environment variable set");
cli::config::Args {
network: network.clone(),
network: Self::get_network_args(network),
locator: Self::get_config_locator(),
source_account: account,
hd_path: Some(0),
Expand All @@ -140,7 +165,7 @@ impl Args {
contract_id: contract_id.to_string(),
out_file: None,
locator: Self::get_config_locator(),
network: network.clone(),
network: Self::get_network_args(network),
}
.run_against_rpc_server(None, None)
.await?;
Expand Down
45 changes: 8 additions & 37 deletions crates/loam-cli/src/commands/build/env_toml.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::collections::BTreeMap as Map;
use std::io;
use std::path::Path;
use soroban_cli::commands::network::Args as Network;
use serde::Deserialize;

#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand All @@ -14,47 +12,20 @@ pub enum Error {

type Environments = Map<Box<str>, Environment>;

#[derive(Debug, Clone)]
#[derive(Debug, serde::Deserialize, Clone)]
pub struct Environment {
pub accounts: Option<Vec<Account>>,
pub network: Network,
pub contracts: Option<Map<Box<str>, Contract>>,
}

impl<'de> Deserialize<'de> for Environment {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
#[derive(Deserialize)]
struct Helper {
accounts: Option<Vec<Account>>,
network: NetworkHelper,
contracts: Option<Map<Box<str>, Contract>>,
}

#[derive(Deserialize)]
struct NetworkHelper {
rpc_url: Option<String>,
network_passphrase: Option<String>,
network: Option<String>,
}

let helper = Helper::deserialize(deserializer)?;

let network = Network {
rpc_url: helper.network.rpc_url,
network_passphrase: helper.network.network_passphrase,
network: helper.network.network,
..Default::default() // This will set any other fields to their default values
};

Ok(Environment {
accounts: helper.accounts,
network,
contracts: helper.contracts,
})
}
#[derive(Debug, serde::Deserialize, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct Network {
pub name: Option<String>,
pub rpc_url: Option<String>,
pub network_passphrase: Option<String>,
// run_locally: Option<bool>,
}

#[derive(Debug, serde::Deserialize, Clone)]
Expand Down

0 comments on commit 2beed2d

Please sign in to comment.