Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fixed cli command for memo parameter #446

Merged
merged 11 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 48 additions & 23 deletions src/commands/tokens/send_ft/amount_ft.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use color_eyre::eyre::ContextCompat;
use inquire::{CustomType, Text};
use inquire::CustomType;

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(input_context = super::SendFtCommandContext)]
Expand All @@ -8,12 +8,9 @@ pub struct AmountFt {
#[interactive_clap(skip_default_input_arg)]
/// Enter an amount FT to transfer:
ft_transfer_amount: crate::types::ft_properties::FungibleTokenTransferAmount,
#[interactive_clap(skip_default_input_arg)]
/// Enter a memo for transfer (optional):
memo: Option<String>,
#[interactive_clap(named_arg)]
/// Enter gas for function call
prepaid_gas: super::preparation_ft_transfer::PrepaidGas,
/// Enter a memo for transfer (optional):
memo: Memo,
}

#[derive(Debug, Clone)]
Expand All @@ -23,7 +20,6 @@ pub struct AmountFtContext {
pub ft_contract_account_id: near_primitives::types::AccountId,
pub receiver_account_id: near_primitives::types::AccountId,
pub ft_transfer_amount: crate::types::ft_properties::FungibleTokenTransferAmount,
pub memo: Option<String>,
}

impl AmountFtContext {
Expand Down Expand Up @@ -61,14 +57,6 @@ impl AmountFtContext {
ft_contract_account_id: previous_context.ft_contract_account_id,
receiver_account_id: previous_context.receiver_account_id,
ft_transfer_amount,
memo: scope.memo.as_ref().and_then(|s| {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer this to be honest, over custom type that is not needed yet in the end.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

let trimmed = s.trim();
if trimmed.is_empty() {
None
} else {
Some(trimmed.to_string())
}
}),
})
}
}
Expand Down Expand Up @@ -113,15 +101,52 @@ impl AmountFt {
.prompt()?,
))
}
}

fn input_memo(
_context: &super::SendFtCommandContext,
) -> color_eyre::eyre::Result<Option<String>> {
let input = Text::new("Enter a memo for transfer (optional):").prompt()?;
Ok(if input.trim().is_empty() {
None
} else {
Some(input)
#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(input_context = AmountFtContext)]
#[interactive_clap(output_context = MemoContext)]
pub struct Memo {
#[interactive_clap(skip_default_input_arg)]
/// Enter a memo for transfer (optional):
memo: crate::types::optional_string::OptionalString,
#[interactive_clap(named_arg)]
/// Enter gas for function call
prepaid_gas: super::preparation_ft_transfer::PrepaidGas,
}

#[derive(Debug, Clone)]
pub struct MemoContext {
pub global_context: crate::GlobalContext,
pub signer_account_id: near_primitives::types::AccountId,
pub ft_contract_account_id: near_primitives::types::AccountId,
pub receiver_account_id: near_primitives::types::AccountId,
pub ft_transfer_amount: crate::types::ft_properties::FungibleTokenTransferAmount,
pub memo: Option<String>,
}

impl MemoContext {
pub fn from_previous_context(
previous_context: AmountFtContext,
scope: &<Memo as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
) -> color_eyre::eyre::Result<Self> {
Ok(Self {
global_context: previous_context.global_context,
signer_account_id: previous_context.signer_account_id,
ft_contract_account_id: previous_context.ft_contract_account_id,
receiver_account_id: previous_context.receiver_account_id,
ft_transfer_amount: previous_context.ft_transfer_amount,
memo: scope.memo.clone().into(),
})
}
}

impl Memo {
fn input_memo(
_context: &AmountFtContext,
) -> color_eyre::eyre::Result<Option<crate::types::optional_string::OptionalString>> {
Ok(Some(
CustomType::new("Enter a memo for transfer (optional):").prompt()?,
))
}
}
6 changes: 3 additions & 3 deletions src/commands/tokens/send_ft/preparation_ft_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::common::JsonRpcClientExt;
use super::super::view_ft_balance::get_ft_balance;

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(input_context = super::amount_ft::AmountFtContext)]
#[interactive_clap(input_context = super::amount_ft::MemoContext)]
#[interactive_clap(output_context = PrepaidGasContext)]
pub struct PrepaidGas {
#[interactive_clap(skip_default_input_arg)]
Expand All @@ -32,7 +32,7 @@ pub struct PrepaidGasContext {

impl PrepaidGasContext {
pub fn from_previous_context(
previous_context: super::amount_ft::AmountFtContext,
previous_context: super::amount_ft::MemoContext,
scope: &<PrepaidGas as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
) -> color_eyre::eyre::Result<Self> {
Ok(Self {
Expand All @@ -49,7 +49,7 @@ impl PrepaidGasContext {

impl PrepaidGas {
fn input_gas(
_context: &super::amount_ft::AmountFtContext,
_context: &super::amount_ft::MemoContext,
) -> color_eyre::eyre::Result<Option<crate::common::NearGas>> {
eprintln!();
Ok(Some(
Expand Down
1 change: 1 addition & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod ft_properties;
pub mod json;
pub mod near_allowance;
pub mod near_token;
pub mod optional_string;
pub mod path_buf;
pub mod public_key;
pub mod public_key_list;
Expand Down
41 changes: 41 additions & 0 deletions src/types/optional_string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#[derive(Debug, Default, Clone)]
Copy link
Collaborator

@akorchyn akorchyn Feb 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need this type? I think, it's a bit weird

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleted

pub struct OptionalString(Option<String>);

impl std::fmt::Display for OptionalString {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if let Some(s) = &self.0 {
s.fmt(f)
} else {
write!(f, "")
}
}
}

impl std::str::FromStr for OptionalString {
type Err = color_eyre::eyre::ErrReport;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let trimmed = s.trim();
if trimmed.is_empty() {
Ok(Self(None))
} else {
Ok(Self(Some(trimmed.to_lowercase())))
}
}
}

impl From<OptionalString> for Option<String> {
fn from(item: OptionalString) -> Self {
item.0
}
}

impl From<Option<String>> for OptionalString {
fn from(item: Option<String>) -> Self {
Self(item)
}
}

impl interactive_clap::ToCli for OptionalString {
type CliVariant = OptionalString;
}
Loading