diff --git a/crates/papyrus_base_layer/src/eth_events.rs b/crates/papyrus_base_layer/src/eth_events.rs index f0e9bc3dbe..cb52187c46 100644 --- a/crates/papyrus_base_layer/src/eth_events.rs +++ b/crates/papyrus_base_layer/src/eth_events.rs @@ -1,10 +1,13 @@ +use std::iter::once; use std::sync::Arc; 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::hash::StarkHash; +use starknet_api::transaction::fields::{Calldata, Fee}; +use starknet_api::transaction::{L1HandlerTransaction, TransactionVersion}; use starknet_types_core::felt::Felt; use crate::ethereum_base_layer_contract::{ @@ -23,8 +26,27 @@ 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 event_data = EventData::try_from(event)?; + // Prepend the L1 sender address to the calldata. + let calldata = Calldata(Arc::new( + once(event_data.from_address.into()) + .chain(Arc::try_unwrap(event_data.payload.0).unwrap()) + .collect(), + )); + + const DEFAULT_L1_HANDLER_VERSION: TransactionVersion = + TransactionVersion(StarkHash::ZERO); + let tx = L1HandlerTransaction { + version: DEFAULT_L1_HANDLER_VERSION, + contract_address: event_data.to_address, + entry_point_selector: event_data.entry_point_selector, + nonce: event_data.nonce, + calldata, + }; + Ok(L1Event::LogMessageToL2 { tx, fee }) } Starknet::StarknetEvents::ConsumedMessageToL2(_event) => { todo!() @@ -68,6 +90,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 310ea28e20..5c3ad7ccb7 100644 --- a/crates/papyrus_base_layer/src/ethereum_base_layer_contract.rs +++ b/crates/papyrus_base_layer/src/ethereum_base_layer_contract.rs @@ -118,6 +118,8 @@ pub enum EthereumBaseLayerError { StarknetApiParsingError(StarknetApiError), #[error("{0:?}")] UnhandledL1Event(alloy_primitives::Log), + #[error("{0}")] + FeeOutOfRange(alloy_primitives::ruint::FromUintError), } #[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]