Skip to content

Commit

Permalink
refactor: use event service in transfer mod
Browse files Browse the repository at this point in the history
  • Loading branch information
damiannolan committed Dec 18, 2024
1 parent 4123036 commit 9a150ff
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 160 deletions.
12 changes: 5 additions & 7 deletions modules/apps/transfer/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/ibc-go/v9/modules/apps/transfer/internal/events"
"github.com/cosmos/ibc-go/v9/modules/apps/transfer/keeper"
"github.com/cosmos/ibc-go/v9/modules/apps/transfer/types"
channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
Expand Down Expand Up @@ -181,7 +180,9 @@ func (im IBCModule) OnRecvPacket(
// we are explicitly wrapping this emit event call in an anonymous function so that
// the packet data is evaluated after it has been assigned a value.
defer func() {
events.EmitOnRecvPacketEvent(ctx, data, ack, ackErr)
if err := im.keeper.EmitOnRecvPacketEvent(ctx, data, ack, ackErr); err != nil {
ack = channeltypes.NewErrorAcknowledgement(err)
}
}()

data, ackErr = types.UnmarshalPacketData(packet.GetData(), channelVersion)
Expand Down Expand Up @@ -230,9 +231,7 @@ func (im IBCModule) OnAcknowledgementPacket(
return err
}

events.EmitOnAcknowledgementPacketEvent(ctx, data, ack)

return nil
return im.keeper.EmitOnAcknowledgementPacketEvent(ctx, data, ack)
}

// OnTimeoutPacket implements the IBCModule interface
Expand All @@ -252,8 +251,7 @@ func (im IBCModule) OnTimeoutPacket(
return err
}

events.EmitOnTimeoutEvent(ctx, data)
return nil
return im.keeper.EmitOnTimeoutEvent(ctx, data)
}

// OnChanUpgradeInit implements the IBCModule interface
Expand Down
150 changes: 0 additions & 150 deletions modules/apps/transfer/internal/events/events.go

This file was deleted.

147 changes: 147 additions & 0 deletions modules/apps/transfer/keeper/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package keeper

import (
"context"
"encoding/json"
"strconv"

"cosmossdk.io/core/event"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/ibc-go/v9/modules/apps/transfer/types"
channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
)

// EmitTransferEvent emits a ibc transfer event on successful transfers.
func (k Keeper) EmitTransferEvent(ctx context.Context, sender, receiver string, tokens types.Tokens, memo string, forwardingHops []types.Hop) error {
tokensStr := mustMarshalJSON(tokens)
forwardingHopsStr := mustMarshalJSON(forwardingHops)

if err := k.EventService.EventManager(ctx).EmitKV(
types.EventTypeTransfer,
event.NewAttribute(types.AttributeKeySender, sender),
event.NewAttribute(types.AttributeKeyReceiver, receiver),
event.NewAttribute(types.AttributeKeyTokens, tokensStr),
event.NewAttribute(types.AttributeKeyMemo, memo),
event.NewAttribute(types.AttributeKeyForwardingHops, forwardingHopsStr),
); err != nil {
return err
}

return k.EventService.EventManager(ctx).EmitKV(
sdk.EventTypeMessage,
event.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
)
}

// EmitOnRecvPacketEvent emits a fungible token packet event in the OnRecvPacket callback
func (k Keeper) EmitOnRecvPacketEvent(ctx context.Context, packetData types.FungibleTokenPacketDataV2, ack channeltypes.Acknowledgement, ackErr error) error {
tokensStr := mustMarshalJSON(packetData.Tokens)
forwardingHopStr := mustMarshalJSON(packetData.Forwarding.Hops)

eventAttributes := []event.Attribute{
event.NewAttribute(types.AttributeKeySender, packetData.Sender),
event.NewAttribute(types.AttributeKeyReceiver, packetData.Receiver),
event.NewAttribute(types.AttributeKeyTokens, tokensStr),
event.NewAttribute(types.AttributeKeyMemo, packetData.Memo),
event.NewAttribute(types.AttributeKeyForwardingHops, forwardingHopStr),
event.NewAttribute(types.AttributeKeyAckSuccess, strconv.FormatBool(ack.Success())),
}

if ackErr != nil {
eventAttributes = append(eventAttributes, event.NewAttribute(types.AttributeKeyAckError, ackErr.Error()))
}

if err := k.EventService.EventManager(ctx).EmitKV(
types.EventTypePacket,
eventAttributes...,
); err != nil {
return err
}

return k.EventService.EventManager(ctx).EmitKV(
sdk.EventTypeMessage,
event.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
)
}

// EmitOnAcknowledgementPacketEvent emits a fungible token packet event in the OnAcknowledgementPacket callback
func (k Keeper) EmitOnAcknowledgementPacketEvent(ctx context.Context, packetData types.FungibleTokenPacketDataV2, ack channeltypes.Acknowledgement) error {
tokensStr := mustMarshalJSON(packetData.Tokens)
forwardingHopsStr := mustMarshalJSON(packetData.Forwarding.Hops)

if err := k.EventService.EventManager(ctx).EmitKV(
types.EventTypePacket,
event.NewAttribute(sdk.AttributeKeySender, packetData.Sender),
event.NewAttribute(types.AttributeKeyReceiver, packetData.Receiver),
event.NewAttribute(types.AttributeKeyTokens, tokensStr),
event.NewAttribute(types.AttributeKeyMemo, packetData.Memo),
event.NewAttribute(types.AttributeKeyForwardingHops, forwardingHopsStr),
event.NewAttribute(types.AttributeKeyAck, ack.String()),
); err != nil {
return err
}

switch resp := ack.Response.(type) {
case *channeltypes.Acknowledgement_Result:
if err := k.EventService.EventManager(ctx).EmitKV(
types.EventTypePacket,
event.NewAttribute(types.AttributeKeyAckSuccess, string(resp.Result)),
); err != nil {
return err
}
case *channeltypes.Acknowledgement_Error:
if err := k.EventService.EventManager(ctx).EmitKV(
types.EventTypePacket,
event.NewAttribute(types.AttributeKeyAckError, resp.Error),
); err != nil {
return err
}
}

return k.EventService.EventManager(ctx).EmitKV(
sdk.EventTypeMessage,
event.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
)
}

// EmitOnTimeoutEvent emits a fungible token packet event in the OnTimeoutPacket callback
func (k Keeper) EmitOnTimeoutEvent(ctx context.Context, packetData types.FungibleTokenPacketDataV2) error {
tokensStr := mustMarshalJSON(packetData.Tokens)
forwardingHopsStr := mustMarshalJSON(packetData.Forwarding.Hops)

if err := k.EventService.EventManager(ctx).EmitKV(
types.EventTypeTimeout,
event.NewAttribute(types.AttributeKeyReceiver, packetData.Sender),
event.NewAttribute(types.AttributeKeyRefundTokens, tokensStr),
event.NewAttribute(types.AttributeKeyMemo, packetData.Memo),
event.NewAttribute(types.AttributeKeyForwardingHops, forwardingHopsStr),
); err != nil {
return err
}

return k.EventService.EventManager(ctx).EmitKV(
sdk.EventTypeMessage,
event.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
)
}

// EmitDenomEvent emits a denomination event in the OnRecv callback.
func (k Keeper) EmitDenomEvent(ctx context.Context, token types.Token) error {
return k.EventService.EventManager(ctx).EmitKV(
types.EventTypeDenom,
event.NewAttribute(types.AttributeKeyDenomHash, token.Denom.Hash().String()),
event.NewAttribute(types.AttributeKeyDenom, mustMarshalJSON(token.Denom)),
)
}

// mustMarshalJSON json marshals the given type and panics on failure.
func mustMarshalJSON(v any) string {
bz, err := json.Marshal(v)
if err != nil {
panic(err)
}

return string(bz)
}
9 changes: 6 additions & 3 deletions modules/apps/transfer/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/ibc-go/v9/modules/apps/transfer/internal/events"
"github.com/cosmos/ibc-go/v9/modules/apps/transfer/internal/telemetry"
internaltypes "github.com/cosmos/ibc-go/v9/modules/apps/transfer/internal/types"
"github.com/cosmos/ibc-go/v9/modules/apps/transfer/types"
Expand Down Expand Up @@ -150,7 +149,9 @@ func (k Keeper) sendTransfer(
return 0, err
}

events.EmitTransferEvent(ctx, sender.String(), receiver, tokens, memo, hops)
if err := k.EmitTransferEvent(ctx, sender.String(), receiver, tokens, memo, hops); err != nil {
return 0, err
}

telemetry.ReportTransfer(sourcePort, sourceChannel, destinationPort, destinationChannel, tokens)

Expand Down Expand Up @@ -231,7 +232,9 @@ func (k Keeper) OnRecvPacket(ctx context.Context, packet channeltypes.Packet, da
k.setDenomMetadata(ctx, token.Denom)
}

events.EmitDenomEvent(ctx, token)
if err := k.EmitDenomEvent(ctx, token); err != nil {
return err
}

voucher := sdk.NewCoin(voucherDenom, transferAmount)

Expand Down

0 comments on commit 9a150ff

Please sign in to comment.