From 4b83c2a8c3e41b038b8c9270214c21babb7d58e6 Mon Sep 17 00:00:00 2001 From: matias-gonz Date: Fri, 15 Nov 2024 13:58:38 -0300 Subject: [PATCH] Add wallets_path to InitArgs --- zkstack_cli/crates/config/src/ecosystem.rs | 6 +++++- .../src/commands/chain/args/init/mod.rs | 21 ++++++++++++++++--- .../zkstack/src/commands/chain/common.rs | 15 +++++++------ .../zkstack/src/commands/chain/init/mod.rs | 9 +++++--- .../zkstack/src/commands/ecosystem/init.rs | 2 ++ zkstack_cli/crates/zkstack/src/messages.rs | 3 +++ 6 files changed, 43 insertions(+), 13 deletions(-) diff --git a/zkstack_cli/crates/config/src/ecosystem.rs b/zkstack_cli/crates/config/src/ecosystem.rs index 5bd11d9d683..3bc05ac4e0b 100644 --- a/zkstack_cli/crates/config/src/ecosystem.rs +++ b/zkstack_cli/crates/config/src/ecosystem.rs @@ -197,7 +197,7 @@ impl EcosystemConfig { } pub fn get_wallets(&self) -> anyhow::Result { - 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); } @@ -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. diff --git a/zkstack_cli/crates/zkstack/src/commands/chain/args/init/mod.rs b/zkstack_cli/crates/zkstack/src/commands/chain/args/init/mod.rs index f7289990065..f2c8444fd29 100644 --- a/zkstack_cli/crates/zkstack/src/commands/chain/args/init/mod.rs +++ b/zkstack_cli/crates/zkstack/src/commands/chain/args/init/mod.rs @@ -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, }, }; @@ -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, + #[clap(long, help = MSG_WALLETS_PATH_HELP)] + pub wallets_path: Option, #[clap(long, help = MSG_DEV_ARG_HELP)] pub dev: bool, } @@ -92,19 +95,29 @@ 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), @@ -112,6 +125,7 @@ impl InitArgs { l1_rpc_url, no_port_reallocation: self.no_port_reallocation, ecosystem_contracts_path, + wallets_path, dev: self.dev, }) } @@ -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, } diff --git a/zkstack_cli/crates/zkstack/src/commands/chain/common.rs b/zkstack_cli/crates/zkstack/src/commands/chain/common.rs index 92a2d6f2674..0b41ab5b552 100644 --- a/zkstack_cli/crates/zkstack/src/commands/chain/common.rs +++ b/zkstack_cli/crates/zkstack/src/commands/chain/common.rs @@ -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, ) -> anyhow::Result<()> { if chain_config.wallet_creation != WalletCreation::Localhost || chain_config.l1_network != L1Network::Localhost @@ -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, @@ -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, ) -> anyhow::Result<()> { if chain_config.wallet_creation != WalletCreation::Localhost || chain_config.l1_network != L1Network::Localhost @@ -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]; diff --git a/zkstack_cli/crates/zkstack/src/commands/chain/init/mod.rs b/zkstack_cli/crates/zkstack/src/commands/chain/init/mod.rs index 3e804a67306..145c442aa8b 100644 --- a/zkstack_cli/crates/zkstack/src/commands/chain/init/mod.rs +++ b/zkstack_cli/crates/zkstack/src/commands/chain/init/mod.rs @@ -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; @@ -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); diff --git a/zkstack_cli/crates/zkstack/src/commands/ecosystem/init.rs b/zkstack_cli/crates/zkstack/src/commands/ecosystem/init.rs index 98f35bd4d24..2f107d9547b 100644 --- a/zkstack_cli/crates/zkstack/src/commands/ecosystem/init.rs +++ b/zkstack_cli/crates/zkstack/src/commands/ecosystem/init.rs @@ -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 @@ -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)?; diff --git a/zkstack_cli/crates/zkstack/src/messages.rs b/zkstack_cli/crates/zkstack/src/messages.rs index 1a6fdb5939a..29f1b9e193f 100644 --- a/zkstack_cli/crates/zkstack/src/messages.rs +++ b/zkstack_cli/crates/zkstack/src/messages.rs @@ -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"; @@ -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}")