diff --git a/src/contract_clients/config.rs b/src/contract_clients/config.rs index c6b91fa..cfe8997 100644 --- a/src/contract_clients/config.rs +++ b/src/contract_clients/config.rs @@ -2,14 +2,14 @@ use ethereum_instance::EthereumClient; use starknet::providers::jsonrpc::{HttpTransport, JsonRpcClient}; use starknet::providers::Url; -use crate::CliArgs; +use crate::ConfigFile; -pub struct Config { +pub struct Clients { eth_client: EthereumClient, provider_l2: JsonRpcClient, } -impl Config { +impl Clients { pub fn provider_l2(&self) -> &JsonRpcClient { &self.provider_l2 } @@ -19,16 +19,31 @@ impl Config { } /// To deploy the instance of ethereum and starknet and returning the struct. - pub async fn init(config: &CliArgs) -> Self { + // pub async fn init(config: &CliArgs) -> Self { + // let client_instance = EthereumClient::attach( + // Option::from(config.eth_rpc.clone()), + // Option::from(config.eth_priv_key.clone()), + // Option::from(config.eth_chain_id), + // ) + // .unwrap(); + // + // let provider_l2 = JsonRpcClient::new(HttpTransport::new( + // Url::parse(&config.rollup_seq_url).expect("Failed to declare provider for app chain"), + // )); + // + // Self { eth_client: client_instance, provider_l2 } + // } + + pub async fn init_from_config(config_file: &ConfigFile) -> Self { let client_instance = EthereumClient::attach( - Option::from(config.eth_rpc.clone()), - Option::from(config.eth_priv_key.clone()), - Option::from(config.eth_chain_id), + Option::from(config_file.eth_rpc.clone()), + Option::from(config_file.eth_priv_key.clone()), + Option::from(config_file.eth_chain_id), ) .unwrap(); let provider_l2 = JsonRpcClient::new(HttpTransport::new( - Url::parse(&config.rollup_seq_url).expect("Failed to declare provider for app chain"), + Url::parse(&config_file.rollup_seq_url).expect("Failed to declare provider for app chain"), )); Self { eth_client: client_instance, provider_l2 } diff --git a/src/contract_clients/core_contract.rs b/src/contract_clients/core_contract.rs index d9074ca..5d85bf6 100644 --- a/src/contract_clients/core_contract.rs +++ b/src/contract_clients/core_contract.rs @@ -9,7 +9,7 @@ use starknet::core::types::Felt; use starknet_proxy_client::interfaces::proxy::{CoreContractInitData, CoreContractState}; use zaun_utils::LocalWalletSignerMiddleware; -use crate::contract_clients::config::Config; +use crate::contract_clients::config::Clients; use crate::utils::convert_felt_to_u256; #[async_trait] @@ -68,7 +68,7 @@ pub trait CoreContract { } pub trait CoreContractDeploy { - fn deploy(config: &Config) -> impl Future + Send; + fn deploy(clients: &Clients) -> impl Future + Send; } pub fn get_init_data_core_contract( diff --git a/src/contract_clients/starknet_sovereign.rs b/src/contract_clients/starknet_sovereign.rs index 5641058..2b1fcd3 100644 --- a/src/contract_clients/starknet_sovereign.rs +++ b/src/contract_clients/starknet_sovereign.rs @@ -9,7 +9,7 @@ use starknet_core_contract_client::interfaces::{OperatorTrait, StarknetGovernanc use starknet_proxy_client::interfaces::proxy::{CoreContractInitData, ProxyInitializeData, ProxySupport3_0_2Trait}; use zaun_utils::{LocalWalletSignerMiddleware, StarknetContractClient}; -use crate::contract_clients::config::Config; +use crate::contract_clients::config::Clients; use crate::contract_clients::core_contract::{ get_calldata_bytes, get_init_data_core_contract, CoreContract, CoreContractDeploy, }; @@ -20,8 +20,8 @@ pub struct StarknetSovereignContract { } impl CoreContractDeploy for StarknetSovereignContract { - async fn deploy(config: &Config) -> Self { - let client = deploy_starknet_sovereign_behind_unsafe_proxy(config.eth_client().signer().clone()) + async fn deploy(clients: &Clients) -> Self { + let client = deploy_starknet_sovereign_behind_unsafe_proxy(clients.eth_client().signer().clone()) .await .expect("Failed to deploy the starknet contact"); diff --git a/src/contract_clients/starknet_validity.rs b/src/contract_clients/starknet_validity.rs index 6582063..8dc6c83 100644 --- a/src/contract_clients/starknet_validity.rs +++ b/src/contract_clients/starknet_validity.rs @@ -9,7 +9,7 @@ use starknet_core_contract_client::interfaces::{OperatorTrait, StarknetGovernanc use starknet_proxy_client::interfaces::proxy::{CoreContractInitData, ProxyInitializeData, ProxySupport3_0_2Trait}; use zaun_utils::{LocalWalletSignerMiddleware, StarknetContractClient}; -use crate::contract_clients::config::Config; +use crate::contract_clients::config::Clients; use crate::contract_clients::core_contract::{ get_calldata_bytes, get_init_data_core_contract, CoreContract, CoreContractDeploy, }; @@ -20,8 +20,8 @@ pub struct StarknetValidityContract { } impl CoreContractDeploy for StarknetValidityContract { - async fn deploy(config: &Config) -> Self { - let client = deploy_starknet_validity_behind_safe_proxy(config.eth_client().signer().clone()) + async fn deploy(clients: &Clients) -> Self { + let client = deploy_starknet_validity_behind_safe_proxy(clients.eth_client().signer().clone()) .await .expect("Failed to deploy the starknet contact"); diff --git a/src/contract_clients/utils.rs b/src/contract_clients/utils.rs index 5c2dd06..490879f 100644 --- a/src/contract_clients/utils.rs +++ b/src/contract_clients/utils.rs @@ -21,7 +21,7 @@ use crate::contract_clients::legacy_class::CompressedLegacyContractClass; use crate::contract_clients::utils::DeclarationInput::{DeclarationInputs, LegacyDeclarationInputs}; use crate::helpers::account_actions::{get_contract_address_from_deploy_tx, AccountActions}; use crate::utils::{invoke_contract, save_to_json, wait_for_transaction, JsonValueType}; -use crate::CliArgs; +use crate::ConfigFile; pub type RpcAccount<'a> = SingleOwnerAccount<&'a JsonRpcClient, LocalWallet>; pub async fn build_single_owner_account<'a>( @@ -75,7 +75,7 @@ pub fn generate_config_hash( Pedersen::hash_array(&[config_hash_version, chain_id, fee_token_address, native_fee_token_address]) } -pub fn get_bridge_init_configs(config: &CliArgs) -> (Felt, Felt) { +pub fn get_bridge_init_configs(config: &ConfigFile) -> (Felt, Felt) { let program_hash = Felt::from_hex(config.sn_os_program_hash.as_str()).unwrap(); let config_hash = generate_config_hash( diff --git a/src/main.rs b/src/main.rs index cc90275..e967720 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,14 +6,15 @@ pub mod tests; pub mod utils; use std::fs::File; +use std::path::PathBuf; use std::str::FromStr; -use clap::{ArgAction, Parser, ValueEnum}; +use clap::{Parser, ValueEnum}; use contract_clients::utils::RpcAccount; use dotenv::dotenv; use ethers::abi::Address; use inline_colorization::*; -use serde::Serialize; +use serde::{Deserialize, Serialize}; use setup_scripts::argent::ArgentSetupOutput; use setup_scripts::braavos::BraavosSetupOutput; use setup_scripts::core_contract::CoreContractStarknetL1Output; @@ -23,7 +24,7 @@ use setup_scripts::udc::UdcSetupOutput; use starknet::accounts::Account; use starknet_core_contract_client::clients::StarknetValidityContractClient; -use crate::contract_clients::config::Config; +use crate::contract_clients::config::Clients; use crate::contract_clients::starknet_validity::StarknetValidityContract; use crate::setup_scripts::account_setup::account_init; use crate::setup_scripts::argent::ArgentSetup; @@ -50,58 +51,65 @@ enum BootstrapMode { #[derive(Parser, Debug)] #[command(version, about, long_about = None)] pub struct CliArgs { - #[clap(long, env, default_value = "http://127.0.0.1:8545")] - eth_rpc: String, - #[clap(long, env, default_value = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80")] - eth_priv_key: String, - #[clap(long, env, default_value = "http://127.0.0.1:9944")] - rollup_seq_url: String, - #[clap(long, env, default_value = "0xabcd")] - rollup_priv_key: String, - #[clap(long, env, default_value_t = 31337)] - eth_chain_id: u64, - #[clap(long, env, default_value = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266")] - l1_deployer_address: String, - #[clap(long, env, default_value = "15")] - l1_wait_time: String, - #[clap(long, env, default_value = "0x41fc2a467ef8649580631912517edcab7674173f1dbfa2e9b64fbcd82bc4d79")] - sn_os_program_hash: String, - #[clap(long, env, default_value = "StarknetOsConfig1")] - config_hash_version: String, - #[clap(long, env, default_value = "MADARA_DEVNET")] - app_chain_id: String, - #[clap(long, env, default_value = "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7")] - fee_token_address: String, - #[clap(long, env, default_value = "0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d")] - native_fee_token_address: String, - #[clap(long, env, default_value_t = 20)] - cross_chain_wait_time: u64, - // Default test address value taken from anvil - // IMP : Not to be used in prod environment - #[clap(long, env, default_value = "0x70997970C51812dc3A010C7d01b50e0d17dc79C8")] - l1_multisig_address: String, - // Default test address value taken from starknet-devnet - // IMP : Not to be used in prod environment - #[clap(long, env, default_value = "0x556455b8ac8bc00e0ad061d7df5458fa3c372304877663fa21d492a8d5e9435")] - l2_multisig_address: String, - // Given as 0xabcd by default - #[clap(long, env, default_value = "0x000000000000000000000000000000000000abcd")] - verifier_address: String, - // Given as 0xabcd by default - #[clap(long, env, default_value = "0x000000000000000000000000000000000000abcd")] - operator_address: String, - #[clap(long, env, action=ArgAction::SetTrue)] - dev: bool, + #[clap(long)] + config: Option, #[clap(long, env, value_enum)] mode: BootstrapMode, #[clap(long, env)] - core_contract_address: Option, - #[clap(long, env)] - core_contract_implementation_address: Option, - #[clap(long, env)] output_file: Option, } +#[derive(Serialize, Deserialize)] +pub struct ConfigFile { + pub eth_rpc: String, + pub eth_priv_key: String, + pub rollup_seq_url: String, + pub rollup_priv_key: String, + pub eth_chain_id: u64, + pub l1_deployer_address: String, + pub l1_wait_time: String, + pub sn_os_program_hash: String, + pub config_hash_version: String, + pub app_chain_id: String, + pub fee_token_address: String, + pub native_fee_token_address: String, + pub cross_chain_wait_time: u64, + pub l1_multisig_address: String, + pub l2_multisig_address: String, + pub verifier_address: String, + pub operator_address: String, + pub dev: bool, + pub core_contract_address: Option, + pub core_contract_implementation_address: Option, +} + +impl Default for ConfigFile { + fn default() -> Self { + Self { + eth_rpc: "http://127.0.0.1:8545".to_string(), + eth_priv_key: "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80".to_string(), + rollup_seq_url: "http://127.0.0.1:9944".to_string(), + rollup_priv_key: "0xabcd".to_string(), + eth_chain_id: 31337, + l1_deployer_address: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266".to_string(), + l1_wait_time: "15".to_string(), + sn_os_program_hash: "0x41fc2a467ef8649580631912517edcab7674173f1dbfa2e9b64fbcd82bc4d79".to_string(), + config_hash_version: "StarknetOsConfig2".to_string(), + app_chain_id: "MADARA_DEVNET".to_string(), + fee_token_address: "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7".to_string(), + native_fee_token_address: "0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d".to_string(), + cross_chain_wait_time: 20, + l1_multisig_address: "0x70997970C51812dc3A010C7d01b50e0d17dc79C8".to_string(), + l2_multisig_address: "0x556455b8ac8bc00e0ad061d7df5458fa3c372304877663fa21d492a8d5e9435".to_string(), + verifier_address: "0x000000000000000000000000000000000000abcd".to_string(), + operator_address: "0x000000000000000000000000000000000000abcd".to_string(), + dev: false, + core_contract_address: None, + core_contract_implementation_address: None, + } + } +} + #[tokio::main] pub async fn main() { env_logger::init(); @@ -111,11 +119,20 @@ pub async fn main() { println!("{color_red}{}{color_reset}", BANNER); - let config = Config::init(&args).await; + // Load config from file or use defaults + let config_file = match args.config { + Some(path) => { + let file = File::open(path).expect("Failed to open config file"); + serde_json::from_reader(file).expect("Failed to parse config file") + } + None => ConfigFile::default(), + }; + + let clients = Clients::init_from_config(&config_file).await; let output = match args.mode { BootstrapMode::Core | BootstrapMode::SetupL1 => { - let output = setup_core_contract(&args, &config).await; + let output = setup_core_contract(&config_file, &clients).await; BootstrapperOutput { starknet_contract_address: Some(output.core_contract_client.address()), @@ -123,27 +140,27 @@ pub async fn main() { ..Default::default() } } - BootstrapMode::SetupL2 => setup_l2(&args, &config).await, + BootstrapMode::SetupL2 => setup_l2(&config_file, &clients).await, BootstrapMode::EthBridge => { - let core_contract_client = get_core_contract_client(&args, &config); - let output = setup_eth_bridge(None, &core_contract_client, &args, &config).await; + let core_contract_client = get_core_contract_client(&config_file, &clients); + let output = setup_eth_bridge(None, &core_contract_client, &config_file, &clients).await; BootstrapperOutput { eth_bridge_setup_outputs: Some(output), ..Default::default() } } BootstrapMode::Erc20Bridge => { - let core_contract_client = get_core_contract_client(&args, &config); - let output = setup_erc20_bridge(None, &core_contract_client, &args, &config).await; + let core_contract_client = get_core_contract_client(&config_file, &clients); + let output = setup_erc20_bridge(None, &core_contract_client, &config_file, &clients).await; BootstrapperOutput { erc20_bridge_setup_outputs: Some(output), ..Default::default() } } BootstrapMode::Udc => { - let output = setup_udc(None, &args, &config).await; + let output = setup_udc(None, &config_file, &clients).await; BootstrapperOutput { udc_setup_outputs: Some(output), ..Default::default() } } BootstrapMode::Argent => { - let output = setup_argent(None, &args, &config).await; + let output = setup_argent(None, &config_file, &clients).await; BootstrapperOutput { argent_setup_outputs: Some(output), ..Default::default() } } BootstrapMode::Braavos => { - let output = setup_braavos(None, &args, &config).await; + let output = setup_braavos(None, &config_file, &clients).await; BootstrapperOutput { braavos_setup_outputs: Some(output), ..Default::default() } } }; @@ -162,24 +179,24 @@ pub async fn main() { } } -fn get_core_contract_client(args: &CliArgs, config: &Config) -> CoreContractStarknetL1Output { - let Some(core_contract_address) = args.core_contract_address.clone() else { +fn get_core_contract_client(config_file: &ConfigFile, clients: &Clients) -> CoreContractStarknetL1Output { + let Some(core_contract_address) = config_file.core_contract_address.clone() else { panic!("Core contract address is required for ETH bridge setup"); }; - let Some(core_contract_implementation_address) = args.core_contract_implementation_address.clone() else { + let Some(core_contract_implementation_address) = config_file.core_contract_implementation_address.clone() else { panic!("Core contract implementation address is required for ETH bridge setup"); }; let core_contract_client = StarknetValidityContractClient::new( Address::from_str(&core_contract_address).unwrap(), - config.eth_client().signer().clone(), + clients.eth_client().signer().clone(), Address::from_str(&core_contract_implementation_address).unwrap(), ); CoreContractStarknetL1Output { core_contract_client: Box::new(StarknetValidityContract { core_contract_client }) } } -async fn get_account<'a>(config: &'a Config, args: &'a CliArgs) -> RpcAccount<'a> { +async fn get_account<'a>(clients: &'a Clients, config_file: &'a ConfigFile) -> RpcAccount<'a> { log::info!("⏳ L2 State and Initialisation Started"); - let account = account_init(config, args).await; + let account = account_init(clients, config_file).await; log::info!("🔐 Account with given private key deployed on L2. [Account Address : {:?}]", account.address()); account } @@ -202,12 +219,12 @@ pub struct BootstrapperOutput { pub braavos_setup_outputs: Option, } -pub async fn bootstrap(args: &CliArgs, config: &Config) -> BootstrapperOutput { +pub async fn bootstrap(config_file: &ConfigFile, clients: &Clients) -> BootstrapperOutput { // setup core contract (L1) - let core_contract_client = setup_core_contract(args, config).await; + let core_contract_client = setup_core_contract(config_file, clients).await; // setup L2 - let l2_output = setup_l2(args, config).await; + let l2_output = setup_l2(config_file, clients).await; BootstrapperOutput { starknet_contract_address: Some(core_contract_client.core_contract_client.address()), @@ -218,8 +235,8 @@ pub async fn bootstrap(args: &CliArgs, config: &Config) -> BootstrapperOutput { } } -async fn setup_core_contract(args: &CliArgs, config: &Config) -> CoreContractStarknetL1Output { - let core_contract = CoreContractStarknetL1::new(args, config); +async fn setup_core_contract(config_file: &ConfigFile, clients: &Clients) -> CoreContractStarknetL1Output { + let core_contract = CoreContractStarknetL1::new(config_file, clients); let core_contract_client = core_contract.setup().await; log::info!("📦 Core address : {:?}", core_contract_client.core_contract_client.address()); log::info!( @@ -238,19 +255,19 @@ async fn setup_core_contract(args: &CliArgs, config: &Config) -> CoreContractSta async fn setup_eth_bridge<'a>( account: Option>, core_contract_client: &CoreContractStarknetL1Output, - args: &CliArgs, - config: &Config, + config_file: &ConfigFile, + clients: &Clients, ) -> EthBridgeSetupOutput { let account = match account { Some(account) => account, - None => get_account(config, args).await, + None => get_account(clients, config_file).await, }; log::info!("⏳ Starting ETH bridge deployment"); let eth_bridge = EthBridge::new( account.clone(), account.address(), - args, - config, + config_file, + clients, core_contract_client.core_contract_client.as_ref(), ); let eth_bridge_setup_outputs = eth_bridge.setup().await; @@ -261,19 +278,19 @@ async fn setup_eth_bridge<'a>( async fn setup_erc20_bridge<'a>( account: Option>, core_contract_client: &CoreContractStarknetL1Output, - args: &CliArgs, - config: &Config, + config_file: &ConfigFile, + clients: &Clients, ) -> Erc20BridgeSetupOutput { let account = match account { Some(account) => account, - None => get_account(config, args).await, + None => get_account(clients, config_file).await, }; log::info!("⏳ Starting ERC20 token bridge deployment"); let erc20_bridge = Erc20Bridge::new( account.clone(), account.address(), - args, - config, + config_file, + clients, core_contract_client.core_contract_client.as_ref(), ); let erc20_bridge_setup_outputs = erc20_bridge.setup().await; @@ -281,13 +298,13 @@ async fn setup_erc20_bridge<'a>( erc20_bridge_setup_outputs } -async fn setup_udc<'a>(account: Option>, args: &CliArgs, config: &Config) -> UdcSetupOutput { +async fn setup_udc<'a>(account: Option>, config_file: &ConfigFile, clients: &Clients) -> UdcSetupOutput { let account = match account { Some(account) => account, - None => get_account(config, args).await, + None => get_account(clients, config_file).await, }; log::info!("⏳ Starting UDC (Universal Deployer Contract) deployment"); - let udc = UdcSetup::new(account.clone(), account.address(), args, config); + let udc = UdcSetup::new(account.clone(), account.address(), config_file, clients); let udc_setup_outputs = udc.setup().await; log::info!( "*️⃣ UDC setup completed. [UDC Address : {:?}, UDC class hash : {:?}]", @@ -298,10 +315,14 @@ async fn setup_udc<'a>(account: Option>, args: &CliArgs, config: udc_setup_outputs } -async fn setup_argent<'a>(account: Option>, args: &CliArgs, config: &Config) -> ArgentSetupOutput { +async fn setup_argent<'a>( + account: Option>, + config_file: &ConfigFile, + clients: &Clients, +) -> ArgentSetupOutput { let account = match account { Some(account) => account, - None => get_account(config, args).await, + None => get_account(clients, config_file).await, }; log::info!("⏳ Starting Argent Account deployment"); let argent = ArgentSetup::new(account.clone()); @@ -311,13 +332,17 @@ async fn setup_argent<'a>(account: Option>, args: &CliArgs, confi argent_setup_outputs } -async fn setup_braavos<'a>(account: Option>, args: &CliArgs, config: &Config) -> BraavosSetupOutput { +async fn setup_braavos<'a>( + account: Option>, + config_file: &ConfigFile, + clients: &Clients, +) -> BraavosSetupOutput { let account = match account { Some(account) => account, - None => get_account(config, args).await, + None => get_account(clients, config_file).await, }; log::info!("⏳ Starting Braavos Account deployment"); - let braavos = BraavosSetup::new(account.clone(), args, config); + let braavos = BraavosSetup::new(account.clone(), config_file, clients); let braavos_setup_outputs = braavos.setup().await; log::info!( "*️⃣ Braavos setup completed. [Braavos account class hash : {:?}]", @@ -327,26 +352,27 @@ async fn setup_braavos<'a>(account: Option>, args: &CliArgs, conf braavos_setup_outputs } -pub async fn setup_l2(args: &CliArgs, config: &Config) -> BootstrapperOutput { - let account = get_account(config, args).await; +pub async fn setup_l2(config_file: &ConfigFile, clients: &Clients) -> BootstrapperOutput { + let account = get_account(clients, config_file).await; - let core_contract_client = get_core_contract_client(args, config); + let core_contract_client = get_core_contract_client(config_file, clients); // setup eth bridge - let eth_bridge_setup_outputs = setup_eth_bridge(Some(account.clone()), &core_contract_client, args, config).await; + let eth_bridge_setup_outputs = + setup_eth_bridge(Some(account.clone()), &core_contract_client, config_file, clients).await; // setup erc20 bridge let erc20_bridge_setup_outputs = - setup_erc20_bridge(Some(account.clone()), &core_contract_client, args, config).await; + setup_erc20_bridge(Some(account.clone()), &core_contract_client, config_file, clients).await; // setup udc - let udc_setup_outputs = setup_udc(Some(account.clone()), args, config).await; + let udc_setup_outputs = setup_udc(Some(account.clone()), config_file, clients).await; // setup argent account - let argent_setup_outputs = setup_argent(Some(account.clone()), args, config).await; + let argent_setup_outputs = setup_argent(Some(account.clone()), config_file, clients).await; // setup braavos account - let braavos_setup_outputs = setup_braavos(Some(account.clone()), args, config).await; + let braavos_setup_outputs = setup_braavos(Some(account.clone()), config_file, clients).await; BootstrapperOutput { eth_bridge_setup_outputs: Some(eth_bridge_setup_outputs), diff --git a/src/setup_scripts/account_setup.rs b/src/setup_scripts/account_setup.rs index 1bbcd39..9bb9bfd 100644 --- a/src/setup_scripts/account_setup.rs +++ b/src/setup_scripts/account_setup.rs @@ -2,16 +2,16 @@ use std::time::Duration; use tokio::time::sleep; -use crate::contract_clients::config::Config; +use crate::contract_clients::config::Clients; use crate::contract_clients::utils::{ build_single_owner_account, declare_contract, deploy_account_using_priv_key, DeclarationInput, RpcAccount, TEMP_ACCOUNT_PRIV_KEY, }; use crate::utils::constants::{OZ_ACCOUNT_CASM_PATH, OZ_ACCOUNT_PATH, OZ_ACCOUNT_SIERRA_PATH}; use crate::utils::{convert_to_hex, save_to_json, JsonValueType}; -use crate::CliArgs; +use crate::ConfigFile; -pub async fn account_init<'a>(clients: &'a Config, arg_config: &'a CliArgs) -> RpcAccount<'a> { +pub async fn account_init<'a>(clients: &'a Clients, arg_config: &'a ConfigFile) -> RpcAccount<'a> { // >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> // Making temp account for declaration of OZ account Cairo 1 contract let oz_account_class_hash = declare_contract(DeclarationInput::LegacyDeclarationInputs( diff --git a/src/setup_scripts/braavos.rs b/src/setup_scripts/braavos.rs index c2c35d3..d747792 100644 --- a/src/setup_scripts/braavos.rs +++ b/src/setup_scripts/braavos.rs @@ -5,7 +5,7 @@ use starknet::accounts::{Account, ConnectedAccount}; use starknet::core::types::Felt; use tokio::time::sleep; -use crate::contract_clients::config::Config; +use crate::contract_clients::config::Clients; use crate::contract_clients::utils::{declare_contract, DeclarationInput, RpcAccount}; use crate::helpers::account_actions::{get_contract_address_from_deploy_tx, AccountActions}; use crate::utils::constants::{ @@ -13,12 +13,12 @@ use crate::utils::constants::{ BRAAVOS_BASE_ACCOUNT_SIERRA_PATH, }; use crate::utils::{save_to_json, wait_for_transaction, JsonValueType}; -use crate::CliArgs; +use crate::ConfigFile; pub struct BraavosSetup<'a> { account: RpcAccount<'a>, - arg_config: &'a CliArgs, - config: &'a Config, + arg_config: &'a ConfigFile, + clients: &'a Clients, } #[derive(Debug, Clone, Serialize)] @@ -27,8 +27,8 @@ pub struct BraavosSetupOutput { } impl<'a> BraavosSetup<'a> { - pub fn new(account: RpcAccount<'a>, arg_config: &'a CliArgs, config: &'a Config) -> Self { - Self { account, arg_config, config } + pub fn new(account: RpcAccount<'a>, arg_config: &'a ConfigFile, clients: &'a Clients) -> Self { + Self { account, arg_config, clients } } pub async fn setup(&self) -> BraavosSetupOutput { @@ -59,7 +59,7 @@ impl<'a> BraavosSetup<'a> { let braavos_aggregator_class_hash = declare_contract(DeclarationInput::LegacyDeclarationInputs( String::from(BRAAVOS_AGGREGATOR_PATH), self.arg_config.rollup_seq_url.clone(), - self.config.provider_l2(), + self.clients.provider_l2(), )) .await; log::info!("📣 Braavos Aggregator class hash declared."); diff --git a/src/setup_scripts/core_contract.rs b/src/setup_scripts/core_contract.rs index 3565eb9..62dc17d 100644 --- a/src/setup_scripts/core_contract.rs +++ b/src/setup_scripts/core_contract.rs @@ -2,17 +2,17 @@ use std::str::FromStr; use ethers::abi::Address; -use crate::contract_clients::config::Config; +use crate::contract_clients::config::Clients; use crate::contract_clients::core_contract::{CoreContract, CoreContractDeploy}; use crate::contract_clients::starknet_sovereign::StarknetSovereignContract; use crate::contract_clients::starknet_validity::StarknetValidityContract; use crate::contract_clients::utils::get_bridge_init_configs; use crate::utils::{save_to_json, JsonValueType}; -use crate::CliArgs; +use crate::ConfigFile; pub struct CoreContractStarknetL1<'a> { - arg_config: &'a CliArgs, - clients: &'a Config, + arg_config: &'a ConfigFile, + clients: &'a Clients, } pub struct CoreContractStarknetL1Output { @@ -20,7 +20,7 @@ pub struct CoreContractStarknetL1Output { } impl<'a> CoreContractStarknetL1<'a> { - pub fn new(arg_config: &'a CliArgs, clients: &'a Config) -> Self { + pub fn new(arg_config: &'a ConfigFile, clients: &'a Clients) -> Self { Self { arg_config, clients } } diff --git a/src/setup_scripts/erc20_bridge.rs b/src/setup_scripts/erc20_bridge.rs index 21907ce..db313ce 100644 --- a/src/setup_scripts/erc20_bridge.rs +++ b/src/setup_scripts/erc20_bridge.rs @@ -11,20 +11,20 @@ use starknet_providers::jsonrpc::HttpTransport; use starknet_providers::{JsonRpcClient, Provider}; use tokio::time::sleep; -use crate::contract_clients::config::Config; +use crate::contract_clients::config::Clients; use crate::contract_clients::core_contract::CoreContract; use crate::contract_clients::eth_bridge::BridgeDeployable; use crate::contract_clients::token_bridge::StarknetTokenBridge; use crate::contract_clients::utils::{build_single_owner_account, declare_contract, DeclarationInput, RpcAccount}; use crate::utils::constants::{ERC20_CASM_PATH, ERC20_SIERRA_PATH}; use crate::utils::{convert_to_hex, save_to_json, JsonValueType}; -use crate::CliArgs; +use crate::ConfigFile; pub struct Erc20Bridge<'a> { account: RpcAccount<'a>, account_address: Felt, - arg_config: &'a CliArgs, - clients: &'a Config, + arg_config: &'a ConfigFile, + clients: &'a Clients, core_contract: &'a dyn CoreContract, } @@ -44,8 +44,8 @@ impl<'a> Erc20Bridge<'a> { pub fn new( account: RpcAccount<'a>, account_address: Felt, - arg_config: &'a CliArgs, - clients: &'a Config, + arg_config: &'a ConfigFile, + clients: &'a Clients, core_contract: &'a dyn CoreContract, ) -> Self { Self { account, account_address, arg_config, clients, core_contract } diff --git a/src/setup_scripts/eth_bridge.rs b/src/setup_scripts/eth_bridge.rs index 85cef69..8c95762 100644 --- a/src/setup_scripts/eth_bridge.rs +++ b/src/setup_scripts/eth_bridge.rs @@ -9,7 +9,7 @@ use starknet_providers::jsonrpc::HttpTransport; use starknet_providers::JsonRpcClient; use tokio::time::sleep; -use crate::contract_clients::config::Config; +use crate::contract_clients::config::Clients; use crate::contract_clients::core_contract::CoreContract; use crate::contract_clients::eth_bridge::{BridgeDeployable, StarknetLegacyEthBridge}; use crate::contract_clients::utils::{ @@ -19,13 +19,13 @@ use crate::contract_clients::utils::{ use crate::helpers::account_actions::{get_contract_address_from_deploy_tx, AccountActions}; use crate::utils::constants::{ERC20_LEGACY_PATH, LEGACY_BRIDGE_PATH, PROXY_LEGACY_PATH, STARKGATE_PROXY_PATH}; use crate::utils::{convert_to_hex, invoke_contract, save_to_json, wait_for_transaction, JsonValueType}; -use crate::CliArgs; +use crate::ConfigFile; pub struct EthBridge<'a> { account: RpcAccount<'a>, account_address: Felt, - arg_config: &'a CliArgs, - clients: &'a Config, + arg_config: &'a ConfigFile, + clients: &'a Clients, core_contract: &'a dyn CoreContract, } @@ -46,8 +46,8 @@ impl<'a> EthBridge<'a> { pub fn new( account: RpcAccount<'a>, account_address: Felt, - arg_config: &'a CliArgs, - clients: &'a Config, + arg_config: &'a ConfigFile, + clients: &'a Clients, core_contract: &'a dyn CoreContract, ) -> Self { Self { account, account_address, arg_config, clients, core_contract } diff --git a/src/setup_scripts/udc.rs b/src/setup_scripts/udc.rs index 98628cc..0773c9a 100644 --- a/src/setup_scripts/udc.rs +++ b/src/setup_scripts/udc.rs @@ -5,18 +5,18 @@ use starknet::accounts::ConnectedAccount; use starknet::core::types::Felt; use tokio::time::sleep; -use crate::contract_clients::config::Config; +use crate::contract_clients::config::Clients; use crate::contract_clients::utils::{declare_contract, DeclarationInput, RpcAccount}; use crate::helpers::account_actions::{get_contract_address_from_deploy_tx, AccountActions}; use crate::utils::constants::UDC_PATH; use crate::utils::{save_to_json, wait_for_transaction, JsonValueType}; -use crate::CliArgs; +use crate::ConfigFile; pub struct UdcSetup<'a> { account: RpcAccount<'a>, account_address: Felt, - arg_config: &'a CliArgs, - config: &'a Config, + arg_config: &'a ConfigFile, + clients: &'a Clients, } #[derive(Debug, Clone, Serialize)] @@ -26,15 +26,20 @@ pub struct UdcSetupOutput { } impl<'a> UdcSetup<'a> { - pub fn new(account: RpcAccount<'a>, account_address: Felt, arg_config: &'a CliArgs, config: &'a Config) -> Self { - Self { account, account_address, arg_config, config } + pub fn new( + account: RpcAccount<'a>, + account_address: Felt, + arg_config: &'a ConfigFile, + clients: &'a Clients, + ) -> Self { + Self { account, account_address, arg_config, clients } } pub async fn setup(&self) -> UdcSetupOutput { let udc_class_hash = declare_contract(DeclarationInput::LegacyDeclarationInputs( String::from(UDC_PATH), self.arg_config.rollup_seq_url.clone(), - self.config.provider_l2(), + self.clients.provider_l2(), )) .await; log::info!("📣 UDC Class Hash Declared."); diff --git a/src/tests/erc20_bridge.rs b/src/tests/erc20_bridge.rs index 0ff4c0a..de00372 100644 --- a/src/tests/erc20_bridge.rs +++ b/src/tests/erc20_bridge.rs @@ -5,16 +5,16 @@ use ethers::prelude::U256; use starknet::core::types::Felt; use tokio::time::sleep; -use crate::contract_clients::config::Config; +use crate::contract_clients::config::Clients; use crate::contract_clients::token_bridge::StarknetTokenBridge; use crate::contract_clients::utils::read_erc20_balance; use crate::tests::constants::L2_DEPLOYER_ADDRESS; -use crate::CliArgs; +use crate::ConfigFile; // TODO : Uncomment L2 --> L1 part after this part is added in the madara-orchestrator. pub async fn erc20_bridge_test_helper( - clients: &Config, - arg_config: &CliArgs, + clients: &Clients, + arg_config: &ConfigFile, l2_erc20_token_address: Felt, token_bridge: StarknetTokenBridge, _l2_bridge_address: Felt, diff --git a/src/tests/eth_bridge.rs b/src/tests/eth_bridge.rs index 44b8878..6f6ae1c 100644 --- a/src/tests/eth_bridge.rs +++ b/src/tests/eth_bridge.rs @@ -5,16 +5,16 @@ use ethers::prelude::U256; use starknet::core::types::Felt; use tokio::time::sleep; -use crate::contract_clients::config::Config; +use crate::contract_clients::config::Clients; use crate::contract_clients::eth_bridge::StarknetLegacyEthBridge; use crate::contract_clients::utils::read_erc20_balance; use crate::tests::constants::L2_DEPLOYER_ADDRESS; -use crate::CliArgs; +use crate::ConfigFile; // TODO : Uncomment L2 --> L1 part after this part is added in the madara-orchestrator. pub async fn eth_bridge_test_helper( - clients: &Config, - arg_config: &CliArgs, + clients: &Clients, + arg_config: &ConfigFile, l2_eth_address: Felt, _l2_bridge_address: Felt, eth_bridge: StarknetLegacyEthBridge, diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 35f3fc2..6f9135d 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -9,24 +9,24 @@ use constants::{ }; use rstest::rstest; -use crate::contract_clients::config::Config; +use crate::contract_clients::config::Clients; use crate::tests::constants::{L1_MULTISIG_ADDRESS, L2_MULTISIG_ADDRESS, OPERATOR_ADDRESS, VERIFIER_ADDRESS}; use crate::tests::erc20_bridge::erc20_bridge_test_helper; use crate::tests::eth_bridge::eth_bridge_test_helper; use crate::tests::madara::{MadaraCmd, MadaraCmdBuilder}; -use crate::{bootstrap, setup_core_contract, setup_l2, BootstrapperOutput, CliArgs}; +use crate::{bootstrap, setup_core_contract, setup_l2, BootstrapperOutput, ConfigFile}; -async fn test_setup(config: &CliArgs, clients: &Config) -> (BootstrapperOutput, MadaraCmd) { +async fn test_setup(args: &ConfigFile, clients: &Clients) -> (BootstrapperOutput, MadaraCmd) { // Setup L1 (core contract) - let core_contract_client = setup_core_contract(config, clients).await; + let core_contract_client = setup_core_contract(args, clients).await; let core_contract_address = core_contract_client.core_contract_client.address(); let core_contract_implementation_address = core_contract_client.core_contract_client.implementation_address(); - // Create a new CliArgs with the core contract addresses - let mut updated_config = get_config(); - updated_config.core_contract_address = Some(format!("{:?}", core_contract_address)); - updated_config.core_contract_implementation_address = Some(format!("{:?}", core_contract_implementation_address)); + // Create a new config with the core contract addresses + let mut config = get_test_config_file(); + config.core_contract_address = Some(format!("{:?}", core_contract_address)); + config.core_contract_implementation_address = Some(format!("{:?}", core_contract_implementation_address)); let mut node = MadaraCmdBuilder::new() .args([ @@ -56,7 +56,7 @@ async fn test_setup(config: &CliArgs, clients: &Config) -> (BootstrapperOutput, node.wait_for_ready().await; // Setup L2 with the updated config - let l2_output = setup_l2(&updated_config, clients).await; + let l2_output = setup_l2(&config, clients).await; let output = BootstrapperOutput { starknet_contract_address: Some(core_contract_address), @@ -72,8 +72,9 @@ async fn test_setup(config: &CliArgs, clients: &Config) -> (BootstrapperOutput, #[ignore = "ignored because we have a e2e test, and this is for a local test"] async fn deploy_bridge() -> Result<(), anyhow::Error> { env_logger::init(); - let clients = Config::init(&get_config()).await; - bootstrap(&get_config(), &clients).await; + let config = get_test_config_file(); + let clients = Clients::init_from_config(&config).await; + bootstrap(&config, &clients).await; Ok(()) } @@ -83,13 +84,14 @@ async fn deploy_bridge() -> Result<(), anyhow::Error> { #[ignore = "ignored because we have a e2e test, and this is for a local test"] async fn deposit_and_withdraw_eth_bridge() -> Result<(), anyhow::Error> { env_logger::init(); - let clients = Config::init(&get_config()).await; - let out = bootstrap(&get_config(), &clients).await; + let config = get_test_config_file(); + let clients = Clients::init_from_config(&config).await; + let out = bootstrap(&config, &clients).await; let eth_bridge_setup = out.eth_bridge_setup_outputs.unwrap(); let _ = eth_bridge_test_helper( &clients, - &get_config(), + &config, eth_bridge_setup.l2_eth_proxy_address, eth_bridge_setup.l2_eth_bridge_proxy_address, eth_bridge_setup.l1_bridge, @@ -104,13 +106,14 @@ async fn deposit_and_withdraw_eth_bridge() -> Result<(), anyhow::Error> { #[ignore = "ignored because we have a e2e test, and this is for a local test"] async fn deposit_and_withdraw_erc20_bridge() -> Result<(), anyhow::Error> { env_logger::init(); - let clients = Config::init(&get_config()).await; - let out = bootstrap(&get_config(), &clients).await; + let config = get_test_config_file(); + let clients = Clients::init_from_config(&config).await; + let out = bootstrap(&config, &clients).await; let eth_token_setup = out.erc20_bridge_setup_outputs.unwrap(); let _ = erc20_bridge_test_helper( &clients, - &get_config(), + &config, eth_token_setup.test_erc20_token_address, eth_token_setup.token_bridge, eth_token_setup.l2_token_bridge, @@ -124,15 +127,16 @@ async fn deposit_and_withdraw_erc20_bridge() -> Result<(), anyhow::Error> { #[tokio::test] async fn deposit_tests_both_bridges() -> Result<(), anyhow::Error> { env_logger::init(); - let clients = Config::init(&get_config()).await; - let (out, _madara) = test_setup(&get_config(), &clients).await; + let config = get_test_config_file(); + let clients = Clients::init_from_config(&config).await; + let (out, _madara) = test_setup(&config, &clients).await; let eth_bridge_setup = out.eth_bridge_setup_outputs.unwrap(); let eth_token_setup = out.erc20_bridge_setup_outputs.unwrap(); let _ = eth_bridge_test_helper( &clients, - &get_config(), + &config, eth_bridge_setup.l2_eth_proxy_address, eth_bridge_setup.l2_eth_bridge_proxy_address, eth_bridge_setup.l1_bridge, @@ -141,7 +145,7 @@ async fn deposit_tests_both_bridges() -> Result<(), anyhow::Error> { let _ = erc20_bridge_test_helper( &clients, - &get_config(), + &config, eth_token_setup.test_erc20_token_address, eth_token_setup.token_bridge, eth_token_setup.l2_token_bridge, @@ -151,13 +155,13 @@ async fn deposit_tests_both_bridges() -> Result<(), anyhow::Error> { Ok(()) } -fn get_config() -> CliArgs { - CliArgs { +fn get_test_config_file() -> ConfigFile { + ConfigFile { eth_rpc: String::from(ETH_RPC), eth_priv_key: String::from(ETH_PRIV_KEY), rollup_seq_url: String::from(ROLLUP_SEQ_URL), rollup_priv_key: String::from(ROLLUP_PRIV_KEY), - eth_chain_id: String::from(ETH_CHAIN_ID).parse().unwrap(), + eth_chain_id: ETH_CHAIN_ID.parse().expect("Invalid ETH chain ID"), l1_deployer_address: String::from(L1_DEPLOYER_ADDRESS), l1_wait_time: String::from(L1_WAIT_TIME), sn_os_program_hash: String::from(SN_OS_PROGRAM_HASH), @@ -171,9 +175,7 @@ fn get_config() -> CliArgs { verifier_address: String::from(VERIFIER_ADDRESS), operator_address: String::from(OPERATOR_ADDRESS), dev: false, - mode: crate::BootstrapMode::SetupL1, core_contract_address: None, core_contract_implementation_address: None, - output_file: None, } }