Skip to content

Commit

Permalink
feat: let users specify a memo when sending ft
Browse files Browse the repository at this point in the history
[NEP-141](https://github.com/near/NEPs/blob/master/neps/nep-0141.md) specifies the
interface for fungible tokens.

The `ft_transfer` method has three parameters: `receiver_id`, `amount` and `memo`.
`memo` is an optional parameter.

This PR allows users to introduce a memo when using sending fungable tokens using
the cli.

The new parameter is optional and its skiped if no data is introduced.
  • Loading branch information
positiveblue committed Feb 1, 2025
1 parent cff9d75 commit 1ddc80e
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 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::{Context, ContextCompat};
use inquire::CustomType;
use inquire::{CustomType, Text};
use serde_json::{json, Value};

use crate::common::CallResultExt;
Expand All @@ -15,6 +15,9 @@ pub struct AmountFt {
#[interactive_clap(named_arg)]
/// Enter gas for function call
prepaid_gas: PrepaidGas,
#[interactive_clap(skip_default_input_arg)]
/// Enter a memo for transfer (optional):
memo: Option<String>,
}

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

impl AmountFtContext {
Expand Down Expand Up @@ -53,6 +57,10 @@ impl AmountFtContext {
ft_contract_account_id: previous_context.ft_contract_account_id,
receiver_account_id: previous_context.receiver_account_id,
amount_ft: scope.amount_ft.normalize(&ft_metadata)?,
memo: scope.memo.as_ref().and_then(|s| {
let trimmed = s.trim();
if trimmed.is_empty() { None } else { Some(trimmed.to_string()) }
}),
})
}
}
Expand Down Expand Up @@ -96,6 +104,13 @@ 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)]
Expand All @@ -118,6 +133,7 @@ pub struct PrepaidGasContext {
receiver_account_id: near_primitives::types::AccountId,
amount_ft: crate::types::ft_properties::FungibleToken,
gas: crate::common::NearGas,
memo: Option<String>,
}

impl PrepaidGasContext {
Expand All @@ -132,6 +148,7 @@ impl PrepaidGasContext {
receiver_account_id: previous_context.receiver_account_id,
amount_ft: previous_context.amount_ft,
gas: scope.gas,
memo: previous_context.memo,
})
}
}
Expand Down Expand Up @@ -187,14 +204,15 @@ impl DepositContext {
let receiver_account_id = previous_context.receiver_account_id.clone();
let deposit = scope.deposit;
let amount_ft = previous_context.amount_ft.clone();

let memo = previous_context.memo.clone();
move |network_config| {
get_prepopulated_transaction(
network_config,
&ft_contract_account_id,
&receiver_account_id,
&signer_account_id,
&amount_ft,
&memo,
&deposit,
&previous_context.gas
)
Expand Down Expand Up @@ -265,16 +283,23 @@ fn get_prepopulated_transaction(
receiver_account_id: &near_primitives::types::AccountId,
signer_id: &near_primitives::types::AccountId,
amount_ft: &crate::types::ft_properties::FungibleToken,
memo: &Option<String>,
deposit: &crate::types::near_token::NearToken,
gas: &crate::common::NearGas,
) -> color_eyre::eyre::Result<crate::commands::PrepopulatedTransaction> {
let mut transfer_args = serde_json::Map::new();
transfer_args.insert("receiver_id".to_string(), json!(receiver_account_id.to_string()));
transfer_args.insert("amount".to_string(), json!(amount_ft.amount().to_string()));
if let Some(m) = memo {
if !m.trim().is_empty() {
transfer_args.insert("memo".to_string(), json!(m));
}
}
let args_ft_transfer = serde_json::to_vec(&transfer_args)?;
let action_ft_transfer = near_primitives::transaction::Action::FunctionCall(Box::new(
near_primitives::transaction::FunctionCallAction {
method_name: "ft_transfer".to_string(),
args: serde_json::to_vec(&json!({
"receiver_id": receiver_account_id.to_string(),
"amount": amount_ft.amount().to_string()
}))?,
args: args_ft_transfer,
gas: gas.as_gas(),
deposit: deposit.as_yoctonear(),
},
Expand Down

0 comments on commit 1ddc80e

Please sign in to comment.