|
| 1 | +package keeper |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "cosmossdk.io/core/event" |
| 9 | + |
| 10 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 11 | + |
| 12 | + "github.com/cosmos/ibc-go/v9/modules/apps/transfer/types" |
| 13 | + channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types" |
| 14 | +) |
| 15 | + |
| 16 | +// EmitTransferEvent emits a ibc transfer event on successful transfers. |
| 17 | +func (k Keeper) EmitTransferEvent(ctx context.Context, sender, receiver string, tokens types.Tokens, memo string, forwardingHops []types.Hop) error { |
| 18 | + tokensStr := mustMarshalJSON(tokens) |
| 19 | + forwardingHopsStr := mustMarshalJSON(forwardingHops) |
| 20 | + |
| 21 | + if err := k.EventService.EventManager(ctx).EmitKV( |
| 22 | + types.EventTypeTransfer, |
| 23 | + event.NewAttribute(types.AttributeKeySender, sender), |
| 24 | + event.NewAttribute(types.AttributeKeyReceiver, receiver), |
| 25 | + event.NewAttribute(types.AttributeKeyTokens, tokensStr), |
| 26 | + event.NewAttribute(types.AttributeKeyMemo, memo), |
| 27 | + event.NewAttribute(types.AttributeKeyForwardingHops, forwardingHopsStr), |
| 28 | + ); err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + |
| 32 | + return k.EventService.EventManager(ctx).EmitKV( |
| 33 | + sdk.EventTypeMessage, |
| 34 | + event.NewAttribute(sdk.AttributeKeyModule, types.ModuleName), |
| 35 | + ) |
| 36 | +} |
| 37 | + |
| 38 | +// EmitOnRecvPacketEvent emits a fungible token packet event in the OnRecvPacket callback |
| 39 | +func (k Keeper) EmitOnRecvPacketEvent(ctx context.Context, packetData types.FungibleTokenPacketDataV2, ack channeltypes.Acknowledgement, ackErr error) error { |
| 40 | + tokensStr := mustMarshalJSON(packetData.Tokens) |
| 41 | + forwardingHopStr := mustMarshalJSON(packetData.Forwarding.Hops) |
| 42 | + |
| 43 | + eventAttributes := []event.Attribute{ |
| 44 | + event.NewAttribute(types.AttributeKeySender, packetData.Sender), |
| 45 | + event.NewAttribute(types.AttributeKeyReceiver, packetData.Receiver), |
| 46 | + event.NewAttribute(types.AttributeKeyTokens, tokensStr), |
| 47 | + event.NewAttribute(types.AttributeKeyMemo, packetData.Memo), |
| 48 | + event.NewAttribute(types.AttributeKeyForwardingHops, forwardingHopStr), |
| 49 | + event.NewAttribute(types.AttributeKeyAckSuccess, strconv.FormatBool(ack.Success())), |
| 50 | + } |
| 51 | + |
| 52 | + if ackErr != nil { |
| 53 | + eventAttributes = append(eventAttributes, event.NewAttribute(types.AttributeKeyAckError, ackErr.Error())) |
| 54 | + } |
| 55 | + |
| 56 | + if err := k.EventService.EventManager(ctx).EmitKV( |
| 57 | + types.EventTypePacket, |
| 58 | + eventAttributes..., |
| 59 | + ); err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + return k.EventService.EventManager(ctx).EmitKV( |
| 64 | + sdk.EventTypeMessage, |
| 65 | + event.NewAttribute(sdk.AttributeKeyModule, types.ModuleName), |
| 66 | + ) |
| 67 | +} |
| 68 | + |
| 69 | +// EmitOnAcknowledgementPacketEvent emits a fungible token packet event in the OnAcknowledgementPacket callback |
| 70 | +func (k Keeper) EmitOnAcknowledgementPacketEvent(ctx context.Context, packetData types.FungibleTokenPacketDataV2, ack channeltypes.Acknowledgement) error { |
| 71 | + tokensStr := mustMarshalJSON(packetData.Tokens) |
| 72 | + forwardingHopsStr := mustMarshalJSON(packetData.Forwarding.Hops) |
| 73 | + |
| 74 | + if err := k.EventService.EventManager(ctx).EmitKV( |
| 75 | + types.EventTypePacket, |
| 76 | + event.NewAttribute(sdk.AttributeKeySender, packetData.Sender), |
| 77 | + event.NewAttribute(types.AttributeKeyReceiver, packetData.Receiver), |
| 78 | + event.NewAttribute(types.AttributeKeyTokens, tokensStr), |
| 79 | + event.NewAttribute(types.AttributeKeyMemo, packetData.Memo), |
| 80 | + event.NewAttribute(types.AttributeKeyForwardingHops, forwardingHopsStr), |
| 81 | + event.NewAttribute(types.AttributeKeyAck, ack.String()), |
| 82 | + ); err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + |
| 86 | + switch resp := ack.Response.(type) { |
| 87 | + case *channeltypes.Acknowledgement_Result: |
| 88 | + if err := k.EventService.EventManager(ctx).EmitKV( |
| 89 | + types.EventTypePacket, |
| 90 | + event.NewAttribute(types.AttributeKeyAckSuccess, string(resp.Result)), |
| 91 | + ); err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + case *channeltypes.Acknowledgement_Error: |
| 95 | + if err := k.EventService.EventManager(ctx).EmitKV( |
| 96 | + types.EventTypePacket, |
| 97 | + event.NewAttribute(types.AttributeKeyAckError, resp.Error), |
| 98 | + ); err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return k.EventService.EventManager(ctx).EmitKV( |
| 104 | + sdk.EventTypeMessage, |
| 105 | + event.NewAttribute(sdk.AttributeKeyModule, types.ModuleName), |
| 106 | + ) |
| 107 | +} |
| 108 | + |
| 109 | +// EmitOnTimeoutEvent emits a fungible token packet event in the OnTimeoutPacket callback |
| 110 | +func (k Keeper) EmitOnTimeoutEvent(ctx context.Context, packetData types.FungibleTokenPacketDataV2) error { |
| 111 | + tokensStr := mustMarshalJSON(packetData.Tokens) |
| 112 | + forwardingHopsStr := mustMarshalJSON(packetData.Forwarding.Hops) |
| 113 | + |
| 114 | + if err := k.EventService.EventManager(ctx).EmitKV( |
| 115 | + types.EventTypeTimeout, |
| 116 | + event.NewAttribute(types.AttributeKeyReceiver, packetData.Sender), |
| 117 | + event.NewAttribute(types.AttributeKeyRefundTokens, tokensStr), |
| 118 | + event.NewAttribute(types.AttributeKeyMemo, packetData.Memo), |
| 119 | + event.NewAttribute(types.AttributeKeyForwardingHops, forwardingHopsStr), |
| 120 | + ); err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + |
| 124 | + return k.EventService.EventManager(ctx).EmitKV( |
| 125 | + sdk.EventTypeMessage, |
| 126 | + event.NewAttribute(sdk.AttributeKeyModule, types.ModuleName), |
| 127 | + ) |
| 128 | +} |
| 129 | + |
| 130 | +// EmitDenomEvent emits a denomination event in the OnRecv callback. |
| 131 | +func (k Keeper) EmitDenomEvent(ctx context.Context, token types.Token) error { |
| 132 | + return k.EventService.EventManager(ctx).EmitKV( |
| 133 | + types.EventTypeDenom, |
| 134 | + event.NewAttribute(types.AttributeKeyDenomHash, token.Denom.Hash().String()), |
| 135 | + event.NewAttribute(types.AttributeKeyDenom, mustMarshalJSON(token.Denom)), |
| 136 | + ) |
| 137 | +} |
| 138 | + |
| 139 | +// mustMarshalJSON json marshals the given type and panics on failure. |
| 140 | +func mustMarshalJSON(v any) string { |
| 141 | + bz, err := json.Marshal(v) |
| 142 | + if err != nil { |
| 143 | + panic(err) |
| 144 | + } |
| 145 | + |
| 146 | + return string(bz) |
| 147 | +} |
0 commit comments