Skip to content

Commit

Permalink
feat: centralize evm network selection code
Browse files Browse the repository at this point in the history
  • Loading branch information
grumbach committed Jan 7, 2025
1 parent 51f00b6 commit 46de268
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 52 deletions.
3 changes: 1 addition & 2 deletions ant-cli/src/actions/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +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 crate::evm_network::get_evm_network;
use crate::network::NetworkPeers;
use autonomi::{Client, ClientConfig};
use autonomi::{get_evm_network, Client, ClientConfig};
use color_eyre::eyre::bail;
use color_eyre::eyre::Result;
use indicatif::ProgressBar;
Expand Down
2 changes: 1 addition & 1 deletion ant-cli/src/commands/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
// 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;
use autonomi::Wallet;
use color_eyre::eyre::eyre;
use color_eyre::Result;
Expand Down
26 changes: 0 additions & 26 deletions ant-cli/src/evm_network.rs

This file was deleted.

1 change: 0 additions & 1 deletion ant-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ extern crate tracing;
mod access;
mod actions;
mod commands;
mod evm_network;
mod opt;
mod utils;
mod wallet;
Expand Down
2 changes: 1 addition & 1 deletion ant-evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub use evmlib::cryptography;
#[cfg(feature = "external-signer")]
pub use evmlib::external_signer;
pub use evmlib::utils;
pub use evmlib::utils::{get_evm_network_from_env, local_evm_network_from_csv};
pub use evmlib::utils::get_evm_network;
pub use evmlib::utils::{DATA_PAYMENTS_ADDRESS, PAYMENT_TOKEN_ADDRESS, RPC_URL};
pub use evmlib::wallet::Error as EvmWalletError;
pub use evmlib::wallet::Wallet as EvmWallet;
Expand Down
4 changes: 2 additions & 2 deletions ant-node-manager/src/bin/cli/subcommands/evm_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// 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 ant_evm::{utils::local_evm_network_from_csv, EvmNetwork};
use ant_evm::{get_evm_network, EvmNetwork};
use clap::Subcommand;
use color_eyre::Result;

Expand Down Expand Up @@ -50,7 +50,7 @@ impl TryInto<EvmNetwork> for EvmNetworkCommand {
Self::EvmArbitrumSepolia => Ok(EvmNetwork::ArbitrumSepolia),
Self::EvmArbitrumSepoliaTest => Ok(EvmNetwork::ArbitrumSepoliaTest),
Self::EvmLocal => {
let network = local_evm_network_from_csv()?;
let network = get_evm_network(true)?;
Ok(network)
}
Self::EvmCustom {
Expand Down
7 changes: 3 additions & 4 deletions ant-node/src/bin/antnode/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod subcommands;

use crate::subcommands::EvmNetworkCommand;
use ant_bootstrap::{BootstrapCacheConfig, BootstrapCacheStore, PeersArgs};
use ant_evm::{get_evm_network_from_env, local_evm_network_from_csv, EvmNetwork, RewardsAddress};
use ant_evm::{get_evm_network, EvmNetwork, RewardsAddress};
use ant_logging::metrics::init_metrics;
use ant_logging::{Level, LogFormat, LogOutputDest, ReloadHandle};
use ant_node::{Marker, NodeBuilder, NodeEvent, NodeEventsReceiver};
Expand Down Expand Up @@ -264,9 +264,8 @@ fn main() -> Result<()> {

let evm_network: EvmNetwork = match opt.evm_network.as_ref() {
Some(evm_network) => Ok(evm_network.clone().into()),
None => match get_evm_network_from_env() {
Ok(evm_network) => Ok(evm_network),
Err(_) if opt.peers.local => Ok(local_evm_network_from_csv()?),
None => match get_evm_network(opt.peers.local) {
Ok(net) => Ok(net),
Err(_) => Err(eyre!(
"EVM network not specified. Please specify a network using the subcommand or by setting the `EVM_NETWORK` environment variable."
)),
Expand Down
3 changes: 1 addition & 2 deletions autonomi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ extern crate tracing;
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::utils::get_evm_network;
pub use ant_evm::Amount;
pub use ant_evm::EvmNetwork as Network;
pub use ant_evm::EvmWallet as Wallet;
Expand Down
6 changes: 3 additions & 3 deletions autonomi/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// 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 ant_evm::get_evm_network_from_env;
use ant_evm::get_evm_network;
use ant_evm::EvmWallet;
use ant_evm::{Amount, RewardsAddress};
use ant_logging::LogBuilder;
Expand All @@ -17,7 +17,7 @@ use test_utils::evm::get_funded_wallet;
async fn from_private_key() {
let private_key = "0xdb1049e76a813c94be0df47ec3e20533ca676b1b9fef2ddbce9daa117e4da4aa";
let network =
get_evm_network_from_env().expect("Could not get EVM network from environment variables");
get_evm_network(true).expect("Could not get EVM network from environment variables");
let wallet = EvmWallet::new_from_private_key(network, private_key).unwrap();

assert_eq!(
Expand All @@ -31,7 +31,7 @@ async fn send_tokens() {
let _log_appender_guard = LogBuilder::init_single_threaded_tokio_test("wallet", false);

let network =
get_evm_network_from_env().expect("Could not get EVM network from environment variables");
get_evm_network(true).expect("Could not get EVM network from environment variables");
let wallet = get_funded_wallet();

let receiving_wallet = EvmWallet::new_with_random_wallet(network);
Expand Down
32 changes: 30 additions & 2 deletions evmlib/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,34 @@ pub fn dummy_hash() -> Hash {
Hash::new(rand::rngs::OsRng.gen())
}

use std::sync::OnceLock;

static EVM_NETWORK: OnceLock<Network> = OnceLock::new();

/// Initialize the EVM Network parameters from environment variables or local CSV file.
///
/// It will first try to get the network from the environment variables.
/// If it fails and `local` is true, it will try to get the network from the local CSV file.
/// If both fail, it will return the default network.
pub fn get_evm_network(local: bool) -> Result<Network, Error> {
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()
.map_err(|e| Error::FailedToGetEvmNetwork(e.to_string()))?),
Err(_) => Ok(Network::default()),
};

if let Ok(network) = res.as_ref() {
let _ = EVM_NETWORK.set(network.clone());
}

res
}

pub fn get_evm_testnet_csv_path() -> Result<PathBuf, Error> {
let file = data_dir()
.ok_or(Error::FailedToGetEvmNetwork(
Expand All @@ -60,7 +88,7 @@ pub fn get_evm_testnet_csv_path() -> Result<PathBuf, Error> {
/// Get the `Network` from environment variables.
///
/// Returns an error if we cannot obtain the network from any means.
pub fn get_evm_network_from_env() -> Result<Network, Error> {
fn get_evm_network_from_env() -> Result<Network, Error> {
let evm_vars = [
env::var(RPC_URL)
.ok()
Expand Down Expand Up @@ -126,7 +154,7 @@ pub fn get_evm_network_from_env() -> Result<Network, Error> {
}

/// Get the `Network::Custom` from the local EVM testnet CSV file
pub fn local_evm_network_from_csv() -> Result<Network, Error> {
fn local_evm_network_from_csv() -> Result<Network, Error> {
// load the csv
let csv_path = get_evm_testnet_csv_path()?;

Expand Down
12 changes: 4 additions & 8 deletions test-utils/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@
// 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 color_eyre::{
eyre::{bail, Context},
Result,
};
use evmlib::{utils::local_evm_network_from_csv, wallet::Wallet, Network};
use color_eyre::{eyre::bail, Result};
use evmlib::{utils::get_evm_network, wallet::Wallet, Network};
use std::env;

pub fn get_funded_wallet() -> evmlib::wallet::Wallet {
let network = local_evm_network_from_csv().expect("Failed to get local EVM network from CSV");
let network = get_evm_network(true).expect("Failed to get local EVM network from CSV");
if matches!(network, Network::ArbitrumOne) {
panic!("You're trying to use ArbitrumOne network. Use a custom network for testing.");
}
Expand All @@ -28,8 +25,7 @@ pub fn get_funded_wallet() -> evmlib::wallet::Wallet {
}

pub fn get_new_wallet() -> Result<Wallet> {
let network =
local_evm_network_from_csv().wrap_err("Failed to get local EVM network from CSV")?;
let network = get_evm_network(true).expect("Failed to get local EVM network from CSV");
if matches!(network, Network::ArbitrumOne) {
bail!("You're trying to use ArbitrumOne network. Use a custom network for testing.");
}
Expand Down

0 comments on commit 46de268

Please sign in to comment.