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

Commit 8166a1d

Browse files
[transfer-hook] Remove clap deprecated functions (#6952)
1 parent 8de9536 commit 8166a1d

File tree

1 file changed

+60
-78
lines changed

1 file changed

+60
-78
lines changed

token/transfer-hook/cli/src/main.rs

Lines changed: 60 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ pub mod meta;
22

33
use {
44
crate::meta::parse_transfer_hook_account_arg,
5-
clap::{crate_description, crate_name, crate_version, Arg, Command},
5+
clap::{crate_description, crate_name, crate_version, Arg, ArgAction, Command},
66
solana_clap_v3_utils::{
77
input_parsers::{
88
parse_url_or_moniker,
99
signer::{SignerSource, SignerSourceParserBuilder},
1010
},
1111
input_validators::normalize_to_url_if_moniker,
12-
keypair::{pubkey_from_source, DefaultSigner},
12+
keypair::signer_from_path,
1313
},
1414
solana_client::nonblocking::rpc_client::RpcClient,
1515
solana_remote_wallet::remote_wallet::RemoteWalletManager,
@@ -233,7 +233,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
233233
Command::new("create-extra-metas")
234234
.about("Create the extra account metas account for a transfer hook program")
235235
.arg(
236-
Arg::with_name("program_id")
236+
Arg::new("program_id")
237237
.value_parser(SignerSourceParserBuilder::default().allow_pubkey().allow_file_path().build())
238238
.value_name("TRANSFER_HOOK_PROGRAM")
239239
.takes_value(true)
@@ -242,7 +242,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
242242
.help("The transfer hook program id"),
243243
)
244244
.arg(
245-
Arg::with_name("token")
245+
Arg::new("token")
246246
.value_parser(SignerSourceParserBuilder::default().allow_pubkey().allow_file_path().build())
247247
.value_name("TOKEN_MINT_ADDRESS")
248248
.takes_value(true)
@@ -251,11 +251,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
251251
.help("The token mint address for the transfer hook"),
252252
)
253253
.arg(
254-
Arg::with_name("transfer_hook_accounts")
254+
Arg::new("transfer_hook_accounts")
255255
.value_parser(parse_transfer_hook_account_arg)
256256
.value_name("TRANSFER_HOOK_ACCOUNTS")
257257
.takes_value(true)
258-
.multiple(true)
258+
.action(ArgAction::Append)
259259
.min_values(0)
260260
.index(3)
261261
.help(r#"Additional account(s) required for a transfer hook and their respective configurations, whether they are a fixed address or PDA.
@@ -317,7 +317,7 @@ extraMetas:
317317
Command::new("update-extra-metas")
318318
.about("Update the extra account metas account for a transfer hook program")
319319
.arg(
320-
Arg::with_name("program_id")
320+
Arg::new("program_id")
321321
.value_parser(SignerSourceParserBuilder::default().allow_pubkey().allow_file_path().build())
322322
.value_name("TRANSFER_HOOK_PROGRAM")
323323
.takes_value(true)
@@ -326,7 +326,7 @@ extraMetas:
326326
.help("The transfer hook program id"),
327327
)
328328
.arg(
329-
Arg::with_name("token")
329+
Arg::new("token")
330330
.value_parser(SignerSourceParserBuilder::default().allow_pubkey().allow_file_path().build())
331331
.value_name("TOKEN_MINT_ADDRESS")
332332
.takes_value(true)
@@ -335,11 +335,11 @@ extraMetas:
335335
.help("The token mint address for the transfer hook"),
336336
)
337337
.arg(
338-
Arg::with_name("transfer_hook_accounts")
338+
Arg::new("transfer_hook_accounts")
339339
.value_parser(parse_transfer_hook_account_arg)
340340
.value_name("TRANSFER_HOOK_ACCOUNTS")
341341
.takes_value(true)
342-
.multiple(true)
342+
.action(ArgAction::Append)
343343
.min_values(0)
344344
.index(3)
345345
.help(r#"Additional account(s) required for a transfer hook and their respective configurations, whether they are a fixed address or PDA.
@@ -401,37 +401,37 @@ extraMetas:
401401
let (command, matches) = app_matches.subcommand().unwrap();
402402
let mut wallet_manager: Option<Rc<RemoteWalletManager>> = None;
403403

404-
let cli_config = if let Some(config_file) = matches.value_of("config_file") {
404+
let cli_config = if let Some(config_file) = matches.get_one::<String>("config_file") {
405405
solana_cli_config::Config::load(config_file).unwrap_or_default()
406406
} else {
407407
solana_cli_config::Config::default()
408408
};
409409

410410
let config = {
411-
let default_signer = DefaultSigner::new(
412-
"fee_payer",
413-
matches
414-
.value_of("fee_payer")
415-
.map(|s| s.to_string())
416-
.unwrap_or_else(|| cli_config.keypair_path.clone()),
417-
);
411+
let default_signer = if let Some((signer, _)) =
412+
SignerSource::try_get_signer(matches, "fee_payer", &mut wallet_manager)?
413+
{
414+
signer
415+
} else {
416+
signer_from_path(
417+
matches,
418+
&cli_config.keypair_path,
419+
"fee_payer",
420+
&mut wallet_manager,
421+
)?
422+
};
418423

419424
let json_rpc_url = normalize_to_url_if_moniker(
420425
matches
421-
.value_of("json_rpc_url")
426+
.get_one::<String>("json_rpc_url")
422427
.unwrap_or(&cli_config.json_rpc_url),
423428
);
424429

425430
Config {
426431
commitment_config: CommitmentConfig::confirmed(),
427-
default_signer: default_signer
428-
.signer_from_path(matches, &mut wallet_manager)
429-
.unwrap_or_else(|err| {
430-
eprintln!("error: {err}");
431-
exit(1);
432-
}),
432+
default_signer,
433433
json_rpc_url,
434-
verbose: matches.is_present("verbose"),
434+
verbose: matches.try_contains_id("verbose")?,
435435
}
436436
};
437437
solana_logger::setup_with_default("solana=info");
@@ -444,39 +444,30 @@ extraMetas:
444444

445445
match (command, matches) {
446446
("create-extra-metas", arg_matches) => {
447-
let program_id_source = arg_matches
448-
.try_get_one::<SignerSource>("program_id")?
449-
.unwrap();
450-
let program_id = pubkey_from_source(
451-
arg_matches,
452-
program_id_source,
453-
"program_id",
454-
&mut wallet_manager,
455-
)
456-
.unwrap();
457-
458-
let token_source = arg_matches.try_get_one::<SignerSource>("token")?.unwrap();
459-
let token = pubkey_from_source(arg_matches, token_source, "token", &mut wallet_manager)
460-
.unwrap();
447+
let program_id =
448+
SignerSource::try_get_pubkey(arg_matches, "program_id", &mut wallet_manager)?
449+
.unwrap();
450+
let token =
451+
SignerSource::try_get_pubkey(arg_matches, "token", &mut wallet_manager)?.unwrap();
461452

462453
let transfer_hook_accounts = arg_matches
463454
.get_many::<Vec<ExtraAccountMeta>>("transfer_hook_accounts")
464455
.unwrap_or_default()
465456
.flatten()
466457
.cloned()
467458
.collect();
468-
let mint_authority = DefaultSigner::new(
469-
"mint_authority",
470-
matches
471-
.value_of("mint_authority")
472-
.map(|s| s.to_string())
473-
.unwrap_or_else(|| cli_config.keypair_path.clone()),
474-
)
475-
.signer_from_path(matches, &mut wallet_manager)
476-
.unwrap_or_else(|err| {
477-
eprintln!("error: {err}");
478-
exit(1);
479-
});
459+
let mint_authority = if let Some((signer, _)) =
460+
SignerSource::try_get_signer(matches, "mint_authority", &mut wallet_manager)?
461+
{
462+
signer
463+
} else {
464+
signer_from_path(
465+
matches,
466+
&cli_config.keypair_path,
467+
"mint_authority",
468+
&mut wallet_manager,
469+
)?
470+
};
480471
let signature = process_create_extra_account_metas(
481472
&rpc_client,
482473
&program_id,
@@ -493,39 +484,30 @@ extraMetas:
493484
println!("Signature: {signature}");
494485
}
495486
("update-extra-metas", arg_matches) => {
496-
let program_id_source = arg_matches
497-
.try_get_one::<SignerSource>("program_id")?
498-
.unwrap();
499-
let program_id = pubkey_from_source(
500-
arg_matches,
501-
program_id_source,
502-
"program_id",
503-
&mut wallet_manager,
504-
)
505-
.unwrap();
506-
507-
let token_source = arg_matches.try_get_one::<SignerSource>("token")?.unwrap();
508-
let token = pubkey_from_source(arg_matches, token_source, "token", &mut wallet_manager)
509-
.unwrap();
487+
let program_id =
488+
SignerSource::try_get_pubkey(arg_matches, "program_id", &mut wallet_manager)?
489+
.unwrap();
490+
let token =
491+
SignerSource::try_get_pubkey(arg_matches, "token", &mut wallet_manager)?.unwrap();
510492

511493
let transfer_hook_accounts = arg_matches
512494
.get_many::<Vec<ExtraAccountMeta>>("transfer_hook_accounts")
513495
.unwrap_or_default()
514496
.flatten()
515497
.cloned()
516498
.collect();
517-
let mint_authority = DefaultSigner::new(
518-
"mint_authority",
519-
matches
520-
.value_of("mint_authority")
521-
.map(|s| s.to_string())
522-
.unwrap_or_else(|| cli_config.keypair_path.clone()),
523-
)
524-
.signer_from_path(matches, &mut wallet_manager)
525-
.unwrap_or_else(|err| {
526-
eprintln!("error: {err}");
527-
exit(1);
528-
});
499+
let mint_authority = if let Some((signer, _)) =
500+
SignerSource::try_get_signer(matches, "mint_authority", &mut wallet_manager)?
501+
{
502+
signer
503+
} else {
504+
signer_from_path(
505+
matches,
506+
&cli_config.keypair_path,
507+
"mint_authority",
508+
&mut wallet_manager,
509+
)?
510+
};
529511
let signature = process_update_extra_account_metas(
530512
&rpc_client,
531513
&program_id,

0 commit comments

Comments
 (0)