unc_cli_rs/
config.rs

1#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
2pub struct Config {
3    pub credentials_home_dir: std::path::PathBuf,
4    pub network_connection: linked_hash_map::LinkedHashMap<String, NetworkConfig>,
5}
6
7impl Default for Config {
8    fn default() -> Self {
9        let home_dir = dirs::home_dir().expect("Impossible to get your home dir!");
10        let mut credentials_home_dir = std::path::PathBuf::from(&home_dir);
11        credentials_home_dir.push(".unc-credentials");
12
13        let mut network_connection = linked_hash_map::LinkedHashMap::new();
14        network_connection.insert(
15            "mainnet".to_string(),
16            NetworkConfig {
17                network_name: "mainnet".to_string(),
18                rpc_url: "http://16.78.8.159:3030".parse().unwrap(),
19                wallet_url: "https://app.wallet.com/".parse().unwrap(),
20                explorer_transaction_url: "https://explorer.unc.org/transactions/"
21                    .parse()
22                    .unwrap(),
23                rpc_api_key: None,
24                linkdrop_account_id: Some("unc".parse().unwrap()),
25                faucet_url: None,
26                meta_transaction_relayer_url: None,
27            },
28        );
29        network_connection.insert(
30            "testnet".to_string(),
31            NetworkConfig {
32                network_name: "testnet".to_string(),
33                rpc_url: "http://108.136.139.238:3030".parse().unwrap(),
34                wallet_url: "https://testnet.wallet.com/".parse().unwrap(),
35                explorer_transaction_url: "https://explorer.testnet.unc.org/transactions/"
36                    .parse()
37                    .unwrap(),
38                rpc_api_key: None,
39                linkdrop_account_id: Some("testnet".parse().unwrap()),
40                faucet_url: Some("https://helper.unc.com/account".parse().unwrap()),
41                meta_transaction_relayer_url: None,
42            },
43        
44        );
45        network_connection.insert(
46            "custom".to_string(),
47            NetworkConfig {
48                network_name: "betanet".to_string(),
49                rpc_url: "http://43.218.226.63:3030".parse().unwrap(),
50                wallet_url: "https://testnet.wallet.com/".parse().unwrap(),
51                explorer_transaction_url: "https://explorer.testnet.unc.org/transactions/"
52                    .parse()
53                    .unwrap(),
54                rpc_api_key: None,
55                linkdrop_account_id: Some("testnet".parse().unwrap()),
56                faucet_url: Some("https://helper.unc.com/account".parse().unwrap()),
57                meta_transaction_relayer_url: None,
58            },
59        
60        );
61        Self {
62            credentials_home_dir,
63            network_connection,
64        }
65    }
66}
67
68impl Config {
69    pub fn network_names(&self) -> Vec<String> {
70        self.network_connection
71            .iter()
72            .map(|(_, network_config)| network_config.network_name.clone())
73            .collect()
74    }
75}
76
77#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
78pub struct NetworkConfig {
79    pub network_name: String,
80    pub rpc_url: url::Url,
81    pub rpc_api_key: Option<crate::types::api_key::ApiKey>,
82    pub wallet_url: url::Url,
83    pub explorer_transaction_url: url::Url,
84    pub linkdrop_account_id: Option<unc_primitives::types::AccountId>,
85    pub faucet_url: Option<url::Url>,
86    pub meta_transaction_relayer_url: Option<url::Url>,
87}
88
89impl NetworkConfig {
90    pub fn json_rpc_client(&self) -> unc_jsonrpc_client::JsonRpcClient {
91        let mut json_rpc_client =
92            unc_jsonrpc_client::JsonRpcClient::connect(self.rpc_url.as_ref());
93        if let Some(rpc_api_key) = &self.rpc_api_key {
94            json_rpc_client =
95                json_rpc_client.header(unc_jsonrpc_client::auth::ApiKey::from(rpc_api_key.clone()))
96        };
97        json_rpc_client
98    }
99}