Skip to content

Commit

Permalink
fix: Display NEAR token amounts precisely (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
FroVolod authored Dec 19, 2023
1 parent 7aca0ac commit ac7c60f
Show file tree
Hide file tree
Showing 30 changed files with 220 additions and 94 deletions.
8 changes: 4 additions & 4 deletions src/commands/account/add_key/access_key_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl From<FullAccessTypeContext> for AccessTypeContext {
pub struct FunctionCallType {
#[interactive_clap(long)]
#[interactive_clap(skip_default_input_arg)]
allowance: Option<near_token::NearToken>,
allowance: Option<crate::types::near_token::NearToken>,
#[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 All @@ -69,7 +69,7 @@ pub struct FunctionCallType {
pub struct FunctionCallTypeContext {
global_context: crate::GlobalContext,
signer_account_id: near_primitives::types::AccountId,
allowance: Option<near_token::NearToken>,
allowance: Option<crate::types::near_token::NearToken>,
receiver_account_id: crate::types::account_id::AccountId,
method_names: crate::types::vec_string::VecString,
}
Expand Down Expand Up @@ -217,7 +217,7 @@ impl FunctionCallType {

pub fn input_allowance(
_context: &super::AddKeyCommandContext,
) -> color_eyre::eyre::Result<Option<near_token::NearToken>> {
) -> color_eyre::eyre::Result<Option<crate::types::near_token::NearToken>> {
eprintln!();
#[derive(strum_macros::Display)]
enum ConfirmOptions {
Expand All @@ -232,7 +232,7 @@ impl FunctionCallType {
)
.prompt()?;
if let ConfirmOptions::Yes = select_choose_input {
let allowance_near_balance: near_token::NearToken =
let allowance_near_balance: crate::types::near_token::NearToken =
CustomType::new("Enter an allowance which is a balance limit to use by this access key to pay for function call gas and transaction fees (example: 10NEAR or 0.5near or 10000yoctonear):")
.prompt()?;
Ok(Some(allowance_near_balance))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct NewAccount {
new_account_id: crate::types::account_id::AccountId,
#[interactive_clap(skip_default_input_arg)]
/// Enter the amount for the account:
initial_balance: near_token::NearToken,
initial_balance: crate::types::near_token::NearToken,
#[interactive_clap(subcommand)]
access_key_mode: add_key::AccessKeyMode,
}
Expand All @@ -25,7 +25,7 @@ pub struct NewAccount {
pub struct NewAccountContext {
global_context: crate::GlobalContext,
new_account_id: near_primitives::types::AccountId,
initial_balance: near_token::NearToken,
initial_balance: crate::types::near_token::NearToken,
}

impl NewAccountContext {
Expand Down Expand Up @@ -133,9 +133,9 @@ impl NewAccount {

fn input_initial_balance(
_context: &crate::GlobalContext,
) -> color_eyre::eyre::Result<Option<near_token::NearToken>> {
) -> color_eyre::eyre::Result<Option<crate::types::near_token::NearToken>> {
eprintln!();
match near_token::NearToken::from_str(&Text::new("Enter the amount of the NEAR tokens you want to fund the new account with (example: 10NEAR or 0.5near or 10000yoctonear):")
match crate::types::near_token::NearToken::from_str(&Text::new("Enter the amount of the NEAR tokens you want to fund the new account with (example: 10NEAR or 0.5near or 10000yoctonear):")
.with_initial_value("0.1 NEAR")
.prompt()?
) {
Expand All @@ -159,5 +159,5 @@ pub struct AccountPropertiesContext {
pub struct AccountProperties {
pub new_account_id: near_primitives::types::AccountId,
pub public_key: near_crypto::PublicKey,
pub initial_balance: near_token::NearToken,
pub initial_balance: crate::types::near_token::NearToken,
}
4 changes: 2 additions & 2 deletions src/commands/account/storage_management/storage_deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct DepositArgs {
/// Which account ID do you want to add a deposit to?
receiver_account_id: crate::types::account_id::AccountId,
/// Enter the amount to deposit into the storage (example: 10NEAR or 0.5near or 10000yoctonear):
deposit: near_token::NearToken,
deposit: crate::types::near_token::NearToken,
#[interactive_clap(named_arg)]
/// What is the signer account ID?
sign_as: SignerAccountId,
Expand All @@ -19,7 +19,7 @@ pub struct DepositArgsContext {
global_context: crate::GlobalContext,
get_contract_account_id: super::GetContractAccountId,
receiver_account_id: near_primitives::types::AccountId,
deposit: near_token::NearToken,
deposit: crate::types::near_token::NearToken,
}

impl DepositArgsContext {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/account/storage_management/storage_withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#[interactive_clap(output_context = WithdrawArgsContext)]
pub struct WithdrawArgs {
/// Enter the amount to withdraw from the storage (example: 10NEAR or 0.5near or 10000yoctonear):
amount: near_token::NearToken,
amount: crate::types::near_token::NearToken,
#[interactive_clap(named_arg)]
/// What is the signer account ID?
sign_as: SignerAccountId,
Expand All @@ -13,7 +13,7 @@ pub struct WithdrawArgs {
pub struct WithdrawArgsContext {
global_context: crate::GlobalContext,
get_contract_account_id: super::GetContractAccountId,
amount: near_token::NearToken,
amount: crate::types::near_token::NearToken,
}

impl WithdrawArgsContext {
Expand Down
10 changes: 5 additions & 5 deletions src/commands/contract/call_function/as_transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl PrepaidGas {
pub struct Deposit {
#[interactive_clap(skip_default_input_arg)]
/// Enter deposit for a function call:
deposit: near_token::NearToken,
deposit: crate::types::near_token::NearToken,
#[interactive_clap(named_arg)]
/// What is the signer account ID?
sign_as: SignerAccountId,
Expand All @@ -145,7 +145,7 @@ pub struct DepositContext {
function_name: String,
function_args: Vec<u8>,
gas: crate::common::NearGas,
deposit: near_token::NearToken,
deposit: crate::types::near_token::NearToken,
}

impl DepositContext {
Expand All @@ -167,9 +167,9 @@ impl DepositContext {
impl Deposit {
fn input_deposit(
_context: &PrepaidGasContext,
) -> color_eyre::eyre::Result<Option<near_token::NearToken>> {
) -> color_eyre::eyre::Result<Option<crate::types::near_token::NearToken>> {
eprintln!();
match near_token::NearToken::from_str(
match crate::types::near_token::NearToken::from_str(
&Text::new(
"Enter deposit for a function call (example: 10NEAR or 0.5near or 10000yoctonear):",
)
Expand Down Expand Up @@ -201,7 +201,7 @@ pub struct SignerAccountIdContext {
function_name: String,
function_args: Vec<u8>,
gas: crate::common::NearGas,
deposit: near_token::NearToken,
deposit: crate::types::near_token::NearToken,
signer_account_id: near_primitives::types::AccountId,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl PrepaidGas {
pub struct Deposit {
#[interactive_clap(skip_default_input_arg)]
/// Enter deposit for a function call:
deposit: near_token::NearToken,
deposit: crate::types::near_token::NearToken,
#[interactive_clap(named_arg)]
/// Select network
network_config: crate::network_for_transaction::NetworkForTransactionArgs,
Expand Down Expand Up @@ -205,9 +205,9 @@ impl From<DepositContext> for crate::commands::ActionContext {
impl Deposit {
fn input_deposit(
_context: &PrepaidGasContext,
) -> color_eyre::eyre::Result<Option<near_token::NearToken>> {
) -> color_eyre::eyre::Result<Option<crate::types::near_token::NearToken>> {
eprintln!();
match near_token::NearToken::from_str(
match crate::types::near_token::NearToken::from_str(
&Text::new(
"Enter deposit for a function call (example: 10NEAR or 0.5near or 10000yoctonear):",
)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/staking/delegate/deposit_and_stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#[interactive_clap(output_context = DepositAndStakeContext)]
pub struct DepositAndStake {
/// Enter the attached amount to be deposited and then staked into the predecessor's internal account (example: 10NEAR or 0.5near or 10000yoctonear):
amount: near_token::NearToken,
amount: crate::types::near_token::NearToken,
#[interactive_clap(skip_default_input_arg)]
/// What is validator account ID?
validator_account_id: crate::types::account_id::AccountId,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/staking/delegate/stake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#[interactive_clap(output_context = StakeContext)]
pub struct Stake {
/// Enter the amount to stake from the inner account of the predecessor (example: 10NEAR or 0.5near or 10000yoctonear):
amount: near_token::NearToken,
amount: crate::types::near_token::NearToken,
#[interactive_clap(skip_default_input_arg)]
/// What is validator account ID?
validator_account_id: crate::types::account_id::AccountId,
Expand All @@ -25,7 +25,7 @@ impl StakeContext {
let signer_id = previous_context.account_id.clone();
let validator_account_id: near_primitives::types::AccountId =
scope.validator_account_id.clone().into();
let amount = scope.amount;
let amount = scope.amount.as_yoctonear();

move |_network_config| {
Ok(crate::commands::PrepopulatedTransaction {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/staking/delegate/unstake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#[interactive_clap(output_context = UnstakeContext)]
pub struct Unstake {
/// Enter the amount to unstake from the inner account of the predecessor (example: 10NEAR or 0.5near or 10000yoctonear):
amount: near_token::NearToken,
amount: crate::types::near_token::NearToken,
#[interactive_clap(skip_default_input_arg)]
/// What is validator account ID?
validator_account_id: crate::types::account_id::AccountId,
Expand All @@ -25,7 +25,7 @@ impl UnstakeContext {
let signer_id = previous_context.account_id.clone();
let validator_account_id: near_primitives::types::AccountId =
scope.validator_account_id.clone().into();
let amount = scope.amount;
let amount = scope.amount.as_yoctonear();

move |_network_config| {
Ok(crate::commands::PrepopulatedTransaction {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/staking/delegate/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#[interactive_clap(output_context = WithdrawContext)]
pub struct Withdraw {
/// Enter the amount to withdraw from the non staked balance (example: 10NEAR or 0.5near or 10000yoctonear):
amount: near_token::NearToken,
amount: crate::types::near_token::NearToken,
#[interactive_clap(skip_default_input_arg)]
/// What is validator account ID?
validator_account_id: crate::types::account_id::AccountId,
Expand All @@ -25,7 +25,7 @@ impl WithdrawContext {
let signer_id = previous_context.account_id.clone();
let validator_account_id: near_primitives::types::AccountId =
scope.validator_account_id.clone().into();
let amount = scope.amount;
let amount = scope.amount.as_yoctonear();

move |_network_config| {
Ok(crate::commands::PrepopulatedTransaction {
Expand Down
8 changes: 4 additions & 4 deletions src/commands/tokens/send_ft/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct SendFtCommand {
#[interactive_clap(long = "attached-deposit")]
#[interactive_clap(skip_default_input_arg)]
/// Enter deposit for a function call:
deposit: near_token::NearToken,
deposit: crate::types::near_token::NearToken,
#[interactive_clap(named_arg)]
/// Select network
network_config: crate::network_for_transaction::NetworkForTransactionArgs,
Expand All @@ -36,7 +36,7 @@ pub struct SendFtCommandContext {
receiver_account_id: near_primitives::types::AccountId,
amount: u128,
gas: crate::common::NearGas,
deposit: near_token::NearToken,
deposit: crate::types::near_token::NearToken,
}

impl SendFtCommandContext {
Expand Down Expand Up @@ -162,9 +162,9 @@ impl SendFtCommand {

fn input_deposit(
_context: &super::TokensCommandsContext,
) -> color_eyre::eyre::Result<Option<near_token::NearToken>> {
) -> color_eyre::eyre::Result<Option<crate::types::near_token::NearToken>> {
eprintln!();
match near_token::NearToken::from_str(
match crate::types::near_token::NearToken::from_str(
&Text::new(
"Enter deposit for a function call (example: 10NEAR or 0.5near or 10000yoctonear):",
)
Expand Down
17 changes: 3 additions & 14 deletions src/commands/tokens/send_near/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
use inquire::CustomType;

#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
#[interactive_clap(input_context = super::TokensCommandsContext)]
#[interactive_clap(output_context = SendNearCommandContext)]
pub struct SendNearCommand {
#[interactive_clap(skip_default_input_arg)]
/// What is the receiver account ID?
receiver_account_id: crate::types::account_id::AccountId,
#[interactive_clap(skip_default_input_arg)]
/// Enter an amount to transfer:
amount_in_near: near_token::NearToken,
/// How many NEAR Tokens do you want to transfer? (example: 10NEAR or 0.5near or 10000yoctonear)
amount_in_near: crate::types::near_token::NearToken,
#[interactive_clap(named_arg)]
/// Select network
network_config: crate::network_for_transaction::NetworkForTransactionArgs,
Expand All @@ -20,7 +17,7 @@ pub struct SendNearCommandContext {
global_context: crate::GlobalContext,
signer_account_id: near_primitives::types::AccountId,
receiver_account_id: near_primitives::types::AccountId,
amount_in_near: near_token::NearToken,
amount_in_near: crate::types::near_token::NearToken,
}

impl SendNearCommandContext {
Expand Down Expand Up @@ -83,12 +80,4 @@ impl SendNearCommand {
"What is the receiver account ID?",
)
}

fn input_amount_in_near(
_context: &super::TokensCommandsContext,
) -> color_eyre::eyre::Result<Option<near_token::NearToken>> {
let input_amount =
CustomType::new("How many NEAR Tokens do you want to transfer? (example: 10NEAR or 0.5near or 10000yoctonear)").prompt()?;
Ok(Some(input_amount))
}
}
8 changes: 4 additions & 4 deletions src/commands/tokens/send_nft/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct SendNftCommand {
#[interactive_clap(long = "attached-deposit")]
#[interactive_clap(skip_default_input_arg)]
/// Enter deposit for a function call:
deposit: near_token::NearToken,
deposit: crate::types::near_token::NearToken,
#[interactive_clap(named_arg)]
/// Select network
network_config: crate::network_for_transaction::NetworkForTransactionArgs,
Expand All @@ -36,7 +36,7 @@ pub struct SendNftCommandContext {
receiver_account_id: near_primitives::types::AccountId,
token_id: String,
gas: crate::common::NearGas,
deposit: near_token::NearToken,
deposit: crate::types::near_token::NearToken,
}

impl SendNftCommandContext {
Expand Down Expand Up @@ -163,9 +163,9 @@ impl SendNftCommand {

fn input_deposit(
_context: &super::TokensCommandsContext,
) -> color_eyre::eyre::Result<Option<near_token::NearToken>> {
) -> color_eyre::eyre::Result<Option<crate::types::near_token::NearToken>> {
eprintln!();
match near_token::NearToken::from_str(
match crate::types::near_token::NearToken::from_str(
&Text::new(
"Enter deposit for a function call (example: 10NEAR or 0.5near or 10000yoctonear):",
)
Expand Down
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 @@ -64,7 +64,7 @@ impl ViewFtBalanceContext {
format!(
"{}.{} {}",
amount / 10u128.pow(decimals as u32),
format!("{:0>24}", amount % 10u128.pow(decimals as u32)).trim_end_matches('0'),
format!("{:0>decimals$}", amount % 10u128.pow(decimals as u32), decimals=decimals.try_into().unwrap()).trim_end_matches('0'),
symbol
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub struct FunctionCallType {
#[interactive_clap(long)]
#[interactive_clap(skip_default_from_cli_arg)]
#[interactive_clap(skip_default_input_arg)]
allowance: Option<near_token::NearToken>,
allowance: Option<crate::types::near_token::NearToken>,
#[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 @@ -205,7 +205,8 @@ impl FunctionCallType {
}
}

pub fn input_allowance() -> color_eyre::eyre::Result<Option<near_token::NearToken>> {
pub fn input_allowance() -> color_eyre::eyre::Result<Option<crate::types::near_token::NearToken>>
{
eprintln!();
#[derive(strum_macros::Display)]
enum ConfirmOptions {
Expand All @@ -220,7 +221,7 @@ impl FunctionCallType {
)
.prompt()?;
if let ConfirmOptions::Yes = select_choose_input {
let allowance_near_balance: near_token::NearToken =
let allowance_near_balance: crate::types::near_token::NearToken =
CustomType::new("Enter an allowance which is a balance limit to use by this access key to pay for function call gas and transaction fees (example: 10NEAR or 0.5near or 10000yoctonear):")
.prompt()?;
Ok(Some(allowance_near_balance))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl PrepaidGas {
pub struct Deposit {
#[interactive_clap(skip_default_input_arg)]
/// Enter deposit for a function call:
deposit: near_token::NearToken,
deposit: crate::types::near_token::NearToken,
#[interactive_clap(subcommand)]
next_action: super::super::super::add_action_2::NextAction,
}
Expand Down Expand Up @@ -174,9 +174,9 @@ impl From<DepositContext> for super::super::super::ConstructTransactionContext {
impl Deposit {
fn input_deposit(
_context: &PrepaidGasContext,
) -> color_eyre::eyre::Result<Option<near_token::NearToken>> {
) -> color_eyre::eyre::Result<Option<crate::types::near_token::NearToken>> {
eprintln!();
match near_token::NearToken::from_str(
match crate::types::near_token::NearToken::from_str(
&Text::new(
"Enter deposit for a function call (example: 10NEAR or 0.5near or 10000yoctonear):",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#[interactive_clap(input_context = super::super::super::ConstructTransactionContext)]
#[interactive_clap(output_context = StakeActionContext)]
pub struct StakeAction {
stake_amount: near_token::NearToken,
stake_amount: crate::types::near_token::NearToken,
public_key: crate::types::public_key::PublicKey,
#[interactive_clap(subcommand)]
next_action: super::super::super::add_action_2::NextAction,
Expand Down
Loading

0 comments on commit ac7c60f

Please sign in to comment.