reth_node_core/args/
gas_price_oracle.rs

1use alloy_primitives::U256;
2use clap::Args;
3use reth_rpc_eth_types::GasPriceOracleConfig;
4use reth_rpc_server_types::constants::gas_oracle::{
5    DEFAULT_GAS_PRICE_BLOCKS, DEFAULT_GAS_PRICE_PERCENTILE, DEFAULT_IGNORE_GAS_PRICE,
6    DEFAULT_MAX_GAS_PRICE,
7};
8
9/// Parameters to configure Gas Price Oracle
10#[derive(Debug, Clone, Copy, Args, PartialEq, Eq)]
11#[command(next_help_heading = "Gas Price Oracle")]
12pub struct GasPriceOracleArgs {
13    /// Number of recent blocks to check for gas price
14    #[arg(long = "gpo.blocks", default_value_t = DEFAULT_GAS_PRICE_BLOCKS)]
15    pub blocks: u32,
16
17    /// Gas Price below which gpo will ignore transactions
18    #[arg(long = "gpo.ignoreprice", default_value_t = DEFAULT_IGNORE_GAS_PRICE.to())]
19    pub ignore_price: u64,
20
21    /// Maximum transaction priority fee(or gasprice before London Fork) to be recommended by gpo
22    #[arg(long = "gpo.maxprice", default_value_t = DEFAULT_MAX_GAS_PRICE.to())]
23    pub max_price: u64,
24
25    /// The percentile of gas prices to use for the estimate
26    #[arg(long = "gpo.percentile", default_value_t = DEFAULT_GAS_PRICE_PERCENTILE)]
27    pub percentile: u32,
28}
29
30impl GasPriceOracleArgs {
31    /// Returns a [`GasPriceOracleConfig`] from the arguments.
32    pub fn gas_price_oracle_config(&self) -> GasPriceOracleConfig {
33        let Self { blocks, ignore_price, max_price, percentile } = self;
34        GasPriceOracleConfig {
35            max_price: Some(U256::from(*max_price)),
36            ignore_price: Some(U256::from(*ignore_price)),
37            percentile: *percentile,
38            blocks: *blocks,
39            ..Default::default()
40        }
41    }
42}
43
44impl Default for GasPriceOracleArgs {
45    fn default() -> Self {
46        Self {
47            blocks: DEFAULT_GAS_PRICE_BLOCKS,
48            ignore_price: DEFAULT_IGNORE_GAS_PRICE.to(),
49            max_price: DEFAULT_MAX_GAS_PRICE.to(),
50            percentile: DEFAULT_GAS_PRICE_PERCENTILE,
51        }
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58    use clap::Parser;
59    /// A helper type to parse Args more easily
60    #[derive(Parser)]
61    struct CommandParser<T: Args> {
62        #[command(flatten)]
63        args: T,
64    }
65
66    #[test]
67    fn test_parse_gpo_args() {
68        let args = CommandParser::<GasPriceOracleArgs>::parse_from(["reth"]).args;
69        assert_eq!(
70            args,
71            GasPriceOracleArgs {
72                blocks: DEFAULT_GAS_PRICE_BLOCKS,
73                ignore_price: DEFAULT_IGNORE_GAS_PRICE.to(),
74                max_price: DEFAULT_MAX_GAS_PRICE.to(),
75                percentile: DEFAULT_GAS_PRICE_PERCENTILE,
76            }
77        );
78    }
79
80    #[test]
81    fn gpo_args_default_sanity_test() {
82        let default_args = GasPriceOracleArgs::default();
83        let args = CommandParser::<GasPriceOracleArgs>::parse_from(["reth"]).args;
84        assert_eq!(args, default_args);
85    }
86}