Skip to content

Commit 5fa5e1a

Browse files
ShahakShamaclaude
andcommitted
apollo_l1_gas_price: hold strk_to_usd oracle client + strk_to_usd_rate
Goal: PR 2 of 6 in the stack moving the STRK/USD oracle from the ad-hoc wiring in SequencerConsensusContext into L1GasPriceProvider. This commit adds the field and inherent method on the provider but nothing calls it yet — the seam is established without behavior change. Change summary: - `L1GasPriceProvider` gains a second `Arc<dyn ExchangeRateOracleClientTrait>` field (`strk_to_usd_oracle_client`), positionally after the existing `eth_to_strk_oracle_client`. - `new` takes a second oracle client parameter. - `new_with_oracle` reads `config.strk_to_usd_oracle_config` (added in the previous commit on this stack) and constructs the client. - New inherent method `strk_to_usd_rate(timestamp)` mirrors the existing `eth_to_fri_rate`. - Unit tests in `l1_gas_price_provider_test.rs` updated to pass a second mock to the constructor. Decision points: - Positional constructor argument over a builder. The struct has two oracle clients now; if a third lands later, builder pattern is the right move. For two, positional is fine. - Same `ExchangeRateOracleClientError` propagation via the existing `L1GasPriceProviderError::ExchangeRateOracleClientError` variant. No new error variant needed — the two oracles are semantically identical, only differ by URL. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent d6dfb69 commit 5fa5e1a

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

crates/apollo_l1_gas_price/src/l1_gas_price_provider.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,20 +59,29 @@ pub struct L1GasPriceProvider {
5959
// If received data before initialization (is None), it means the scraper has restarted.
6060
price_samples_by_block: Option<RingBuffer<GasPriceData>>,
6161
eth_to_strk_oracle_client: Arc<dyn ExchangeRateOracleClientTrait>,
62+
strk_to_usd_oracle_client: Arc<dyn ExchangeRateOracleClientTrait>,
6263
}
6364

6465
impl L1GasPriceProvider {
6566
pub fn new(
6667
config: L1GasPriceProviderConfig,
6768
eth_to_strk_oracle_client: Arc<dyn ExchangeRateOracleClientTrait>,
69+
strk_to_usd_oracle_client: Arc<dyn ExchangeRateOracleClientTrait>,
6870
) -> Self {
69-
Self { config, price_samples_by_block: None, eth_to_strk_oracle_client }
71+
Self {
72+
config,
73+
price_samples_by_block: None,
74+
eth_to_strk_oracle_client,
75+
strk_to_usd_oracle_client,
76+
}
7077
}
7178

7279
pub fn new_with_oracle(config: L1GasPriceProviderConfig) -> Self {
7380
let eth_to_strk_oracle_client =
7481
ExchangeRateOracleClient::new(config.eth_to_strk_oracle_config.clone());
75-
Self::new(config, Arc::new(eth_to_strk_oracle_client))
82+
let strk_to_usd_oracle_client =
83+
ExchangeRateOracleClient::new(config.strk_to_usd_oracle_config.clone());
84+
Self::new(config, Arc::new(eth_to_strk_oracle_client), Arc::new(strk_to_usd_oracle_client))
7685
}
7786

7887
pub fn initialize(&mut self) -> L1GasPriceProviderResult<()> {
@@ -190,6 +199,13 @@ impl L1GasPriceProvider {
190199
.await
191200
.map_err(L1GasPriceProviderError::ExchangeRateOracleClientError)
192201
}
202+
203+
pub async fn strk_to_usd_rate(&self, timestamp: u64) -> L1GasPriceProviderResult<u128> {
204+
self.strk_to_usd_oracle_client
205+
.fetch_rate(timestamp)
206+
.await
207+
.map_err(L1GasPriceProviderError::ExchangeRateOracleClientError)
208+
}
193209
}
194210

195211
#[async_trait]

crates/apollo_l1_gas_price/src/l1_gas_price_provider_test.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ use crate::l1_gas_price_provider::{L1GasPriceProvider, L1GasPriceProviderError};
1111
// Returns the provider, a vector of block prices to compare with, and the timestamp of block[3].
1212
fn make_provider() -> (L1GasPriceProvider, Vec<PriceInfo>, u64) {
1313
let eth_to_strk_oracle_client = Arc::new(MockExchangeRateOracleClientTrait::new());
14+
let strk_to_usd_oracle_client = Arc::new(MockExchangeRateOracleClientTrait::new());
1415
let mut provider = L1GasPriceProvider::new(
1516
L1GasPriceProviderConfig { number_of_blocks_for_mean: 3, ..Default::default() },
1617
eth_to_strk_oracle_client,
18+
strk_to_usd_oracle_client,
1719
);
1820
provider.initialize().unwrap();
1921
let mut prices = Vec::new();
@@ -117,9 +119,11 @@ fn gas_price_provider_timestamp_changes_mean() {
117119
#[test]
118120
fn gas_price_provider_can_start_at_nonzero_height() {
119121
let eth_to_strk_oracle_client = Arc::new(MockExchangeRateOracleClientTrait::new());
122+
let strk_to_usd_oracle_client = Arc::new(MockExchangeRateOracleClientTrait::new());
120123
let mut provider = L1GasPriceProvider::new(
121124
L1GasPriceProviderConfig { number_of_blocks_for_mean: 3, ..Default::default() },
122125
eth_to_strk_oracle_client,
126+
strk_to_usd_oracle_client,
123127
);
124128
provider.initialize().unwrap();
125129
let price_info = PriceInfo { base_fee_per_gas: GasPrice(0), blob_fee: GasPrice(0) };
@@ -130,9 +134,11 @@ fn gas_price_provider_can_start_at_nonzero_height() {
130134
#[test]
131135
fn gas_price_provider_uninitialized_error() {
132136
let eth_to_strk_oracle_client = Arc::new(MockExchangeRateOracleClientTrait::new());
137+
let strk_to_usd_oracle_client = Arc::new(MockExchangeRateOracleClientTrait::new());
133138
let mut provider = L1GasPriceProvider::new(
134139
L1GasPriceProviderConfig { number_of_blocks_for_mean: 3, ..Default::default() },
135140
eth_to_strk_oracle_client,
141+
strk_to_usd_oracle_client,
136142
);
137143
let price_info = PriceInfo { base_fee_per_gas: GasPrice(0), blob_fee: GasPrice(0) };
138144
let timestamp = BlockTimestamp(0);

0 commit comments

Comments
 (0)