|
| 1 | +use std::collections::VecDeque; |
| 2 | +use std::fmt::Debug; |
| 3 | +use std::sync::Arc; |
| 4 | + |
| 5 | +use alloy_provider::{Provider, ProviderBuilder}; |
| 6 | +use alloy_rpc_types_eth::{BlockNumberOrTag, FeeHistory}; |
| 7 | +use anyhow::{Context, Ok}; |
| 8 | +use katana_primitives::block::GasPrice; |
| 9 | +use katana_tasks::TaskSpawner; |
| 10 | +use num_traits::ToPrimitive; |
| 11 | +use parking_lot::Mutex; |
| 12 | +use starknet::core::types::{BlockId, BlockTag, MaybePendingBlockWithTxHashes, ResourcePrice}; |
| 13 | +use starknet::providers::jsonrpc::HttpTransport; |
| 14 | +use starknet::providers::{JsonRpcClient, Provider as StarknetProvider}; |
| 15 | +use tokio::time::Duration; |
| 16 | +use tracing::error; |
| 17 | +use url::Url; |
| 18 | + |
| 19 | +#[derive(Debug, Clone)] |
| 20 | +pub struct GasOracle { |
| 21 | + prices: Arc<Mutex<SampledPrices>>, |
| 22 | + provider: Url, |
| 23 | +} |
| 24 | + |
| 25 | +impl GasOracle { |
| 26 | + pub fn new(provider: Url) -> Self { |
| 27 | + Self { prices: Default::default(), provider } |
| 28 | + } |
| 29 | + |
| 30 | + pub fn current_l1_data_gas_prices(&self) -> GasPrice { |
| 31 | + self.prices.lock().l1_data_gas_prices.clone() |
| 32 | + } |
| 33 | + |
| 34 | + pub fn current_l1_gas_prices(&self) -> GasPrice { |
| 35 | + self.prices.lock().l1_gas_prices.clone() |
| 36 | + } |
| 37 | + |
| 38 | + pub fn current_l2_gas_prices(&self) -> GasPrice { |
| 39 | + GasPrice::MIN |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +#[derive(Debug, Clone)] |
| 44 | +pub struct EthereumGasOracleWorker { |
| 45 | + pub prices: Arc<Mutex<SampledPrices>>, |
| 46 | + pub l1_provider_url: Url, |
| 47 | + pub gas_price_buffer: GasPriceBuffer, |
| 48 | + pub data_gas_price_buffer: GasPriceBuffer, |
| 49 | +} |
| 50 | + |
| 51 | +impl EthereumGasOracleWorker { |
| 52 | + pub fn new(prices: Arc<Mutex<SampledPrices>>, l1_provider_url: Url) -> Self { |
| 53 | + Self { |
| 54 | + prices, |
| 55 | + l1_provider_url, |
| 56 | + gas_price_buffer: GasPriceBuffer::new(), |
| 57 | + data_gas_price_buffer: GasPriceBuffer::new(), |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + pub async fn run(&mut self) -> anyhow::Result<()> { |
| 62 | + let provider = ProviderBuilder::new().on_http(self.l1_provider_url.clone()); |
| 63 | + // every 60 seconds, Starknet samples the base price of gas and data gas on L1 |
| 64 | + let mut interval = tokio::time::interval(INTERVAL); |
| 65 | + interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); |
| 66 | + |
| 67 | + loop { |
| 68 | + // Wait for the interval to tick |
| 69 | + interval.tick().await; |
| 70 | + |
| 71 | + { |
| 72 | + // Attempt to get the gas price from L1 |
| 73 | + let last_block_number = provider.get_block_number().await?; |
| 74 | + |
| 75 | + let fee_history = provider |
| 76 | + .get_fee_history(1, BlockNumberOrTag::Number(last_block_number), &[]) |
| 77 | + .await?; |
| 78 | + |
| 79 | + let mut prices = self.prices.lock(); |
| 80 | + |
| 81 | + if let Err(error) = update_gas_price( |
| 82 | + &mut prices, |
| 83 | + &mut self.gas_price_buffer, |
| 84 | + &mut self.data_gas_price_buffer, |
| 85 | + fee_history, |
| 86 | + ) { |
| 87 | + error!(target: "gas_oracle", %error, "Error updating gas prices."); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + pub async fn update_once(&mut self) -> anyhow::Result<()> { |
| 94 | + let provider = ProviderBuilder::new().on_http(self.l1_provider_url.clone()); |
| 95 | + |
| 96 | + // Attempt to get the gas price from L1 |
| 97 | + let last_block_number = provider.get_block_number().await?; |
| 98 | + |
| 99 | + let fee_history = |
| 100 | + provider.get_fee_history(1, BlockNumberOrTag::Number(last_block_number), &[]).await?; |
| 101 | + |
| 102 | + let mut prices = self.prices.lock(); |
| 103 | + |
| 104 | + update_gas_price( |
| 105 | + &mut prices, |
| 106 | + &mut self.gas_price_buffer, |
| 107 | + &mut self.data_gas_price_buffer, |
| 108 | + fee_history, |
| 109 | + ) |
| 110 | + } |
| 111 | +} |
0 commit comments