Skip to content

Commit

Permalink
Merge pull request #9 from mickvandijke/local_as_runtime_opt
Browse files Browse the repository at this point in the history
Local as runtime opt
  • Loading branch information
grumbach authored Jan 7, 2025
2 parents c0d6bfd + 339b13f commit e7ea9bc
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 12 deletions.
24 changes: 18 additions & 6 deletions ant-cli/src/actions/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,41 @@
// 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<Client> {
let progress_bar = ProgressBar::new_spinner();
progress_bar.enable_steady_tick(Duration::from_millis(120));
progress_bar.set_message("Connecting to The Autonomi Network...");
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");
Expand Down
2 changes: 1 addition & 1 deletion ant-cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions ant-cli/src/commands/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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?;
Expand Down
24 changes: 24 additions & 0 deletions ant-cli/src/evm_network.rs
Original file line number Diff line number Diff line change
@@ -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<Network> = OnceLock::new();

pub(crate) fn get_evm_network(local: bool) -> Result<Network> {
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
}
1 change: 1 addition & 0 deletions ant-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ extern crate tracing;
mod access;
mod actions;
mod commands;
mod evm_network;
mod opt;
mod utils;
mod wallet;
Expand Down
6 changes: 5 additions & 1 deletion autonomi/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ pub struct ClientConfig {
///
/// If not provided, the client will use the default bootstrap peers.
pub peers: Option<Vec<Multiaddr>>,

/// EVM network to use for quotations and payments.
pub evm_network: EvmNetwork,
}

/// Error returned by [`Client::init`].
Expand Down Expand Up @@ -136,6 +139,7 @@ impl Client {
Self::init_with_config(ClientConfig {
local,
peers: Some(peers),
evm_network: Default::default(),
})
.await
}
Expand Down Expand Up @@ -188,7 +192,7 @@ impl Client {
Ok(Self {
network,
client_event_sender: Arc::new(None),
evm_network: Default::default(),
evm_network: config.evm_network,
})
}

Expand Down
1 change: 1 addition & 0 deletions autonomi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion evmlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ impl CustomNetwork {

#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub enum Network {
#[default]
ArbitrumOne,
#[default]
ArbitrumSepolia,
Custom(CustomNetwork),
}
Expand Down

0 comments on commit e7ea9bc

Please sign in to comment.