Skip to content

Commit

Permalink
feat(client): try get evm network from env by default on client::init…
Browse files Browse the repository at this point in the history
…(_local)
  • Loading branch information
mickvandijke committed Jan 9, 2025
1 parent 605a3eb commit 38b90c8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
6 changes: 2 additions & 4 deletions ant-cli/src/actions/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
// 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 autonomi::Multiaddr;
use autonomi::{get_evm_network_from_env, Client};
use color_eyre::eyre::bail;
use color_eyre::eyre::Result;
use indicatif::ProgressBar;
Expand All @@ -23,9 +23,7 @@ pub async fn connect_to_network(peers: Vec<Multiaddr>) -> Result<Client> {
progress_bar.set_message("Connecting to The Autonomi Network...");

match Client::init_with_peers(peers).await {
Ok(mut client) => {
let evm_network = get_evm_network_from_env()?;
client.set_evm_network(evm_network);
Ok(client) => {
info!("Connected to the Network");
progress_bar.finish_with_message("Connected to the Network");
Ok(client)
Expand Down
6 changes: 1 addition & 5 deletions autonomi/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl Default for ClientConfig {
#[cfg(not(feature = "local"))]
local: false,
peers: None,
evm_network: Default::default(),
evm_network: EvmNetwork::try_from_env().unwrap_or_default(),
}
}
}
Expand Down Expand Up @@ -275,10 +275,6 @@ impl Client {

client_event_receiver
}

pub fn set_evm_network(&mut self, evm_network: EvmNetwork) {
self.evm_network = evm_network;
}
}

fn build_client_and_run_swarm(local: bool) -> (Network, mpsc::Receiver<NetworkEvent>) {
Expand Down
7 changes: 7 additions & 0 deletions evmlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// permissions and limitations relating to use of the SAFE Network Software.

use crate::common::Address;
use crate::utils::get_evm_network_from_env;
use alloy::primitives::address;
use alloy::transports::http::reqwest;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -103,6 +104,12 @@ impl std::fmt::Display for Network {
}

impl Network {
pub fn try_from_env() -> Result<Self, utils::Error> {
get_evm_network_from_env().inspect_err(|err| {
warn!("Failed to select EVM network from ENV: {err}");
})
}

pub fn new_custom(rpc_url: &str, payment_token_addr: &str, chunk_payments_addr: &str) -> Self {
Self::Custom(CustomNetwork::new(
rpc_url,
Expand Down
8 changes: 4 additions & 4 deletions evmlib/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub fn get_evm_network_from_env() -> Result<Network, Error> {
})
.collect::<Result<Vec<String>, Error>>();

let mut use_local_evm = std::env::var("EVM_NETWORK")
let mut use_local_evm = env::var("EVM_NETWORK")
.map(|v| v == "local")
.unwrap_or(false);
if use_local_evm {
Expand All @@ -104,15 +104,15 @@ pub fn get_evm_network_from_env() -> Result<Network, Error> {
info!("Using local EVM network as 'local' feature flag is enabled");
}

let use_arbitrum_one = std::env::var("EVM_NETWORK")
let use_arbitrum_one = env::var("EVM_NETWORK")
.map(|v| v == "arbitrum-one")
.unwrap_or(false);

let use_arbitrum_sepolia = std::env::var("EVM_NETWORK")
let use_arbitrum_sepolia = env::var("EVM_NETWORK")
.map(|v| v == "arbitrum-sepolia")
.unwrap_or(false);

let use_arbitrum_sepolia_test = std::env::var("EVM_NETWORK")
let use_arbitrum_sepolia_test = env::var("EVM_NETWORK")
.map(|v| v == "arbitrum-sepolia-test")
.unwrap_or(false);

Expand Down

0 comments on commit 38b90c8

Please sign in to comment.