Skip to content

Commit

Permalink
start
Browse files Browse the repository at this point in the history
  • Loading branch information
FroVolod committed Jan 25, 2025
1 parent 4287b2e commit 4fe7333
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 14 deletions.
72 changes: 62 additions & 10 deletions src/commands/tokens/send_ft/amount_ft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use serde_json::{json, Value};
use crate::common::CallResultExt;
use crate::common::JsonRpcClientExt;

use super::super::view_ft_balance::get_ft_balance;

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(input_context = super::SendFtCommandContext)]
#[interactive_clap(output_context = AmountFtContext)]
Expand All @@ -19,11 +21,12 @@ pub struct AmountFt {

#[derive(Debug, Clone)]
pub struct AmountFtContext {
global_context: crate::GlobalContext,
signer_account_id: near_primitives::types::AccountId,
ft_contract_account_id: near_primitives::types::AccountId,
receiver_account_id: near_primitives::types::AccountId,
amount_ft: crate::types::ft_properties::FungibleToken,
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 transfer_amount_option: super::TransferAmountFtDiscriminants,
pub amount_ft: Option<crate::types::ft_properties::FungibleToken>,
}

impl AmountFtContext {
Expand Down Expand Up @@ -52,7 +55,8 @@ impl AmountFtContext {
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,
amount_ft: scope.amount_ft.normalize(&ft_metadata)?,
transfer_amount_option: super::TransferAmountFtDiscriminants::ExactAmount,
amount_ft: Some(scope.amount_ft.normalize(&ft_metadata)?),
})
}
}
Expand Down Expand Up @@ -116,7 +120,8 @@ pub struct PrepaidGasContext {
signer_account_id: near_primitives::types::AccountId,
ft_contract_account_id: near_primitives::types::AccountId,
receiver_account_id: near_primitives::types::AccountId,
amount_ft: crate::types::ft_properties::FungibleToken,
transfer_amount_option: super::TransferAmountFtDiscriminants,
amount_ft: Option<crate::types::ft_properties::FungibleToken>,
gas: crate::common::NearGas,
}

Expand All @@ -130,6 +135,7 @@ impl PrepaidGasContext {
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,
transfer_amount_option: previous_context.transfer_amount_option,
amount_ft: previous_context.amount_ft,
gas: scope.gas,
})
Expand Down Expand Up @@ -186,9 +192,32 @@ impl DepositContext {
let ft_contract_account_id = previous_context.ft_contract_account_id.clone();
let receiver_account_id = previous_context.receiver_account_id.clone();
let deposit = scope.deposit;
let amount_ft = previous_context.amount_ft.clone();
let optional_amount_ft = previous_context.amount_ft.clone();

move |network_config| {
let amount_ft = if let super::TransferAmountFtDiscriminants::MaxAmount = previous_context.transfer_amount_option {
let function_args = serde_json::to_vec(&json!({"account_id": signer_account_id.to_string()}))?;
let amount = get_ft_balance(
network_config,
&ft_contract_account_id,
function_args,
near_primitives::types::Finality::Final.into()
)?
.parse_result_from_json::<String>()?;
let crate::types::ft_properties::FtMetadata { decimals, symbol } = crate::types::ft_properties::params_ft_metadata(
ft_contract_account_id.clone(),
network_config,
near_primitives::types::Finality::Final.into(),
)?;
crate::types::ft_properties::FungibleToken::from_params_ft(
amount.parse::<u128>()?,
decimals,
symbol
)
} else {
optional_amount_ft.clone().expect("Internal error")
};

get_prepopulated_transaction(
network_config,
&ft_contract_account_id,
Expand All @@ -203,11 +232,34 @@ impl DepositContext {

let on_after_sending_transaction_callback: crate::transaction_signature_options::OnAfterSendingTransactionCallback = std::sync::Arc::new({
let signer_account_id = previous_context.signer_account_id.clone();
let amount_ft = previous_context.amount_ft.clone();
let ft_contract_account_id = previous_context.ft_contract_account_id.clone();
let receiver_account_id = previous_context.receiver_account_id.clone();
let optional_amount_ft = previous_context.amount_ft.clone();

move |outcome_view, network_config| {
let amount_ft = if let super::TransferAmountFtDiscriminants::MaxAmount = previous_context.transfer_amount_option {
let function_args = serde_json::to_vec(&json!({"account_id": signer_account_id.to_string()}))?;
let amount = get_ft_balance(
network_config,
&ft_contract_account_id,
function_args,
near_primitives::types::Finality::Final.into()
)?
.parse_result_from_json::<String>()?;
let crate::types::ft_properties::FtMetadata { decimals, symbol } = crate::types::ft_properties::params_ft_metadata(
ft_contract_account_id.clone(),
network_config,
near_primitives::types::Finality::Final.into(),
)?;
crate::types::ft_properties::FungibleToken::from_params_ft(
amount.parse::<u128>()?,
decimals,
symbol
)
} else {
optional_amount_ft.clone().expect("Internal error")
};

move |outcome_view, _network_config| {
if let near_primitives::views::FinalExecutionStatus::SuccessValue(_) = outcome_view.status {
eprintln!(
"<{signer_account_id}> has successfully transferred {amount_ft} (FT-contract: {ft_contract_account_id}) to <{receiver_account_id}>.",
Expand Down
33 changes: 33 additions & 0 deletions src/commands/tokens/send_ft/max_amount_ft.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(input_context = super::SendFtCommandContext)]
#[interactive_clap(output_context = MaxAmountFtContext)]
pub struct MaxAmountFt {
#[interactive_clap(named_arg)]
/// Enter gas for function call
prepaid_gas: super::amount_ft::PrepaidGas,
}

#[derive(Debug, Clone)]
pub struct MaxAmountFtContext(super::amount_ft::AmountFtContext);

impl MaxAmountFtContext {
pub fn from_previous_context(
previous_context: super::SendFtCommandContext,
_scope: &<MaxAmountFt as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
) -> color_eyre::eyre::Result<Self> {
Ok(Self(super::amount_ft::AmountFtContext {
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,
transfer_amount_option: super::TransferAmountFtDiscriminants::MaxAmount,
amount_ft: None,
}))
}
}

impl From<MaxAmountFtContext> for super::amount_ft::AmountFtContext {
fn from(item: MaxAmountFtContext) -> Self {
item.0
}
}
25 changes: 22 additions & 3 deletions src/commands/tokens/send_ft/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use strum::{EnumDiscriminants, EnumIter, EnumMessage};

mod amount_ft;
mod max_amount_ft;

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(input_context = super::TokensCommandsContext)]
Expand All @@ -10,9 +13,8 @@ pub struct SendFtCommand {
#[interactive_clap(skip_default_input_arg)]
/// What is the receiver account ID?
receiver_account_id: crate::types::account_id::AccountId,
#[interactive_clap(named_arg)]
/// Specify amount FT
amount_ft: self::amount_ft::AmountFt,
#[interactive_clap(subcommand)]
transfer_amount_ft: TransferAmountFt,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -56,3 +58,20 @@ impl SendFtCommand {
)
}
}

#[derive(Debug, EnumDiscriminants, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(context = SendFtCommandContext)]
#[strum_discriminants(derive(EnumMessage, EnumIter))]
/// Select an action with the amount of fungible tokens to transfer:
pub enum TransferAmountFt {
#[strum_discriminants(strum(
message = "exact-amount - Transfer of the specified amount of fungible tokens (wNearAmount (10 wNEAR))"
))]
/// Transfer of the specified amount of fungible tokens (wNearAmount (10 wNEAR))
ExactAmount(self::amount_ft::AmountFt),
#[strum_discriminants(strum(
message = "max-amount - Transfer the entire amount of fungible tokens from your account ID"
))]
/// Transfer the entire amount of fungible tokens from your account ID
MaxAmount(self::max_amount_ft::MaxAmountFt),
}
2 changes: 1 addition & 1 deletion src/commands/tokens/view_ft_balance/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl ViewFtBalance {
}

#[tracing::instrument(name = "Getting FT balance ...", skip_all)]
fn get_ft_balance(
pub fn get_ft_balance(
network_config: &crate::config::NetworkConfig,
ft_contract_account_id: &near_primitives::types::AccountId,
args: Vec<u8>,
Expand Down

0 comments on commit 4fe7333

Please sign in to comment.