1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//! Defines all token transfer event types
use ibc_core::channel::types::acknowledgement::AcknowledgementStatus;
use ibc_core::primitives::prelude::*;
use ibc_core::primitives::Signer;
use ibc_core::router::types::event::ModuleEvent;

use super::Memo;
use crate::{Amount, PrefixedDenom, MODULE_ID_STR};

const EVENT_TYPE_PACKET: &str = "fungible_token_packet";
const EVENT_TYPE_TIMEOUT: &str = "timeout";
const EVENT_TYPE_DENOM_TRACE: &str = "denomination_trace";
const EVENT_TYPE_TRANSFER: &str = "ibc_transfer";

/// Contains all events variants that can be emitted from the token transfer application
pub enum Event {
    Recv(RecvEvent),
    Ack(AckEvent),
    AckStatus(AckStatusEvent),
    Timeout(TimeoutEvent),
    DenomTrace(DenomTraceEvent),
    Transfer(TransferEvent),
}

/// Event emitted by the `onRecvPacket` module callback to indicate the that the
/// `RecvPacket` message was processed
pub struct RecvEvent {
    pub sender: Signer,
    pub receiver: Signer,
    pub denom: PrefixedDenom,
    pub amount: Amount,
    pub memo: Memo,
    pub success: bool,
}

impl From<RecvEvent> for ModuleEvent {
    fn from(ev: RecvEvent) -> Self {
        let RecvEvent {
            sender,
            receiver,
            denom,
            amount,
            memo,
            success,
        } = ev;
        Self {
            kind: EVENT_TYPE_PACKET.to_string(),
            attributes: vec![
                ("module", MODULE_ID_STR).into(),
                ("sender", sender).into(),
                ("receiver", receiver).into(),
                ("denom", denom).into(),
                ("amount", amount).into(),
                ("memo", memo).into(),
                ("success", success).into(),
            ],
        }
    }
}

/// Event emitted in the `onAcknowledgePacket` module callback
pub struct AckEvent {
    pub sender: Signer,
    pub receiver: Signer,
    pub denom: PrefixedDenom,
    pub amount: Amount,
    pub memo: Memo,
    pub acknowledgement: AcknowledgementStatus,
}

impl From<AckEvent> for ModuleEvent {
    fn from(ev: AckEvent) -> Self {
        let AckEvent {
            sender,
            receiver,
            denom,
            amount,
            memo,
            acknowledgement,
        } = ev;
        Self {
            kind: EVENT_TYPE_PACKET.to_string(),
            attributes: vec![
                ("module", MODULE_ID_STR).into(),
                ("sender", sender).into(),
                ("receiver", receiver).into(),
                ("denom", denom).into(),
                ("amount", amount).into(),
                ("memo", memo).into(),
                ("acknowledgement", acknowledgement).into(),
            ],
        }
    }
}

/// Event emitted in the `onAcknowledgePacket` module callback to indicate
/// whether the acknowledgement is a success or a failure
pub struct AckStatusEvent {
    pub acknowledgement: AcknowledgementStatus,
}

impl From<AckStatusEvent> for ModuleEvent {
    fn from(ev: AckStatusEvent) -> Self {
        let AckStatusEvent { acknowledgement } = ev;
        let attr_label = match acknowledgement {
            AcknowledgementStatus::Success(_) => "success",
            AcknowledgementStatus::Error(_) => "error",
        };

        Self {
            kind: EVENT_TYPE_PACKET.to_string(),
            attributes: vec![(attr_label, acknowledgement.to_string()).into()],
        }
    }
}

/// Event emitted in the `onTimeoutPacket` module callback
pub struct TimeoutEvent {
    pub refund_receiver: Signer,
    pub refund_denom: PrefixedDenom,
    pub refund_amount: Amount,
    pub memo: Memo,
}

impl From<TimeoutEvent> for ModuleEvent {
    fn from(ev: TimeoutEvent) -> Self {
        let TimeoutEvent {
            refund_receiver,
            refund_denom,
            refund_amount,
            memo,
        } = ev;
        Self {
            kind: EVENT_TYPE_TIMEOUT.to_string(),
            attributes: vec![
                ("module", MODULE_ID_STR).into(),
                ("refund_receiver", refund_receiver).into(),
                ("refund_denom", refund_denom).into(),
                ("refund_amount", refund_amount).into(),
                ("memo", memo).into(),
            ],
        }
    }
}

/// Event emitted in the `onRecvPacket` module callback when new tokens are minted
pub struct DenomTraceEvent {
    pub trace_hash: Option<String>,
    pub denom: PrefixedDenom,
}

impl From<DenomTraceEvent> for ModuleEvent {
    fn from(ev: DenomTraceEvent) -> Self {
        let DenomTraceEvent { trace_hash, denom } = ev;
        let mut ev = Self {
            kind: EVENT_TYPE_DENOM_TRACE.to_string(),
            attributes: vec![("denom", denom).into()],
        };
        if let Some(hash) = trace_hash {
            ev.attributes.push(("trace_hash", hash).into());
        }
        ev
    }
}

/// Event emitted after a successful `sendTransfer`
pub struct TransferEvent {
    pub sender: Signer,
    pub receiver: Signer,
    pub amount: Amount,
    pub denom: PrefixedDenom,
    pub memo: Memo,
}

impl From<TransferEvent> for ModuleEvent {
    fn from(ev: TransferEvent) -> Self {
        let TransferEvent {
            sender,
            receiver,
            amount,
            denom,
            memo,
        } = ev;

        Self {
            kind: EVENT_TYPE_TRANSFER.to_string(),
            attributes: vec![
                ("sender", sender).into(),
                ("receiver", receiver).into(),
                ("amount", amount).into(),
                ("denom", denom).into(),
                ("memo", memo).into(),
            ],
        }
    }
}

impl From<Event> for ModuleEvent {
    fn from(ev: Event) -> Self {
        match ev {
            Event::Recv(ev) => ev.into(),
            Event::Ack(ev) => ev.into(),
            Event::AckStatus(ev) => ev.into(),
            Event::Timeout(ev) => ev.into(),
            Event::DenomTrace(ev) => ev.into(),
            Event::Transfer(ev) => ev.into(),
        }
    }
}