CLI: Support querying fees by blockhash

This commit is contained in:
Trent Nelson 2021-02-25 23:48:33 -07:00 committed by Trent Nelson
parent ebd56f7ff4
commit 21e08b5b2c
3 changed files with 75 additions and 19 deletions

View File

@ -1475,8 +1475,11 @@ impl fmt::Display for CliFeesInner {
"Lamports per signature:", "Lamports per signature:",
&self.lamports_per_signature.to_string(), &self.lamports_per_signature.to_string(),
)?; )?;
writeln_name_value(f, "Last valid slot:", &self.last_valid_slot.to_string())?; let last_valid_slot = self
Ok(()) .last_valid_slot
.map(|s| s.to_string())
.unwrap_or_default();
writeln_name_value(f, "Last valid slot:", &last_valid_slot)
} }
} }

View File

@ -90,7 +90,9 @@ pub enum CliCommand {
}, },
Feature(FeatureCliCommand), Feature(FeatureCliCommand),
Inflation(InflationCliCommand), Inflation(InflationCliCommand),
Fees, Fees {
blockhash: Option<Hash>,
},
FirstAvailableBlock, FirstAvailableBlock,
GetBlock { GetBlock {
slot: Option<Slot>, slot: Option<Slot>,
@ -593,10 +595,13 @@ pub fn parse_command(
("feature", Some(matches)) => { ("feature", Some(matches)) => {
parse_feature_subcommand(matches, default_signer, wallet_manager) parse_feature_subcommand(matches, default_signer, wallet_manager)
} }
("fees", Some(_matches)) => Ok(CliCommandInfo { ("fees", Some(matches)) => {
command: CliCommand::Fees, let blockhash = value_of::<Hash>(matches, "blockhash");
signers: vec![], Ok(CliCommandInfo {
}), command: CliCommand::Fees { blockhash },
signers: vec![],
})
}
("first-available-block", Some(_matches)) => Ok(CliCommandInfo { ("first-available-block", Some(_matches)) => Ok(CliCommandInfo {
command: CliCommand::FirstAvailableBlock, command: CliCommand::FirstAvailableBlock,
signers: vec![], signers: vec![],
@ -1263,7 +1268,7 @@ pub fn process_command(config: &CliConfig) -> ProcessResult {
seed, seed,
program_id, program_id,
} => process_create_address_with_seed(config, from_pubkey.as_ref(), &seed, &program_id), } => process_create_address_with_seed(config, from_pubkey.as_ref(), &seed, &program_id),
CliCommand::Fees => process_fees(&rpc_client, config), CliCommand::Fees { ref blockhash } => process_fees(&rpc_client, config, blockhash.as_ref()),
CliCommand::Feature(feature_subcommand) => { CliCommand::Feature(feature_subcommand) => {
process_feature_subcommand(&rpc_client, config, feature_subcommand) process_feature_subcommand(&rpc_client, config, feature_subcommand)
} }

View File

@ -134,7 +134,17 @@ impl ClusterQuerySubCommands for App<'_, '_> {
SubCommand::with_name("cluster-version") SubCommand::with_name("cluster-version")
.about("Get the version of the cluster entrypoint"), .about("Get the version of the cluster entrypoint"),
) )
.subcommand(SubCommand::with_name("fees").about("Display current cluster fees"), .subcommand(
SubCommand::with_name("fees")
.about("Display current cluster fees")
.arg(
Arg::with_name("blockhash")
.long("blockhash")
.takes_value(true)
.value_name("BLOCKHASH")
.validator(is_hash)
.help("Query fees for BLOCKHASH instead of the the most recent blockhash")
),
) )
.subcommand( .subcommand(
SubCommand::with_name("first-available-block") SubCommand::with_name("first-available-block")
@ -820,15 +830,36 @@ pub fn process_cluster_version(rpc_client: &RpcClient, config: &CliConfig) -> Pr
} }
} }
pub fn process_fees(rpc_client: &RpcClient, config: &CliConfig) -> ProcessResult { pub fn process_fees(
let result = rpc_client.get_recent_blockhash_with_commitment(config.commitment)?; rpc_client: &RpcClient,
let (recent_blockhash, fee_calculator, last_valid_slot) = result.value; config: &CliConfig,
let fees = CliFees::some( blockhash: Option<&Hash>,
result.context.slot, ) -> ProcessResult {
recent_blockhash, let fees = if let Some(recent_blockhash) = blockhash {
fee_calculator.lamports_per_signature, let result = rpc_client.get_fee_calculator_for_blockhash_with_commitment(
last_valid_slot, recent_blockhash,
); config.commitment,
)?;
if let Some(fee_calculator) = result.value {
CliFees::some(
result.context.slot,
*recent_blockhash,
fee_calculator.lamports_per_signature,
None,
)
} else {
CliFees::none()
}
} else {
let result = rpc_client.get_recent_blockhash_with_commitment(config.commitment)?;
let (recent_blockhash, fee_calculator, last_valid_slot) = result.value;
CliFees::some(
result.context.slot,
recent_blockhash,
fee_calculator.lamports_per_signature,
Some(last_valid_slot),
)
};
Ok(config.output_format.formatted_string(&fees)) Ok(config.output_format.formatted_string(&fees))
} }
@ -1903,7 +1934,24 @@ mod tests {
assert_eq!( assert_eq!(
parse_command(&test_fees, &default_signer, &mut None).unwrap(), parse_command(&test_fees, &default_signer, &mut None).unwrap(),
CliCommandInfo { CliCommandInfo {
command: CliCommand::Fees, command: CliCommand::Fees { blockhash: None },
signers: vec![],
}
);
let blockhash = Hash::new_unique();
let test_fees = test_commands.clone().get_matches_from(vec![
"test",
"fees",
"--blockhash",
&blockhash.to_string(),
]);
assert_eq!(
parse_command(&test_fees, &default_signer, &mut None).unwrap(),
CliCommandInfo {
command: CliCommand::Fees {
blockhash: Some(blockhash)
},
signers: vec![], signers: vec![],
} }
); );