Skip to content

Commit

Permalink
feat(starknet_l1_provider): add baselayer interface to query ethereum…
Browse files Browse the repository at this point in the history
… gas price
  • Loading branch information
guy-starkware committed Jan 29, 2025
1 parent 993f242 commit 4fd24cf
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
71 changes: 69 additions & 2 deletions crates/papyrus_base_layer/src/ethereum_base_layer_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use alloy_json_rpc::RpcError;
use alloy_primitives::Address as EthereumContractAddress;
use alloy_provider::network::Ethereum;
use alloy_provider::{Provider, ProviderBuilder, RootProvider};
use alloy_rpc_types_eth::{BlockId, BlockTransactionsKind, Filter as EthEventFilter};
use alloy_rpc_types_eth::{BlockId, BlockNumberOrTag, BlockTransactionsKind, Filter as EthEventFilter};
use alloy_sol_types::{sol, sol_data};
use alloy_transport::TransportErrorKind;
use alloy_transport_http::{Client, Http};
Expand All @@ -21,7 +21,7 @@ use starknet_api::StarknetApiError;
use url::Url;
use validator::Validate;

use crate::{BaseLayerContract, L1BlockNumber, L1BlockReference, L1Event};
use crate::{BaseLayerContract, L1BlockNumber, L1BlockReference, L1Event, PriceSample};

pub type EthereumBaseLayerResult<T> = Result<T, EthereumBaseLayerError>;

Expand Down Expand Up @@ -131,6 +131,73 @@ impl BaseLayerContract for EthereumBaseLayerContract {
hash: block.header.hash.0,
}))
}

async fn get_block_timestamp(&self, block_number: u64) -> EthereumBaseLayerResult<Option<u64>> {
let block = self
.contract
.provider()
.get_block(
BlockId::Number(BlockNumberOrTag::Number(block_number)),
BlockTransactionsKind::Hashes,
)
.await?;
Ok(block.map(|block| block.header.timestamp))
}

async fn get_block_gas_price(
&self,
block_number: u64,
) -> EthereumBaseLayerResult<Option<u128>> {
let block = self
.contract
.provider()
.get_block(
BlockId::Number(BlockNumberOrTag::Number(block_number)),
BlockTransactionsKind::Hashes,
)
.await?;
Ok(block.and_then(|block| block.header.base_fee_per_gas))
}

async fn get_block_data_gas_price(
&self,
block_number: u64,
) -> EthereumBaseLayerResult<Option<u128>> {
let block = self
.contract
.provider()
.get_block(
BlockId::Number(BlockNumberOrTag::Number(block_number)),
BlockTransactionsKind::Hashes,
)
.await?;
Ok(block.and_then(|block| block.header.blob_fee()))
}

// Combine the above three functions into one call, which is more efficient.
async fn get_price_sample(
&self,
block_number: u64,
) -> EthereumBaseLayerResult<Option<PriceSample>> {
let block = self
.contract
.provider()
.get_block(
BlockId::Number(BlockNumberOrTag::Number(block_number)),
BlockTransactionsKind::Hashes,
)
.await?;
if let Some(block) = block {
match (block.header.timestamp, block.header.base_fee_per_gas, block.header.blob_fee()) {
(timestamp, Some(base_fee_per_gas), Some(blob_fee)) => {
Ok(Some(PriceSample { timestamp, base_fee_per_gas, blob_fee }))
}
_ => Ok(None),
}
} else {
Ok(None)
}
}
}

#[derive(thiserror::Error, Debug)]
Expand Down
26 changes: 26 additions & 0 deletions crates/papyrus_base_layer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,32 @@ pub trait BaseLayerContract {
block_range: RangeInclusive<L1BlockNumber>,
event_identifiers: &[&str],
) -> Result<Vec<L1Event>, Self::Error>;

async fn get_block_timestamp(&self, block_number: u64) -> Result<Option<u64>, Self::Error>;
async fn get_block_gas_price(&self, block_number: u64) -> Result<Option<u128>, Self::Error>;
async fn get_block_data_gas_price(
&self,
block_number: u64,
) -> Result<Option<u128>, Self::Error>;
async fn get_price_sample(&self, block_number: u64)
-> Result<Option<PriceSample>, Self::Error>;
}

/// A struct the holds together the data on the baselayer's gas prices, for a given timestamp.
pub struct PriceSample {
timestamp: u64,
base_fee_per_gas: u128,
blob_fee: u128,
}

impl Display for PriceSample {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"PriceSample {{ timestamp: {}, base_fee_per_gas: {}, blob_fee: {} }}",
self.timestamp, self.base_fee_per_gas, self.blob_fee
)
}
}

/// Reference to an L1 block, extend as needed.
Expand Down

0 comments on commit 4fd24cf

Please sign in to comment.