From 49976e2d47cf83e8df1ec006f06464fd68337568 Mon Sep 17 00:00:00 2001 From: Devashish Dixit Date: Tue, 29 Nov 2022 10:45:07 +0800 Subject: [PATCH] Problem: `query nft owner` returns error when user has NFTs transferred over IBC (#903) Solution: Fixed denomID and tokenID splitting logic when denom has `ibc`. * Fix lint errors --- x/nft/types/keys.go | 24 ++++++++++++++++++++---- x/nft/types/keys_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 x/nft/types/keys_test.go diff --git a/x/nft/types/keys.go b/x/nft/types/keys.go index 326bf6116..6ddeb6816 100644 --- a/x/nft/types/keys.go +++ b/x/nft/types/keys.go @@ -58,12 +58,28 @@ func SplitKeyOwner(key []byte) (address sdk.AccAddress, denomID, tokenID string, func SplitKeyDenom(key []byte) (denomID, tokenID string, err error) { keys := bytes.Split(key, delimiter) - if len(keys) != 2 { - return denomID, tokenID, errors.New("wrong KeyOwner") + + switch len(keys) { + case 2: + { + denomID = string(keys[0]) + tokenID = string(keys[1]) + } + case 3: + { + if string(keys[0]) == "ibc" { + denomID = "ibc/" + string(keys[1]) + tokenID = string(keys[2]) + } else { + return denomID, tokenID, errors.New("wrong KeyOwner") + } + } + default: + { + return denomID, tokenID, errors.New("wrong KeyOwner") + } } - denomID = string(keys[0]) - tokenID = string(keys[1]) return } diff --git a/x/nft/types/keys_test.go b/x/nft/types/keys_test.go new file mode 100644 index 000000000..783c3d88c --- /dev/null +++ b/x/nft/types/keys_test.go @@ -0,0 +1,30 @@ +package types_test + +import ( + "testing" + + "github.com/crypto-org-chain/chain-main/v4/x/nft/types" + "github.com/stretchr/testify/require" +) + +func TestSplitKeyDenomWithoutIBC(t *testing.T) { + keyDenom := []byte("testdenomid/testtokenid") + + // nolint: govet + denomID, tokenID, err := types.SplitKeyDenom(keyDenom) + + require.NoError(t, err) + require.Equal(t, "testdenomid", denomID) + require.Equal(t, "testtokenid", tokenID) +} + +func TestSplitKeyDenomWithIBC(t *testing.T) { + keyDenom := []byte("ibc/testdenomid/testtokenid") + + // nolint: govet + denomID, tokenID, err := types.SplitKeyDenom(keyDenom) + + require.NoError(t, err) + require.Equal(t, "ibc/testdenomid", denomID) + require.Equal(t, "testtokenid", tokenID) +}