Skip to content

Commit

Permalink
chore: make UnmarshalPacketData public (#6897)
Browse files Browse the repository at this point in the history
  • Loading branch information
gjermundgaraba authored Jul 22, 2024
1 parent a6fd4d7 commit bb9d12c
Show file tree
Hide file tree
Showing 5 changed files with 291 additions and 319 deletions.
3 changes: 1 addition & 2 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"

capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
"github.com/cosmos/ibc-go/v9/modules/apps/transfer/internal"
"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"
Expand Down Expand Up @@ -322,5 +321,5 @@ func (im IBCModule) getICS20PacketData(ctx sdk.Context, packetData []byte, portI
return types.FungibleTokenPacketDataV2{}, errorsmod.Wrapf(ibcerrors.ErrNotFound, "app version not found for port %s and channel %s", portID, channelID)
}

return internal.UnmarshalPacketData(packetData, ics20Version)
return types.UnmarshalPacketData(packetData, ics20Version)
}
67 changes: 0 additions & 67 deletions modules/apps/transfer/internal/packet.go

This file was deleted.

250 changes: 0 additions & 250 deletions modules/apps/transfer/internal/packet_test.go

This file was deleted.

55 changes: 55 additions & 0 deletions modules/apps/transfer/types/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/codec/unknownproto"

ibcerrors "github.com/cosmos/ibc-go/v9/modules/core/errors"
ibcexported "github.com/cosmos/ibc-go/v9/modules/core/exported"
)
Expand Down Expand Up @@ -204,3 +206,56 @@ func (ftpd FungibleTokenPacketDataV2) GetPacketSender(sourcePortID string) strin
func (ftpd FungibleTokenPacketDataV2) HasForwarding() bool {
return len(ftpd.Forwarding.Hops) > 0
}

// UnmarshalPacketData attempts to unmarshal the provided packet data bytes into a FungibleTokenPacketDataV2.
// The version of ics20 should be provided and should be either ics20-1 or ics20-2.
func UnmarshalPacketData(bz []byte, ics20Version string) (FungibleTokenPacketDataV2, error) {
switch ics20Version {
case V1:
var datav1 FungibleTokenPacketData
if err := json.Unmarshal(bz, &datav1); err != nil {
return FungibleTokenPacketDataV2{}, errorsmod.Wrapf(ibcerrors.ErrInvalidType, "cannot unmarshal ICS20-V1 transfer packet data: %s", err.Error())
}

return PacketDataV1ToV2(datav1)
case V2:
var datav2 FungibleTokenPacketDataV2
if err := unknownproto.RejectUnknownFieldsStrict(bz, &datav2, unknownproto.DefaultAnyResolver{}); err != nil {
return FungibleTokenPacketDataV2{}, errorsmod.Wrapf(ibcerrors.ErrInvalidType, "cannot unmarshal ICS20-V2 transfer packet data: %s", err.Error())
}

if err := proto.Unmarshal(bz, &datav2); err != nil {
return FungibleTokenPacketDataV2{}, errorsmod.Wrapf(ibcerrors.ErrInvalidType, "cannot unmarshal ICS20-V2 transfer packet data: %s", err.Error())
}

if err := datav2.ValidateBasic(); err != nil {
return FungibleTokenPacketDataV2{}, err
}

return datav2, nil
default:
return FungibleTokenPacketDataV2{}, errorsmod.Wrap(ErrInvalidVersion, ics20Version)
}
}

// PacketDataV1ToV2 converts a v1 packet data to a v2 packet data. The packet data is validated
// before conversion.
func PacketDataV1ToV2(packetData FungibleTokenPacketData) (FungibleTokenPacketDataV2, error) {
if err := packetData.ValidateBasic(); err != nil {
return FungibleTokenPacketDataV2{}, errorsmod.Wrapf(err, "invalid packet data")
}

denom := ExtractDenomFromPath(packetData.Denom)
return FungibleTokenPacketDataV2{
Tokens: []Token{
{
Denom: denom,
Amount: packetData.Amount,
},
},
Sender: packetData.Sender,
Receiver: packetData.Receiver,
Memo: packetData.Memo,
Forwarding: ForwardingPacketData{},
}, nil
}
Loading

0 comments on commit bb9d12c

Please sign in to comment.