Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: evm network from env vars #2170

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions autonomi_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ autonomi = { path = "../autonomi", version = "0.1.0", features = ["data", "files
clap = { version = "4.2.1", features = ["derive"] }
color-eyre = "~0.6"
dirs-next = "~2.0.0"
evmlib = { path = "../evmlib", version = "0.1.0" }
indicatif = { version = "0.17.5", features = ["tokio"] }
tokio = { version = "1.32.0", features = [
"io-util",
Expand Down
2 changes: 1 addition & 1 deletion autonomi_cli/src/commands/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub async fn cost(file: &str, peers: Vec<Multiaddr>) -> Result<()> {
pub async fn upload(file: &str, peers: Vec<Multiaddr>) -> Result<()> {
let secret_key = crate::utils::get_secret_key()
.wrap_err("The secret key is required to perform this action")?;
let network = crate::utils::get_evm_network().wrap_err("Failed to get evm network")?;
let network = crate::utils::get_evm_network_from_environment()?;
let wallet =
Wallet::new_from_private_key(network, &secret_key).wrap_err("Failed to load wallet")?;

Expand Down
7 changes: 3 additions & 4 deletions autonomi_cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ pub async fn get_peers(peers: PeersArgs) -> Result<Vec<Multiaddr>> {
.with_suggestion(|| "a peer address looks like this: /ip4/42.42.42.42/udp/4242/quic-v1/p2p/B64nodePeerIDvdjb3FAJF4ks3moreBase64CharsHere".to_string())
}

pub(crate) fn get_evm_network() -> Result<Network> {
// NB TODO load custom network from config file/env/cmd line
let network = Network::ArbitrumOne;
Ok(network)
pub(crate) fn get_evm_network_from_environment() -> Result<Network> {
evmlib::utils::evm_network_from_env()
.map_err(|err| eyre!("Failed to get EVM network from environment: {err}"))
}
21 changes: 20 additions & 1 deletion evmlib/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
use crate::common::{Address, Hash};
use crate::{CustomNetwork, Network};
use rand::Rng;
use std::env;
use std::env::VarError;

/// Generate a random Address.
pub fn dummy_address() -> Address {
Address::new(rand::rngs::OsRng.gen())
}

/// generate a random Hash.
/// Generate a random Hash.
pub fn dummy_hash() -> Hash {
Hash::new(rand::rngs::OsRng.gen())
}

/// Get the `Network` from environment variables
pub fn evm_network_from_env() -> Result<Network, VarError> {
const EVM_VARS: [&str; 3] = ["RPC_URL", "PAYMENT_TOKEN_ADDRESS", "CHUNK_PAYMENTS_ADDRESS"];
let custom_vars_exist = EVM_VARS.iter().all(|var| env::var(var).is_ok());

if custom_vars_exist {
Ok(Network::Custom(CustomNetwork::new(
&env::var(EVM_VARS[0])?,
&env::var(EVM_VARS[1])?,
&env::var(EVM_VARS[2])?,
)))
} else {
Ok(Network::ArbitrumOne)
}
}
Loading