Skip to content

Commit

Permalink
Add get-recent-prioritization-fees command to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
sakridge committed Apr 25, 2024
1 parent e7452b7 commit 1993ff0
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
46 changes: 46 additions & 0 deletions cli-output/src/cli_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,52 @@ impl OutputFormat {
}
}

#[derive(Serialize)]
pub struct CliPrioritizationFeeStats {
pub fees: Vec<CliPrioritizationFee>,
pub min: u64,
pub max: u64,
pub average: u64,
pub num_slots: u64,
}

impl QuietDisplay for CliPrioritizationFeeStats {}
impl VerboseDisplay for CliPrioritizationFeeStats {
fn write_str(&self, f: &mut dyn std::fmt::Write) -> fmt::Result {
writeln!(f, "{:<11} prioritization_fee", "slot")?;
for fee in &self.fees {
writeln!(f, "{:<11} {}", fee.slot, fee.prioritization_fee)?;
}
Ok(())
}
}

impl fmt::Display for CliPrioritizationFeeStats {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(
f,
"Fees in recent {} slots: Min: {} Max: {} Average: {}",
self.num_slots, self.min, self.max, self.average
)
}
}

#[derive(Serialize)]
pub struct CliPrioritizationFee {
pub slot: Slot,
pub prioritization_fee: u64,
}

impl QuietDisplay for CliPrioritizationFee {}
impl VerboseDisplay for CliPrioritizationFee {}

impl fmt::Display for CliPrioritizationFee {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
writeln!(f, "{:<11} {}", self.slot, self.prioritization_fee)?;
Ok(())
}
}

#[derive(Serialize, Deserialize)]
pub struct CliAccount {
#[serde(flatten)]
Expand Down
11 changes: 11 additions & 0 deletions cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ pub enum CliCommand {
GetBlock {
slot: Option<Slot>,
},
GetRecentPrioritizationFees {
accounts: Vec<Pubkey>,
limit_num_slots: Option<Slot>,
},
GetBlockTime {
slot: Option<Slot>,
},
Expand Down Expand Up @@ -609,6 +613,9 @@ pub fn parse_command(
}
// Cluster Query Commands
("block", Some(matches)) => parse_get_block(matches),
("recent-prioritization-fees", Some(matches)) => {
parse_get_recent_prioritization_fees(matches)
}
("block-height", Some(matches)) => parse_get_block_height(matches),
("block-production", Some(matches)) => parse_show_block_production(matches),
("block-time", Some(matches)) => parse_get_block_time(matches),
Expand Down Expand Up @@ -905,6 +912,10 @@ pub fn process_command(config: &CliConfig) -> ProcessResult {
CliCommand::FirstAvailableBlock => process_first_available_block(&rpc_client),
CliCommand::GetBlock { slot } => process_get_block(&rpc_client, config, *slot),
CliCommand::GetBlockTime { slot } => process_get_block_time(&rpc_client, config, *slot),
CliCommand::GetRecentPrioritizationFees {
accounts,
limit_num_slots,
} => process_get_recent_priority_fees(&rpc_client, config, accounts, *limit_num_slots),
CliCommand::GetEpoch => process_get_epoch(&rpc_client, config),
CliCommand::GetEpochInfo => process_get_epoch_info(&rpc_client, config),
CliCommand::GetGenesisHash => process_get_genesis_hash(&rpc_client),
Expand Down
70 changes: 70 additions & 0 deletions cli/src/cluster_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,28 @@ impl ClusterQuerySubCommands for App<'_, '_> {
.index(1),
),
)
.subcommand(
SubCommand::with_name("recent-prioritization-fees")
.about("Get recent prioritization fees")
.arg(
Arg::with_name("accounts")
.value_name("ACCOUNTS")
.takes_value(true)
.multiple(true)
.index(1)
.help(
"A list of accounts which if provided the fee response will represent\
the fee to land a transaction with those accounts as writable",
),
)
.arg(
Arg::with_name("limit_num_slots")
.long("limit-num-slots")
.value_name("SLOTS")
.takes_value(true)
.help("Limit the number of slots to the last <N> slots"),
),
)
.subcommand(
SubCommand::with_name("catchup")
.about("Wait for a validator to catch up to the cluster")
Expand Down Expand Up @@ -573,6 +595,19 @@ pub fn parse_get_block(matches: &ArgMatches<'_>) -> Result<CliCommandInfo, CliEr
}))
}

pub fn parse_get_recent_prioritization_fees(
matches: &ArgMatches<'_>,
) -> Result<CliCommandInfo, CliError> {
let accounts = values_of(matches, "accounts").unwrap_or(vec![]);
let limit_num_slots = value_of(matches, "limit_num_slots");
Ok(CliCommandInfo::without_signers(
CliCommand::GetRecentPrioritizationFees {
accounts,
limit_num_slots,
},
))
}

pub fn parse_get_block_time(matches: &ArgMatches<'_>) -> Result<CliCommandInfo, CliError> {
let slot = value_of(matches, "slot");
Ok(CliCommandInfo::without_signers(CliCommand::GetBlockTime {
Expand Down Expand Up @@ -1044,6 +1079,41 @@ pub fn process_leader_schedule(
}))
}

pub fn process_get_recent_priority_fees(
rpc_client: &RpcClient,
config: &CliConfig,
accounts: &[Pubkey],
limit_num_slots: Option<Slot>,
) -> ProcessResult {
let fees = rpc_client.get_recent_prioritization_fees(accounts)?;
let mut min = u64::MAX;
let mut max = 0;
let mut total = 0;
let num_slots = limit_num_slots
.unwrap_or(fees.len() as u64)
.min(fees.len() as u64)
.max(1) as usize;
let mut cli_fees = Vec::with_capacity(fees.len());
for fee in fees.iter().skip(fees.len().saturating_sub(num_slots)) {
min = min.min(fee.prioritization_fee);
max = max.max(fee.prioritization_fee);
total += fee.prioritization_fee;
cli_fees.push(CliPrioritizationFee {
slot: fee.slot,
prioritization_fee: fee.prioritization_fee,
});
}
Ok(config
.output_format
.formatted_string(&CliPrioritizationFeeStats {
fees: cli_fees,
min,
max,
average: total / num_slots as u64,
num_slots: num_slots as u64,
}))
}

pub fn process_get_block(
rpc_client: &RpcClient,
config: &CliConfig,
Expand Down

0 comments on commit 1993ff0

Please sign in to comment.