Skip to content

Commit

Permalink
feat(papyrus_base_layer): add l1handler msg
Browse files Browse the repository at this point in the history
  • Loading branch information
Gilad Chase committed Dec 24, 2024
1 parent df5d733 commit ee4639b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
42 changes: 39 additions & 3 deletions crates/papyrus_base_layer/src/eth_events.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -23,8 +26,27 @@ impl TryFrom<Log> 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!()
Expand Down Expand Up @@ -68,6 +90,20 @@ impl TryFrom<Starknet::MessageToL2CancellationStarted> for EventData {
}
}

impl TryFrom<Starknet::LogMessageToL2> for EventData {
type Error = EthereumBaseLayerError;

fn try_from(decoded: Starknet::LogMessageToL2) -> EthereumBaseLayerResult<Self> {
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,
Expand Down
2 changes: 2 additions & 0 deletions crates/papyrus_base_layer/src/ethereum_base_layer_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ pub enum EthereumBaseLayerError {
StarknetApiParsingError(StarknetApiError),
#[error("{0:?}")]
UnhandledL1Event(alloy_primitives::Log),
#[error("{0}")]
FeeOutOfRange(alloy_primitives::ruint::FromUintError<u128>),
}

#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
Expand Down

0 comments on commit ee4639b

Please sign in to comment.