Skip to content

Commit

Permalink
fix: Wrong console command for adding Function-Call key with unlimite…
Browse files Browse the repository at this point in the history
…d allowance (#342)

Resolves #341 

_near-cli_ (JS) command with "allowance":
```
near add-key example-acct.testnet GkMNfc92fwM1AmwH1MTjF4b7UZuceamsq96XPkHsQ9vi --allowance 30000000000 --contract-id example-contract.testnet --method-names example_method
```
_near-cli-rs_ command:
```
near account add-key example-acct.testnet grant-function-call-access --allowance '30000000000 NEAR' --receiver-account-id example-contract.testnet --method-names example_method use-manually-provided-public-key GkMNfc92fwM1AmwH1MTjF4b7UZuceamsq96XPkHsQ9vi network-config testnet sign-with-keychain send
```

_near-cli_ (JS) command without "allowance":
```
near add-key example-acct.testnet GkMNfc92fwM1AmwH1MTjF4b7UZuceamsq96XPkHsQ9vi --contract-id example-contract.testnet --method-names example_method
```
_near-cli-rs_ command:
```
near account add-key example-acct.testnet grant-function-call-access --allowance unlimited --receiver-account-id example-contract.testnet --method-names example_method use-manually-provided-public-key ed25519:GkMNfc92fwM1AmwH1MTjF4b7UZuceamsq96XPkHsQ9vi network-config testnet
```
  • Loading branch information
FroVolod authored May 21, 2024
1 parent 5515836 commit d614e38
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 8 deletions.
10 changes: 5 additions & 5 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: crate::types::near_token::NearToken,
allowance: crate::types::near_allowance::NearAllowance,
#[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: Some(scope.allowance),
allowance: scope.allowance.optional_near_token(),
receiver_account_id: scope.receiver_account_id.clone(),
method_names: scope.method_names.clone(),
})
Expand Down Expand Up @@ -144,10 +144,10 @@ impl FunctionCallType {

pub fn input_allowance(
_context: &super::AddKeyCommandContext,
) -> color_eyre::eyre::Result<Option<crate::types::near_token::NearToken>> {
let allowance_near_balance: crate::types::near_token::NearToken =
) -> color_eyre::eyre::Result<Option<crate::types::near_allowance::NearAllowance>> {
let allowance_near_balance: crate::types::near_allowance::NearAllowance =
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")
.with_starting_input("unlimited")
.prompt()?;
Ok(Some(allowance_near_balance))
}
Expand Down
11 changes: 8 additions & 3 deletions src/js_command_match/add_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,27 @@ pub struct AddKeyArgs {
contract_id: Option<String>,
#[clap(long, aliases = ["method_names", "methodNames"], requires = "contract_id", value_delimiter = ',', num_args = 1..)]
method_names: Vec<String>,
#[clap(long, default_value = "0")]
allowance: String,
#[clap(long, default_value = None)]
allowance: Option<String>,
#[clap(allow_hyphen_values = true, num_args = 0..)]
_unknown_args: Vec<String>,
}

impl AddKeyArgs {
pub fn to_cli_args(&self, network_config: String) -> Vec<String> {
if let Some(contract_id) = self.contract_id.as_deref() {
let allowance = if let Some(allowance) = &self.allowance {
format!("{} NEAR", allowance)
} else {
"unlimited".to_string()
};
return vec![
"account".to_owned(),
"add-key".to_owned(),
self.account_id.to_owned(),
"grant-function-call-access".to_owned(),
"--allowance".to_owned(),
format!("{} NEAR", self.allowance),
allowance,
"--receiver-account-id".to_owned(),
contract_id.to_owned(),
"--method-names".to_owned(),
Expand Down
1 change: 1 addition & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod crypto_hash;
pub mod file_bytes;
pub mod ft_properties;
pub mod json;
pub mod near_allowance;
pub mod near_token;
pub mod path_buf;
pub mod public_key;
Expand Down
91 changes: 91 additions & 0 deletions src/types/near_allowance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const UNLIMITED: &str = "unlimited";

#[derive(
Debug,
Clone,
Copy,
serde::Serialize,
serde::Deserialize,
derive_more::AsRef,
derive_more::From,
derive_more::Into,
)]
#[as_ref(forward)]
pub struct NearAllowance(Option<crate::types::near_token::NearToken>);

impl std::fmt::Display for NearAllowance {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(amount) = self.0 {
amount.fmt(f)
} else {
write!(f, "{UNLIMITED}")
}
}
}

impl std::str::FromStr for NearAllowance {
type Err = near_token::NearTokenError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if s == UNLIMITED {
return Ok(Self(None));
}
Ok(Self(Some(crate::types::near_token::NearToken::from_str(
s,
)?)))
}
}

impl NearAllowance {
pub fn optional_near_token(&self) -> Option<crate::types::near_token::NearToken> {
self.0
}
}

impl interactive_clap::ToCli for NearAllowance {
type CliVariant = NearAllowance;
}

#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;

#[test]
fn near_allowance_to_string_0_near() {
assert_eq!(
NearAllowance(Some(near_token::NearToken::from_near(0).into())).to_string(),
"0 NEAR".to_string()
)
}
#[test]
fn near_allowance_to_string_0_millinear() {
assert_eq!(
NearAllowance(Some(near_token::NearToken::from_millinear(0).into())).to_string(),
"0 NEAR".to_string()
)
}
#[test]
fn near_allowance_to_string_none() {
assert_eq!(NearAllowance(None).to_string(), "unlimited".to_string())
}
#[test]
fn near_allowance_to_string_0dot02_near() {
assert_eq!(
NearAllowance(Some(
near_token::NearToken::from_yoctonear(20_000_000_000_000_000_000_000).into()
))
.to_string(),
"0.02 NEAR".to_string()
)
}
#[test]
fn near_allowance_from_str_unlimited() {
assert_eq!(
NearAllowance::from_str("unlimited")
.unwrap()
.optional_near_token(),
None
)
}
}
4 changes: 4 additions & 0 deletions src/types/near_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const ONE_NEAR: u128 = 10u128.pow(24);
Default,
Clone,
Copy,
Eq,
PartialEq,
Ord,
PartialOrd,
serde::Serialize,
serde::Deserialize,
derive_more::AsRef,
Expand Down

0 comments on commit d614e38

Please sign in to comment.