Skip to content

Commit

Permalink
clap-utils: Add more compute unit helpers (solana-labs#440)
Browse files Browse the repository at this point in the history
* clap-utils: Refactor compute_unit_price into compute_budget

* clap-utils: Validate compute unit price as a u64

* clap-utils: Add compute unit limit arg

* clap-v3-utils: Add compute unit price and limit helpers

* Add deprecation on `pub use` even though it isn't triggered
  • Loading branch information
joncinque authored Mar 28, 2024
1 parent 10d0677 commit 8246590
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 15 deletions.
34 changes: 34 additions & 0 deletions clap-utils/src/compute_budget.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use {
crate::{input_validators::is_parsable, ArgConstant},
clap::Arg,
};

pub const COMPUTE_UNIT_PRICE_ARG: ArgConstant<'static> = ArgConstant {
name: "compute_unit_price",
long: "--with-compute-unit-price",
help: "Set compute unit price for transaction, in increments of 0.000001 lamports per compute unit.",
};

pub const COMPUTE_UNIT_LIMIT_ARG: ArgConstant<'static> = ArgConstant {
name: "compute_unit_limit",
long: "--with-compute-unit-limit",
help: "Set compute unit limit for transaction.",
};

pub fn compute_unit_price_arg<'a, 'b>() -> Arg<'a, 'b> {
Arg::with_name(COMPUTE_UNIT_PRICE_ARG.name)
.long(COMPUTE_UNIT_PRICE_ARG.long)
.takes_value(true)
.value_name("COMPUTE-UNIT-PRICE")
.validator(is_parsable::<u64>)
.help(COMPUTE_UNIT_PRICE_ARG.help)
}

pub fn compute_unit_limit_arg<'a, 'b>() -> Arg<'a, 'b> {
Arg::with_name(COMPUTE_UNIT_LIMIT_ARG.name)
.long(COMPUTE_UNIT_LIMIT_ARG.long)
.takes_value(true)
.value_name("COMPUTE-UNIT-LIMIT")
.validator(is_parsable::<u32>)
.help(COMPUTE_UNIT_LIMIT_ARG.help)
}
17 changes: 2 additions & 15 deletions clap-utils/src/compute_unit_price.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,2 @@
use {crate::ArgConstant, clap::Arg};

pub const COMPUTE_UNIT_PRICE_ARG: ArgConstant<'static> = ArgConstant {
name: "compute_unit_price",
long: "--with-compute-unit-price",
help: "Set compute unit price for transaction, in increments of 0.000001 lamports per compute unit.",
};

pub fn compute_unit_price_arg<'a, 'b>() -> Arg<'a, 'b> {
Arg::with_name(COMPUTE_UNIT_PRICE_ARG.name)
.long(COMPUTE_UNIT_PRICE_ARG.long)
.takes_value(true)
.value_name("COMPUTE-UNIT-PRICE")
.help(COMPUTE_UNIT_PRICE_ARG.help)
}
#[deprecated(since = "2.0.0", note = "Please use `compute_budget` instead")]
pub use crate::compute_budget::{compute_unit_price_arg, COMPUTE_UNIT_PRICE_ARG};
1 change: 1 addition & 0 deletions clap-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub fn hidden_unless_forced() -> bool {
std::env::var("SOLANA_NO_HIDDEN_CLI_ARGS").is_err()
}

pub mod compute_budget;
pub mod compute_unit_price;
pub mod fee_payer;
pub mod input_parsers;
Expand Down
34 changes: 34 additions & 0 deletions clap-v3-utils/src/compute_budget.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use {
crate::ArgConstant,
clap::{value_parser, Arg},
};

pub const COMPUTE_UNIT_PRICE_ARG: ArgConstant<'static> = ArgConstant {
name: "compute_unit_price",
long: "--with-compute-unit-price",
help: "Set compute unit price for transaction, in increments of 0.000001 lamports per compute unit.",
};

pub const COMPUTE_UNIT_LIMIT_ARG: ArgConstant<'static> = ArgConstant {
name: "compute_unit_limit",
long: "--with-compute-unit-limit",
help: "Set compute unit limit for transaction.",
};

pub fn compute_unit_price_arg<'a>() -> Arg<'a> {
Arg::with_name(COMPUTE_UNIT_PRICE_ARG.name)
.long(COMPUTE_UNIT_PRICE_ARG.long)
.takes_value(true)
.value_name("COMPUTE-UNIT-PRICE")
.value_parser(value_parser!(u64))
.help(COMPUTE_UNIT_PRICE_ARG.help)
}

pub fn compute_unit_limit_arg<'a>() -> Arg<'a> {
Arg::with_name(COMPUTE_UNIT_LIMIT_ARG.name)
.long(COMPUTE_UNIT_LIMIT_ARG.long)
.takes_value(true)
.value_name("COMPUTE-UNIT-LIMIT")
.value_parser(value_parser!(u32))
.help(COMPUTE_UNIT_LIMIT_ARG.help)
}
1 change: 1 addition & 0 deletions clap-v3-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl std::fmt::Debug for DisplayError {
}
}

pub mod compute_budget;
pub mod fee_payer;
pub mod input_parsers;
pub mod input_validators;
Expand Down

0 comments on commit 8246590

Please sign in to comment.