Skip to content

Commit

Permalink
Make token transfer events compatible with latest ibc-go (#618)
Browse files Browse the repository at this point in the history
* add `module` event where needed

* remove unused `module_name`

* add sender event field

* cleanup

* TransferEvent

* changelog

* add missing event emission in send_transfer
  • Loading branch information
plafer authored Apr 20, 2023
1 parent 8ed4655 commit 9bb6327
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Make token transfer events compatible with latest ibc-go
([#495](https://github.com/cosmos/ibc-rs/pull/495))
2 changes: 2 additions & 0 deletions crates/ibc/src/applications/transfer/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ pub fn on_recv_packet_execute(
};

let recv_event = RecvEvent {
sender: data.sender,
receiver: data.receiver,
denom: data.token.denom,
amount: data.token.amount,
Expand Down Expand Up @@ -355,6 +356,7 @@ pub fn on_acknowledgement_packet_execute(
}

let ack_event = AckEvent {
sender: data.sender,
receiver: data.receiver,
denom: data.token.denom,
amount: data.token.amount,
Expand Down
45 changes: 29 additions & 16 deletions crates/ibc/src/applications/transfer/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub enum Event {
}

pub struct RecvEvent {
pub sender: Signer,
pub receiver: Signer,
pub denom: PrefixedDenom,
pub amount: Amount,
Expand All @@ -28,15 +29,17 @@ pub struct RecvEvent {
impl From<RecvEvent> for ModuleEvent {
fn from(ev: RecvEvent) -> Self {
let RecvEvent {
sender,
receiver,
denom,
amount,
success,
} = ev;
Self {
kind: EVENT_TYPE_PACKET.to_string(),
module_name: MODULE_ID_STR.parse().expect("invalid ModuleId"),
attributes: vec![
("module", MODULE_ID_STR).into(),
("sender", sender).into(),
("receiver", receiver).into(),
("denom", denom).into(),
("amount", amount).into(),
Expand All @@ -47,6 +50,7 @@ impl From<RecvEvent> for ModuleEvent {
}

pub struct AckEvent {
pub sender: Signer,
pub receiver: Signer,
pub denom: PrefixedDenom,
pub amount: Amount,
Expand All @@ -56,15 +60,17 @@ pub struct AckEvent {
impl From<AckEvent> for ModuleEvent {
fn from(ev: AckEvent) -> Self {
let AckEvent {
sender,
receiver,
denom,
amount,
acknowledgement,
} = ev;
Self {
kind: EVENT_TYPE_PACKET.to_string(),
module_name: MODULE_ID_STR.parse().expect("invalid ModuleId"),
attributes: vec![
("module", MODULE_ID_STR).into(),
("sender", sender).into(),
("receiver", receiver).into(),
("denom", denom).into(),
("amount", amount).into(),
Expand All @@ -81,19 +87,15 @@ pub struct AckStatusEvent {
impl From<AckStatusEvent> for ModuleEvent {
fn from(ev: AckStatusEvent) -> Self {
let AckStatusEvent { acknowledgement } = ev;
let mut event = Self {
kind: EVENT_TYPE_PACKET.to_string(),
module_name: MODULE_ID_STR.parse().expect("invalid ModuleId"),
attributes: vec![],
};
let attr_label = match acknowledgement {
TokenTransferAcknowledgement::Success(_) => "success",
TokenTransferAcknowledgement::Error(_) => "error",
};
event
.attributes
.push((attr_label, acknowledgement.to_string()).into());
event

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

Expand All @@ -112,8 +114,8 @@ impl From<TimeoutEvent> for ModuleEvent {
} = ev;
Self {
kind: EVENT_TYPE_TIMEOUT.to_string(),
module_name: MODULE_ID_STR.parse().expect("invalid ModuleId"),
attributes: vec![
("module", MODULE_ID_STR).into(),
("refund_receiver", refund_receiver).into(),
("refund_denom", refund_denom).into(),
("refund_amount", refund_amount).into(),
Expand All @@ -132,7 +134,6 @@ impl From<DenomTraceEvent> for ModuleEvent {
let DenomTraceEvent { trace_hash, denom } = ev;
let mut ev = Self {
kind: EVENT_TYPE_DENOM_TRACE.to_string(),
module_name: MODULE_ID_STR.parse().expect("invalid ModuleId"),
attributes: vec![("denom", denom).into()],
};
if let Some(hash) = trace_hash {
Expand All @@ -145,15 +146,27 @@ impl From<DenomTraceEvent> for ModuleEvent {
pub struct TransferEvent {
pub sender: Signer,
pub receiver: Signer,
pub amount: Amount,
pub denom: PrefixedDenom,
}

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

Self {
kind: EVENT_TYPE_TRANSFER.to_string(),
module_name: MODULE_ID_STR.parse().expect("invalid ModuleId"),
attributes: vec![("sender", sender).into(), ("receiver", receiver).into()],
attributes: vec![
("sender", sender).into(),
("receiver", receiver).into(),
("amount", amount).into(),
("denom", denom).into(),
],
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions crates/ibc/src/applications/transfer/relay/send_transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use crate::applications::transfer::context::{
};
use crate::applications::transfer::error::TokenTransferError;
use crate::applications::transfer::events::TransferEvent;
use crate::applications::transfer::is_sender_chain_source;
use crate::applications::transfer::msgs::transfer::MsgTransfer;
use crate::applications::transfer::{is_sender_chain_source, MODULE_ID_STR};
use crate::core::ics04_channel::handler::send_packet::{send_packet_execute, send_packet_validate};
use crate::core::ics04_channel::packet::Packet;
use crate::core::ics24_host::path::{ChannelEndPath, SeqSendPath};
use crate::events::ModuleEvent;
use crate::events::{MessageEvent, ModuleEvent};
use crate::prelude::*;

/// This function handles the transfer sending logic.
Expand Down Expand Up @@ -166,8 +166,12 @@ where
let transfer_event = TransferEvent {
sender: msg.packet_data.sender,
receiver: msg.packet_data.receiver,
amount: msg.packet_data.token.amount,
denom: msg.packet_data.token.denom,
};
ctx_a.emit_ibc_event(ModuleEvent::from(transfer_event).into());

ctx_a.emit_ibc_event(MessageEvent::Module(MODULE_ID_STR.to_string()).into());
}

Ok(())
Expand Down
8 changes: 6 additions & 2 deletions crates/ibc/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::core::ics03_connection::events as ConnectionEvents;
use crate::core::ics04_channel::error as channel_error;
use crate::core::ics04_channel::events as ChannelEvents;
use crate::core::ics24_host::error::ValidationError;
use crate::core::ics26_routing::context::ModuleId;
use crate::timestamp::ParseTimestampError;

#[derive(Debug, Display)]
Expand Down Expand Up @@ -198,7 +197,6 @@ impl IbcEvent {
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModuleEvent {
pub kind: String,
pub module_name: ModuleId,
pub attributes: Vec<ModuleEventAttribute>,
}

Expand Down Expand Up @@ -289,6 +287,12 @@ impl MessageEvent {
}
}

impl From<MessageEvent> for IbcEvent {
fn from(e: MessageEvent) -> Self {
IbcEvent::Message(e)
}
}

#[cfg(test)]
pub mod tests {
use super::*;
Expand Down

0 comments on commit 9bb6327

Please sign in to comment.