-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(papyrus_base_layer): start implementing eth events api
- Currently only implementing the simplest event to parse, the rest are coming up. - removed two unused error variants: `FeltParseError`, `Serde`
- Loading branch information
Gilad Chase
committed
Dec 29, 2024
1 parent
3bf13f0
commit cd19380
Showing
7 changed files
with
112 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ runs: | |
with: | ||
cache-base: main(-v[0-9].*)? | ||
inherit-toolchain: true | ||
bins: [email protected].0, cargo-machete | ||
bins: [email protected].3, cargo-machete | ||
# Install additional non-default toolchains (for rustfmt for example), NOP if input omitted. | ||
channel: ${{ inputs.extra_rust_toolchains }} | ||
env: | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
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_types_core::felt::Felt; | ||
|
||
use crate::ethereum_base_layer_contract::{ | ||
EthereumBaseLayerError, | ||
EthereumBaseLayerResult, | ||
Starknet, | ||
}; | ||
use crate::{EventData, L1Event}; | ||
|
||
impl TryFrom<Log> for L1Event { | ||
type Error = EthereumBaseLayerError; | ||
|
||
fn try_from(log: Log) -> EthereumBaseLayerResult<Self> { | ||
let validate = true; | ||
let log = log.inner; | ||
|
||
let event = Starknet::StarknetEvents::decode_log(&log, validate)?.data; | ||
match event { | ||
Starknet::StarknetEvents::LogMessageToL2(_event) => { | ||
todo!() | ||
} | ||
Starknet::StarknetEvents::ConsumedMessageToL2(_event) => { | ||
todo!() | ||
} | ||
Starknet::StarknetEvents::MessageToL2Canceled(event) => { | ||
Ok(L1Event::MessageToL2Canceled(event.try_into()?)) | ||
} | ||
Starknet::StarknetEvents::MessageToL2CancellationStarted(_event) => { | ||
todo!() | ||
} | ||
_ => Err(EthereumBaseLayerError::UnhandledL1Event(log)), | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<Starknet::MessageToL2Canceled> for EventData { | ||
type Error = EthereumBaseLayerError; | ||
|
||
fn try_from(event: Starknet::MessageToL2Canceled) -> EthereumBaseLayerResult<Self> { | ||
create_l1_event_data( | ||
event.fromAddress, | ||
event.toAddress, | ||
event.selector, | ||
&event.payload, | ||
event.nonce, | ||
) | ||
} | ||
} | ||
|
||
pub fn create_l1_event_data( | ||
from_address: EthereumContractAddress, | ||
to_address: U256, | ||
selector: U256, | ||
payload: &[U256], | ||
nonce: U256, | ||
) -> EthereumBaseLayerResult<EventData> { | ||
Ok(EventData { | ||
from_address: Felt::from_bytes_be_slice(from_address.0.as_slice()) | ||
.try_into() | ||
.map_err(EthereumBaseLayerError::StarknetApiParsingError)?, | ||
to_address: felt_from_u256(to_address) | ||
.try_into() | ||
.map_err(EthereumBaseLayerError::StarknetApiParsingError)?, | ||
entry_point_selector: EntryPointSelector(felt_from_u256(selector)), | ||
payload: Calldata(Arc::new(payload.iter().map(|&x| felt_from_u256(x)).collect())), | ||
nonce: Nonce(felt_from_u256(nonce)), | ||
}) | ||
} | ||
|
||
pub fn felt_from_u256(num: U256) -> Felt { | ||
Felt::from_bytes_be(&num.to_be_bytes()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters