hdp_cli/
config.rs

1use alloy::{primitives::ChainId, transports::http::reqwest::Url};
2use hdp::primitives::constant::{
3    DEFAULT_DRY_CAIRO_RUN_CAIRO_FILE, DEFAULT_SOUND_CAIRO_RUN_CAIRO_FILE,
4};
5use hdp::provider::evm::config::EvmProviderConfig;
6
7use std::{env, path::PathBuf};
8use tokio::sync::OnceCell;
9
10pub static CONFIG: OnceCell<Config> = OnceCell::const_new();
11
12/// Configuration for the CLI
13#[derive(Debug)]
14pub struct Config {
15    pub evm_provider: EvmProviderConfig,
16    pub dry_run_program_path: PathBuf,
17    pub sound_run_program_path: PathBuf,
18    pub save_fetch_keys_file: Option<PathBuf>,
19}
20
21impl Config {
22    pub async fn init(
23        cli_rpc_url: Option<Url>,
24        cli_chain_id: Option<ChainId>,
25        cli_dry_run_cairo_file: Option<PathBuf>,
26        cli_sound_run_cairo_file: Option<PathBuf>,
27        cli_save_fetch_keys_file: Option<PathBuf>,
28    ) -> &'static Self {
29        let chain_id = cli_chain_id.unwrap_or_else(|| {
30            env::var("CHAIN_ID")
31                .expect("CHAIN_ID must be set")
32                .parse()
33                .expect("CHAIN_ID must be a number")
34        });
35        let rpc_url = cli_rpc_url.unwrap_or_else(|| {
36            env::var("RPC_URL")
37                .expect("RPC_URL must be set")
38                .parse()
39                .expect("RPC_URL must be a valid URL")
40        });
41        let rpc_chunk_size = env::var("RPC_CHUNK_SIZE")
42            .unwrap_or_else(|_| "40".to_string())
43            .parse()
44            .expect("RPC_CHUNK_SIZE must be a number");
45        let save_fetch_keys_file: Option<PathBuf> = cli_save_fetch_keys_file
46            .or_else(|| env::var("SAVE_FETCH_KEYS_FILE").ok().map(PathBuf::from));
47        let dry_run_cairo_path: PathBuf = cli_dry_run_cairo_file.unwrap_or_else(|| {
48            env::var("DRY_RUN_CAIRO_PATH")
49                .unwrap_or_else(|_| DEFAULT_DRY_CAIRO_RUN_CAIRO_FILE.to_string())
50                .parse()
51                .expect("DRY_RUN_CAIRO_PATH must be a path to a cairo file")
52        });
53        let sound_run_cairo_path: PathBuf = cli_sound_run_cairo_file.unwrap_or_else(|| {
54            env::var("SOUND_RUN_CAIRO_PATH")
55                .unwrap_or_else(|_| DEFAULT_SOUND_CAIRO_RUN_CAIRO_FILE.to_string())
56                .parse()
57                .expect("SOUND_RUN_CAIRO_PATH must be a path to a cairo file")
58        });
59
60        CONFIG
61            .get_or_init(|| async {
62                Config {
63                    evm_provider: EvmProviderConfig {
64                        rpc_url,
65                        chain_id,
66                        max_requests: rpc_chunk_size,
67                    },
68                    dry_run_program_path: dry_run_cairo_path,
69                    sound_run_program_path: sound_run_cairo_path,
70                    save_fetch_keys_file,
71                }
72            })
73            .await
74    }
75}