Skip to content

Commit

Permalink
replace is_amount with parser
Browse files Browse the repository at this point in the history
  • Loading branch information
samkim-crypto committed Nov 3, 2024
1 parent 97ef9a3 commit a3527b7
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 57 deletions.
20 changes: 15 additions & 5 deletions token/cli/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use {
crate::{clap_app::Error, command::CommandResult, config::Config},
clap::{value_t_or_exit, ArgMatches},
solana_clap_v3_utils::input_parsers::pubkey_of_signer,
solana_clap_v3_utils::input_parsers::{pubkey_of_signer, Amount},
solana_client::{
nonblocking::rpc_client::RpcClient, rpc_client::RpcClient as BlockingRpcClient,
tpu_client::TpuClient, tpu_client::TpuClientConfig,
Expand Down Expand Up @@ -58,7 +58,7 @@ pub(crate) async fn bench_process_command(
.unwrap()
.unwrap();
let n = value_t_or_exit!(arg_matches, "n", usize);
let ui_amount = value_t_or_exit!(arg_matches, "amount", f64);
let ui_amount = *arg_matches.get_one::<Amount>("amount").unwrap();
let (owner_signer, owner) =
config.signer_or_default(arg_matches, "owner", wallet_manager);
signers.push(owner_signer);
Expand All @@ -73,7 +73,7 @@ pub(crate) async fn bench_process_command(
.unwrap()
.unwrap();
let n = value_t_or_exit!(arg_matches, "n", usize);
let ui_amount = value_t_or_exit!(arg_matches, "amount", f64);
let ui_amount = *arg_matches.get_one::<Amount>("amount").unwrap();
let (owner_signer, owner) =
config.signer_or_default(arg_matches, "owner", wallet_manager);
signers.push(owner_signer);
Expand Down Expand Up @@ -237,7 +237,7 @@ async fn command_deposit_into_or_withdraw_from(
token: &Pubkey,
n: usize,
owner: &Pubkey,
ui_amount: f64,
ui_amount: Amount,
from_or_to: Option<Pubkey>,
deposit_into: bool,
) -> Result<(), Error> {
Expand All @@ -250,7 +250,17 @@ async fn command_deposit_into_or_withdraw_from(
let from_or_to = from_or_to
.unwrap_or_else(|| get_associated_token_address_with_program_id(owner, token, &program_id));
config.check_account(&from_or_to, Some(*token)).await?;
let amount = spl_token::ui_amount_to_amount(ui_amount, mint_info.decimals);
let amount = match ui_amount {
Amount::Raw(ui_amount) => ui_amount,
Amount::Decimal(ui_amount) => spl_token::ui_amount_to_amount(ui_amount, mint_info.decimals),
Amount::All => {
return Err(
"Use of ALL keyword currently not supported for the bench command"
.to_string()
.into(),
);
}
};

let token_addresses_with_seed = get_token_addresses_with_seed(&program_id, token, owner, n);
let mut messages = vec![];
Expand Down
28 changes: 14 additions & 14 deletions token/cli/src/clap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use {
fee_payer::fee_payer_arg,
input_parsers::Amount,
input_validators::{
is_amount, is_parsable, is_pubkey, is_url_or_moniker, is_valid_pubkey, is_valid_signer,
is_parsable, is_pubkey, is_url_or_moniker, is_valid_pubkey, is_valid_signer,
},
memo::memo_arg,
nonce::*,
Expand Down Expand Up @@ -340,7 +340,7 @@ pub fn transfer_lamports_arg<'a>() -> Arg<'a> {
.long(TRANSFER_LAMPORTS_ARG.long)
.takes_value(true)
.value_name("LAMPORTS")
.validator(|s| is_amount(s))
.value_parser(clap::value_parser!(u64))
.help(TRANSFER_LAMPORTS_ARG.help)
}

Expand Down Expand Up @@ -528,7 +528,7 @@ impl BenchSubCommand for App<'_> {
)
.arg(
Arg::with_name("amount")
.validator(|s| is_amount(s))
.value_parser(Amount::parse)
.value_name("TOKEN_AMOUNT")
.takes_value(true)
.index(3)
Expand Down Expand Up @@ -568,7 +568,7 @@ impl BenchSubCommand for App<'_> {
)
.arg(
Arg::with_name("amount")
.validator(|s| is_amount(s))
.value_parser(Amount::parse)
.value_name("TOKEN_AMOUNT")
.takes_value(true)
.index(3)
Expand Down Expand Up @@ -835,7 +835,7 @@ pub fn app<'a>(
.number_of_values(1)
.conflicts_with("transfer_fee")
.requires("transfer_fee_basis_points")
.validator(|s| is_amount(s))
.value_parser(Amount::parse)
.help(
"Add a UI amount maximum transfer fee to the mint. \
The mint authority can set and collect fees"
Expand Down Expand Up @@ -1086,7 +1086,7 @@ pub fn app<'a>(
)
.arg(
Arg::with_name("max_size")
.validator(|s| is_amount(s))
.value_parser(clap::value_parser!(u64))
.value_name("MAX_SIZE")
.takes_value(true)
.required(true)
Expand Down Expand Up @@ -1132,7 +1132,7 @@ pub fn app<'a>(
)
.arg(
Arg::with_name("new_max_size")
.validator(|s| is_amount(s))
.value_parser(clap::value_parser!(u64))
.value_name("NEW_MAX_SIZE")
.takes_value(true)
.required(true)
Expand Down Expand Up @@ -1430,8 +1430,8 @@ pub fn app<'a>(
.arg(
Arg::with_name("expected_fee")
.long("expected-fee")
.validator(|s| is_amount(s))
.value_name("TOKEN_AMOUNT")
.value_parser(Amount::parse)
.value_name("EXPECTED_FEE")
.takes_value(true)
.help("Expected fee amount collected during the transfer"),
)
Expand Down Expand Up @@ -1510,7 +1510,7 @@ pub fn app<'a>(
)
.arg(
Arg::with_name("amount")
.validator(|s| is_amount(s))
.value_parser(Amount::parse)
.value_name("TOKEN_AMOUNT")
.takes_value(true)
.index(2)
Expand Down Expand Up @@ -1620,7 +1620,7 @@ pub fn app<'a>(
.about("Wrap native SOL in a SOL token account")
.arg(
Arg::with_name("amount")
.validator(|s| is_amount(s))
.value_parser(Amount::parse)
.value_name("AMOUNT")
.takes_value(true)
.index(1)
Expand Down Expand Up @@ -1702,7 +1702,7 @@ pub fn app<'a>(
)
.arg(
Arg::with_name("amount")
.validator(|s| is_amount(s))
.value_parser(Amount::parse)
.value_name("TOKEN_AMOUNT")
.takes_value(true)
.index(2)
Expand Down Expand Up @@ -2333,8 +2333,8 @@ pub fn app<'a>(
)
.arg(
Arg::with_name("maximum_fee")
.value_name("TOKEN_AMOUNT")
.validator(|s| is_amount(s))
.value_name("MAXIMUM_FEE")
.value_parser(Amount::parse)
.takes_value(true)
.required(true)
.help("The new maximum transfer fee in UI amount"),
Expand Down
Loading

0 comments on commit a3527b7

Please sign in to comment.