diff --git a/crates/papyrus_base_layer/src/eth_events.rs b/crates/papyrus_base_layer/src/eth_events.rs index d81ed7e61e..a471567c55 100644 --- a/crates/papyrus_base_layer/src/eth_events.rs +++ b/crates/papyrus_base_layer/src/eth_events.rs @@ -4,7 +4,8 @@ use alloy_primitives::{Address as EthereumContractAddress, U256}; use alloy_rpc_types_eth::Log; use alloy_sol_types::SolEventInterface; use starknet_api::core::{EntryPointSelector, Nonce}; -use starknet_api::transaction::fields::Calldata; +use starknet_api::transaction::fields::{Calldata, Fee}; +use starknet_api::transaction::L1HandlerTransaction; use starknet_types_core::felt::Felt; use crate::ethereum_base_layer_contract::{ @@ -23,8 +24,24 @@ impl TryFrom for L1Event { let event = Starknet::StarknetEvents::decode_log(&log, validate)?.data; match event { - Starknet::StarknetEvents::LogMessageToL2(_event) => { - todo!() + Starknet::StarknetEvents::LogMessageToL2(event) => { + let fee = + Fee(event.fee.try_into().map_err(EthereumBaseLayerError::FeeOutOfRange)?); + let mut event_data = EventData::try_from(event)?; + let payload_inner = Arc::get_mut(&mut event_data.payload.0).expect( + "The event data is the only owner and was initialized in the previous line", + ); + // Prepend the L1 sender address to the calldata. + payload_inner.insert(0, event_data.from_address.into()); + + let tx = L1HandlerTransaction { + version: L1HandlerTransaction::VERSION, + contract_address: event_data.to_address, + entry_point_selector: event_data.entry_point_selector, + nonce: event_data.nonce, + calldata: event_data.payload, + }; + Ok(L1Event::LogMessageToL2 { tx, fee }) } Starknet::StarknetEvents::ConsumedMessageToL2(_event) => { todo!() @@ -68,6 +85,20 @@ impl TryFrom for EventData { } } +impl TryFrom for EventData { + type Error = EthereumBaseLayerError; + + fn try_from(decoded: Starknet::LogMessageToL2) -> EthereumBaseLayerResult { + create_l1_event_data( + decoded.fromAddress, + decoded.toAddress, + decoded.selector, + &decoded.payload, + decoded.nonce, + ) + } +} + pub fn create_l1_event_data( from_address: EthereumContractAddress, to_address: U256, 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 4e185c9892..ab16b48e40 100644 --- a/crates/papyrus_base_layer/src/ethereum_base_layer_contract.rs +++ b/crates/papyrus_base_layer/src/ethereum_base_layer_contract.rs @@ -104,17 +104,19 @@ impl BaseLayerContract for EthereumBaseLayerContract { pub enum EthereumBaseLayerError { #[error(transparent)] Contract(#[from] alloy_contract::Error), + #[error("{0}")] + FeeOutOfRange(alloy_primitives::ruint::FromUintError), #[error(transparent)] RpcError(#[from] RpcError), - #[error(transparent)] - TypeError(#[from] alloy_sol_types::Error), #[error("{0}")] StarknetApiParsingError(StarknetApiError), + #[error(transparent)] + TypeError(#[from] alloy_sol_types::Error), #[error("{0:?}")] UnhandledL1Event(alloy_primitives::Log), } -#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)] +#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)] pub struct EthereumBaseLayerConfig { pub node_url: Url, pub starknet_contract_address: EthereumContractAddress,