-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
95 additions
and
0 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
81 changes: 81 additions & 0 deletions
81
relayer/crates/starknet-chain-components/src/types/events/connection.rs
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,81 @@ | ||
use cgp::prelude::*; | ||
use hermes_cairo_encoding_components::strategy::ViaCairo; | ||
use hermes_cairo_encoding_components::types::as_felt::AsFelt; | ||
use hermes_encoding_components::traits::decode::{CanDecode, Decoder}; | ||
use hermes_encoding_components::traits::has_encoding::HasEncoding; | ||
use hermes_encoding_components::traits::types::encoded::HasEncodedType; | ||
use starknet::core::types::Felt; | ||
use starknet::macros::selector; | ||
|
||
use crate::types::client_id::ClientId; | ||
use crate::types::connection_id::ConnectionId; | ||
use crate::types::event::{StarknetEvent, UnknownEvent}; | ||
|
||
#[derive(Debug)] | ||
pub enum ConnectionHandshakeEvents { | ||
Init(InitConnectionEvent), | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct InitConnectionEvent { | ||
pub client_id_on_a: ClientId, | ||
pub connection_id_on_a: ConnectionId, | ||
pub client_id_on_b: ClientId, | ||
} | ||
|
||
pub struct DecodeConnectionHandshakeEvents; | ||
|
||
impl<Encoding, Strategy> Decoder<Encoding, Strategy, ConnectionHandshakeEvents> | ||
for DecodeConnectionHandshakeEvents | ||
where | ||
Encoding: HasEncodedType<Encoded = StarknetEvent> | ||
+ CanDecode<Strategy, InitConnectionEvent> | ||
+ for<'a> CanRaiseError<UnknownEvent<'a>>, | ||
{ | ||
fn decode( | ||
encoding: &Encoding, | ||
event: &StarknetEvent, | ||
) -> Result<ConnectionHandshakeEvents, Encoding::Error> { | ||
let selector = event | ||
.selector | ||
.ok_or_else(|| Encoding::raise_error(UnknownEvent { event }))?; | ||
|
||
if selector == selector!("ConnOpenInitEvent") { | ||
Ok(ConnectionHandshakeEvents::Init(encoding.decode(event)?)) | ||
} else { | ||
Err(Encoding::raise_error(UnknownEvent { event })) | ||
} | ||
} | ||
} | ||
|
||
impl<EventEncoding, CairoEncoding, Strategy> Decoder<EventEncoding, Strategy, InitConnectionEvent> | ||
for DecodeConnectionHandshakeEvents | ||
where | ||
EventEncoding: HasEncodedType<Encoded = StarknetEvent> | ||
+ HasEncoding<AsFelt, Encoding = CairoEncoding> | ||
+ CanRaiseError<CairoEncoding::Error> | ||
+ for<'a> CanRaiseError<UnknownEvent<'a>>, | ||
CairoEncoding: HasEncodedType<Encoded = Vec<Felt>> | ||
+ CanDecode<ViaCairo, Product![ClientId, ConnectionId, ClientId]>, | ||
{ | ||
fn decode( | ||
event_encoding: &EventEncoding, | ||
event: &StarknetEvent, | ||
) -> Result<InitConnectionEvent, EventEncoding::Error> { | ||
let cairo_encoding = event_encoding.encoding(); | ||
|
||
let product![client_id_on_a, connection_id_on_a, client_id_on_b] = cairo_encoding | ||
.decode(&event.keys) | ||
.map_err(EventEncoding::raise_error)?; | ||
|
||
if !event.data.is_empty() { | ||
return Err(EventEncoding::raise_error(UnknownEvent { event })); | ||
} | ||
|
||
Ok(InitConnectionEvent { | ||
client_id_on_a, | ||
connection_id_on_a, | ||
client_id_on_b, | ||
}) | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
relayer/crates/starknet-chain-components/src/types/events/mod.rs
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod connection; | ||
pub mod erc20; | ||
pub mod ics20; |
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