From 378a6950af33eafe47e52ccc72c6b82b5b943f75 Mon Sep 17 00:00:00 2001 From: FroVolod Date: Sat, 4 May 2024 00:52:03 +0300 Subject: [PATCH] refactor: Refactored the command for adding Function-Call Access key (#330) --- .../account/add_key/access_key_type/mod.rs | 38 ++++++------------- .../add_action/add_key/access_key_type/mod.rs | 38 ++++++------------- .../add_action/add_key/access_key_type/mod.rs | 38 ++++++------------- .../add_action/add_key/access_key_type/mod.rs | 38 ++++++------------- src/js_command_match/deploy.rs | 10 ++--- src/js_command_match/dev_deploy.rs | 10 ++--- src/types/near_token.rs | 4 ++ 7 files changed, 54 insertions(+), 122 deletions(-) diff --git a/src/commands/account/add_key/access_key_type/mod.rs b/src/commands/account/add_key/access_key_type/mod.rs index 77f9c1e99..6a6c4c437 100644 --- a/src/commands/account/add_key/access_key_type/mod.rs +++ b/src/commands/account/add_key/access_key_type/mod.rs @@ -53,7 +53,7 @@ impl From for AccessTypeContext { pub struct FunctionCallType { #[interactive_clap(long)] #[interactive_clap(skip_default_input_arg)] - allowance: Option, + allowance: crate::types::near_token::NearToken, #[interactive_clap(long)] /// Enter a receiver to use by this access key to pay for function call gas and transaction fees: receiver_account_id: crate::types::account_id::AccountId, @@ -81,7 +81,7 @@ impl FunctionCallTypeContext { Ok(Self { global_context: previous_context.global_context, signer_account_id: previous_context.owner_account_id.into(), - allowance: scope.allowance, + allowance: Some(scope.allowance), receiver_account_id: scope.receiver_account_id.clone(), method_names: scope.method_names.clone(), }) @@ -110,17 +110,17 @@ impl FunctionCallType { ) -> color_eyre::eyre::Result> { #[derive(strum_macros::Display)] enum ConfirmOptions { - #[strum(to_string = "Yes, I want to input a list of method names that can be used")] - Yes, #[strum( - to_string = "No, I don't want to input a list of method names that can be used" + to_string = "Yes, I want to input a list of function names that can be called when transaction is signed by this access key" )] + Yes, + #[strum(to_string = "No, I allow it to call any functions on the specified contract")] No, } eprintln!(); let select_choose_input = Select::new( - "Do You want to input a list of method names that can be used?", + "Would you like the access key to be valid exclusively for calling specific functions on the contract?", vec![ConfirmOptions::Yes, ConfirmOptions::No], ) .prompt()?; @@ -145,26 +145,10 @@ impl FunctionCallType { pub fn input_allowance( _context: &super::AddKeyCommandContext, ) -> color_eyre::eyre::Result> { - eprintln!(); - #[derive(strum_macros::Display)] - enum ConfirmOptions { - #[strum(to_string = "Yes, I want to input allowance for receiver ID")] - Yes, - #[strum(to_string = "No, I don't want to input allowance for receiver ID")] - No, - } - let select_choose_input = Select::new( - "Do You want to input an allowance for receiver ID?", - vec![ConfirmOptions::Yes, ConfirmOptions::No], - ) - .prompt()?; - if let ConfirmOptions::Yes = select_choose_input { - let allowance_near_balance: crate::types::near_token::NearToken = - CustomType::new("Enter an allowance which is a balance limit to use by this access key to pay for function call gas and transaction fees (example: 10NEAR or 0.5near or 10000yoctonear):") - .prompt()?; - Ok(Some(allowance_near_balance)) - } else { - Ok(None) - } + let allowance_near_balance: crate::types::near_token::NearToken = + CustomType::new("Enter the allowance, a budget this access key can use to pay for transaction fees (example: 10NEAR or 0.5near or 10000yoctonear):") + .with_starting_input("0.25 NEAR") + .prompt()?; + Ok(Some(allowance_near_balance)) } } diff --git a/src/commands/transaction/construct_transaction/add_action_1/add_action/add_key/access_key_type/mod.rs b/src/commands/transaction/construct_transaction/add_action_1/add_action/add_key/access_key_type/mod.rs index 679c9e924..2bd08486c 100644 --- a/src/commands/transaction/construct_transaction/add_action_1/add_action/add_key/access_key_type/mod.rs +++ b/src/commands/transaction/construct_transaction/add_action_1/add_action/add_key/access_key_type/mod.rs @@ -49,7 +49,7 @@ impl From for AccessKeyPermissionContext { pub struct FunctionCallType { #[interactive_clap(long)] #[interactive_clap(skip_default_input_arg)] - allowance: Option, + allowance: crate::types::near_token::NearToken, #[interactive_clap(long)] /// Enter a receiver to use by this access key to pay for function call gas and transaction fees: receiver_account_id: crate::types::account_id::AccountId, @@ -70,7 +70,7 @@ impl FunctionCallTypeContext { ) -> color_eyre::eyre::Result { let access_key_permission = near_primitives::account::AccessKeyPermission::FunctionCall( near_primitives::account::FunctionCallPermission { - allowance: scope.allowance.map(|allowance| allowance.as_yoctonear()), + allowance: Some(scope.allowance.as_yoctonear()), receiver_id: scope.receiver_account_id.to_string(), method_names: scope.method_names.clone().into(), }, @@ -98,15 +98,15 @@ impl FunctionCallType { eprintln!(); #[derive(strum_macros::Display)] enum ConfirmOptions { - #[strum(to_string = "Yes, I want to input a list of method names that can be used")] - Yes, #[strum( - to_string = "No, I don't want to input a list of method names that can be used" + to_string = "Yes, I want to input a list of function names that can be called when transaction is signed by this access key" )] + Yes, + #[strum(to_string = "No, I allow it to call any functions on the specified contract")] No, } let select_choose_input = Select::new( - "Do You want to input a list of method names that can be used?", + "Would you like the access key to be valid exclusively for calling specific functions on the contract?", vec![ConfirmOptions::Yes, ConfirmOptions::No], ) .prompt()?; @@ -132,26 +132,10 @@ impl FunctionCallType { pub fn input_allowance( _context: &super::super::super::super::ConstructTransactionContext, ) -> color_eyre::eyre::Result> { - eprintln!(); - #[derive(strum_macros::Display)] - enum ConfirmOptions { - #[strum(to_string = "Yes, I want to input allowance for receiver ID")] - Yes, - #[strum(to_string = "No, I don't want to input allowance for receiver ID")] - No, - } - let select_choose_input = Select::new( - "Do You want to input an allowance for receiver ID?", - vec![ConfirmOptions::Yes, ConfirmOptions::No], - ) - .prompt()?; - if let ConfirmOptions::Yes = select_choose_input { - let allowance_near_balance: crate::types::near_token::NearToken = - CustomType::new("Enter an allowance which is a balance limit to use by this access key to pay for function call gas and transaction fees (example: 10NEAR or 0.5near or 10000yoctonear):") - .prompt()?; - Ok(Some(allowance_near_balance)) - } else { - Ok(None) - } + let allowance_near_balance: crate::types::near_token::NearToken = + CustomType::new("Enter the allowance, a budget this access key can use to pay for transaction fees (example: 10NEAR or 0.5near or 10000yoctonear):") + .with_starting_input("0.25 NEAR") + .prompt()?; + Ok(Some(allowance_near_balance)) } } diff --git a/src/commands/transaction/construct_transaction/add_action_2/add_action/add_key/access_key_type/mod.rs b/src/commands/transaction/construct_transaction/add_action_2/add_action/add_key/access_key_type/mod.rs index 679c9e924..2bd08486c 100644 --- a/src/commands/transaction/construct_transaction/add_action_2/add_action/add_key/access_key_type/mod.rs +++ b/src/commands/transaction/construct_transaction/add_action_2/add_action/add_key/access_key_type/mod.rs @@ -49,7 +49,7 @@ impl From for AccessKeyPermissionContext { pub struct FunctionCallType { #[interactive_clap(long)] #[interactive_clap(skip_default_input_arg)] - allowance: Option, + allowance: crate::types::near_token::NearToken, #[interactive_clap(long)] /// Enter a receiver to use by this access key to pay for function call gas and transaction fees: receiver_account_id: crate::types::account_id::AccountId, @@ -70,7 +70,7 @@ impl FunctionCallTypeContext { ) -> color_eyre::eyre::Result { let access_key_permission = near_primitives::account::AccessKeyPermission::FunctionCall( near_primitives::account::FunctionCallPermission { - allowance: scope.allowance.map(|allowance| allowance.as_yoctonear()), + allowance: Some(scope.allowance.as_yoctonear()), receiver_id: scope.receiver_account_id.to_string(), method_names: scope.method_names.clone().into(), }, @@ -98,15 +98,15 @@ impl FunctionCallType { eprintln!(); #[derive(strum_macros::Display)] enum ConfirmOptions { - #[strum(to_string = "Yes, I want to input a list of method names that can be used")] - Yes, #[strum( - to_string = "No, I don't want to input a list of method names that can be used" + to_string = "Yes, I want to input a list of function names that can be called when transaction is signed by this access key" )] + Yes, + #[strum(to_string = "No, I allow it to call any functions on the specified contract")] No, } let select_choose_input = Select::new( - "Do You want to input a list of method names that can be used?", + "Would you like the access key to be valid exclusively for calling specific functions on the contract?", vec![ConfirmOptions::Yes, ConfirmOptions::No], ) .prompt()?; @@ -132,26 +132,10 @@ impl FunctionCallType { pub fn input_allowance( _context: &super::super::super::super::ConstructTransactionContext, ) -> color_eyre::eyre::Result> { - eprintln!(); - #[derive(strum_macros::Display)] - enum ConfirmOptions { - #[strum(to_string = "Yes, I want to input allowance for receiver ID")] - Yes, - #[strum(to_string = "No, I don't want to input allowance for receiver ID")] - No, - } - let select_choose_input = Select::new( - "Do You want to input an allowance for receiver ID?", - vec![ConfirmOptions::Yes, ConfirmOptions::No], - ) - .prompt()?; - if let ConfirmOptions::Yes = select_choose_input { - let allowance_near_balance: crate::types::near_token::NearToken = - CustomType::new("Enter an allowance which is a balance limit to use by this access key to pay for function call gas and transaction fees (example: 10NEAR or 0.5near or 10000yoctonear):") - .prompt()?; - Ok(Some(allowance_near_balance)) - } else { - Ok(None) - } + let allowance_near_balance: crate::types::near_token::NearToken = + CustomType::new("Enter the allowance, a budget this access key can use to pay for transaction fees (example: 10NEAR or 0.5near or 10000yoctonear):") + .with_starting_input("0.25 NEAR") + .prompt()?; + Ok(Some(allowance_near_balance)) } } diff --git a/src/commands/transaction/construct_transaction/add_action_3/add_action/add_key/access_key_type/mod.rs b/src/commands/transaction/construct_transaction/add_action_3/add_action/add_key/access_key_type/mod.rs index 679c9e924..2bd08486c 100644 --- a/src/commands/transaction/construct_transaction/add_action_3/add_action/add_key/access_key_type/mod.rs +++ b/src/commands/transaction/construct_transaction/add_action_3/add_action/add_key/access_key_type/mod.rs @@ -49,7 +49,7 @@ impl From for AccessKeyPermissionContext { pub struct FunctionCallType { #[interactive_clap(long)] #[interactive_clap(skip_default_input_arg)] - allowance: Option, + allowance: crate::types::near_token::NearToken, #[interactive_clap(long)] /// Enter a receiver to use by this access key to pay for function call gas and transaction fees: receiver_account_id: crate::types::account_id::AccountId, @@ -70,7 +70,7 @@ impl FunctionCallTypeContext { ) -> color_eyre::eyre::Result { let access_key_permission = near_primitives::account::AccessKeyPermission::FunctionCall( near_primitives::account::FunctionCallPermission { - allowance: scope.allowance.map(|allowance| allowance.as_yoctonear()), + allowance: Some(scope.allowance.as_yoctonear()), receiver_id: scope.receiver_account_id.to_string(), method_names: scope.method_names.clone().into(), }, @@ -98,15 +98,15 @@ impl FunctionCallType { eprintln!(); #[derive(strum_macros::Display)] enum ConfirmOptions { - #[strum(to_string = "Yes, I want to input a list of method names that can be used")] - Yes, #[strum( - to_string = "No, I don't want to input a list of method names that can be used" + to_string = "Yes, I want to input a list of function names that can be called when transaction is signed by this access key" )] + Yes, + #[strum(to_string = "No, I allow it to call any functions on the specified contract")] No, } let select_choose_input = Select::new( - "Do You want to input a list of method names that can be used?", + "Would you like the access key to be valid exclusively for calling specific functions on the contract?", vec![ConfirmOptions::Yes, ConfirmOptions::No], ) .prompt()?; @@ -132,26 +132,10 @@ impl FunctionCallType { pub fn input_allowance( _context: &super::super::super::super::ConstructTransactionContext, ) -> color_eyre::eyre::Result> { - eprintln!(); - #[derive(strum_macros::Display)] - enum ConfirmOptions { - #[strum(to_string = "Yes, I want to input allowance for receiver ID")] - Yes, - #[strum(to_string = "No, I don't want to input allowance for receiver ID")] - No, - } - let select_choose_input = Select::new( - "Do You want to input an allowance for receiver ID?", - vec![ConfirmOptions::Yes, ConfirmOptions::No], - ) - .prompt()?; - if let ConfirmOptions::Yes = select_choose_input { - let allowance_near_balance: crate::types::near_token::NearToken = - CustomType::new("Enter an allowance which is a balance limit to use by this access key to pay for function call gas and transaction fees (example: 10NEAR or 0.5near or 10000yoctonear):") - .prompt()?; - Ok(Some(allowance_near_balance)) - } else { - Ok(None) - } + let allowance_near_balance: crate::types::near_token::NearToken = + CustomType::new("Enter the allowance, a budget this access key can use to pay for transaction fees (example: 10NEAR or 0.5near or 10000yoctonear):") + .with_starting_input("0.25 NEAR") + .prompt()?; + Ok(Some(allowance_near_balance)) } } diff --git a/src/js_command_match/deploy.rs b/src/js_command_match/deploy.rs index f33fdc963..30b7769b4 100644 --- a/src/js_command_match/deploy.rs +++ b/src/js_command_match/deploy.rs @@ -40,11 +40,7 @@ impl DeployArgs { ]; }; - if let Some(init_args) = &self.init_args { - let mut initial_function = "new".to_string(); - if let Some(init_function) = &self.init_function { - initial_function = init_function.clone(); - } + if self.init_function.is_some() { vec![ "contract".to_owned(), "deploy".to_owned(), @@ -52,9 +48,9 @@ impl DeployArgs { "use-file".to_owned(), wasm_file.to_owned(), "with-init-call".to_owned(), - initial_function, + self.init_function.as_deref().unwrap_or("new").to_owned(), "json-args".to_owned(), - init_args.to_owned(), + self.init_args.as_deref().unwrap_or("{}").to_owned(), "prepaid-gas".to_owned(), format!("{} TeraGas", self.init_gas / 1_000_000_000_000), "attached-deposit".to_owned(), diff --git a/src/js_command_match/dev_deploy.rs b/src/js_command_match/dev_deploy.rs index 85b6e0422..1448e3b65 100644 --- a/src/js_command_match/dev_deploy.rs +++ b/src/js_command_match/dev_deploy.rs @@ -30,11 +30,7 @@ impl DevDeployArgs { "2. Run the following command, after inserting the name of the created dev-account:" ); - if let Some(init_args) = &self.init_args { - let mut initial_function = "new".to_string(); - if let Some(init_function) = &self.init_function { - initial_function = init_function.clone(); - } + if self.init_function.is_some() { eprintln!( " {}", shell_words::join(vec![ @@ -43,9 +39,9 @@ impl DevDeployArgs { "deploy".to_owned(), "".to_owned(), "with-init-call".to_owned(), - initial_function, + self.init_function.as_deref().unwrap_or("new").to_owned(), "json-args".to_owned(), - init_args.to_owned(), + self.init_args.as_deref().unwrap_or("{}").to_owned(), "prepaid-gas".to_owned(), format!("{} TeraGas", self.init_gas / 1_000_000_000_000), "attached-deposit".to_owned(), diff --git a/src/types/near_token.rs b/src/types/near_token.rs index 694dd9640..416300d8b 100644 --- a/src/types/near_token.rs +++ b/src/types/near_token.rs @@ -42,6 +42,10 @@ impl NearToken { pub fn from_yoctonear(inner: u128) -> Self { Self(near_token::NearToken::from_yoctonear(inner)) } + + pub const fn is_zero(&self) -> bool { + self.0.is_zero() + } } impl interactive_clap::ToCli for NearToken {