Skip to content

Commit

Permalink
feat(papyrus_base_layer): add l1handler msg (#2847)
Browse files Browse the repository at this point in the history
Co-authored-by: Gilad Chase <[email protected]>
  • Loading branch information
giladchase and Gilad Chase authored Jan 1, 2025
1 parent 4633b92 commit 6878a91
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
37 changes: 34 additions & 3 deletions crates/papyrus_base_layer/src/eth_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -23,8 +24,24 @@ 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 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!()
Expand Down Expand Up @@ -68,6 +85,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
8 changes: 5 additions & 3 deletions crates/papyrus_base_layer/src/ethereum_base_layer_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u128>),
#[error(transparent)]
RpcError(#[from] RpcError<TransportErrorKind>),
#[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,
Expand Down

0 comments on commit 6878a91

Please sign in to comment.