Skip to content

Commit

Permalink
update Config::pubkey_or_default
Browse files Browse the repository at this point in the history
  • Loading branch information
samkim-crypto committed Nov 21, 2024
1 parent 0f54203 commit 76f5da8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
43 changes: 27 additions & 16 deletions token/cli/src/clap_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use {
},
solana_clap_v3_utils::{
fee_payer::fee_payer_arg,
input_parsers::Amount,
input_parsers::{signer::SignerSourceParserBuilder, Amount},
input_validators::{is_pubkey, is_url_or_moniker, is_valid_pubkey, is_valid_signer},
memo::memo_arg,
nonce::*,
Expand Down Expand Up @@ -282,6 +282,17 @@ pub fn owner_address_arg<'a>() -> Arg<'a> {
.help(OWNER_ADDRESS_ARG.help)
}

// Temporary function that uses proper parsing to minimize commit size
// TODO: use this to replace `owner_address_arg`
pub fn owner_address_arg_temp<'a>() -> Arg<'a> {
Arg::with_name(OWNER_ADDRESS_ARG.name)
.long(OWNER_ADDRESS_ARG.long)
.takes_value(true)
.value_name("OWNER_ADDRESS")
.value_parser(SignerSourceParserBuilder::default().allow_all().build())
.help(OWNER_ADDRESS_ARG.help)
}

pub fn owner_keypair_arg_with_value_name<'a>(value_name: &'static str) -> Arg<'a> {
Arg::with_name(OWNER_KEYPAIR_ARG.name)
.long(OWNER_KEYPAIR_ARG.long)
Expand Down Expand Up @@ -1228,7 +1239,7 @@ pub fn app<'a>(
"Lock the owner of this token account from ever being changed"
),
)
.arg(owner_address_arg())
.arg(owner_address_arg_temp())
.nonce_args(true)
)
.subcommand(
Expand Down Expand Up @@ -1759,7 +1770,7 @@ pub fn app<'a>(
.arg(
Arg::with_name("recipient")
.long("recipient")
.validator(|s| is_valid_pubkey(s))
.value_parser(SignerSourceParserBuilder::default().allow_all().build())
.value_name("REFUND_ACCOUNT_ADDRESS")
.takes_value(true)
.help("The address of the account to receive remaining SOL [default: --owner]"),
Expand Down Expand Up @@ -1807,7 +1818,7 @@ pub fn app<'a>(
.arg(
Arg::with_name("recipient")
.long("recipient")
.validator(|s| is_valid_pubkey(s))
.value_parser(SignerSourceParserBuilder::default().allow_all().build())
.value_name("REFUND_ACCOUNT_ADDRESS")
.takes_value(true)
.help("The address of the account to receive remaining SOL [default: --owner]"),
Expand All @@ -1834,17 +1845,17 @@ pub fn app<'a>(
.about("Get token account balance")
.arg(
Arg::with_name("token")
.validator(|s| is_valid_pubkey(s))
.value_parser(SignerSourceParserBuilder::default().allow_all().build())
.value_name("TOKEN_MINT_ADDRESS")
.takes_value(true)
.index(1)
.required_unless("address")
.help("Token of associated account. To query a specific account, use the `--address` parameter instead"),
)
.arg(owner_address_arg().conflicts_with("address"))
.arg(owner_address_arg_temp().conflicts_with("address"))
.arg(
Arg::with_name("address")
.validator(|s| is_valid_pubkey(s))
.value_parser(SignerSourceParserBuilder::default().allow_all().build())
.value_name("TOKEN_ACCOUNT_ADDRESS")
.takes_value(true)
.long("address")
Expand Down Expand Up @@ -1905,7 +1916,7 @@ pub fn app<'a>(
"Print token account addresses only"
),
)
.arg(owner_address_arg())
.arg(owner_address_arg_temp())
)
.subcommand(
SubCommand::with_name(CommandName::Address.into())
Expand All @@ -1921,7 +1932,7 @@ pub fn app<'a>(
[Default: return the client keypair address]")
)
.arg(
owner_address_arg()
owner_address_arg_temp()
.requires("token")
.help("Return the associated token address for the given owner. \
[Default: return the associated token address for the client keypair]"),
Expand All @@ -1933,7 +1944,7 @@ pub fn app<'a>(
.setting(AppSettings::Hidden)
.arg(
Arg::with_name("token")
.validator(|s| is_valid_pubkey(s))
.value_parser(SignerSourceParserBuilder::default().allow_all().build())
.value_name("TOKEN_MINT_ADDRESS")
.takes_value(true)
.index(1)
Expand Down Expand Up @@ -2028,7 +2039,7 @@ pub fn app<'a>(
.about("Enable required transfer memos for token account")
.arg(
Arg::with_name("account")
.validator(|s| is_valid_pubkey(s))
.value_parser(SignerSourceParserBuilder::default().allow_all().build())
.value_name("TOKEN_ACCOUNT_ADDRESS")
.takes_value(true)
.index(1)
Expand All @@ -2046,7 +2057,7 @@ pub fn app<'a>(
.about("Disable required transfer memos for token account")
.arg(
Arg::with_name("account")
.validator(|s| is_valid_pubkey(s))
.value_parser(SignerSourceParserBuilder::default().allow_all().build())
.value_name("TOKEN_ACCOUNT_ADDRESS")
.takes_value(true)
.index(1)
Expand All @@ -2064,7 +2075,7 @@ pub fn app<'a>(
.about("Enable CPI Guard for token account")
.arg(
Arg::with_name("account")
.validator(|s| is_valid_pubkey(s))
.value_parser(SignerSourceParserBuilder::default().allow_all().build())
.value_name("TOKEN_ACCOUNT_ADDRESS")
.takes_value(true)
.index(1)
Expand All @@ -2082,7 +2093,7 @@ pub fn app<'a>(
.about("Disable CPI Guard for token account")
.arg(
Arg::with_name("account")
.validator(|s| is_valid_pubkey(s))
.value_parser(SignerSourceParserBuilder::default().allow_all().build())
.value_name("TOKEN_ACCOUNT_ADDRESS")
.takes_value(true)
.index(1)
Expand Down Expand Up @@ -2357,15 +2368,15 @@ pub fn app<'a>(
.about("Withdraw lamports from a Token Program owned account")
.arg(
Arg::with_name("from")
.validator(|s| is_valid_pubkey(s))
.value_parser(SignerSourceParserBuilder::default().allow_all().build())
.value_name("SOURCE_ACCOUNT_ADDRESS")
.takes_value(true)
.required(true)
.help("Specify the address of the account to recover lamports from"),
)
.arg(
Arg::with_name("recipient")
.validator(|s| is_valid_pubkey(s))
.value_parser(SignerSourceParserBuilder::default().allow_all().build())
.value_name("REFUND_ACCOUNT_ADDRESS")
.takes_value(true)
.required(true)
Expand Down
9 changes: 5 additions & 4 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,
input_parsers::{pubkey_of_signer, signer::SignerSource},
input_validators::normalize_to_url_if_moniker,
keypair::SignerFromPathConfig,
nonce::{NONCE_ARG, NONCE_AUTHORITY_ARG},
Expand Down Expand Up @@ -398,7 +398,7 @@ impl<'a> Config<'a> {
override_name: &str,
wallet_manager: &mut Option<Rc<RemoteWalletManager>>,
) -> Result<Pubkey, Error> {
let token = pubkey_of_signer(arg_matches, "token", wallet_manager)
let token = SignerSource::try_get_pubkey(arg_matches, "token", wallet_manager)
.map_err(|e| -> Error { e.to_string().into() })?;
self.associated_token_address_for_token_or_override(
arg_matches,
Expand Down Expand Up @@ -449,8 +449,9 @@ impl<'a> Config<'a> {
address_name: &str,
wallet_manager: &mut Option<Rc<RemoteWalletManager>>,
) -> Result<Pubkey, Error> {
if let Some(address) = pubkey_of_signer(arg_matches, address_name, wallet_manager)
.map_err(|e| -> Error { e.to_string().into() })?
if let Some(address) =
SignerSource::try_get_pubkey(arg_matches, address_name, wallet_manager)
.map_err(|e| -> Error { e.to_string().into() })?
{
return Ok(address);
}
Expand Down

0 comments on commit 76f5da8

Please sign in to comment.