Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(starknet_l1_provider): add interface to query ethereum gas price #3679

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions crates/papyrus_base_layer/src/base_layer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,23 @@ async fn get_proved_block_at_unknown_block_number() {
.contains("BlockOutOfRangeError")
);
}

#[tokio::test]
async fn get_gas_price_and_timestamps() {
if !in_ci() {
return;
}

let (node_handle, starknet_contract_address) = get_test_ethereum_node();
let node_url = node_handle.0.endpoint().parse().unwrap();
let contract = ethereum_base_layer_contract(node_url, starknet_contract_address);

let block_number = 30;
let price_sample = contract.get_price_sample(block_number).await.unwrap().unwrap();

// TODO(guyn): Figure out how these numbers are calculated, instead of just printing and testing
// against what we got.
assert_eq!(price_sample.timestamp, 1676992456);
assert_eq!(price_sample.base_fee_per_gas, 20168195);
assert_eq!(price_sample.blob_fee, 0);
}
35 changes: 33 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,12 @@ 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 +26,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 +136,32 @@ impl BaseLayerContract for EthereumBaseLayerContract {
hash: block.header.hash.0,
}))
}

// Query the Ethereum base layer for the timestamp, gas price, and data gas price of a block.
async fn get_price_sample(
&self,
block_number: L1BlockNumber,
) -> EthereumBaseLayerResult<Option<PriceSample>> {
let block = self
.contract
.provider()
.get_block(
BlockId::Number(BlockNumberOrTag::Number(block_number)),
BlockTransactionsKind::Hashes,
)
.await?;
let Some(block) = block else {
return Ok(None);
};
match (block.header.timestamp, block.header.base_fee_per_gas) {
(timestamp, Some(base_fee_per_gas)) => Ok(Some(PriceSample {
timestamp,
base_fee_per_gas,
blob_fee: block.header.blob_fee().unwrap_or(0),
})),
_ => Ok(None),
}
}
}

#[derive(thiserror::Error, Debug)]
Expand Down
12 changes: 12 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,18 @@ pub trait BaseLayerContract {
block_range: RangeInclusive<L1BlockNumber>,
event_identifiers: &[&str],
) -> Result<Vec<L1Event>, Self::Error>;

async fn get_price_sample(
&self,
block_number: L1BlockNumber,
) -> Result<Option<PriceSample>, Self::Error>;
}

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

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