Skip to content

Commit

Permalink
fix: ics20 memo handler denom
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-zaremba committed Feb 27, 2024
1 parent 3531be0 commit 6055b2a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
29 changes: 29 additions & 0 deletions x/uibc/ics20.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,38 @@ package uibc
import (
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/types/tx"
ics20types "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"
)

// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (m ICS20Memo) UnpackInterfaces(unpacker types.AnyUnpacker) error {
return tx.UnpackInterfaces(unpacker, m.Messages)
}

// MustExtractDenomFromPacketOnRecv takes a packet with a valid ICS20 token data in the Data field and returns the
// denom as represented in the local chain.
// If the data cannot be unmarshalled this function will panic
func MustExtractDenomFromPacketOnRecv(packet ibcexported.PacketI, denom string) string {
if ics20types.ReceiverChainIsSource(packet.GetSourcePort(), packet.GetSourceChannel(), denom) {

Check warning on line 19 in x/uibc/ics20.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/ics20.go#L18-L19

Added lines #L18 - L19 were not covered by tests
// if we receive back a token, that was originally sent from UMEE, then we need to remove
// prefix added by the sender chain: port/channel/base_denom -> base_denom.

voucherPrefix := ics20types.GetDenomPrefix(packet.GetSourcePort(), packet.GetSourceChannel())
unprefixedDenom := denom[len(voucherPrefix):]

Check warning on line 24 in x/uibc/ics20.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/ics20.go#L23-L24

Added lines #L23 - L24 were not covered by tests

// coin denomination used in sending from the escrow address
denom = unprefixedDenom

Check warning on line 27 in x/uibc/ics20.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/ics20.go#L27

Added line #L27 was not covered by tests

// The denomination used to send the coins is either the native denom or the hash of the path
// if the denomination is not native.
denomTrace := ics20types.ParseDenomTrace(unprefixedDenom)
if !denomTrace.IsNativeDenom() {
denom = denomTrace.IBCDenom()

Check warning on line 33 in x/uibc/ics20.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/ics20.go#L31-L33

Added lines #L31 - L33 were not covered by tests
}
} else {
prefixedDenom := ics20types.GetDenomPrefix(packet.GetDestPort(), packet.GetDestChannel()) + denom
denom = ics20types.ParseDenomTrace(prefixedDenom).IBCDenom()

Check warning on line 37 in x/uibc/ics20.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/ics20.go#L35-L37

Added lines #L35 - L37 were not covered by tests
}
return denom

Check warning on line 39 in x/uibc/ics20.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/ics20.go#L39

Added line #L39 was not covered by tests
}
26 changes: 1 addition & 25 deletions x/uibc/quota/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
ics20types "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
"github.com/cosmos/ibc-go/v7/modules/core/exported"

Expand Down Expand Up @@ -231,30 +230,7 @@ func (k Keeper) UndoUpdateQuota(denom string, amount sdkmath.Int) error {
// RecordIBCInflow will save the inflow amount if token is registered otherwise it will skip
func (k Keeper) RecordIBCInflow(packet channeltypes.Packet, denom, amount string,
) exported.Acknowledgement {
// if chain is recevier and sender chain is source then we need create ibc_denom (ibc/hash(channel,denom)).
if ics20types.SenderChainIsSource(packet.GetSourcePort(), packet.GetSourceChannel(), denom) {
// SendPacket did not prefix the denom, so we must prefix denom here
// NOTE: sourcePrefix already contains the trailing "/"
sourcePrefix := ics20types.GetDenomPrefix(packet.GetDestPort(), packet.GetDestChannel())
prefixedDenom := sourcePrefix + denom
denom = ics20types.ParseDenomTrace(prefixedDenom).IBCDenom()
} else {
// if we receive back a token, that was originally sent from UMEE, then we need to fetch the native denom
// receive denom(port/channel/base_denom) to base_denom

// remove prefix added by the sender chain
voucherPrefix := ics20types.GetDenomPrefix(packet.GetSourcePort(), packet.GetSourceChannel())
unprefixedDenom := denom[len(voucherPrefix):]
// coin denomination used in sending from the escrow address
denom = unprefixedDenom
// The denomination used to send the coins is either the native denom or the hash of the path
// if the denomination is not native.
denomTrace := ics20types.ParseDenomTrace(unprefixedDenom)
if !denomTrace.IsNativeDenom() {
denom = denomTrace.IBCDenom()
}
}

denom = uibc.MustExtractDenomFromPacketOnRecv(packet, denom)

Check warning on line 233 in x/uibc/quota/quota.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/quota/quota.go#L233

Added line #L233 was not covered by tests
ts, err := k.leverage.GetTokenSettings(*k.ctx, denom)
if err != nil {
if ltypes.ErrNotRegisteredToken.Is(err) {
Expand Down
2 changes: 1 addition & 1 deletion x/uibc/uics20/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (im ICS20Module) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet,
if ftData.Memo != "" {
logger := recvPacketLogger(&ctx)
mh := MemoHandler{im.cdc, im.leverage}
if err := mh.onRecvPacket(&ctx, ftData); err != nil {
if err := mh.onRecvPacket(&ctx, packet, ftData); err != nil {

Check warning on line 60 in x/uibc/uics20/ibc_module.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/uics20/ibc_module.go#L60

Added line #L60 was not covered by tests
logger.Error("can't handle ICS20 memo", "err", err)
}
}
Expand Down
6 changes: 4 additions & 2 deletions x/uibc/uics20/memo_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
ics20types "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"

ltypes "github.com/umee-network/umee/v6/x/leverage/types"
"github.com/umee-network/umee/v6/x/uibc"
Expand All @@ -19,7 +20,7 @@ type MemoHandler struct {
leverage ltypes.MsgServer
}

func (mh MemoHandler) onRecvPacket(ctx *sdk.Context, ftData ics20types.FungibleTokenPacketData) error {
func (mh MemoHandler) onRecvPacket(ctx *sdk.Context, packet ibcexported.PacketI, ftData ics20types.FungibleTokenPacketData) error {

Check failure on line 23 in x/uibc/uics20/memo_handler.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

line is 131 characters (lll)

Check warning on line 23 in x/uibc/uics20/memo_handler.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/uics20/memo_handler.go#L23

Added line #L23 was not covered by tests
msgs, err := deserializeMemoMsgs(mh.cdc, []byte(ftData.Memo))
if err != nil {
recvPacketLogger(ctx).Debug("Can't deserialize ICS20 memo for hook execution", "err", err)
Expand All @@ -36,7 +37,8 @@ func (mh MemoHandler) onRecvPacket(ctx *sdk.Context, ftData ics20types.FungibleT
if !ok {
return fmt.Errorf("can't parse transfer amount: %s [%w]", ftData.Amount, err)
}
return mh.dispatchMemoMsgs(ctx, receiver, sdk.NewCoin(ftData.Denom, amount), msgs)
ibcDenom := uibc.MustExtractDenomFromPacketOnRecv(packet, ftData.Denom)
return mh.dispatchMemoMsgs(ctx, receiver, sdk.NewCoin(ibcDenom, amount), msgs)

Check warning on line 41 in x/uibc/uics20/memo_handler.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/uics20/memo_handler.go#L40-L41

Added lines #L40 - L41 were not covered by tests
}

// runs messages encoded in the ICS20 memo.
Expand Down

0 comments on commit 6055b2a

Please sign in to comment.