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    /// The default gas price to use if there are no blocks to use
30    #[arg(long = "gpo.default-suggested-fee")]
31    pub default_suggested_fee: Option<U256>,
32}
33
34impl GasPriceOracleArgs {
35    /// Returns a [`GasPriceOracleConfig`] from the arguments.
36    pub fn gas_price_oracle_config(&self) -> GasPriceOracleConfig {
37        let Self { blocks, ignore_price, max_price, percentile, default_suggested_fee } = self;
38        GasPriceOracleConfig {
39            max_price: Some(U256::from(*max_price)),
40            ignore_price: Some(U256::from(*ignore_price)),
41            percentile: *percentile,
42            blocks: *blocks,
43            default_suggested_fee: *default_suggested_fee,
44            ..Default::default()
45        }
46    }
47}
48
49impl Default for GasPriceOracleArgs {
50    fn default() -> Self {
51        Self {
52            blocks: DEFAULT_GAS_PRICE_BLOCKS,
53            ignore_price: DEFAULT_IGNORE_GAS_PRICE.to(),
54            max_price: DEFAULT_MAX_GAS_PRICE.to(),
55            percentile: DEFAULT_GAS_PRICE_PERCENTILE,
56            default_suggested_fee: None,
57        }
58    }
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64    use clap::Parser;
65    /// A helper type to parse Args more easily
66    #[derive(Parser)]
67    struct CommandParser<T: Args> {
68        #[command(flatten)]
69        args: T,
70    }
71
72    #[test]
73    fn test_parse_gpo_args() {
74        let args = CommandParser::<GasPriceOracleArgs>::parse_from(["reth"]).args;
75        assert_eq!(
76            args,
77            GasPriceOracleArgs {
78                blocks: DEFAULT_GAS_PRICE_BLOCKS,
79                ignore_price: DEFAULT_IGNORE_GAS_PRICE.to(),
80                max_price: DEFAULT_MAX_GAS_PRICE.to(),
81                percentile: DEFAULT_GAS_PRICE_PERCENTILE,
82                default_suggested_fee: None,
83            }
84        );
85    }
86
87    #[test]
88    fn gpo_args_default_sanity_test() {
89        let default_args = GasPriceOracleArgs::default();
90        let args = CommandParser::<GasPriceOracleArgs>::parse_from(["reth"]).args;
91        assert_eq!(args, default_args);
92    }
93}