-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Wrong console command for adding Function-Call key with unlimite…
…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
Showing
5 changed files
with
109 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters