From 9579f56179c345608cb5465d962db8327652eb1b Mon Sep 17 00:00:00 2001 From: Gilad Chase Date: Fri, 20 Dec 2024 15:09:13 +0200 Subject: [PATCH] feat(papyrus_base_layer): add events api --- .../src/ethereum_base_layer_contract.rs | 11 ++++++- crates/papyrus_base_layer/src/lib.rs | 31 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/crates/papyrus_base_layer/src/ethereum_base_layer_contract.rs b/crates/papyrus_base_layer/src/ethereum_base_layer_contract.rs index f29aca518a..f6a02b566f 100644 --- a/crates/papyrus_base_layer/src/ethereum_base_layer_contract.rs +++ b/crates/papyrus_base_layer/src/ethereum_base_layer_contract.rs @@ -18,7 +18,7 @@ use starknet_api::hash::StarkHash; use starknet_types_core::felt; use url::Url; -use crate::BaseLayerContract; +use crate::{BaseLayerContract, L1Event}; type EthereumBaseLayerResult = Result; @@ -79,6 +79,15 @@ impl BaseLayerContract for EthereumBaseLayerContract { })) } + async fn events( + &self, + _from_block: u64, + _until_block: u64, + _event_identifiers: &[&str], + ) -> EthereumBaseLayerResult> { + todo!("Implmeneted in a subsequent commit") + } + async fn latest_l1_block_number(&self, finality: u64) -> EthereumBaseLayerResult> { Ok(self.contract.provider().get_block_number().await?.checked_sub(finality)) } diff --git a/crates/papyrus_base_layer/src/lib.rs b/crates/papyrus_base_layer/src/lib.rs index 271fd0990a..e79957c8bc 100644 --- a/crates/papyrus_base_layer/src/lib.rs +++ b/crates/papyrus_base_layer/src/lib.rs @@ -1,5 +1,9 @@ use async_trait::async_trait; +use serde::{Deserialize, Serialize}; use starknet_api::block::BlockHashAndNumber; +use starknet_api::core::{ContractAddress, EntryPointSelector, EthAddress, Nonce}; +use starknet_api::transaction::fields::{Calldata, Fee}; +use starknet_api::transaction::L1HandlerTransaction; pub mod ethereum_base_layer_contract; @@ -21,5 +25,32 @@ pub trait BaseLayerContract { finality: u64, ) -> Result, Self::Error>; + /// Get specific events from the Starknet base contract between two l1 block numbers. + async fn events( + &self, + from_block: u64, + until_block: u64, + event_identifiers: &[&str], + ) -> Result, Self::Error>; + async fn latest_l1_block_number(&self, finality: u64) -> Result, Self::Error>; } + +/// Wraps Starknet L1 events with Starknet API types. +#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] +pub enum L1Event { + LogMessageToL2 { tx: L1HandlerTransaction, fee: Fee }, + MessageToL2CancellationStarted(EventData), + MessageToL2Canceled(EventData), + ConsumedMessageToL1 { from_address: EthAddress, to_address: ContractAddress, payload: Calldata }, +} + +/// Shared fields in Starknet messaging. +#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Default)] +pub struct EventData { + pub from_address: EthAddress, + pub to_address: ContractAddress, + pub entry_point_selector: EntryPointSelector, + pub payload: Calldata, + pub nonce: Nonce, +}