Skip to content

Commit

Permalink
connection open init event
Browse files Browse the repository at this point in the history
  • Loading branch information
rnbguy committed Dec 10, 2024
1 parent 79fee68 commit 91ef1ca
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use hermes_encoding_components::traits::types::encoded::ProvideEncodedType;

use crate::impls::encoding::option::DecodeOptionalByClassHash;
use crate::types::event::StarknetEvent;
use crate::types::events::connection::{
ConnectionHandshakeEvents, DecodeConnectionHandshakeEvents, InitConnectionEvent,
};
use crate::types::events::erc20::{ApprovalEvent, DecodeErc20Events, Erc20Event, TransferEvent};
use crate::types::events::ics20::{
CreateIbcTokenEvent, DecodeIbcTransferEvents, IbcTransferEvent, ReceiveIbcTransferEvent,
Expand Down Expand Up @@ -41,9 +44,16 @@ delegate_components! {
(ViaCairo, CreateIbcTokenEvent),
]:
DecodeIbcTransferEvents,
[
(ViaCairo, ConnectionHandshakeEvents),
(ViaCairo, InitConnectionEvent),
]:
DecodeConnectionHandshakeEvents,
(ViaCairo, Option<Erc20Event>):
DecodeOptionalByClassHash<symbol!("erc20_hashes")>,
(ViaCairo, Option<IbcTransferEvent>):
DecodeOptionalByClassHash<symbol!("ics20_hashes")>,
(ViaCairo, Option<ConnectionHandshakeEvents>):
DecodeOptionalByClassHash<symbol!("ibc_core_hashes")>,
}
}
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,
})
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod connection;
pub mod erc20;
pub mod ics20;
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use hermes_encoding_components::traits::types::encoded::HasEncodedType;
use hermes_error::impls::ProvideHermesError;
use hermes_starknet_chain_components::components::encoding::event::*;
use hermes_starknet_chain_components::types::event::StarknetEvent;
use hermes_starknet_chain_components::types::events::connection::ConnectionHandshakeEvents;
use hermes_starknet_chain_components::types::events::erc20::Erc20Event;
use hermes_starknet_chain_components::types::events::ics20::IbcTransferEvent;
use starknet::core::types::Felt;
Expand Down Expand Up @@ -63,6 +64,8 @@ pub trait CanUseStarknetEventEncoding:
+ CanDecode<ViaCairo, IbcTransferEvent>
+ CanDecode<ViaCairo, Option<Erc20Event>>
+ CanDecode<ViaCairo, Option<IbcTransferEvent>>
+ CanDecode<ViaCairo, ConnectionHandshakeEvents>
+ CanDecode<ViaCairo, Option<ConnectionHandshakeEvents>>
{
}

Expand Down

0 comments on commit 91ef1ca

Please sign in to comment.