diff --git a/ant-cli/src/actions/connect.rs b/ant-cli/src/actions/connect.rs index 091c87e1c8..5ae12694ad 100644 --- a/ant-cli/src/actions/connect.rs +++ b/ant-cli/src/actions/connect.rs @@ -6,14 +6,14 @@ // KIND, either express or implied. Please review the Licences for the specific language governing // permissions and limitations relating to use of the SAFE Network Software. -use autonomi::Client; +use crate::evm_network::get_evm_network; +use crate::network::NetworkPeers; +use autonomi::{Client, ClientConfig}; use color_eyre::eyre::bail; use color_eyre::eyre::Result; use indicatif::ProgressBar; use std::time::Duration; -use crate::network::NetworkPeers; - pub async fn connect_to_network(peers: NetworkPeers) -> Result { let progress_bar = ProgressBar::new_spinner(); progress_bar.enable_steady_tick(Duration::from_millis(120)); @@ -21,14 +21,26 @@ pub async fn connect_to_network(peers: NetworkPeers) -> Result { let new_style = progress_bar.style().tick_chars("⠁⠂⠄⡀⢀⠠⠐⠈🔗"); progress_bar.set_style(new_style); - let res = if peers.is_local() { + let local = peers.is_local(); + + let peers_opt = if local { progress_bar.set_message("Connecting to a local Autonomi Network..."); - Client::init_local().await + None } else { progress_bar.set_message("Connecting to The Autonomi Network..."); - Client::init_with_peers(peers.peers().to_vec()).await + Some(peers.peers().to_vec()) }; + let evm_network = get_evm_network(local)?; + + let config = ClientConfig { + local, + peers: peers_opt, + evm_network, + }; + + let res = Client::init_with_config(config).await; + match res { Ok(client) => { info!("Connected to the Network"); diff --git a/ant-cli/src/commands.rs b/ant-cli/src/commands.rs index 1bedd25f31..515a1470d8 100644 --- a/ant-cli/src/commands.rs +++ b/ant-cli/src/commands.rs @@ -228,7 +228,7 @@ pub async fn handle_subcommand(opt: Opt) -> Result<()> { password, } => wallet::import(private_key, no_password, password), WalletCmd::Export => wallet::export(), - WalletCmd::Balance => wallet::balance().await, + WalletCmd::Balance => wallet::balance(peers.await?.is_local()).await, }, None => { // If no subcommand is given, default to clap's error behaviour. diff --git a/ant-cli/src/commands/wallet.rs b/ant-cli/src/commands/wallet.rs index 4bfe385b26..c11bc41802 100644 --- a/ant-cli/src/commands/wallet.rs +++ b/ant-cli/src/commands/wallet.rs @@ -6,10 +6,11 @@ // KIND, either express or implied. Please review the Licences for the specific language governing // permissions and limitations relating to use of the SAFE Network Software. +use crate::evm_network::get_evm_network; use crate::wallet::fs::{select_wallet_private_key, store_private_key}; use crate::wallet::input::request_password; use crate::wallet::DUMMY_NETWORK; -use autonomi::{get_evm_network_from_env, Wallet}; +use autonomi::Wallet; use color_eyre::eyre::eyre; use color_eyre::Result; use prettytable::{Cell, Row, Table}; @@ -80,8 +81,8 @@ pub fn export() -> Result<()> { Ok(()) } -pub async fn balance() -> Result<()> { - let network = get_evm_network_from_env().unwrap_or_default(); +pub async fn balance(local: bool) -> Result<()> { + let network = get_evm_network(local)?; let wallet = crate::wallet::load_wallet(&network)?; let token_balance = wallet.balance_of_tokens().await?; diff --git a/ant-cli/src/evm_network.rs b/ant-cli/src/evm_network.rs new file mode 100644 index 0000000000..48998f1c0f --- /dev/null +++ b/ant-cli/src/evm_network.rs @@ -0,0 +1,24 @@ +use autonomi::{get_evm_network_from_env, local_evm_network_from_csv, Network}; +use color_eyre::eyre::Result; + +use std::sync::OnceLock; + +static EVM_NETWORK: OnceLock = OnceLock::new(); + +pub(crate) fn get_evm_network(local: bool) -> Result { + if let Some(network) = EVM_NETWORK.get() { + return Ok(network.clone()); + } + + let res = match get_evm_network_from_env() { + Ok(evm_network) => Ok(evm_network), + Err(_) if local => Ok(local_evm_network_from_csv()?), + Err(_) => Ok(Default::default()), + }; + + if let Ok(network) = res.as_ref() { + let _ = EVM_NETWORK.set(network.clone()); + } + + res +} diff --git a/ant-cli/src/main.rs b/ant-cli/src/main.rs index e0fe5cf644..b7f6bc8fcb 100644 --- a/ant-cli/src/main.rs +++ b/ant-cli/src/main.rs @@ -12,6 +12,7 @@ extern crate tracing; mod access; mod actions; mod commands; +mod evm_network; mod opt; mod utils; mod wallet; diff --git a/autonomi/src/client/mod.rs b/autonomi/src/client/mod.rs index 2f11877623..5073a2bd76 100644 --- a/autonomi/src/client/mod.rs +++ b/autonomi/src/client/mod.rs @@ -79,6 +79,9 @@ pub struct ClientConfig { /// /// If not provided, the client will use the default bootstrap peers. pub peers: Option>, + + /// EVM network to use for quotations and payments. + pub evm_network: EvmNetwork, } /// Error returned by [`Client::init`]. @@ -136,6 +139,7 @@ impl Client { Self::init_with_config(ClientConfig { local, peers: Some(peers), + evm_network: Default::default(), }) .await } @@ -188,7 +192,7 @@ impl Client { Ok(Self { network, client_event_sender: Arc::new(None), - evm_network: Default::default(), + evm_network: config.evm_network, }) } diff --git a/autonomi/src/lib.rs b/autonomi/src/lib.rs index 99bb92e51d..bfb1d705b2 100644 --- a/autonomi/src/lib.rs +++ b/autonomi/src/lib.rs @@ -66,6 +66,7 @@ pub mod client; pub mod self_encryption; pub use ant_evm::get_evm_network_from_env; +pub use ant_evm::local_evm_network_from_csv; pub use ant_evm::Amount; pub use ant_evm::EvmNetwork as Network; pub use ant_evm::EvmWallet as Wallet; diff --git a/evmlib/src/lib.rs b/evmlib/src/lib.rs index e2715c0ed6..480ac8270b 100644 --- a/evmlib/src/lib.rs +++ b/evmlib/src/lib.rs @@ -78,8 +78,8 @@ impl CustomNetwork { #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] pub enum Network { - #[default] ArbitrumOne, + #[default] ArbitrumSepolia, Custom(CustomNetwork), }