token-cli: infer program_id based on cli args

This commit is contained in:
hanako mumei 2022-09-01 15:05:40 -07:00 committed by hana
parent 191977e2c6
commit d2c33e26a4
2 changed files with 32 additions and 18 deletions

View File

@ -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<Arc<RemoteWalletManager>>,
bulk_signers: &mut Vec<Arc<dyn Signer>>,
multisigner_ids: &'a mut Vec<Pubkey>,
) -> 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<Arc<RemoteWalletManager>>,
bulk_signers: &mut Vec<Arc<dyn Signer>>,
multisigner_ids: &'a mut Vec<Pubkey>,
rpc_client: Arc<RpcClient>,
program_client: Arc<dyn ProgramClient<ProgramRpcClientSendTransaction>>,
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,

View File

@ -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
}