Skip to content

Commit

Permalink
chore: implement required FungibleTokenPacketData v3 interface meth…
Browse files Browse the repository at this point in the history
…ods (#6126)
  • Loading branch information
charleenfei authored Apr 22, 2024
1 parent 28ff9b6 commit 4e55137
Show file tree
Hide file tree
Showing 6 changed files with 736 additions and 21 deletions.
22 changes: 22 additions & 0 deletions modules/apps/transfer/internal/denom/denom.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package denom

import (
"fmt"
"strings"

errorsmod "cosmossdk.io/errors"

channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"
)

// ExtractPathAndBaseFromFullDenom returns the trace path and the base denom from
Expand Down Expand Up @@ -37,3 +41,21 @@ func ExtractPathAndBaseFromFullDenom(fullDenomItems []string) ([]string, string)

return pathSlice, baseDenom
}

// ValidateTraceIdentifiers validates the correctness of the trace associated with a particular base denom.
func ValidateTraceIdentifiers(identifiers []string) error {
if len(identifiers) == 0 || len(identifiers)%2 != 0 {
return fmt.Errorf("trace info must come in pairs of port and channel identifiers '{portID}/{channelID}', got the identifiers: %s", identifiers)
}

// validate correctness of port and channel identifiers
for i := 0; i < len(identifiers); i += 2 {
if err := host.PortIdentifierValidator(identifiers[i]); err != nil {
return errorsmod.Wrapf(err, "invalid port ID at position %d", i)
}
if err := host.ChannelIdentifierValidator(identifiers[i+1]); err != nil {
return errorsmod.Wrapf(err, "invalid channel ID at position %d", i)
}
}
return nil
}
22 changes: 2 additions & 20 deletions modules/apps/transfer/types/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
cmttypes "github.com/cometbft/cometbft/types"

denominternal "github.com/cosmos/ibc-go/v8/modules/apps/transfer/internal/denom"
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"
)

// ParseDenomTrace parses a string with the ibc prefix (denom trace) and the base denomination
Expand Down Expand Up @@ -82,23 +81,6 @@ func (dt DenomTrace) IsNativeDenom() bool {
return dt.Path == ""
}

func validateTraceIdentifiers(identifiers []string) error {
if len(identifiers) == 0 || len(identifiers)%2 != 0 {
return fmt.Errorf("trace info must come in pairs of port and channel identifiers '{portID}/{channelID}', got the identifiers: %s", identifiers)
}

// validate correctness of port and channel identifiers
for i := 0; i < len(identifiers); i += 2 {
if err := host.PortIdentifierValidator(identifiers[i]); err != nil {
return errorsmod.Wrapf(err, "invalid port ID at position %d", i)
}
if err := host.ChannelIdentifierValidator(identifiers[i+1]); err != nil {
return errorsmod.Wrapf(err, "invalid channel ID at position %d", i)
}
}
return nil
}

// Validate performs a basic validation of the DenomTrace fields.
func (dt DenomTrace) Validate() error {
// empty trace is accepted when token lives on the original chain
Expand All @@ -112,7 +94,7 @@ func (dt DenomTrace) Validate() error {
// NOTE: no base denomination validation

identifiers := strings.Split(dt.Path, "/")
return validateTraceIdentifiers(identifiers)
return denominternal.ValidateTraceIdentifiers(identifiers)
}

// Traces defines a wrapper type for a slice of DenomTrace.
Expand Down Expand Up @@ -178,7 +160,7 @@ func ValidatePrefixedDenom(denom string) error {
}

identifiers := strings.Split(path, "/")
return validateTraceIdentifiers(identifiers)
return denominternal.ValidateTraceIdentifiers(identifiers)
}

// ValidateIBCDenom validates that the given denomination is either:
Expand Down
101 changes: 100 additions & 1 deletion modules/apps/transfer/types/v3/packet.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
package v3

// NewFungibleTokenPacketData constructs a new FungibleTokenPacketData instance
import (
"encoding/json"
"errors"
"strings"

errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"

"github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
ibcerrors "github.com/cosmos/ibc-go/v8/modules/core/errors"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
)

var (
_ ibcexported.PacketData = (*FungibleTokenPacketData)(nil)
_ ibcexported.PacketDataProvider = (*FungibleTokenPacketData)(nil)
)

// NewFungibleTokenPacketData constructs a new NewFungibleTokenPacketData instance
func NewFungibleTokenPacketData(
tokens []*Token,
sender, receiver string,
Expand All @@ -13,3 +31,84 @@ func NewFungibleTokenPacketData(
Memo: memo,
}
}

// ValidateBasic is used for validating the token transfer.
// NOTE: The addresses formats are not validated as the sender and recipient can have different
// formats defined by their corresponding chains that are not known to IBC.
func (ftpd FungibleTokenPacketData) ValidateBasic() error {
if strings.TrimSpace(ftpd.Sender) == "" {
return errorsmod.Wrap(ibcerrors.ErrInvalidAddress, "sender address cannot be blank")
}

if strings.TrimSpace(ftpd.Receiver) == "" {
return errorsmod.Wrap(ibcerrors.ErrInvalidAddress, "receiver address cannot be blank")
}

if len(ftpd.Tokens) == 0 {
return errorsmod.Wrap(types.ErrInvalidAmount, "tokens cannot be empty")
}

for _, token := range ftpd.Tokens {
amount, ok := sdkmath.NewIntFromString(token.Amount)
if !ok {
return errorsmod.Wrapf(types.ErrInvalidAmount, "unable to parse transfer amount (%s) into math.Int", token.Amount)
}

if !amount.IsPositive() {
return errorsmod.Wrapf(types.ErrInvalidAmount, "amount must be strictly positive: got %d", amount)
}

if err := token.Validate(); err != nil {
return err
}
}

if len(ftpd.Memo) > types.MaximumMemoLength {
return errorsmod.Wrapf(types.ErrInvalidMemo, "memo must not exceed %d bytes", types.MaximumMemoLength)
}

return nil
}

// GetBytes is a helper for serialising
func (ftpd FungibleTokenPacketData) GetBytes() []byte {
bz, err := json.Marshal(&ftpd)
if err != nil {
panic(errors.New("cannot marshal v3 FungibleTokenPacketData into bytes"))
}

return bz
}

// GetCustomPacketData interprets the memo field of the packet data as a JSON object
// and returns the value associated with the given key.
// If the key is missing or the memo is not properly formatted, then nil is returned.
func (ftpd FungibleTokenPacketData) GetCustomPacketData(key string) interface{} {
if len(ftpd.Memo) == 0 {
return nil
}

jsonObject := make(map[string]interface{})
err := json.Unmarshal([]byte(ftpd.Memo), &jsonObject)
if err != nil {
return nil
}

memoData, found := jsonObject[key]
if !found {
return nil
}

return memoData
}

// GetPacketSender returns the sender address embedded in the packet data.
//
// NOTE:
// - The sender address is set by the module which requested the packet to be sent,
// and this module may not have validated the sender address by a signature check.
// - The sender address must only be used by modules on the sending chain.
// - sourcePortID is not used in this implementation.
func (ftpd FungibleTokenPacketData) GetPacketSender(sourcePortID string) string {
return ftpd.Sender
}
Loading

0 comments on commit 4e55137

Please sign in to comment.