cw_ica_controller/ibc/types/
events.rs

1//! # Events
2//!
3//! This module defines the events emitted by the ICA controller contract.
4//!
5//! The core modules will emit events when certain actions occur whether or not
6//! the ICA controller contract emits them. This module only defines the events
7//! that add more information to the events emitted by the core modules.
8//!
9//! Therefore;
10//!
11//! - We need not emit events during the handshake.
12//! - We need not emit events during packet sending.
13//! - When we emit events associated with packets, it suffices to add attributes
14//!   that uniquely identify the packet, and only add attributes that are relevant
15//!   to the ICA controller on top of those attributes.
16
17use cosmwasm_std::{Event, IbcPacket};
18
19/// contains the events emitted during packet acknowledgement.
20pub mod packet_ack {
21    use cosmwasm_std::Binary;
22
23    use super::{attributes, Event, IbcPacket};
24
25    const EVENT_TYPE: &str = "acknowledge_packet";
26
27    /// returns an event for a successful packet acknowledgement.
28    #[must_use]
29    pub fn success(packet: &IbcPacket, resp: &Binary) -> Event {
30        Event::new(EVENT_TYPE)
31            .add_attributes(attributes::from_packet(packet))
32            .add_attribute(attributes::ACK_BASE64, resp.to_base64())
33    }
34
35    /// returns an event for an unsuccessful packet acknowledgement.
36    #[must_use]
37    pub fn error(packet: &IbcPacket, err: &str) -> Event {
38        Event::new(EVENT_TYPE)
39            .add_attributes(attributes::from_packet(packet))
40            .add_attribute(attributes::ERROR, err)
41    }
42}
43
44mod attributes {
45    use super::IbcPacket;
46    use cosmwasm_std::Attribute;
47
48    pub const ACK_BASE64: &str = "packet_ack_base64";
49    pub const SEQUENCE: &str = "packet_sequence";
50    pub const SRC_PORT: &str = "packet_src_port";
51    pub const SRC_CHANNEL: &str = "packet_src_channel";
52
53    pub const ERROR: &str = "error";
54
55    /// returns the attributes for uniquely identifying a packet.
56    pub fn from_packet(packet: &IbcPacket) -> Vec<Attribute> {
57        vec![
58            Attribute::new(SEQUENCE, packet.sequence.to_string()),
59            Attribute::new(SRC_PORT, packet.src.port_id.clone()),
60            Attribute::new(SRC_CHANNEL, packet.src.channel_id.clone()),
61        ]
62    }
63}