Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 9dc55e5

Browse files
[token-cli] Remove is_amount_or_all, is_amount, and is_parsable 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
1 parent 55f3d4d commit 9dc55e5

File tree

4 files changed

+222
-201
lines changed

4 files changed

+222
-201
lines changed

token/cli/src/bench.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/// The `bench` subcommand
22
use {
33
crate::{clap_app::Error, command::CommandResult, config::Config},
4-
clap::{value_t_or_exit, ArgMatches},
5-
solana_clap_v3_utils::input_parsers::pubkey_of_signer,
4+
clap::ArgMatches,
5+
solana_clap_v3_utils::input_parsers::{pubkey_of_signer, Amount},
66
solana_client::{
77
nonblocking::rpc_client::RpcClient, rpc_client::RpcClient as BlockingRpcClient,
88
tpu_client::TpuClient, tpu_client::TpuClientConfig,
@@ -34,7 +34,7 @@ pub(crate) async fn bench_process_command(
3434
let token = pubkey_of_signer(arg_matches, "token", wallet_manager)
3535
.unwrap()
3636
.unwrap();
37-
let n = value_t_or_exit!(arg_matches, "n", usize);
37+
let n = *arg_matches.get_one::<usize>("n").unwrap();
3838

3939
let (owner_signer, owner) =
4040
config.signer_or_default(arg_matches, "owner", wallet_manager);
@@ -46,7 +46,7 @@ pub(crate) async fn bench_process_command(
4646
let token = pubkey_of_signer(arg_matches, "token", wallet_manager)
4747
.unwrap()
4848
.unwrap();
49-
let n = value_t_or_exit!(arg_matches, "n", usize);
49+
let n = *arg_matches.get_one::<usize>("n").unwrap();
5050
let (owner_signer, owner) =
5151
config.signer_or_default(arg_matches, "owner", wallet_manager);
5252
signers.push(owner_signer);
@@ -57,8 +57,8 @@ pub(crate) async fn bench_process_command(
5757
let token = pubkey_of_signer(arg_matches, "token", wallet_manager)
5858
.unwrap()
5959
.unwrap();
60-
let n = value_t_or_exit!(arg_matches, "n", usize);
61-
let ui_amount = value_t_or_exit!(arg_matches, "amount", f64);
60+
let n = *arg_matches.get_one::<usize>("n").unwrap();
61+
let ui_amount = *arg_matches.get_one::<Amount>("amount").unwrap();
6262
let (owner_signer, owner) =
6363
config.signer_or_default(arg_matches, "owner", wallet_manager);
6464
signers.push(owner_signer);
@@ -72,8 +72,8 @@ pub(crate) async fn bench_process_command(
7272
let token = pubkey_of_signer(arg_matches, "token", wallet_manager)
7373
.unwrap()
7474
.unwrap();
75-
let n = value_t_or_exit!(arg_matches, "n", usize);
76-
let ui_amount = value_t_or_exit!(arg_matches, "amount", f64);
75+
let n = *arg_matches.get_one::<usize>("n").unwrap();
76+
let ui_amount = *arg_matches.get_one::<Amount>("amount").unwrap();
7777
let (owner_signer, owner) =
7878
config.signer_or_default(arg_matches, "owner", wallet_manager);
7979
signers.push(owner_signer);
@@ -237,7 +237,7 @@ async fn command_deposit_into_or_withdraw_from(
237237
token: &Pubkey,
238238
n: usize,
239239
owner: &Pubkey,
240-
ui_amount: f64,
240+
ui_amount: Amount,
241241
from_or_to: Option<Pubkey>,
242242
deposit_into: bool,
243243
) -> Result<(), Error> {
@@ -250,7 +250,17 @@ async fn command_deposit_into_or_withdraw_from(
250250
let from_or_to = from_or_to
251251
.unwrap_or_else(|| get_associated_token_address_with_program_id(owner, token, &program_id));
252252
config.check_account(&from_or_to, Some(*token)).await?;
253-
let amount = spl_token::ui_amount_to_amount(ui_amount, mint_info.decimals);
253+
let amount = match ui_amount {
254+
Amount::Raw(ui_amount) => ui_amount,
255+
Amount::Decimal(ui_amount) => spl_token::ui_amount_to_amount(ui_amount, mint_info.decimals),
256+
Amount::All => {
257+
return Err(
258+
"Use of ALL keyword currently not supported for the bench command"
259+
.to_string()
260+
.into(),
261+
);
262+
}
263+
};
254264

255265
let token_addresses_with_seed = get_token_addresses_with_seed(&program_id, token, owner, n);
256266
let mut messages = vec![];

token/cli/src/clap_app.rs

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ use {
66
},
77
solana_clap_v3_utils::{
88
fee_payer::fee_payer_arg,
9-
input_validators::{
10-
is_amount, is_amount_or_all, is_parsable, is_pubkey, is_url_or_moniker,
11-
is_valid_pubkey, is_valid_signer,
12-
},
9+
input_parsers::Amount,
10+
input_validators::{is_pubkey, is_url_or_moniker, is_valid_pubkey, is_valid_signer},
1311
memo::memo_arg,
1412
nonce::*,
1513
offline::{self, *},
@@ -306,16 +304,12 @@ pub fn mint_address_arg<'a>() -> Arg<'a> {
306304
.help(MINT_ADDRESS_ARG.help)
307305
}
308306

309-
fn is_mint_decimals(string: &str) -> Result<(), String> {
310-
is_parsable::<u8>(string)
311-
}
312-
313307
pub fn mint_decimals_arg<'a>() -> Arg<'a> {
314308
Arg::with_name(MINT_DECIMALS_ARG.name)
315309
.long(MINT_DECIMALS_ARG.long)
316310
.takes_value(true)
317311
.value_name("MINT_DECIMALS")
318-
.validator(is_mint_decimals)
312+
.value_parser(clap::value_parser!(u8))
319313
.help(MINT_DECIMALS_ARG.help)
320314
}
321315

@@ -344,7 +338,7 @@ pub fn transfer_lamports_arg<'a>() -> Arg<'a> {
344338
.long(TRANSFER_LAMPORTS_ARG.long)
345339
.takes_value(true)
346340
.value_name("LAMPORTS")
347-
.validator(|s| is_amount(s))
341+
.value_parser(clap::value_parser!(u64))
348342
.help(TRANSFER_LAMPORTS_ARG.help)
349343
}
350344

@@ -477,7 +471,7 @@ impl BenchSubCommand for App<'_> {
477471
)
478472
.arg(
479473
Arg::with_name("n")
480-
.validator(is_parsable::<usize>)
474+
.value_parser(clap::value_parser!(usize))
481475
.value_name("N")
482476
.takes_value(true)
483477
.index(2)
@@ -500,7 +494,7 @@ impl BenchSubCommand for App<'_> {
500494
)
501495
.arg(
502496
Arg::with_name("n")
503-
.validator(is_parsable::<usize>)
497+
.value_parser(clap::value_parser!(usize))
504498
.value_name("N")
505499
.takes_value(true)
506500
.index(2)
@@ -523,7 +517,7 @@ impl BenchSubCommand for App<'_> {
523517
)
524518
.arg(
525519
Arg::with_name("n")
526-
.validator(is_parsable::<usize>)
520+
.value_parser(clap::value_parser!(usize))
527521
.value_name("N")
528522
.takes_value(true)
529523
.index(2)
@@ -532,7 +526,7 @@ impl BenchSubCommand for App<'_> {
532526
)
533527
.arg(
534528
Arg::with_name("amount")
535-
.validator(|s| is_amount(s))
529+
.value_parser(Amount::parse)
536530
.value_name("TOKEN_AMOUNT")
537531
.takes_value(true)
538532
.index(3)
@@ -563,7 +557,7 @@ impl BenchSubCommand for App<'_> {
563557
)
564558
.arg(
565559
Arg::with_name("n")
566-
.validator(is_parsable::<usize>)
560+
.value_parser(clap::value_parser!(usize))
567561
.value_name("N")
568562
.takes_value(true)
569563
.index(2)
@@ -572,7 +566,7 @@ impl BenchSubCommand for App<'_> {
572566
)
573567
.arg(
574568
Arg::with_name("amount")
575-
.validator(|s| is_amount(s))
569+
.value_parser(Amount::parse)
576570
.value_name("TOKEN_AMOUNT")
577571
.takes_value(true)
578572
.index(3)
@@ -676,7 +670,7 @@ pub fn app<'a>(
676670
.takes_value(true)
677671
.global(true)
678672
.value_name("COMPUTE-UNIT-LIMIT")
679-
.validator(is_parsable::<u32>)
673+
.value_parser(clap::value_parser!(u32))
680674
.help(COMPUTE_UNIT_LIMIT_ARG.help)
681675
)
682676
.arg(
@@ -685,7 +679,7 @@ pub fn app<'a>(
685679
.takes_value(true)
686680
.global(true)
687681
.value_name("COMPUTE-UNIT-PRICE")
688-
.validator(is_parsable::<u64>)
682+
.value_parser(clap::value_parser!(u64))
689683
.help(COMPUTE_UNIT_PRICE_ARG.help)
690684
)
691685
.bench_subcommand()
@@ -717,7 +711,7 @@ pub fn app<'a>(
717711
.arg(
718712
Arg::with_name("decimals")
719713
.long("decimals")
720-
.validator(is_mint_decimals)
714+
.value_parser(clap::value_parser!(u8))
721715
.value_name("DECIMALS")
722716
.takes_value(true)
723717
.default_value(default_decimals)
@@ -839,7 +833,7 @@ pub fn app<'a>(
839833
.number_of_values(1)
840834
.conflicts_with("transfer_fee")
841835
.requires("transfer_fee_basis_points")
842-
.validator(|s| is_amount(s))
836+
.value_parser(Amount::parse)
843837
.help(
844838
"Add a UI amount maximum transfer fee to the mint. \
845839
The mint authority can set and collect fees"
@@ -1090,7 +1084,7 @@ pub fn app<'a>(
10901084
)
10911085
.arg(
10921086
Arg::with_name("max_size")
1093-
.validator(|s| is_amount(s))
1087+
.value_parser(clap::value_parser!(u64))
10941088
.value_name("MAX_SIZE")
10951089
.takes_value(true)
10961090
.required(true)
@@ -1136,7 +1130,7 @@ pub fn app<'a>(
11361130
)
11371131
.arg(
11381132
Arg::with_name("new_max_size")
1139-
.validator(|s| is_amount(s))
1133+
.value_parser(clap::value_parser!(u64))
11401134
.value_name("NEW_MAX_SIZE")
11411135
.takes_value(true)
11421136
.required(true)
@@ -1350,7 +1344,7 @@ pub fn app<'a>(
13501344
)
13511345
.arg(
13521346
Arg::with_name("amount")
1353-
.validator(|s| is_amount_or_all(s))
1347+
.value_parser(Amount::parse)
13541348
.value_name("TOKEN_AMOUNT")
13551349
.takes_value(true)
13561350
.index(2)
@@ -1434,8 +1428,8 @@ pub fn app<'a>(
14341428
.arg(
14351429
Arg::with_name("expected_fee")
14361430
.long("expected-fee")
1437-
.validator(|s| is_amount(s))
1438-
.value_name("TOKEN_AMOUNT")
1431+
.value_parser(Amount::parse)
1432+
.value_name("EXPECTED_FEE")
14391433
.takes_value(true)
14401434
.help("Expected fee amount collected during the transfer"),
14411435
)
@@ -1480,7 +1474,7 @@ pub fn app<'a>(
14801474
)
14811475
.arg(
14821476
Arg::with_name("amount")
1483-
.validator(|s| is_amount_or_all(s))
1477+
.value_parser(Amount::parse)
14841478
.value_name("TOKEN_AMOUNT")
14851479
.takes_value(true)
14861480
.index(2)
@@ -1514,7 +1508,7 @@ pub fn app<'a>(
15141508
)
15151509
.arg(
15161510
Arg::with_name("amount")
1517-
.validator(|s| is_amount(s))
1511+
.value_parser(Amount::parse)
15181512
.value_name("TOKEN_AMOUNT")
15191513
.takes_value(true)
15201514
.index(2)
@@ -1624,7 +1618,7 @@ pub fn app<'a>(
16241618
.about("Wrap native SOL in a SOL token account")
16251619
.arg(
16261620
Arg::with_name("amount")
1627-
.validator(|s| is_amount(s))
1621+
.value_parser(Amount::parse)
16281622
.value_name("AMOUNT")
16291623
.takes_value(true)
16301624
.index(1)
@@ -1706,7 +1700,7 @@ pub fn app<'a>(
17061700
)
17071701
.arg(
17081702
Arg::with_name("amount")
1709-
.validator(|s| is_amount(s))
1703+
.value_parser(Amount::parse)
17101704
.value_name("TOKEN_AMOUNT")
17111705
.takes_value(true)
17121706
.index(2)
@@ -2337,8 +2331,8 @@ pub fn app<'a>(
23372331
)
23382332
.arg(
23392333
Arg::with_name("maximum_fee")
2340-
.value_name("TOKEN_AMOUNT")
2341-
.validator(|s| is_amount(s))
2334+
.value_name("MAXIMUM_FEE")
2335+
.value_parser(Amount::parse)
23422336
.takes_value(true)
23432337
.required(true)
23442338
.help("The new maximum transfer fee in UI amount"),
@@ -2606,7 +2600,7 @@ pub fn app<'a>(
26062600
)
26072601
.arg(
26082602
Arg::with_name("amount")
2609-
.validator(|s| is_amount_or_all(s))
2603+
.value_parser(Amount::parse)
26102604
.value_name("TOKEN_AMOUNT")
26112605
.takes_value(true)
26122606
.index(2)
@@ -2643,7 +2637,7 @@ pub fn app<'a>(
26432637
)
26442638
.arg(
26452639
Arg::with_name("amount")
2646-
.validator(|s| is_amount_or_all(s))
2640+
.value_parser(Amount::parse)
26472641
.value_name("TOKEN_AMOUNT")
26482642
.takes_value(true)
26492643
.index(2)

0 commit comments

Comments
 (0)