Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ics20 memo handler denom #2439

Merged
merged 5 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions x/uibc/ics20.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,37 @@
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)
}

// ExtractDenomFromPacketOnRecv takes a packet with a valid ICS20 token data in the Data field
// and returns the denom as represented in the local chain.
func ExtractDenomFromPacketOnRecv(packet ibcexported.PacketI, denom string) string {
if ics20types.ReceiverChainIsSource(packet.GetSourcePort(), packet.GetSourceChannel(), denom) {

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

View check run for this annotation

Codecov / codecov/patch

x/uibc/ics20.go#L17-L18

Added lines #L17 - L18 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 23 in x/uibc/ics20.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/ics20.go#L22-L23

Added lines #L22 - L23 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

x/uibc/ics20.go#L26

Added line #L26 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 32 in x/uibc/ics20.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/ics20.go#L30-L32

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

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

View check run for this annotation

Codecov / codecov/patch

x/uibc/ics20.go#L34-L36

Added lines #L34 - L36 were not covered by tests
}
return denom

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

View check run for this annotation

Codecov / codecov/patch

x/uibc/ics20.go#L38

Added line #L38 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 @@

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 @@
// 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.ExtractDenomFromPacketOnRecv(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 @@
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
9 changes: 7 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 @@
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,10 @@
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 warning on line 25 in x/uibc/uics20/memo_handler.go

View check run for this annotation

Codecov / codecov/patch

x/uibc/uics20/memo_handler.go#L25

Added line #L25 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 +40,8 @@
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.ExtractDenomFromPacketOnRecv(packet, ftData.Denom)
return mh.dispatchMemoMsgs(ctx, receiver, sdk.NewCoin(ibcDenom, amount), msgs)

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

View check run for this annotation

Codecov / codecov/patch

x/uibc/uics20/memo_handler.go#L43-L44

Added lines #L43 - L44 were not covered by tests
}

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