From d2c33e26a4732a058dc258581133b8fbd1743399 Mon Sep 17 00:00:00 2001 From: hanako mumei <81144685+2501babe@users.noreply.github.com> Date: Thu, 1 Sep 2022 15:05:40 -0700 Subject: [PATCH] token-cli: infer program_id based on cli args --- token/cli/src/config.rs | 36 ++++++++++++++++++++++++++++-------- token/cli/src/main.rs | 14 ++++---------- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/token/cli/src/config.rs b/token/cli/src/config.rs index d6a3edbf..5f926cae 100644 --- a/token/cli/src/config.rs +++ b/token/cli/src/config.rs @@ -1,7 +1,7 @@ use crate::{signers_of, Error, MULTISIG_SIGNER_ARG}; use clap::ArgMatches; use solana_clap_utils::{ - input_parsers::{pubkey_of, pubkey_of_signer, value_of}, + input_parsers::{pubkey_of_signer, value_of}, input_validators::normalize_to_url_if_moniker, keypair::{signer_from_path, signer_from_path_with_config, SignerFromPathConfig}, nonce::{NONCE_ARG, NONCE_AUTHORITY_ARG}, @@ -43,12 +43,12 @@ pub(crate) struct Config<'a> { } impl<'a> Config<'a> { - pub(crate) fn new( - matches: &ArgMatches, + pub(crate) async fn new( + matches: &ArgMatches<'_>, wallet_manager: &mut Option>, bulk_signers: &mut Vec>, multisigner_ids: &'a mut Vec, - ) -> Self { + ) -> Config<'a> { let cli_config = if let Some(config_file) = matches.value_of("config_file") { solana_cli_config::Config::load(config_file).unwrap_or_else(|_| { eprintln!("error: Could not find config file `{}`", config_file); @@ -89,17 +89,18 @@ impl<'a> Config<'a> { program_client, websocket_url, ) + .await } - pub(crate) fn new_with_clients_and_ws_url( - matches: &ArgMatches, + pub(crate) async fn new_with_clients_and_ws_url( + matches: &ArgMatches<'_>, wallet_manager: &mut Option>, bulk_signers: &mut Vec>, multisigner_ids: &'a mut Vec, rpc_client: Arc, program_client: Arc>, websocket_url: String, - ) -> Self { + ) -> Config<'a> { let cli_config = if let Some(config_file) = matches.value_of("config_file") { solana_cli_config::Config::load(config_file).unwrap_or_else(|_| { eprintln!("error: Could not find config file `{}`", config_file); @@ -207,7 +208,26 @@ impl<'a> Config<'a> { let sign_only = matches.is_present(SIGN_ONLY_ARG.name); let dump_transaction_message = matches.is_present(DUMP_TRANSACTION_MESSAGE.name); - let program_id = pubkey_of(matches, "program_id").unwrap(); + + let default_program_id = spl_token::id(); + let program_id = if let Some(program_id) = value_of(matches, "program_id") { + program_id + } else if !sign_only { + if let Some(address) = value_of(matches, "token") + .or_else(|| value_of(matches, "account")) + .or_else(|| value_of(matches, "address")) + { + rpc_client + .get_account(&address) + .await + .map(|account| account.owner) + .unwrap_or(default_program_id) + } else { + default_program_id + } + } else { + default_program_id + }; Self { default_signer, diff --git a/token/cli/src/main.rs b/token/cli/src/main.rs index 3c642648..3a64e7ea 100644 --- a/token/cli/src/main.rs +++ b/token/cli/src/main.rs @@ -1960,7 +1960,6 @@ fn multisig_member_help_string() -> String { fn app<'a, 'b>( default_decimals: &'a str, - default_program_id: &'a str, minimum_signers_help: &'b str, multisig_member_help: &'b str, ) -> App<'a, 'b> { @@ -2001,7 +2000,6 @@ fn app<'a, 'b>( .value_name("ADDRESS") .takes_value(true) .global(true) - .default_value(default_program_id) .validator(is_valid_pubkey) .help("SPL Token program id"), ) @@ -2842,12 +2840,10 @@ fn app<'a, 'b>( #[tokio::main] async fn main() -> Result<(), Error> { let default_decimals = format!("{}", spl_token_2022::native_mint::DECIMALS); - let default_program_id = spl_token::id().to_string(); let minimum_signers_help = minimum_signers_help_string(); let multisig_member_help = multisig_member_help_string(); let app_matches = app( &default_decimals, - &default_program_id, &minimum_signers_help, &multisig_member_help, ) @@ -2866,7 +2862,8 @@ async fn main() -> Result<(), Error> { &mut wallet_manager, &mut bulk_signers, &mut multisigner_ids, - ); + ) + .await; solana_logger::setup_with_default("solana=info"); let result = @@ -3677,13 +3674,11 @@ mod tests { args: &[&str], ) -> CommandResult { let default_decimals = format!("{}", spl_token_2022::native_mint::DECIMALS); - let default_program_id = spl_token::id().to_string(); let minimum_signers_help = minimum_signers_help_string(); let multisig_member_help = multisig_member_help_string(); let app_matches = app( &default_decimals, - &default_program_id, &minimum_signers_help, &multisig_member_help, ) @@ -3699,13 +3694,11 @@ mod tests { async fn exec_test_cmd(config: &Config<'_>, args: &[&str]) -> CommandResult { let default_decimals = format!("{}", spl_token_2022::native_mint::DECIMALS); - let default_program_id = spl_token::id().to_string(); let minimum_signers_help = minimum_signers_help_string(); let multisig_member_help = multisig_member_help_string(); let app_matches = app( &default_decimals, - &default_program_id, &minimum_signers_help, &multisig_member_help, ) @@ -3726,7 +3719,8 @@ mod tests { config.rpc_client.clone(), config.program_client.clone(), config.websocket_url.clone(), - ); + ) + .await; process_command(&sub_command, matches, &config, wallet_manager, bulk_signers).await }