Skip to content

Commit

Permalink
[token-cli] Remove is_amount_or_all, is_amount, and is_parsable
Browse files Browse the repository at this point in the history
… validators (#7448)

* parse instead of validate for `decimals`

* replace `is_amount_or_all` with parser

* replace `is_amount` with parser

* repalce `is_parsable` with parser

* remove unnecessary imports
  • Loading branch information
samkim-crypto authored Nov 8, 2024
1 parent 55f3d4d commit 9dc55e5
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 201 deletions.
30 changes: 20 additions & 10 deletions token/cli/src/bench.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/// The `bench` subcommand
use {
crate::{clap_app::Error, command::CommandResult, config::Config},
clap::{value_t_or_exit, ArgMatches},
solana_clap_v3_utils::input_parsers::pubkey_of_signer,
clap::ArgMatches,
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 @@ -34,7 +34,7 @@ pub(crate) async fn bench_process_command(
let token = pubkey_of_signer(arg_matches, "token", wallet_manager)
.unwrap()
.unwrap();
let n = value_t_or_exit!(arg_matches, "n", usize);
let n = *arg_matches.get_one::<usize>("n").unwrap();

let (owner_signer, owner) =
config.signer_or_default(arg_matches, "owner", wallet_manager);
Expand All @@ -46,7 +46,7 @@ pub(crate) async fn bench_process_command(
let token = pubkey_of_signer(arg_matches, "token", wallet_manager)
.unwrap()
.unwrap();
let n = value_t_or_exit!(arg_matches, "n", usize);
let n = *arg_matches.get_one::<usize>("n").unwrap();
let (owner_signer, owner) =
config.signer_or_default(arg_matches, "owner", wallet_manager);
signers.push(owner_signer);
Expand All @@ -57,8 +57,8 @@ pub(crate) async fn bench_process_command(
let token = pubkey_of_signer(arg_matches, "token", wallet_manager)
.unwrap()
.unwrap();
let n = value_t_or_exit!(arg_matches, "n", usize);
let ui_amount = value_t_or_exit!(arg_matches, "amount", f64);
let n = *arg_matches.get_one::<usize>("n").unwrap();
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 @@ -72,8 +72,8 @@ pub(crate) async fn bench_process_command(
let token = pubkey_of_signer(arg_matches, "token", wallet_manager)
.unwrap()
.unwrap();
let n = value_t_or_exit!(arg_matches, "n", usize);
let ui_amount = value_t_or_exit!(arg_matches, "amount", f64);
let n = *arg_matches.get_one::<usize>("n").unwrap();
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
60 changes: 27 additions & 33 deletions token/cli/src/clap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ use {
},
solana_clap_v3_utils::{
fee_payer::fee_payer_arg,
input_validators::{
is_amount, is_amount_or_all, is_parsable, is_pubkey, is_url_or_moniker,
is_valid_pubkey, is_valid_signer,
},
input_parsers::Amount,
input_validators::{is_pubkey, is_url_or_moniker, is_valid_pubkey, is_valid_signer},
memo::memo_arg,
nonce::*,
offline::{self, *},
Expand Down Expand Up @@ -306,16 +304,12 @@ pub fn mint_address_arg<'a>() -> Arg<'a> {
.help(MINT_ADDRESS_ARG.help)
}

fn is_mint_decimals(string: &str) -> Result<(), String> {
is_parsable::<u8>(string)
}

pub fn mint_decimals_arg<'a>() -> Arg<'a> {
Arg::with_name(MINT_DECIMALS_ARG.name)
.long(MINT_DECIMALS_ARG.long)
.takes_value(true)
.value_name("MINT_DECIMALS")
.validator(is_mint_decimals)
.value_parser(clap::value_parser!(u8))
.help(MINT_DECIMALS_ARG.help)
}

Expand Down Expand Up @@ -344,7 +338,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 @@ -477,7 +471,7 @@ impl BenchSubCommand for App<'_> {
)
.arg(
Arg::with_name("n")
.validator(is_parsable::<usize>)
.value_parser(clap::value_parser!(usize))
.value_name("N")
.takes_value(true)
.index(2)
Expand All @@ -500,7 +494,7 @@ impl BenchSubCommand for App<'_> {
)
.arg(
Arg::with_name("n")
.validator(is_parsable::<usize>)
.value_parser(clap::value_parser!(usize))
.value_name("N")
.takes_value(true)
.index(2)
Expand All @@ -523,7 +517,7 @@ impl BenchSubCommand for App<'_> {
)
.arg(
Arg::with_name("n")
.validator(is_parsable::<usize>)
.value_parser(clap::value_parser!(usize))
.value_name("N")
.takes_value(true)
.index(2)
Expand All @@ -532,7 +526,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 @@ -563,7 +557,7 @@ impl BenchSubCommand for App<'_> {
)
.arg(
Arg::with_name("n")
.validator(is_parsable::<usize>)
.value_parser(clap::value_parser!(usize))
.value_name("N")
.takes_value(true)
.index(2)
Expand All @@ -572,7 +566,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 @@ -676,7 +670,7 @@ pub fn app<'a>(
.takes_value(true)
.global(true)
.value_name("COMPUTE-UNIT-LIMIT")
.validator(is_parsable::<u32>)
.value_parser(clap::value_parser!(u32))
.help(COMPUTE_UNIT_LIMIT_ARG.help)
)
.arg(
Expand All @@ -685,7 +679,7 @@ pub fn app<'a>(
.takes_value(true)
.global(true)
.value_name("COMPUTE-UNIT-PRICE")
.validator(is_parsable::<u64>)
.value_parser(clap::value_parser!(u64))
.help(COMPUTE_UNIT_PRICE_ARG.help)
)
.bench_subcommand()
Expand Down Expand Up @@ -717,7 +711,7 @@ pub fn app<'a>(
.arg(
Arg::with_name("decimals")
.long("decimals")
.validator(is_mint_decimals)
.value_parser(clap::value_parser!(u8))
.value_name("DECIMALS")
.takes_value(true)
.default_value(default_decimals)
Expand Down Expand Up @@ -839,7 +833,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 @@ -1090,7 +1084,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 @@ -1136,7 +1130,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 @@ -1350,7 +1344,7 @@ pub fn app<'a>(
)
.arg(
Arg::with_name("amount")
.validator(|s| is_amount_or_all(s))
.value_parser(Amount::parse)
.value_name("TOKEN_AMOUNT")
.takes_value(true)
.index(2)
Expand Down Expand Up @@ -1434,8 +1428,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 @@ -1480,7 +1474,7 @@ pub fn app<'a>(
)
.arg(
Arg::with_name("amount")
.validator(|s| is_amount_or_all(s))
.value_parser(Amount::parse)
.value_name("TOKEN_AMOUNT")
.takes_value(true)
.index(2)
Expand Down Expand Up @@ -1514,7 +1508,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 @@ -1624,7 +1618,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 @@ -1706,7 +1700,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 @@ -2337,8 +2331,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 Expand Up @@ -2606,7 +2600,7 @@ pub fn app<'a>(
)
.arg(
Arg::with_name("amount")
.validator(|s| is_amount_or_all(s))
.value_parser(Amount::parse)
.value_name("TOKEN_AMOUNT")
.takes_value(true)
.index(2)
Expand Down Expand Up @@ -2643,7 +2637,7 @@ pub fn app<'a>(
)
.arg(
Arg::with_name("amount")
.validator(|s| is_amount_or_all(s))
.value_parser(Amount::parse)
.value_name("TOKEN_AMOUNT")
.takes_value(true)
.index(2)
Expand Down
Loading

0 comments on commit 9dc55e5

Please sign in to comment.