Skip to main content

reth_node_core/args/
gas_price_oracle.rs

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