Skip to content

Commit

Permalink
Merge pull request #6647 from multiversx/fix-api-nfts-tokens-type
Browse files Browse the repository at this point in the history
Fix get non fungible tokens list
  • Loading branch information
sstanculeanu authored Dec 5, 2024
2 parents 7c4667b + 04fd98d commit e65a3d4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion api/groups/networkGroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func NewNetworkGroup(facade networkFacadeHandler) (*networkGroup, error) {
{
Path: getNFTsPath,
Method: http.MethodGet,
Handler: ng.getHandlerFuncForEsdt(core.NonFungibleESDT),
Handler: ng.getHandlerFuncForEsdt(core.NonFungibleESDTv2),
},
{
Path: directStakedInfoPath,
Expand Down
15 changes: 14 additions & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func (n *Node) GetAllIssuedESDTs(tokenType string, ctx context.Context) ([]strin
continue
}

if bytes.Equal(esdtToken.TokenType, []byte(tokenType)) {
if tokenTypeEquals(esdtToken.TokenType, tokenType) {
tokens = append(tokens, tokenName)
}
}
Expand All @@ -268,6 +268,19 @@ func (n *Node) GetAllIssuedESDTs(tokenType string, ctx context.Context) ([]strin
return tokens, nil
}

func tokenTypeEquals(tokenType []byte, providedTokenType string) bool {
if providedTokenType == core.NonFungibleESDTv2 ||
providedTokenType == core.NonFungibleESDT {
return bytes.Equal(tokenType, []byte(core.NonFungibleESDTv2)) || bytes.Equal(tokenType, []byte(core.NonFungibleESDT)) || bytes.Equal(tokenType, []byte(core.DynamicNFTESDT))
}

if providedTokenType == core.SemiFungibleESDT {
return bytes.Equal(tokenType, []byte(core.SemiFungibleESDT)) || bytes.Equal(tokenType, []byte(core.DynamicSFTESDT))
}

return bytes.Equal(tokenType, []byte(providedTokenType))
}

func (n *Node) getEsdtDataFromLeaf(leaf core.KeyValueHolder) (*systemSmartContracts.ESDTDataV2, bool) {
esdtToken := &systemSmartContracts.ESDTDataV2{}

Expand Down
41 changes: 38 additions & 3 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,10 @@ func TestNode_GetAllIssuedESDTs(t *testing.T) {
acc := createAcc([]byte("newaddress"))
esdtToken := []byte("TCK-RANDOM")
sftToken := []byte("SFT-RANDOM")
sftTokenDynamic := []byte("SFT-Dynamic")
nftToken := []byte("NFT-RANDOM")
nftTokenV2 := []byte("NFT-RANDOM-V2")
nftTokenDynamic := []byte("NFT-Dynamic")

esdtData := &systemSmartContracts.ESDTDataV2{TokenName: []byte("fungible"), TokenType: []byte(core.FungibleESDT)}
marshalledData, _ := getMarshalizer().Marshal(esdtData)
Expand All @@ -1169,13 +1172,28 @@ func TestNode_GetAllIssuedESDTs(t *testing.T) {
sftMarshalledData, _ := getMarshalizer().Marshal(sftData)
_ = acc.SaveKeyValue(sftToken, sftMarshalledData)

sftDataDynamic := &systemSmartContracts.ESDTDataV2{TokenName: []byte("semi fungible dynamic"), TokenType: []byte(core.DynamicSFTESDT)}
sftMarshalledDataDynamic, _ := getMarshalizer().Marshal(sftDataDynamic)
_ = acc.SaveKeyValue(sftTokenDynamic, sftMarshalledDataDynamic)

nftData := &systemSmartContracts.ESDTDataV2{TokenName: []byte("non fungible"), TokenType: []byte(core.NonFungibleESDT)}
nftMarshalledData, _ := getMarshalizer().Marshal(nftData)
_ = acc.SaveKeyValue(nftToken, nftMarshalledData)

nftData2 := &systemSmartContracts.ESDTDataV2{TokenName: []byte("non fungible v2"), TokenType: []byte(core.NonFungibleESDT)}
nftMarshalledData2, _ := getMarshalizer().Marshal(nftData2)
_ = acc.SaveKeyValue(nftTokenV2, nftMarshalledData2)

nftDataDynamic := &systemSmartContracts.ESDTDataV2{TokenName: []byte("non fungible dynamic"), TokenType: []byte(core.DynamicNFTESDT)}
nftMarshalledDataDyamic, _ := getMarshalizer().Marshal(nftDataDynamic)
_ = acc.SaveKeyValue(nftTokenDynamic, nftMarshalledDataDyamic)

esdtSuffix := append(esdtToken, acc.AddressBytes()...)
nftSuffix := append(nftToken, acc.AddressBytes()...)
nftSuffix2 := append(nftTokenV2, acc.AddressBytes()...)
nftDynamicSuffix := append(nftTokenDynamic, acc.AddressBytes()...)
sftSuffix := append(sftToken, acc.AddressBytes()...)
sftDynamicSuffix := append(sftTokenDynamic, acc.AddressBytes()...)

acc.SetDataTrie(
&trieMock.TrieStub{
Expand All @@ -1186,9 +1204,16 @@ func TestNode_GetAllIssuedESDTs(t *testing.T) {

trieLeaf, _ = tlp.ParseLeaf(sftToken, append(sftMarshalledData, sftSuffix...), core.NotSpecified)
leavesChannels.LeavesChan <- trieLeaf
trieLeaf, _ = tlp.ParseLeaf(sftTokenDynamic, append(sftMarshalledDataDynamic, sftDynamicSuffix...), core.NotSpecified)
leavesChannels.LeavesChan <- trieLeaf

trieLeaf, _ = tlp.ParseLeaf(nftToken, append(nftMarshalledData, nftSuffix...), core.NotSpecified)
leavesChannels.LeavesChan <- trieLeaf
trieLeaf, _ = tlp.ParseLeaf(nftTokenV2, append(nftMarshalledData2, nftSuffix2...), core.NotSpecified)
leavesChannels.LeavesChan <- trieLeaf
trieLeaf, _ = tlp.ParseLeaf(nftTokenDynamic, append(nftMarshalledDataDyamic, nftDynamicSuffix...), core.NotSpecified)
leavesChannels.LeavesChan <- trieLeaf

close(leavesChannels.LeavesChan)
leavesChannels.ErrChan.Close()
}()
Expand Down Expand Up @@ -1236,17 +1261,27 @@ func TestNode_GetAllIssuedESDTs(t *testing.T) {

value, err = n.GetAllIssuedESDTs(core.SemiFungibleESDT, context.Background())
assert.Nil(t, err)
assert.Equal(t, 1, len(value))
assert.Equal(t, 2, len(value))
assert.Equal(t, string(sftToken), value[0])
assert.Equal(t, string(sftTokenDynamic), value[1])

value, err = n.GetAllIssuedESDTs(core.NonFungibleESDT, context.Background())
assert.Nil(t, err)
assert.Equal(t, 1, len(value))
assert.Equal(t, 3, len(value)) // for both versions
assert.Equal(t, string(nftToken), value[0])
assert.Equal(t, string(nftTokenV2), value[1])
assert.Equal(t, string(nftTokenDynamic), value[2])

value, err = n.GetAllIssuedESDTs(core.NonFungibleESDTv2, context.Background())
assert.Nil(t, err)
assert.Equal(t, 3, len(value)) // for both versions
assert.Equal(t, string(nftToken), value[0])
assert.Equal(t, string(nftTokenV2), value[1])
assert.Equal(t, string(nftTokenDynamic), value[2])

value, err = n.GetAllIssuedESDTs("", context.Background())
assert.Nil(t, err)
assert.Equal(t, 3, len(value))
assert.Equal(t, 6, len(value))
}

func TestNode_GetESDTsWithRole(t *testing.T) {
Expand Down

0 comments on commit e65a3d4

Please sign in to comment.