Skip to content

Commit

Permalink
feat(common): added more attributes to rollapp packet delayed ack eve…
Browse files Browse the repository at this point in the history
…nt (#1381)
  • Loading branch information
keruch authored Nov 4, 2024
1 parent fca090d commit c150a43
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
14 changes: 13 additions & 1 deletion x/common/types/events.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
package types

// RollappPacket attributes
const (
// RollappPacket events
AttributeKeyRollappId = "rollapp_id"
AttributeKeyPacketStatus = "status"
AttributeKeyPacketSourcePort = "source_port"
AttributeKeyPacketSourceChannel = "source_channel"
AttributeKeyPacketDestinationPort = "destination_port"
AttributeKeyPacketDestinationChannel = "destination_channel"
AttributeKeyPacketSequence = "packet_sequence"
AttributeKeyPacketProofHeight = "proof_height"
AttributeKeyPacketType = "type"
AttributeKeyPacketAcknowledgement = "acknowledgement"
AttributeKeyPacketError = "error"
)

// FungibleTokenPacketData attributes
const (
AttributeKeyPacketDataDenom = "packet_data_denom"
AttributeKeyPacketDataAmount = "packet_data_amount"
AttributeKeyPacketDataSender = "packet_data_sender"
AttributeKeyPacketDataReceiver = "packet_data_receiver"
AttributeKeyPacketDataMemo = "packet_data_memo"
)
37 changes: 37 additions & 0 deletions x/common/types/rollapp_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

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

func (r RollappPacket) LogString() string {
Expand Down Expand Up @@ -38,6 +39,26 @@ func (r RollappPacket) ValidateBasic() error {
}

func (r RollappPacket) GetEvents() []sdk.Attribute {
var pd transfertypes.FungibleTokenPacketData
if len(r.Packet.Data) != 0 {
// It's okay if we can't get packet data
pd, _ = r.GetTransferPacketData()
}

acknowledgement := "none"
if len(r.Acknowledgement) != 0 {
ack, err := r.GetAck()
// It's okay if we can't get acknowledgement
if err == nil {
switch ack.GetResponse().(type) {
case *channeltypes.Acknowledgement_Result:
acknowledgement = "success"
case *channeltypes.Acknowledgement_Error:
acknowledgement = "error"
}
}
}

eventAttributes := []sdk.Attribute{
sdk.NewAttribute(AttributeKeyRollappId, r.RollappId),
sdk.NewAttribute(AttributeKeyPacketStatus, r.Status.String()),
Expand All @@ -46,6 +67,14 @@ func (r RollappPacket) GetEvents() []sdk.Attribute {
sdk.NewAttribute(AttributeKeyPacketDestinationPort, r.Packet.DestinationPort),
sdk.NewAttribute(AttributeKeyPacketDestinationChannel, r.Packet.DestinationChannel),
sdk.NewAttribute(AttributeKeyPacketSequence, strconv.FormatUint(r.Packet.Sequence, 10)),
sdk.NewAttribute(AttributeKeyPacketProofHeight, strconv.FormatUint(r.ProofHeight, 10)),
sdk.NewAttribute(AttributeKeyPacketType, r.Type.String()),
sdk.NewAttribute(AttributeKeyPacketAcknowledgement, acknowledgement),
sdk.NewAttribute(AttributeKeyPacketDataDenom, pd.Denom),
sdk.NewAttribute(AttributeKeyPacketDataAmount, pd.Amount),
sdk.NewAttribute(AttributeKeyPacketDataSender, pd.Sender),
sdk.NewAttribute(AttributeKeyPacketDataReceiver, pd.Receiver),
sdk.NewAttribute(AttributeKeyPacketDataMemo, pd.Memo),
}
if r.Error != "" {
eventAttributes = append(eventAttributes, sdk.NewAttribute(AttributeKeyPacketError, r.Error))
Expand All @@ -62,6 +91,14 @@ func (r RollappPacket) GetTransferPacketData() (transfertypes.FungibleTokenPacke
return data, nil
}

func (r RollappPacket) GetAck() (channeltypes.Acknowledgement, error) {
var ack channeltypes.Acknowledgement
if err := transfertypes.ModuleCdc.UnmarshalJSON(r.Acknowledgement, &ack); err != nil {
return channeltypes.Acknowledgement{}, err
}
return ack, nil
}

func (r RollappPacket) RestoreOriginalTransferTarget() (RollappPacket, error) {
transferPacketData, err := r.GetTransferPacketData()
if err != nil {
Expand Down

0 comments on commit c150a43

Please sign in to comment.