Skip to content

Commit

Permalink
replace value_of function
Browse files Browse the repository at this point in the history
  • Loading branch information
samkim-crypto committed Sep 10, 2024
1 parent f040679 commit f420049
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion token/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ serde = "1.0.209"
serde_derive = "1.0.103"
serde_json = "1.0.128"
solana-account-decoder = "2.0.3"
solana-clap-utils = "2.0.3"
solana-clap-v3-utils = "2.0.3"
solana-cli-config = "2.0.3"
solana-cli-output = "2.0.3"
solana-client = "2.0.3"
Expand Down
7 changes: 4 additions & 3 deletions token/cli/src/clap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ pub fn app<'a>(
.number_of_values(1)
.conflicts_with("transfer_fee")
.requires("transfer_fee_maximum_fee")
.validator(is_parsable::<u16>)
.value_parser(clap::value_parser!(u16))
.help(
"Add transfer fee to the mint. \
The mint authority can set the fee.",
Expand All @@ -839,7 +839,7 @@ pub fn app<'a>(
.number_of_values(1)
.conflicts_with("transfer_fee")
.requires("transfer_fee_basis_points")
.validator(is_amount)
.value_parser(clap::value_parser!(f64))
.help(
"Add a UI amount maximum transfer fee to the mint. \
The mint authority can set and collect fees"
Expand Down Expand Up @@ -896,6 +896,7 @@ pub fn app<'a>(
.takes_value(false)
.help("Enables group member configurations in the mint. The mint authority must initialize the member."),
)
.arg(multisig_signer_arg())
.nonce_args(true)
.arg(memo_arg())
)
Expand Down Expand Up @@ -1433,7 +1434,7 @@ pub fn app<'a>(
.arg(
Arg::with_name("expected_fee")
.long("expected-fee")
.validator(|s| is_amount(s))
.value_parser(clap::value_parser!(f64))
.value_name("TOKEN_AMOUNT")
.takes_value(true)
.help("Expected fee amount collected during the transfer"),
Expand Down
48 changes: 33 additions & 15 deletions token/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use {
UiAccountData,
},
solana_clap_v3_utils::{
input_parsers::{pubkey_of_signer, pubkeys_of_multiple_signers, value_of},
input_parsers::{pubkey_of_signer, pubkeys_of_multiple_signers},
keypair::signer_from_path,
},
solana_cli_output::{
Expand Down Expand Up @@ -3533,11 +3533,12 @@ pub async fn process_command<'a>(
)
});

let tranfer_fee_basis_point = value_of::<u16>(arg_matches, "transfer_fee_basis_points");
let transfer_fee_maximum_fee = value_of::<f64>(arg_matches, "transfer_fee_maximum_fee")
.map(|v| spl_token::ui_amount_to_amount(v, decimals));
let transfer_fee = tranfer_fee_basis_point
.map(|v| (v, transfer_fee_maximum_fee.unwrap()))
let transfer_fee_basis_point = arg_matches.get_one::<u16>("transfer_fee_basis_points");
let transfer_fee_maximum_fee = arg_matches
.get_one::<f64>("transfer_fee_maximum_fee")
.map(|v| spl_token::ui_amount_to_amount(*v, decimals));
let transfer_fee = transfer_fee_basis_point
.map(|v| (*v, transfer_fee_maximum_fee.unwrap()))
.or(transfer_fee);

let (token_signer, token) =
Expand Down Expand Up @@ -3660,7 +3661,9 @@ pub async fn process_command<'a>(
_ => Field::Key(field.to_string()),
};
let value = arg_matches.value_of("value").map(|v| v.to_string());
let transfer_lamports = value_of::<u64>(arg_matches, TRANSFER_LAMPORTS_ARG.name);
let transfer_lamports = arg_matches
.get_one(TRANSFER_LAMPORTS_ARG.name)
.map(|v: &String| v.parse::<u64>().unwrap());
let bulk_signers = vec![authority_signer];

command_update_metadata(
Expand Down Expand Up @@ -3766,7 +3769,10 @@ pub async fn process_command<'a>(
.await
}
(CommandName::CreateMultisig, arg_matches) => {
let minimum_signers = value_of::<u8>(arg_matches, "minimum_signers").unwrap();
let minimum_signers = arg_matches
.get_one("minimum_signers")
.map(|v: &String| v.parse::<u8>().unwrap())
.unwrap();
let multisig_members =
pubkeys_of_multiple_signers(arg_matches, "multisig_member", &mut wallet_manager)
.unwrap_or_else(print_error_and_exit)
Expand Down Expand Up @@ -3850,7 +3856,9 @@ pub async fn process_command<'a>(
push_signer_with_dedup(owner_signer, &mut bulk_signers);
}

let mint_decimals = value_of::<u8>(arg_matches, MINT_DECIMALS_ARG.name);
let mint_decimals = arg_matches
.get_one(MINT_DECIMALS_ARG.name)
.map(|v: &String| v.parse::<u8>().unwrap());
let fund_recipient = arg_matches.is_present("fund_recipient");
let allow_unfunded_recipient = arg_matches.is_present("allow_empty_recipient")
|| arg_matches.is_present("allow_unfunded_recipient");
Expand All @@ -3862,7 +3870,7 @@ pub async fn process_command<'a>(
println_display(config, "recipient-is-ata-owner is now the default behavior. The option has been deprecated and will be removed in a future release.".to_string());
}
let use_unchecked_instruction = arg_matches.is_present("use_unchecked_instruction");
let expected_fee = value_of::<f64>(arg_matches, "expected_fee");
let expected_fee = arg_matches.get_one::<f64>("expected_fee").copied();
let memo = value_t!(arg_matches, "memo", String).ok();
let transfer_hook_accounts = arg_matches.values_of("transfer_hook_account").map(|v| {
v.into_iter()
Expand Down Expand Up @@ -3906,7 +3914,9 @@ pub async fn process_command<'a>(
let amount = parse_amount_or_all(arg_matches);
let mint_address =
pubkey_of_signer(arg_matches, MINT_ADDRESS_ARG.name, &mut wallet_manager).unwrap();
let mint_decimals = value_of::<u8>(arg_matches, MINT_DECIMALS_ARG.name);
let mint_decimals = arg_matches
.get_one(MINT_DECIMALS_ARG.name)
.map(|v: &String| v.parse::<u8>().unwrap());
let use_unchecked_instruction = arg_matches.is_present("use_unchecked_instruction");
let memo = value_t!(arg_matches, "memo", String).ok();
command_burn(
Expand All @@ -3933,7 +3943,9 @@ pub async fn process_command<'a>(
.unwrap()
.unwrap();
let amount = value_t_or_exit!(arg_matches, "amount", f64);
let mint_decimals = value_of::<u8>(arg_matches, MINT_DECIMALS_ARG.name);
let mint_decimals = arg_matches
.get_one(MINT_DECIMALS_ARG.name)
.map(|v: &String| v.parse::<u8>().unwrap());
let mint_info = config.get_mint_info(&token, mint_decimals).await?;
let recipient = if let Some(address) =
pubkey_of_signer(arg_matches, "recipient", &mut wallet_manager).unwrap()
Expand Down Expand Up @@ -4058,7 +4070,9 @@ pub async fn process_command<'a>(
.unwrap();
let mint_address =
pubkey_of_signer(arg_matches, MINT_ADDRESS_ARG.name, &mut wallet_manager).unwrap();
let mint_decimals = value_of::<u8>(arg_matches, MINT_DECIMALS_ARG.name);
let mint_decimals = arg_matches
.get_one(MINT_DECIMALS_ARG.name)
.map(|v: &String| v.parse::<u8>().unwrap());
let use_unchecked_instruction = arg_matches.is_present("use_unchecked_instruction");
command_approve(
config,
Expand Down Expand Up @@ -4395,7 +4409,9 @@ pub async fn process_command<'a>(
let maximum_fee = value_t_or_exit!(arg_matches, "maximum_fee", f64);
let (transfer_fee_authority_signer, transfer_fee_authority_pubkey) = config
.signer_or_default(arg_matches, "transfer_fee_authority", &mut wallet_manager);
let mint_decimals = value_of::<u8>(arg_matches, MINT_DECIMALS_ARG.name);
let mint_decimals = arg_matches
.get_one(MINT_DECIMALS_ARG.name)
.map(|v: &String| v.parse::<u8>().unwrap());
let bulk_signers = vec![transfer_fee_authority_signer];

command_set_transfer_fee(
Expand Down Expand Up @@ -4541,7 +4557,9 @@ pub async fn process_command<'a>(
let (owner_signer, owner) =
config.signer_or_default(arg_matches, "owner", &mut wallet_manager);

let mint_decimals = value_of::<u8>(arg_matches, MINT_DECIMALS_ARG.name);
let mint_decimals = arg_matches
.get_one(MINT_DECIMALS_ARG.name)
.map(|v: &String| v.parse::<u8>().unwrap());

let (instruction_type, elgamal_keypair, aes_key) = match c {
CommandName::DepositConfidentialTokens => {
Expand Down
9 changes: 6 additions & 3 deletions token/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use {
crate::clap_app::{Error, COMPUTE_UNIT_LIMIT_ARG, COMPUTE_UNIT_PRICE_ARG, MULTISIG_SIGNER_ARG},
clap::ArgMatches,
solana_clap_v3_utils::{
input_parsers::{pubkey_of_signer, value_of},
input_parsers::pubkey_of_signer,
input_validators::normalize_to_url_if_moniker,
keypair::SignerFromPathConfig,
nonce::{NONCE_ARG, NONCE_AUTHORITY_ARG},
Expand Down Expand Up @@ -103,7 +103,10 @@ impl<'a> Config<'a> {
));
let sign_only = matches.is_present(SIGN_ONLY_ARG.name);
let program_client: Arc<dyn ProgramClient<ProgramRpcClientSendTransaction>> = if sign_only {
let blockhash = value_of(matches, BLOCKHASH_ARG.name).unwrap_or_default();
let blockhash = matches
.get_one::<Hash>(BLOCKHASH_ARG.name)
.copied()
.unwrap_or_default();
Arc::new(ProgramOfflineClient::new(
blockhash,
ProgramRpcClientSendTransaction,
Expand Down Expand Up @@ -175,7 +178,7 @@ impl<'a> Config<'a> {
let default_keypair = cli_config.keypair_path.clone();

let default_signer: Option<Arc<dyn Signer>> = {
if let Some(owner_path) = matches.value_of("owner") {
if let Some(owner_path) = matches.try_get_one::<String>("owner").ok().flatten() {
signer_from_path_with_config(matches, owner_path, "owner", wallet_manager, &config)
.ok()
} else {
Expand Down
3 changes: 2 additions & 1 deletion token/cli/tests/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,10 @@ async fn create_token_2022(test_validator: &TestValidator, payer: &Keypair) {
&multisig_member_help,
)
.get_matches_from(args);
let (_, matches) = app_matches.subcommand().unwrap();

let config = Config::new_with_clients_and_ws_url(
&app_matches,
matches,
&mut wallet_manager,
&mut bulk_signers,
&mut multisigner_ids,
Expand Down

0 comments on commit f420049

Please sign in to comment.