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

Commit 861f918

Browse files
committed
Parse args as suggested
1 parent 08b78be commit 861f918

File tree

2 files changed

+24
-40
lines changed

2 files changed

+24
-40
lines changed

token/cli/src/clap_app.rs

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use {
1515
},
1616
solana_sdk::{instruction::AccountMeta, pubkey::Pubkey},
1717
spl_token_2022::instruction::{AuthorityType, MAX_SIGNERS, MIN_SIGNERS},
18-
spl_token_client::token::ComputeUnitLimit,
1918
std::{fmt, str::FromStr},
2019
strum::IntoEnumIterator,
2120
strum_macros::{EnumIter, EnumString, IntoStaticStr},
@@ -74,10 +73,7 @@ pub const COMPUTE_UNIT_PRICE_ARG: ArgConstant<'static> = ArgConstant {
7473
pub const COMPUTE_UNIT_LIMIT_ARG: ArgConstant<'static> = ArgConstant {
7574
name: "compute_unit_limit",
7675
long: "--with-compute-unit-limit",
77-
help: "Set compute unit limit for transaction, in compute units; also accepts \
78-
keyword SIMULATED to use compute units from transaction simulation prior \
79-
to sending. Note that SIMULATED may fail if accounts are modified by another \
80-
transaction between simulation and execution.",
76+
help: "Set compute unit limit for transaction, in compute units.",
8177
};
8278

8379
pub static VALID_TOKEN_PROGRAM_IDS: [Pubkey; 2] = [spl_token_2022::ID, spl_token::ID];
@@ -355,31 +351,6 @@ where
355351
}
356352
}
357353

358-
fn is_compute_unit_limit_or_simulated<T>(string: T) -> Result<(), String>
359-
where
360-
T: AsRef<str> + fmt::Display,
361-
{
362-
if string.as_ref().parse::<u32>().is_ok() || string.as_ref() == "SIMULATED" {
363-
Ok(())
364-
} else {
365-
Err(format!(
366-
"Unable to parse input compute unit limit as integer or SIMULATED, provided: {string}"
367-
))
368-
}
369-
}
370-
pub(crate) fn parse_compute_unit_limit<T>(string: T) -> Result<ComputeUnitLimit, String>
371-
where
372-
T: AsRef<str> + fmt::Display,
373-
{
374-
match string.as_ref().parse::<u32>() {
375-
Ok(compute_unit_limit) => Ok(ComputeUnitLimit::Static(compute_unit_limit)),
376-
Err(_) if string.as_ref() == "SIMULATED" => Ok(ComputeUnitLimit::Simulated),
377-
_ => Err(format!(
378-
"Unable to parse compute unit limit, provided: {string}"
379-
)),
380-
}
381-
}
382-
383354
struct SignOnlyNeedsFullMintSpec {}
384355
impl offline::ArgsConfig for SignOnlyNeedsFullMintSpec {
385356
fn sign_only_arg<'a, 'b>(&self, arg: Arg<'a, 'b>) -> Arg<'a, 'b> {
@@ -659,7 +630,7 @@ pub fn app<'a, 'b>(
659630
.takes_value(true)
660631
.global(true)
661632
.value_name("COMPUTE-UNIT-LIMIT")
662-
.validator(is_compute_unit_limit_or_simulated)
633+
.validator(is_parsable::<u32>)
663634
.help(COMPUTE_UNIT_LIMIT_ARG.help)
664635
)
665636
.arg(
@@ -670,7 +641,6 @@ pub fn app<'a, 'b>(
670641
.value_name("COMPUTE-UNIT-PRICE")
671642
.validator(is_parsable::<u64>)
672643
.help(COMPUTE_UNIT_PRICE_ARG.help)
673-
.requires(COMPUTE_UNIT_LIMIT_ARG.name)
674644
)
675645
.bench_subcommand()
676646
.subcommand(SubCommand::with_name(CommandName::CreateToken.into()).about("Create a new token")

token/cli/src/config.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use {
2-
crate::clap_app::{
3-
parse_compute_unit_limit, Error, COMPUTE_UNIT_LIMIT_ARG, COMPUTE_UNIT_PRICE_ARG,
4-
MULTISIG_SIGNER_ARG,
5-
},
2+
crate::clap_app::{Error, COMPUTE_UNIT_LIMIT_ARG, COMPUTE_UNIT_PRICE_ARG, MULTISIG_SIGNER_ARG},
63
clap::ArgMatches,
74
solana_clap_utils::{
85
input_parsers::{pubkey_of_signer, value_of},
@@ -286,12 +283,29 @@ impl<'a> Config<'a> {
286283
(default_program_id, false)
287284
};
288285

286+
// need to specify a compute limit if compute price and blockhash are specified
287+
if matches.is_present(BLOCKHASH_ARG.name)
288+
&& matches.is_present(COMPUTE_UNIT_PRICE_ARG.name)
289+
&& !matches.is_present(COMPUTE_UNIT_LIMIT_ARG.name)
290+
{
291+
eprintln!(
292+
"error: need to set {} if {} and {} are set",
293+
COMPUTE_UNIT_LIMIT_ARG.name, COMPUTE_UNIT_PRICE_ARG.name, BLOCKHASH_ARG.name,
294+
);
295+
exit(1);
296+
}
297+
289298
let nonce_blockhash = value_of(matches, BLOCKHASH_ARG.name);
290299
let compute_unit_price = value_of(matches, COMPUTE_UNIT_PRICE_ARG.name);
291-
let compute_unit_limit = matches
292-
.value_of(COMPUTE_UNIT_LIMIT_ARG.name)
293-
.map(|x| parse_compute_unit_limit(x).unwrap())
294-
.unwrap_or(ComputeUnitLimit::Default);
300+
let compute_unit_limit = value_of(matches, COMPUTE_UNIT_LIMIT_ARG.name)
301+
.map(ComputeUnitLimit::Static)
302+
.unwrap_or_else(|| {
303+
if nonce_blockhash.is_some() {
304+
ComputeUnitLimit::Default
305+
} else {
306+
ComputeUnitLimit::Simulated
307+
}
308+
});
295309
Self {
296310
default_signer,
297311
rpc_client,

0 commit comments

Comments
 (0)