Skip to content

Commit

Permalink
Add wallets_path to InitArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
matias-gonz committed Nov 15, 2024
1 parent 48490c1 commit 4b83c2a
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 13 deletions.
6 changes: 5 additions & 1 deletion zkstack_cli/crates/config/src/ecosystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ impl EcosystemConfig {
}

pub fn get_wallets(&self) -> anyhow::Result<WalletsConfig> {
let path = self.config.join(WALLETS_FILE);
let path = self.get_wallets_path();
if self.get_shell().path_exists(&path) {
return WalletsConfig::read(self.get_shell(), &path);
}
Expand Down Expand Up @@ -276,6 +276,10 @@ impl EcosystemConfig {
pub fn get_contracts_path(&self) -> PathBuf {
self.config.join(CONTRACTS_FILE)
}

pub fn get_wallets_path(&self) -> PathBuf {
self.config.join(WALLETS_FILE)
}
}

/// Result of checking if the ecosystem exists.
Expand Down
21 changes: 18 additions & 3 deletions zkstack_cli/crates/zkstack/src/commands/chain/args/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{
MSG_DEPLOY_PAYMASTER_PROMPT, MSG_DEV_ARG_HELP, MSG_ECOSYSTEM_CONTRACTS_PATH_HELP,
MSG_L1_RPC_URL_HELP, MSG_L1_RPC_URL_INVALID_ERR, MSG_L1_RPC_URL_PROMPT,
MSG_NO_PORT_REALLOCATION_HELP, MSG_SERVER_DB_NAME_HELP, MSG_SERVER_DB_URL_HELP,
MSG_WALLETS_PATH_HELP, MSG_WALLETS_PATH_PROMPT,
},
};

Expand All @@ -40,6 +41,8 @@ pub struct InitArgs {
pub no_port_reallocation: bool,
#[clap(long, help = MSG_ECOSYSTEM_CONTRACTS_PATH_HELP)]
pub ecosystem_contracts_path: Option<String>,
#[clap(long, help = MSG_WALLETS_PATH_HELP)]
pub wallets_path: Option<String>,
#[clap(long, help = MSG_DEV_ARG_HELP)]
pub dev: bool,
}
Expand Down Expand Up @@ -92,26 +95,37 @@ impl InitArgs {
let ecosystem_contracts_path = if self.dev {
self.ecosystem_contracts_path.map_or_else(
|| {
ecosystem.map_or_else(
ecosystem.as_ref().map_or_else(
|| chain.get_preexisting_ecosystem_contracts_path(),
|e| e.get_contracts_path(),
)
},
PathBuf::from,
)
} else if let Some(ecosystem) = ecosystem {
} else if let Some(ecosystem) = &ecosystem {
ecosystem.get_contracts_path()
} else {
get_ecosystem_contracts_path(self.ecosystem_contracts_path, ecosystem, chain)?
get_ecosystem_contracts_path(self.ecosystem_contracts_path, ecosystem.clone(), chain)?
};

let wallets_path = ecosystem.map_or_else(
|| {
self.wallets_path.map_or_else(
|| Prompt::new(MSG_WALLETS_PATH_PROMPT).allow_empty().ask(),
PathBuf::from,
)
},
|e| e.get_wallets_path(),
);

Ok(InitArgsFinal {
forge_args: self.forge_args,
genesis_args: genesis.fill_values_with_prompt(chain),
deploy_paymaster,
l1_rpc_url,
no_port_reallocation: self.no_port_reallocation,
ecosystem_contracts_path,
wallets_path,
dev: self.dev,
})
}
Expand All @@ -125,5 +139,6 @@ pub struct InitArgsFinal {
pub l1_rpc_url: String,
pub no_port_reallocation: bool,
pub ecosystem_contracts_path: PathBuf,
pub wallets_path: PathBuf,
pub dev: bool,
}
15 changes: 9 additions & 6 deletions zkstack_cli/crates/zkstack/src/commands/chain/common.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use anyhow::Context;
use common::spinner::Spinner;
use config::{ChainConfig, EcosystemConfig};
use config::{ChainConfig, WalletsConfig};
use types::{BaseToken, L1Network, WalletCreation};

use crate::{
consts::AMOUNT_FOR_DISTRIBUTION_TO_WALLETS,
messages::{MSG_DISTRIBUTING_ETH_SPINNER, MSG_MINT_BASE_TOKEN_SPINNER},
messages::{
MSG_DISTRIBUTING_ETH_SPINNER, MSG_MINT_BASE_TOKEN_SPINNER, MSG_MISSING_WALLETS_CONFIG,
},
};

// Distribute eth to the chain wallets for localhost environment
pub async fn distribute_eth(
ecosystem_config: &EcosystemConfig,
chain_config: &ChainConfig,
l1_rpc_url: String,
wallets: Option<WalletsConfig>,
) -> anyhow::Result<()> {
if chain_config.wallet_creation != WalletCreation::Localhost
|| chain_config.l1_network != L1Network::Localhost
Expand All @@ -20,7 +23,7 @@ pub async fn distribute_eth(
}

let spinner = Spinner::new(MSG_DISTRIBUTING_ETH_SPINNER);
let wallets = ecosystem_config.get_wallets()?;
let wallets = wallets.context(MSG_MISSING_WALLETS_CONFIG)?;
let chain_wallets = chain_config.get_wallets_config()?;
let mut addresses = vec![
chain_wallets.operator.address,
Expand All @@ -47,9 +50,9 @@ pub async fn distribute_eth(
}

pub async fn mint_base_token(
ecosystem_config: &EcosystemConfig,
chain_config: &ChainConfig,
l1_rpc_url: String,
wallets: Option<WalletsConfig>,
) -> anyhow::Result<()> {
if chain_config.wallet_creation != WalletCreation::Localhost
|| chain_config.l1_network != L1Network::Localhost
Expand All @@ -59,7 +62,7 @@ pub async fn mint_base_token(
}

let spinner = Spinner::new(MSG_MINT_BASE_TOKEN_SPINNER);
let wallets = ecosystem_config.get_wallets()?;
let wallets = wallets.context(MSG_MISSING_WALLETS_CONFIG)?;
let chain_wallets = chain_config.get_wallets_config()?;
let base_token = &chain_config.base_token;
let addresses = vec![wallets.governor.address, chain_wallets.governor.address];
Expand Down
9 changes: 6 additions & 3 deletions zkstack_cli/crates/zkstack/src/commands/chain/init/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use anyhow::Context;
use clap::{command, Parser, Subcommand};
use common::{git, logger, spinner::Spinner};
use config::{
traits::SaveConfigWithBasePath, zkstack_config::ZkStackConfig, ChainConfig, EcosystemConfig,
traits::{ReadConfig, SaveConfigWithBasePath},
zkstack_config::ZkStackConfig,
ChainConfig, EcosystemConfig, WalletsConfig,
};
use types::BaseToken;
use xshell::Shell;
Expand Down Expand Up @@ -81,8 +83,9 @@ pub async fn init(
let mut contracts_config = init_configs(&init_configs_args, shell, chain_config).await?;

// Fund some wallet addresses with ETH or base token (only for Localhost)
distribute_eth(ecosystem_config, chain_config, init_args.l1_rpc_url.clone()).await?;
mint_base_token(ecosystem_config, chain_config, init_args.l1_rpc_url.clone()).await?;
let wallets = WalletsConfig::read(shell, init_args.wallets_path.clone()).ok();
distribute_eth(chain_config, init_args.l1_rpc_url.clone(), wallets.clone()).await?;
mint_base_token(chain_config, init_args.l1_rpc_url.clone(), wallets).await?;

// Register chain on BridgeHub (run by L1 Governor)
let spinner = Spinner::new(MSG_REGISTERING_CHAIN_SPINNER);
Expand Down
2 changes: 2 additions & 0 deletions zkstack_cli/crates/zkstack/src/commands/ecosystem/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ async fn init_chains(
}
let ecosystem_contracts_path =
Some(ecosystem_config.get_contracts_path().display().to_string());
let wallets_path = Some(ecosystem_config.get_wallets_path().display().to_string());
logger::debug(format!(
"Ecosystem contracts path: {:?}",
ecosystem_contracts_path
Expand All @@ -371,6 +372,7 @@ async fn init_chains(
no_port_reallocation: final_init_args.no_port_reallocation,
dev: final_init_args.dev,
ecosystem_contracts_path: ecosystem_contracts_path.clone(),
wallets_path: wallets_path.clone(),
};
let final_chain_init_args = chain_init_args
.fill_values_with_prompt(Some(ecosystem_config.clone()), &chain_config)?;
Expand Down
3 changes: 3 additions & 0 deletions zkstack_cli/crates/zkstack/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub(super) fn msg_path_to_zksync_does_not_exist_err(path: &str) -> String {

/// Ecosystem and chain init related messages
pub(super) const MSG_ECOSYSTEM_CONTRACTS_PATH_HELP: &str = "Ecosystem contracts path";
pub(super) const MSG_WALLETS_PATH_HELP: &str = "Wallets path";
pub(super) const MSG_L1_RPC_URL_HELP: &str = "L1 RPC URL";
pub(super) const MSG_NO_PORT_REALLOCATION_HELP: &str = "Do not reallocate ports";
pub(super) const MSG_GENESIS_ARGS_HELP: &str = "Genesis options";
Expand Down Expand Up @@ -103,6 +104,8 @@ pub(super) const MSG_RECREATE_ROCKS_DB_ERRROR: &str = "Failed to create rocks db
pub(super) const MSG_ERA_OBSERVABILITY_ALREADY_SETUP: &str = "Era observability already setup";
pub(super) const MSG_DOWNLOADING_ERA_OBSERVABILITY_SPINNER: &str =
"Downloading era observability...";
pub(super) const MSG_MISSING_WALLETS_CONFIG: &str = "Missing wallets config";
pub(super) const MSG_WALLETS_PATH_PROMPT: &str = "Provide the path to the wallets";

pub(super) fn msg_ecosystem_no_found_preexisting_contract(chains: &str) -> String {
format!("Not found preexisting ecosystem Contracts with chains {chains}")
Expand Down

0 comments on commit 4b83c2a

Please sign in to comment.