Skip to content

Commit

Permalink
refactor: Refactored the command for adding Function-Call Access key (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
FroVolod authored May 3, 2024
1 parent 918673b commit 378a695
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 122 deletions.
38 changes: 11 additions & 27 deletions src/commands/account/add_key/access_key_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl From<FullAccessTypeContext> for AccessTypeContext {
pub struct FunctionCallType {
#[interactive_clap(long)]
#[interactive_clap(skip_default_input_arg)]
allowance: Option<crate::types::near_token::NearToken>,
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,
Expand Down Expand Up @@ -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(),
})
Expand Down Expand Up @@ -110,17 +110,17 @@ impl FunctionCallType {
) -> color_eyre::eyre::Result<Option<crate::types::vec_string::VecString>> {
#[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()?;
Expand All @@ -145,26 +145,10 @@ impl FunctionCallType {
pub fn input_allowance(
_context: &super::AddKeyCommandContext,
) -> color_eyre::eyre::Result<Option<crate::types::near_token::NearToken>> {
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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl From<FullAccessTypeContext> for AccessKeyPermissionContext {
pub struct FunctionCallType {
#[interactive_clap(long)]
#[interactive_clap(skip_default_input_arg)]
allowance: Option<crate::types::near_token::NearToken>,
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,
Expand All @@ -70,7 +70,7 @@ impl FunctionCallTypeContext {
) -> color_eyre::eyre::Result<Self> {
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(),
},
Expand Down Expand Up @@ -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()?;
Expand All @@ -132,26 +132,10 @@ impl FunctionCallType {
pub fn input_allowance(
_context: &super::super::super::super::ConstructTransactionContext,
) -> color_eyre::eyre::Result<Option<crate::types::near_token::NearToken>> {
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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl From<FullAccessTypeContext> for AccessKeyPermissionContext {
pub struct FunctionCallType {
#[interactive_clap(long)]
#[interactive_clap(skip_default_input_arg)]
allowance: Option<crate::types::near_token::NearToken>,
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,
Expand All @@ -70,7 +70,7 @@ impl FunctionCallTypeContext {
) -> color_eyre::eyre::Result<Self> {
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(),
},
Expand Down Expand Up @@ -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()?;
Expand All @@ -132,26 +132,10 @@ impl FunctionCallType {
pub fn input_allowance(
_context: &super::super::super::super::ConstructTransactionContext,
) -> color_eyre::eyre::Result<Option<crate::types::near_token::NearToken>> {
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))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl From<FullAccessTypeContext> for AccessKeyPermissionContext {
pub struct FunctionCallType {
#[interactive_clap(long)]
#[interactive_clap(skip_default_input_arg)]
allowance: Option<crate::types::near_token::NearToken>,
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,
Expand All @@ -70,7 +70,7 @@ impl FunctionCallTypeContext {
) -> color_eyre::eyre::Result<Self> {
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(),
},
Expand Down Expand Up @@ -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()?;
Expand All @@ -132,26 +132,10 @@ impl FunctionCallType {
pub fn input_allowance(
_context: &super::super::super::super::ConstructTransactionContext,
) -> color_eyre::eyre::Result<Option<crate::types::near_token::NearToken>> {
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))
}
}
10 changes: 3 additions & 7 deletions src/js_command_match/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,17 @@ 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(),
contract_account_id.to_owned(),
"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(),
Expand Down
10 changes: 3 additions & 7 deletions src/js_command_match/dev_deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![
Expand All @@ -43,9 +39,9 @@ impl DevDeployArgs {
"deploy".to_owned(),
"<created-dev-account>".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(),
Expand Down
4 changes: 4 additions & 0 deletions src/types/near_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 378a695

Please sign in to comment.