@@ -16,24 +16,22 @@ const BUFFER_SIZE: usize = 60;
16
16
const INTERVAL : Duration = Duration :: from_secs ( 60 ) ;
17
17
const ONE_GWEI : u128 = 1_000_000_000 ;
18
18
19
- // TODO: implement a proper gas oracle function - sample the l1 gas and data gas prices
20
- // currently this just return the hardcoded value set from the cli or if not set, the default value.
21
19
#[ derive( Debug ) ]
22
- pub enum L1GasOracle {
23
- Fixed ( FixedL1GasOracle ) ,
24
- Sampled ( SampledL1GasOracle ) ,
20
+ pub enum GasOracle {
21
+ Fixed ( FixedGasOracle ) ,
22
+ Sampled ( EthereumSampledGasOracle ) ,
25
23
}
26
24
27
25
#[ derive( Debug ) ]
28
- pub struct FixedL1GasOracle {
26
+ pub struct FixedGasOracle {
29
27
gas_prices : GasPrices ,
30
28
data_gas_prices : GasPrices ,
31
29
}
32
30
33
31
#[ derive( Debug , Clone ) ]
34
- pub struct SampledL1GasOracle {
32
+ pub struct EthereumSampledGasOracle {
35
33
prices : Arc < Mutex < SampledPrices > > ,
36
- l1_provider : Url ,
34
+ provider : Url ,
37
35
}
38
36
39
37
#[ derive( Debug , Default ) ]
@@ -50,29 +48,39 @@ pub struct GasOracleWorker {
50
48
pub data_gas_price_buffer : GasPriceBuffer ,
51
49
}
52
50
53
- impl L1GasOracle {
51
+ impl GasOracle {
54
52
pub fn fixed ( gas_prices : GasPrices , data_gas_prices : GasPrices ) -> Self {
55
- L1GasOracle :: Fixed ( FixedL1GasOracle { gas_prices, data_gas_prices } )
53
+ GasOracle :: Fixed ( FixedGasOracle { gas_prices, data_gas_prices } )
56
54
}
57
55
58
- pub fn sampled ( l1_provider : Url ) -> Self {
56
+ /// Creates a new gas oracle that samples the gas prices from an Ethereum chain.
57
+ pub fn sampled_ethereum ( eth_provider : Url ) -> Self {
59
58
let prices: Arc < Mutex < SampledPrices > > = Arc :: new ( Mutex :: new ( SampledPrices :: default ( ) ) ) ;
60
- L1GasOracle :: Sampled ( SampledL1GasOracle { prices, l1_provider } )
59
+ GasOracle :: Sampled ( EthereumSampledGasOracle { prices, provider : eth_provider } )
60
+ }
61
+
62
+ /// This is just placeholder for now, as Starknet doesn't provide a way to get the L2 gas
63
+ /// prices, we just return a fixed gas price values of 0. This is equivalent to calling
64
+ /// [`GasOracle::fixed`] with 0 values for both gas and data prices.
65
+ ///
66
+ /// The result of this is the same as running the node with fee disabled.
67
+ pub fn sampled_starknet ( ) -> Self {
68
+ Self :: fixed ( GasPrices { eth : 0 , strk : 0 } , GasPrices { eth : 0 , strk : 0 } )
61
69
}
62
70
63
71
/// Returns the current gas prices.
64
72
pub fn current_gas_prices ( & self ) -> GasPrices {
65
73
match self {
66
- L1GasOracle :: Fixed ( fixed) => fixed. current_gas_prices ( ) ,
67
- L1GasOracle :: Sampled ( sampled) => sampled. prices . lock ( ) . gas_prices . clone ( ) ,
74
+ GasOracle :: Fixed ( fixed) => fixed. current_gas_prices ( ) ,
75
+ GasOracle :: Sampled ( sampled) => sampled. prices . lock ( ) . gas_prices . clone ( ) ,
68
76
}
69
77
}
70
78
71
79
/// Returns the current data gas prices.
72
80
pub fn current_data_gas_prices ( & self ) -> GasPrices {
73
81
match self {
74
- L1GasOracle :: Fixed ( fixed) => fixed. current_data_gas_prices ( ) ,
75
- L1GasOracle :: Sampled ( sampled) => sampled. prices . lock ( ) . data_gas_prices . clone ( ) ,
82
+ GasOracle :: Fixed ( fixed) => fixed. current_data_gas_prices ( ) ,
83
+ GasOracle :: Sampled ( sampled) => sampled. prices . lock ( ) . data_gas_prices . clone ( ) ,
76
84
}
77
85
}
78
86
@@ -81,7 +89,7 @@ impl L1GasOracle {
81
89
Self :: Fixed ( ..) => { }
82
90
Self :: Sampled ( oracle) => {
83
91
let prices = oracle. prices . clone ( ) ;
84
- let l1_provider = oracle. l1_provider . clone ( ) ;
92
+ let l1_provider = oracle. provider . clone ( ) ;
85
93
86
94
task_spawner. build_task ( ) . critical ( ) . name ( "L1 Gas Oracle Worker" ) . spawn (
87
95
async move {
@@ -97,7 +105,7 @@ impl L1GasOracle {
97
105
}
98
106
}
99
107
100
- impl SampledL1GasOracle {
108
+ impl EthereumSampledGasOracle {
101
109
pub fn current_data_gas_prices ( & self ) -> GasPrices {
102
110
self . prices . lock ( ) . data_gas_prices . clone ( )
103
111
}
@@ -107,7 +115,7 @@ impl SampledL1GasOracle {
107
115
}
108
116
}
109
117
110
- impl FixedL1GasOracle {
118
+ impl FixedGasOracle {
111
119
pub fn current_data_gas_prices ( & self ) -> GasPrices {
112
120
self . data_gas_prices . clone ( )
113
121
}
@@ -289,10 +297,10 @@ mod tests {
289
297
#[ ignore = "Requires external assumption" ]
290
298
async fn test_gas_oracle ( ) {
291
299
let url = Url :: parse ( "https://eth.merkle.io/" ) . expect ( "Invalid URL" ) ;
292
- let oracle = L1GasOracle :: sampled ( url. clone ( ) ) ;
300
+ let oracle = GasOracle :: sampled_ethereum ( url. clone ( ) ) ;
293
301
294
302
let shared_prices = match & oracle {
295
- L1GasOracle :: Sampled ( sampled) => sampled. prices . clone ( ) ,
303
+ GasOracle :: Sampled ( sampled) => sampled. prices . clone ( ) ,
296
304
_ => panic ! ( "Expected sampled oracle" ) ,
297
305
} ;
298
306
0 commit comments