From c5724929121ca975f6e21df189143fcf76121ba5 Mon Sep 17 00:00:00 2001 From: FroVolod Date: Sun, 14 Jul 2024 22:21:31 +0300 Subject: [PATCH 1/7] start --- src/commands/config/mod.rs | 6 +++ src/commands/config/update/mod.rs | 20 +++++++ src/commands/config/update/rpc_url.rs | 76 +++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 src/commands/config/update/mod.rs create mode 100644 src/commands/config/update/rpc_url.rs diff --git a/src/commands/config/mod.rs b/src/commands/config/mod.rs index 982b6f6cc..9a1143354 100644 --- a/src/commands/config/mod.rs +++ b/src/commands/config/mod.rs @@ -3,6 +3,7 @@ use strum::{EnumDiscriminants, EnumIter, EnumMessage}; mod add_connection; mod delete_connection; +mod update; #[derive(Debug, Clone, interactive_clap::InteractiveClap)] #[interactive_clap(context = crate::GlobalContext)] @@ -25,6 +26,11 @@ pub enum ConfigActions { #[strum_discriminants(strum(message = "add-connection - Add a network connection"))] /// Add a network connection AddConnection(self::add_connection::AddNetworkConnection), + #[strum_discriminants(strum( + message = "update - Update the network connection option" + ))] + /// Update the network connection option + Update(self::update::Update), #[strum_discriminants(strum( message = "delete-connection - Delete a network connection" ))] diff --git a/src/commands/config/update/mod.rs b/src/commands/config/update/mod.rs new file mode 100644 index 000000000..5477ad6d1 --- /dev/null +++ b/src/commands/config/update/mod.rs @@ -0,0 +1,20 @@ +use strum::{EnumDiscriminants, EnumIter, EnumMessage}; + +mod rpc_url; + +#[derive(Debug, Clone, interactive_clap::InteractiveClap)] +#[interactive_clap(input_context = crate::GlobalContext)] +pub struct Update { + #[interactive_clap(subcommand)] + option: NetworkConnectionOptions, +} + +#[derive(Debug, EnumDiscriminants, Clone, interactive_clap::InteractiveClap)] +#[interactive_clap(context = crate::GlobalContext)] +#[strum_discriminants(derive(EnumMessage, EnumIter))] +/// What are you updating? +pub enum NetworkConnectionOptions { + #[strum_discriminants(strum(message = "rpc-url - Update the rpc url to connect"))] + /// Update the rpc url to connect + RpcUrl(self::rpc_url::RpcUrl), +} diff --git a/src/commands/config/update/rpc_url.rs b/src/commands/config/update/rpc_url.rs new file mode 100644 index 000000000..760a5c763 --- /dev/null +++ b/src/commands/config/update/rpc_url.rs @@ -0,0 +1,76 @@ +use inquire::{CustomType, Select}; + +#[derive(Debug, Clone, interactive_clap::InteractiveClap)] +#[interactive_clap(input_context = crate::GlobalContext)] +#[interactive_clap(output_context = RpcUrlContext)] +pub struct RpcUrl { + /// What is the RPC endpoint? + rpc_url: crate::types::url::Url, + #[interactive_clap(long)] + #[interactive_clap(skip_default_input_arg)] + rpc_api_key: Option, + /// What is the network connection name? + #[interactive_clap(skip_default_input_arg)] + connection_name: String, +} + +#[derive(Debug, Clone)] +pub struct RpcUrlContext; + +impl RpcUrlContext { + pub fn from_previous_context( + previous_context: crate::GlobalContext, + scope: &::InteractiveClapContextScope, + ) -> color_eyre::eyre::Result { + let mut config = previous_context.config; + if let Some(network_config) = config.network_connection.get_mut(&scope.connection_name) { + network_config.rpc_url = scope.rpc_url.clone().into(); + network_config.rpc_api_key.clone_from(&scope.rpc_api_key); + } else { + return color_eyre::eyre::Result::Err(color_eyre::eyre::eyre!( + "Network connection \"{}\" not found", + &scope.connection_name + )); + }; + eprintln!(); + config.write_config_toml()?; + eprintln!( + "Rpc URL successfully updated for Network connection \"{}\"", + &scope.connection_name + ); + Ok(Self) + } +} + +impl RpcUrl { + fn input_rpc_api_key( + _context: &crate::GlobalContext, + ) -> color_eyre::eyre::Result> { + eprintln!(); + #[derive(strum_macros::Display)] + enum ConfirmOptions { + #[strum(to_string = "Yes, the RPC endpoint requires API key")] + Yes, + #[strum(to_string = "No, the RPC endpoint does not require API key")] + No, + } + let select_choose_input = Select::new( + "Do you want to input an API key?", + vec![ConfirmOptions::Yes, ConfirmOptions::No], + ) + .prompt()?; + if let ConfirmOptions::Yes = select_choose_input { + let api_key: crate::types::api_key::ApiKey = + CustomType::new("Enter an API key").prompt()?; + Ok(Some(api_key)) + } else { + Ok(None) + } + } + + fn input_connection_name( + context: &crate::GlobalContext, + ) -> color_eyre::eyre::Result> { + crate::common::input_network_name(&context.config, &[]) + } +} From d11ca82393318d113507aa188acdd09ee75e83ac Mon Sep 17 00:00:00 2001 From: FroVolod Date: Mon, 15 Jul 2024 12:18:51 +0300 Subject: [PATCH 2/7] added WalletUrl to update mode --- src/commands/config/update/mod.rs | 6 +++- src/commands/config/update/wallet_url.rs | 45 ++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/commands/config/update/wallet_url.rs diff --git a/src/commands/config/update/mod.rs b/src/commands/config/update/mod.rs index 5477ad6d1..a4efc9c69 100644 --- a/src/commands/config/update/mod.rs +++ b/src/commands/config/update/mod.rs @@ -1,6 +1,7 @@ use strum::{EnumDiscriminants, EnumIter, EnumMessage}; mod rpc_url; +mod wallet_url; #[derive(Debug, Clone, interactive_clap::InteractiveClap)] #[interactive_clap(input_context = crate::GlobalContext)] @@ -14,7 +15,10 @@ pub struct Update { #[strum_discriminants(derive(EnumMessage, EnumIter))] /// What are you updating? pub enum NetworkConnectionOptions { - #[strum_discriminants(strum(message = "rpc-url - Update the rpc url to connect"))] + #[strum_discriminants(strum(message = "rpc-url - Update the rpc url to connect"))] /// Update the rpc url to connect RpcUrl(self::rpc_url::RpcUrl), + #[strum_discriminants(strum(message = "wallet-url - Update the wallet url to connect"))] + /// Update the wallet url to connect + WalletUrl(self::wallet_url::WalletUrl), } diff --git a/src/commands/config/update/wallet_url.rs b/src/commands/config/update/wallet_url.rs new file mode 100644 index 000000000..fe90a146a --- /dev/null +++ b/src/commands/config/update/wallet_url.rs @@ -0,0 +1,45 @@ +#[derive(Debug, Clone, interactive_clap::InteractiveClap)] +#[interactive_clap(input_context = crate::GlobalContext)] +#[interactive_clap(output_context = WalletUrlContext)] +pub struct WalletUrl { + /// What is the wallet endpoint? + wallet_url: crate::types::url::Url, + /// What is the network connection name? + #[interactive_clap(skip_default_input_arg)] + connection_name: String, +} + +#[derive(Debug, Clone)] +pub struct WalletUrlContext; + +impl WalletUrlContext { + pub fn from_previous_context( + previous_context: crate::GlobalContext, + scope: &::InteractiveClapContextScope, + ) -> color_eyre::eyre::Result { + let mut config = previous_context.config; + if let Some(network_config) = config.network_connection.get_mut(&scope.connection_name) { + network_config.wallet_url = scope.wallet_url.clone().into(); + } else { + return color_eyre::eyre::Result::Err(color_eyre::eyre::eyre!( + "Network connection \"{}\" not found", + &scope.connection_name + )); + }; + eprintln!(); + config.write_config_toml()?; + eprintln!( + "Wallet URL successfully updated for Network connection \"{}\"", + &scope.connection_name + ); + Ok(Self) + } +} + +impl WalletUrl { + fn input_connection_name( + context: &crate::GlobalContext, + ) -> color_eyre::eyre::Result> { + crate::common::input_network_name(&context.config, &[]) + } +} From 663ac6eb5ba974104f76c276462be55642da8968 Mon Sep 17 00:00:00 2001 From: FroVolod Date: Mon, 15 Jul 2024 12:48:19 +0300 Subject: [PATCH 3/7] added ExplorerTransactionUrl to update mode --- .../config/update/explorer_transaction_url.rs | 45 +++++++++++++++++++ src/commands/config/update/mod.rs | 14 +++++- 2 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 src/commands/config/update/explorer_transaction_url.rs diff --git a/src/commands/config/update/explorer_transaction_url.rs b/src/commands/config/update/explorer_transaction_url.rs new file mode 100644 index 000000000..b065ef4ea --- /dev/null +++ b/src/commands/config/update/explorer_transaction_url.rs @@ -0,0 +1,45 @@ +#[derive(Debug, Clone, interactive_clap::InteractiveClap)] +#[interactive_clap(input_context = crate::GlobalContext)] +#[interactive_clap(output_context = ExplorerTransactionUrlContext)] +pub struct ExplorerTransactionUrl { + /// What is the transaction explorer endpoint? + explorer_transaction_url: crate::types::url::Url, + /// What is the network connection name? + #[interactive_clap(skip_default_input_arg)] + connection_name: String, +} + +#[derive(Debug, Clone)] +pub struct ExplorerTransactionUrlContext; + +impl ExplorerTransactionUrlContext { + pub fn from_previous_context( + previous_context: crate::GlobalContext, + scope: &::InteractiveClapContextScope, + ) -> color_eyre::eyre::Result { + let mut config = previous_context.config; + if let Some(network_config) = config.network_connection.get_mut(&scope.connection_name) { + network_config.explorer_transaction_url = scope.explorer_transaction_url.clone().into(); + } else { + return color_eyre::eyre::Result::Err(color_eyre::eyre::eyre!( + "Network connection \"{}\" not found", + &scope.connection_name + )); + }; + eprintln!(); + config.write_config_toml()?; + eprintln!( + "Explorer transaction URL successfully updated for Network connection \"{}\"", + &scope.connection_name + ); + Ok(Self) + } +} + +impl ExplorerTransactionUrl { + fn input_connection_name( + context: &crate::GlobalContext, + ) -> color_eyre::eyre::Result> { + crate::common::input_network_name(&context.config, &[]) + } +} diff --git a/src/commands/config/update/mod.rs b/src/commands/config/update/mod.rs index a4efc9c69..afc2679e9 100644 --- a/src/commands/config/update/mod.rs +++ b/src/commands/config/update/mod.rs @@ -1,5 +1,6 @@ use strum::{EnumDiscriminants, EnumIter, EnumMessage}; +mod explorer_transaction_url; mod rpc_url; mod wallet_url; @@ -15,10 +16,19 @@ pub struct Update { #[strum_discriminants(derive(EnumMessage, EnumIter))] /// What are you updating? pub enum NetworkConnectionOptions { - #[strum_discriminants(strum(message = "rpc-url - Update the rpc url to connect"))] + #[strum_discriminants(strum( + message = "rpc-url - Update the rpc URL to connect" + ))] /// Update the rpc url to connect RpcUrl(self::rpc_url::RpcUrl), - #[strum_discriminants(strum(message = "wallet-url - Update the wallet url to connect"))] + #[strum_discriminants(strum( + message = "wallet-url - Update the wallet URL to connect" + ))] /// Update the wallet url to connect WalletUrl(self::wallet_url::WalletUrl), + #[strum_discriminants(strum( + message = "explorer-transaction-url - Update the explorer transaction URL to connect" + ))] + /// Update the explorer transaction URL to connect + ExplorerTransactionUrl(self::explorer_transaction_url::ExplorerTransactionUrl), } From 420e8b70dffcedd72cf0fbc4889a824aea45c2d0 Mon Sep 17 00:00:00 2001 From: FroVolod Date: Mon, 15 Jul 2024 15:48:41 +0300 Subject: [PATCH 4/7] added LinkdropAccountId to update mode --- .../config/update/linkdrop_account_id.rs | 45 +++++++++++++++++++ src/commands/config/update/mod.rs | 6 +++ 2 files changed, 51 insertions(+) create mode 100644 src/commands/config/update/linkdrop_account_id.rs diff --git a/src/commands/config/update/linkdrop_account_id.rs b/src/commands/config/update/linkdrop_account_id.rs new file mode 100644 index 000000000..4dd7c6e96 --- /dev/null +++ b/src/commands/config/update/linkdrop_account_id.rs @@ -0,0 +1,45 @@ +#[derive(Debug, Clone, interactive_clap::InteractiveClap)] +#[interactive_clap(input_context = crate::GlobalContext)] +#[interactive_clap(output_context = LinkdropAccountIdContext)] +pub struct LinkdropAccountId { + /// What is the name of the account that hosts the "linkdrop" program? (e.g. on mainnet it is near, and on testnet it is testnet) + linkdrop_account_id: crate::types::account_id::AccountId, + /// What is the network connection name? + #[interactive_clap(skip_default_input_arg)] + connection_name: String, +} + +#[derive(Debug, Clone)] +pub struct LinkdropAccountIdContext; + +impl LinkdropAccountIdContext { + pub fn from_previous_context( + previous_context: crate::GlobalContext, + scope: &::InteractiveClapContextScope, + ) -> color_eyre::eyre::Result { + let mut config = previous_context.config; + if let Some(network_config) = config.network_connection.get_mut(&scope.connection_name) { + network_config.linkdrop_account_id = Some(scope.linkdrop_account_id.clone().into()); + } else { + return color_eyre::eyre::Result::Err(color_eyre::eyre::eyre!( + "Network connection \"{}\" not found", + &scope.connection_name + )); + }; + eprintln!(); + config.write_config_toml()?; + eprintln!( + "Linkdrop account ID successfully updated for Network connection \"{}\"", + &scope.connection_name + ); + Ok(Self) + } +} + +impl LinkdropAccountId { + fn input_connection_name( + context: &crate::GlobalContext, + ) -> color_eyre::eyre::Result> { + crate::common::input_network_name(&context.config, &[]) + } +} diff --git a/src/commands/config/update/mod.rs b/src/commands/config/update/mod.rs index afc2679e9..76d7e7ae0 100644 --- a/src/commands/config/update/mod.rs +++ b/src/commands/config/update/mod.rs @@ -1,6 +1,7 @@ use strum::{EnumDiscriminants, EnumIter, EnumMessage}; mod explorer_transaction_url; +mod linkdrop_account_id; mod rpc_url; mod wallet_url; @@ -31,4 +32,9 @@ pub enum NetworkConnectionOptions { ))] /// Update the explorer transaction URL to connect ExplorerTransactionUrl(self::explorer_transaction_url::ExplorerTransactionUrl), + #[strum_discriminants(strum( + message = "linkdrop-account-id - Update the linkdrop account ID to connect" + ))] + /// Update the linkdrop account ID to connect + LinkdropAccountId(self::linkdrop_account_id::LinkdropAccountId), } From 3b40482a82aeb488906cf3cc9418eb60f1585d81 Mon Sep 17 00:00:00 2001 From: FroVolod Date: Wed, 17 Jul 2024 11:47:25 +0300 Subject: [PATCH 5/7] added EditConnection --- src/commands/config/edit_connection/mod.rs | 189 +++++++++++++++++++++ src/commands/config/mod.rs | 10 +- src/config/mod.rs | 11 ++ 3 files changed, 204 insertions(+), 6 deletions(-) create mode 100644 src/commands/config/edit_connection/mod.rs diff --git a/src/commands/config/edit_connection/mod.rs b/src/commands/config/edit_connection/mod.rs new file mode 100644 index 000000000..52544ebbf --- /dev/null +++ b/src/commands/config/edit_connection/mod.rs @@ -0,0 +1,189 @@ +use std::str::FromStr; + +use inquire::{Select, Text}; + +#[derive(Debug, Clone, interactive_clap::InteractiveClap)] +#[interactive_clap(input_context = crate::GlobalContext)] +#[interactive_clap(output_context = EditConnectionContext)] +pub struct EditConnection { + #[interactive_clap(skip_default_input_arg)] + /// What is the network connection name? + connection_name: String, + #[interactive_clap(subargs)] + parameter: Parameter, +} + +#[derive(Debug, Clone)] +pub struct EditConnectionContext { + global_context: crate::GlobalContext, + connection_name: String, + network_config: crate::config::NetworkConfig, +} + +impl EditConnectionContext { + pub fn from_previous_context( + previous_context: crate::GlobalContext, + scope: &::InteractiveClapContextScope, + ) -> color_eyre::eyre::Result { + let network_config = previous_context + .config + .network_connection + .get(&scope.connection_name) + .unwrap_or_else(|| { + panic!( + "Network connection \"{}\" not found", + &scope.connection_name + ) + }) + .clone(); + + Ok(Self { + global_context: previous_context, + connection_name: scope.connection_name.clone(), + network_config, + }) + } +} + +impl EditConnection { + fn input_connection_name( + context: &crate::GlobalContext, + ) -> color_eyre::eyre::Result> { + crate::common::input_network_name(&context.config, &[]) + } +} + +#[derive(Debug, Clone, interactive_clap::InteractiveClap)] +#[interactive_clap(input_context = EditConnectionContext)] +#[interactive_clap(output_context = ParameterContext)] +pub struct Parameter { + #[interactive_clap(long)] + #[interactive_clap(skip_default_input_arg)] + /// Which parameter do you want to update? + key: String, + #[interactive_clap(long)] + #[interactive_clap(skip_default_input_arg)] + /// Enter a new value for this parameter: + value: String, +} + +#[derive(Debug, Clone)] +pub struct ParameterContext; + +impl ParameterContext { + pub fn from_previous_context( + previous_context: EditConnectionContext, + scope: &::InteractiveClapContextScope, + ) -> color_eyre::eyre::Result { + let mut config = previous_context.global_context.config; + if let Some(network_config) = config + .network_connection + .get_mut(&previous_context.connection_name) + { + if scope.key.contains("network_name") { + network_config.network_name.clone_from(&scope.value) + } else if scope.key.contains("rpc_url") { + network_config.rpc_url = scope.value.clone().parse()?; + } else if scope.key.contains("rpc_api_key") { + network_config.rpc_api_key = if &scope.value == "null" { + None + } else { + Some(crate::types::api_key::ApiKey::from_str(&scope.value)?) + }; + } else if scope.key.contains("wallet_url") { + network_config.wallet_url = scope.value.clone().parse()?; + } else if scope.key.contains("explorer_transaction_url") { + network_config.explorer_transaction_url = scope.value.clone().parse()?; + } else if scope.key.contains("linkdrop_account_id") { + network_config.linkdrop_account_id = if &scope.value == "null" { + None + } else { + Some(near_primitives::types::AccountId::from_str(&scope.value)?) + }; + } else if scope.key.contains("near_social_db_contract_account_id") { + network_config.near_social_db_contract_account_id = if &scope.value == "null" { + None + } else { + Some(near_primitives::types::AccountId::from_str(&scope.value)?) + }; + } else if scope.key.contains("faucet_url") { + network_config.faucet_url = if &scope.value == "null" { + None + } else { + Some(scope.value.clone().parse()?) + }; + } else if scope.key.contains("meta_transaction_relayer_url") { + network_config.meta_transaction_relayer_url = if &scope.value == "null" { + None + } else { + Some(scope.value.clone().parse()?) + }; + } else if scope.key.contains("fastnear_url") { + network_config.fastnear_url = if &scope.value == "null" { + None + } else { + Some(scope.value.clone().parse()?) + }; + } else if scope.key.contains("staking_pools_factory_account_id") { + network_config.staking_pools_factory_account_id = if &scope.value == "null" { + None + } else { + Some(near_primitives::types::AccountId::from_str(&scope.value)?) + }; + } else if scope.key.contains("coingecko_url") { + network_config.coingecko_url = if &scope.value == "null" { + None + } else { + Some(scope.value.clone().parse()?) + }; + } else { + return color_eyre::eyre::Result::Err(color_eyre::eyre::eyre!( + "Parameter <{}> not found", + &scope.key + )); + } + } else { + return color_eyre::eyre::Result::Err(color_eyre::eyre::eyre!( + "Network connection \"{}\" not found", + &previous_context.connection_name + )); + }; + + eprintln!(); + config.write_config_toml()?; + eprintln!( + "Parameter <{}> successfully updated for Network connection \"{}\"", + &scope.key, &previous_context.connection_name + ); + Ok(Self) + } +} + +impl Parameter { + fn input_key(context: &EditConnectionContext) -> color_eyre::eyre::Result> { + let variants = context.network_config.get_fields()?; + + let select_submit = + Select::new("Whitch of the parametrs do you want to change?", variants).prompt(); + match select_submit { + Ok(value) => Ok(Some( + value.split_once(':').expect("Internal error").0.to_string(), + )), + Err( + inquire::error::InquireError::OperationCanceled + | inquire::error::InquireError::OperationInterrupted, + ) => Ok(None), + Err(err) => Err(err.into()), + } + } + + pub fn input_value( + _context: &EditConnectionContext, + ) -> color_eyre::eyre::Result> { + let value: String = + Text::new("Enter a new value for this parameter (If you want to remove this optional parameter, leave \"null\"):") + .with_initial_value("null") + .prompt()?; + Ok(Some(value)) + } +} diff --git a/src/commands/config/mod.rs b/src/commands/config/mod.rs index 9a1143354..c38096bff 100644 --- a/src/commands/config/mod.rs +++ b/src/commands/config/mod.rs @@ -3,7 +3,7 @@ use strum::{EnumDiscriminants, EnumIter, EnumMessage}; mod add_connection; mod delete_connection; -mod update; +mod edit_connection; #[derive(Debug, Clone, interactive_clap::InteractiveClap)] #[interactive_clap(context = crate::GlobalContext)] @@ -26,11 +26,9 @@ pub enum ConfigActions { #[strum_discriminants(strum(message = "add-connection - Add a network connection"))] /// Add a network connection AddConnection(self::add_connection::AddNetworkConnection), - #[strum_discriminants(strum( - message = "update - Update the network connection option" - ))] - /// Update the network connection option - Update(self::update::Update), + #[strum_discriminants(strum(message = "edit-connection - Edit a network connection"))] + /// Edit a network connection + EditConnection(self::edit_connection::EditConnection), #[strum_discriminants(strum( message = "delete-connection - Delete a network connection" ))] diff --git a/src/config/mod.rs b/src/config/mod.rs index 3e835de40..f6f8fb0cf 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -144,6 +144,17 @@ pub struct NetworkConfig { } impl NetworkConfig { + pub fn get_fields(&self) -> color_eyre::eyre::Result> { + let network_config_value: serde_json::Value = + serde_json::from_slice(serde_json::to_string(self)?.as_bytes())?; + Ok(network_config_value + .as_object() + .wrap_err("Internal error")? + .iter() + .map(|(key, value)| format!("{key}: {value}")) + .collect()) + } + #[tracing::instrument(name = "Connecting to RPC", skip_all)] pub fn json_rpc_client(&self) -> near_jsonrpc_client::JsonRpcClient { tracing::Span::current().pb_set_message(self.rpc_url.as_str()); From 83e43bb7790916787cfbfd820612058174583739 Mon Sep 17 00:00:00 2001 From: Vlad Frolov Date: Sat, 20 Jul 2024 09:40:17 +0200 Subject: [PATCH 6/7] refactored the edit config keys handling --- src/commands/config/edit_connection/mod.rs | 95 ++++++++++++---------- src/config/mod.rs | 4 +- 2 files changed, 52 insertions(+), 47 deletions(-) diff --git a/src/commands/config/edit_connection/mod.rs b/src/commands/config/edit_connection/mod.rs index 52544ebbf..94f9792a3 100644 --- a/src/commands/config/edit_connection/mod.rs +++ b/src/commands/config/edit_connection/mod.rs @@ -1,5 +1,3 @@ -use std::str::FromStr; - use inquire::{Select, Text}; #[derive(Debug, Clone, interactive_clap::InteractiveClap)] @@ -76,84 +74,93 @@ impl ParameterContext { scope: &::InteractiveClapContextScope, ) -> color_eyre::eyre::Result { let mut config = previous_context.global_context.config; - if let Some(network_config) = config + let Some(network_config) = config .network_connection .get_mut(&previous_context.connection_name) - { - if scope.key.contains("network_name") { - network_config.network_name.clone_from(&scope.value) - } else if scope.key.contains("rpc_url") { - network_config.rpc_url = scope.value.clone().parse()?; - } else if scope.key.contains("rpc_api_key") { - network_config.rpc_api_key = if &scope.value == "null" { + else { + return color_eyre::eyre::Result::Err(color_eyre::eyre::eyre!( + "Network connection \"{}\" not found", + &previous_context.connection_name + )); + }; + match scope.key.as_str() { + "network_name" => network_config.network_name.clone_from(&scope.value), + "rpc_url" => network_config.rpc_url = scope.value.parse()?, + "rpc_api_key" => { + network_config.rpc_api_key = if scope.value == "null" { None } else { - Some(crate::types::api_key::ApiKey::from_str(&scope.value)?) + Some(scope.value.parse()?) }; - } else if scope.key.contains("wallet_url") { - network_config.wallet_url = scope.value.clone().parse()?; - } else if scope.key.contains("explorer_transaction_url") { - network_config.explorer_transaction_url = scope.value.clone().parse()?; - } else if scope.key.contains("linkdrop_account_id") { - network_config.linkdrop_account_id = if &scope.value == "null" { + } + "wallet_url" => { + network_config.wallet_url = scope.value.parse()?; + } + "explorer_transaction_url" => { + network_config.explorer_transaction_url = scope.value.parse()?; + } + "linkdrop_account_id" => { + network_config.linkdrop_account_id = if scope.value == "null" { None } else { - Some(near_primitives::types::AccountId::from_str(&scope.value)?) + Some(scope.value.parse()?) }; - } else if scope.key.contains("near_social_db_contract_account_id") { + } + "near_social_db_contract_account_id" => { network_config.near_social_db_contract_account_id = if &scope.value == "null" { None } else { - Some(near_primitives::types::AccountId::from_str(&scope.value)?) + Some(scope.value.parse()?) }; - } else if scope.key.contains("faucet_url") { - network_config.faucet_url = if &scope.value == "null" { + } + "faucet_url" => { + network_config.faucet_url = if scope.value == "null" { None } else { - Some(scope.value.clone().parse()?) + Some(scope.value.parse()?) }; - } else if scope.key.contains("meta_transaction_relayer_url") { + } + "meta_transaction_relayer_url" => { network_config.meta_transaction_relayer_url = if &scope.value == "null" { None } else { - Some(scope.value.clone().parse()?) + Some(scope.value.parse()?) }; - } else if scope.key.contains("fastnear_url") { + } + "fastnear_url" => { network_config.fastnear_url = if &scope.value == "null" { None } else { - Some(scope.value.clone().parse()?) + Some(scope.value.parse()?) }; - } else if scope.key.contains("staking_pools_factory_account_id") { + } + "staking_pools_factory_account_id" => { network_config.staking_pools_factory_account_id = if &scope.value == "null" { None } else { - Some(near_primitives::types::AccountId::from_str(&scope.value)?) + Some(scope.value.parse()?) }; - } else if scope.key.contains("coingecko_url") { + } + "coingecko_url" => { network_config.coingecko_url = if &scope.value == "null" { None } else { - Some(scope.value.clone().parse()?) + Some(scope.value.parse()?) }; - } else { + } + _ => { return color_eyre::eyre::Result::Err(color_eyre::eyre::eyre!( - "Parameter <{}> not found", + "Configuration key <{}> not found", &scope.key )); } - } else { - return color_eyre::eyre::Result::Err(color_eyre::eyre::eyre!( - "Network connection \"{}\" not found", - &previous_context.connection_name - )); - }; + } eprintln!(); config.write_config_toml()?; eprintln!( - "Parameter <{}> successfully updated for Network connection \"{}\"", - &scope.key, &previous_context.connection_name + "Network connection \"{}\" was successfully updated with the new value for <{}>", + &previous_context.connection_name, &scope.key ); Ok(Self) } @@ -163,8 +170,7 @@ impl Parameter { fn input_key(context: &EditConnectionContext) -> color_eyre::eyre::Result> { let variants = context.network_config.get_fields()?; - let select_submit = - Select::new("Whitch of the parametrs do you want to change?", variants).prompt(); + let select_submit = Select::new("Which setting do you want to change?", variants).prompt(); match select_submit { Ok(value) => Ok(Some( value.split_once(':').expect("Internal error").0.to_string(), @@ -181,8 +187,7 @@ impl Parameter { _context: &EditConnectionContext, ) -> color_eyre::eyre::Result> { let value: String = - Text::new("Enter a new value for this parameter (If you want to remove this optional parameter, leave \"null\"):") - .with_initial_value("null") + Text::new("Enter a new value for this parameter (if you want to remove an optional parameter, use \"null\"):") .prompt()?; Ok(Some(value)) } diff --git a/src/config/mod.rs b/src/config/mod.rs index f6f8fb0cf..319542df5 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -144,9 +144,9 @@ pub struct NetworkConfig { } impl NetworkConfig { - pub fn get_fields(&self) -> color_eyre::eyre::Result> { + pub(crate) fn get_fields(&self) -> color_eyre::eyre::Result> { let network_config_value: serde_json::Value = - serde_json::from_slice(serde_json::to_string(self)?.as_bytes())?; + serde_json::from_str(&serde_json::to_string(self)?)?; Ok(network_config_value .as_object() .wrap_err("Internal error")? From cfa2ef58dc306b9cc7844ba9be28d92cd9a5be3a Mon Sep 17 00:00:00 2001 From: FroVolod Date: Sun, 21 Jul 2024 08:34:09 +0300 Subject: [PATCH 7/7] update mode removed --- .../config/update/explorer_transaction_url.rs | 45 ----------- .../config/update/linkdrop_account_id.rs | 45 ----------- src/commands/config/update/mod.rs | 40 ---------- src/commands/config/update/rpc_url.rs | 76 ------------------- src/commands/config/update/wallet_url.rs | 45 ----------- 5 files changed, 251 deletions(-) delete mode 100644 src/commands/config/update/explorer_transaction_url.rs delete mode 100644 src/commands/config/update/linkdrop_account_id.rs delete mode 100644 src/commands/config/update/mod.rs delete mode 100644 src/commands/config/update/rpc_url.rs delete mode 100644 src/commands/config/update/wallet_url.rs diff --git a/src/commands/config/update/explorer_transaction_url.rs b/src/commands/config/update/explorer_transaction_url.rs deleted file mode 100644 index b065ef4ea..000000000 --- a/src/commands/config/update/explorer_transaction_url.rs +++ /dev/null @@ -1,45 +0,0 @@ -#[derive(Debug, Clone, interactive_clap::InteractiveClap)] -#[interactive_clap(input_context = crate::GlobalContext)] -#[interactive_clap(output_context = ExplorerTransactionUrlContext)] -pub struct ExplorerTransactionUrl { - /// What is the transaction explorer endpoint? - explorer_transaction_url: crate::types::url::Url, - /// What is the network connection name? - #[interactive_clap(skip_default_input_arg)] - connection_name: String, -} - -#[derive(Debug, Clone)] -pub struct ExplorerTransactionUrlContext; - -impl ExplorerTransactionUrlContext { - pub fn from_previous_context( - previous_context: crate::GlobalContext, - scope: &::InteractiveClapContextScope, - ) -> color_eyre::eyre::Result { - let mut config = previous_context.config; - if let Some(network_config) = config.network_connection.get_mut(&scope.connection_name) { - network_config.explorer_transaction_url = scope.explorer_transaction_url.clone().into(); - } else { - return color_eyre::eyre::Result::Err(color_eyre::eyre::eyre!( - "Network connection \"{}\" not found", - &scope.connection_name - )); - }; - eprintln!(); - config.write_config_toml()?; - eprintln!( - "Explorer transaction URL successfully updated for Network connection \"{}\"", - &scope.connection_name - ); - Ok(Self) - } -} - -impl ExplorerTransactionUrl { - fn input_connection_name( - context: &crate::GlobalContext, - ) -> color_eyre::eyre::Result> { - crate::common::input_network_name(&context.config, &[]) - } -} diff --git a/src/commands/config/update/linkdrop_account_id.rs b/src/commands/config/update/linkdrop_account_id.rs deleted file mode 100644 index 4dd7c6e96..000000000 --- a/src/commands/config/update/linkdrop_account_id.rs +++ /dev/null @@ -1,45 +0,0 @@ -#[derive(Debug, Clone, interactive_clap::InteractiveClap)] -#[interactive_clap(input_context = crate::GlobalContext)] -#[interactive_clap(output_context = LinkdropAccountIdContext)] -pub struct LinkdropAccountId { - /// What is the name of the account that hosts the "linkdrop" program? (e.g. on mainnet it is near, and on testnet it is testnet) - linkdrop_account_id: crate::types::account_id::AccountId, - /// What is the network connection name? - #[interactive_clap(skip_default_input_arg)] - connection_name: String, -} - -#[derive(Debug, Clone)] -pub struct LinkdropAccountIdContext; - -impl LinkdropAccountIdContext { - pub fn from_previous_context( - previous_context: crate::GlobalContext, - scope: &::InteractiveClapContextScope, - ) -> color_eyre::eyre::Result { - let mut config = previous_context.config; - if let Some(network_config) = config.network_connection.get_mut(&scope.connection_name) { - network_config.linkdrop_account_id = Some(scope.linkdrop_account_id.clone().into()); - } else { - return color_eyre::eyre::Result::Err(color_eyre::eyre::eyre!( - "Network connection \"{}\" not found", - &scope.connection_name - )); - }; - eprintln!(); - config.write_config_toml()?; - eprintln!( - "Linkdrop account ID successfully updated for Network connection \"{}\"", - &scope.connection_name - ); - Ok(Self) - } -} - -impl LinkdropAccountId { - fn input_connection_name( - context: &crate::GlobalContext, - ) -> color_eyre::eyre::Result> { - crate::common::input_network_name(&context.config, &[]) - } -} diff --git a/src/commands/config/update/mod.rs b/src/commands/config/update/mod.rs deleted file mode 100644 index 76d7e7ae0..000000000 --- a/src/commands/config/update/mod.rs +++ /dev/null @@ -1,40 +0,0 @@ -use strum::{EnumDiscriminants, EnumIter, EnumMessage}; - -mod explorer_transaction_url; -mod linkdrop_account_id; -mod rpc_url; -mod wallet_url; - -#[derive(Debug, Clone, interactive_clap::InteractiveClap)] -#[interactive_clap(input_context = crate::GlobalContext)] -pub struct Update { - #[interactive_clap(subcommand)] - option: NetworkConnectionOptions, -} - -#[derive(Debug, EnumDiscriminants, Clone, interactive_clap::InteractiveClap)] -#[interactive_clap(context = crate::GlobalContext)] -#[strum_discriminants(derive(EnumMessage, EnumIter))] -/// What are you updating? -pub enum NetworkConnectionOptions { - #[strum_discriminants(strum( - message = "rpc-url - Update the rpc URL to connect" - ))] - /// Update the rpc url to connect - RpcUrl(self::rpc_url::RpcUrl), - #[strum_discriminants(strum( - message = "wallet-url - Update the wallet URL to connect" - ))] - /// Update the wallet url to connect - WalletUrl(self::wallet_url::WalletUrl), - #[strum_discriminants(strum( - message = "explorer-transaction-url - Update the explorer transaction URL to connect" - ))] - /// Update the explorer transaction URL to connect - ExplorerTransactionUrl(self::explorer_transaction_url::ExplorerTransactionUrl), - #[strum_discriminants(strum( - message = "linkdrop-account-id - Update the linkdrop account ID to connect" - ))] - /// Update the linkdrop account ID to connect - LinkdropAccountId(self::linkdrop_account_id::LinkdropAccountId), -} diff --git a/src/commands/config/update/rpc_url.rs b/src/commands/config/update/rpc_url.rs deleted file mode 100644 index 760a5c763..000000000 --- a/src/commands/config/update/rpc_url.rs +++ /dev/null @@ -1,76 +0,0 @@ -use inquire::{CustomType, Select}; - -#[derive(Debug, Clone, interactive_clap::InteractiveClap)] -#[interactive_clap(input_context = crate::GlobalContext)] -#[interactive_clap(output_context = RpcUrlContext)] -pub struct RpcUrl { - /// What is the RPC endpoint? - rpc_url: crate::types::url::Url, - #[interactive_clap(long)] - #[interactive_clap(skip_default_input_arg)] - rpc_api_key: Option, - /// What is the network connection name? - #[interactive_clap(skip_default_input_arg)] - connection_name: String, -} - -#[derive(Debug, Clone)] -pub struct RpcUrlContext; - -impl RpcUrlContext { - pub fn from_previous_context( - previous_context: crate::GlobalContext, - scope: &::InteractiveClapContextScope, - ) -> color_eyre::eyre::Result { - let mut config = previous_context.config; - if let Some(network_config) = config.network_connection.get_mut(&scope.connection_name) { - network_config.rpc_url = scope.rpc_url.clone().into(); - network_config.rpc_api_key.clone_from(&scope.rpc_api_key); - } else { - return color_eyre::eyre::Result::Err(color_eyre::eyre::eyre!( - "Network connection \"{}\" not found", - &scope.connection_name - )); - }; - eprintln!(); - config.write_config_toml()?; - eprintln!( - "Rpc URL successfully updated for Network connection \"{}\"", - &scope.connection_name - ); - Ok(Self) - } -} - -impl RpcUrl { - fn input_rpc_api_key( - _context: &crate::GlobalContext, - ) -> color_eyre::eyre::Result> { - eprintln!(); - #[derive(strum_macros::Display)] - enum ConfirmOptions { - #[strum(to_string = "Yes, the RPC endpoint requires API key")] - Yes, - #[strum(to_string = "No, the RPC endpoint does not require API key")] - No, - } - let select_choose_input = Select::new( - "Do you want to input an API key?", - vec![ConfirmOptions::Yes, ConfirmOptions::No], - ) - .prompt()?; - if let ConfirmOptions::Yes = select_choose_input { - let api_key: crate::types::api_key::ApiKey = - CustomType::new("Enter an API key").prompt()?; - Ok(Some(api_key)) - } else { - Ok(None) - } - } - - fn input_connection_name( - context: &crate::GlobalContext, - ) -> color_eyre::eyre::Result> { - crate::common::input_network_name(&context.config, &[]) - } -} diff --git a/src/commands/config/update/wallet_url.rs b/src/commands/config/update/wallet_url.rs deleted file mode 100644 index fe90a146a..000000000 --- a/src/commands/config/update/wallet_url.rs +++ /dev/null @@ -1,45 +0,0 @@ -#[derive(Debug, Clone, interactive_clap::InteractiveClap)] -#[interactive_clap(input_context = crate::GlobalContext)] -#[interactive_clap(output_context = WalletUrlContext)] -pub struct WalletUrl { - /// What is the wallet endpoint? - wallet_url: crate::types::url::Url, - /// What is the network connection name? - #[interactive_clap(skip_default_input_arg)] - connection_name: String, -} - -#[derive(Debug, Clone)] -pub struct WalletUrlContext; - -impl WalletUrlContext { - pub fn from_previous_context( - previous_context: crate::GlobalContext, - scope: &::InteractiveClapContextScope, - ) -> color_eyre::eyre::Result { - let mut config = previous_context.config; - if let Some(network_config) = config.network_connection.get_mut(&scope.connection_name) { - network_config.wallet_url = scope.wallet_url.clone().into(); - } else { - return color_eyre::eyre::Result::Err(color_eyre::eyre::eyre!( - "Network connection \"{}\" not found", - &scope.connection_name - )); - }; - eprintln!(); - config.write_config_toml()?; - eprintln!( - "Wallet URL successfully updated for Network connection \"{}\"", - &scope.connection_name - ); - Ok(Self) - } -} - -impl WalletUrl { - fn input_connection_name( - context: &crate::GlobalContext, - ) -> color_eyre::eyre::Result> { - crate::common::input_network_name(&context.config, &[]) - } -}