fix(activity)_: Remove duplicated ids for nft fetching

This commit is contained in:
Emil Sawicki 2024-07-14 18:34:43 +02:00 committed by Stefan Dunca
parent 89f1feade0
commit eac07af5d7
2 changed files with 50 additions and 19 deletions

View File

@ -217,7 +217,7 @@ func (s *Service) getActivityDetails(ctx context.Context, entries []Entry) ([]*E
res := make([]*EntryData, 0)
var err error
ids := make([]thirdparty.CollectibleUniqueID, 0)
entriesForIds := make([]*Entry, 0)
entriesForIds := make(map[string][]*Entry)
idExists := func(ids []thirdparty.CollectibleUniqueID, id *thirdparty.CollectibleUniqueID) bool {
for _, existingID := range ids {
@ -234,12 +234,14 @@ func (s *Service) getActivityDetails(ctx context.Context, entries []Entry) ([]*E
}
id := entries[i].anyIdentity()
if id == nil || idExists(ids, id) {
if id == nil {
continue
}
entriesForIds[id.HashKey()] = append(entriesForIds[id.HashKey()], &entries[i])
if !idExists(ids, id) {
ids = append(ids, *id)
entriesForIds = append(entriesForIds, &entries[i])
}
}
if len(ids) == 0 {
@ -255,25 +257,33 @@ func (s *Service) getActivityDetails(ctx context.Context, entries []Entry) ([]*E
}
for _, col := range colData {
data := &EntryData{
NftName: w_common.NewAndSet(col.CollectibleData.Name),
NftURL: w_common.NewAndSet(col.CollectibleData.ImageURL),
}
nftName := w_common.NewAndSet(col.CollectibleData.Name)
nftURL := w_common.NewAndSet(col.CollectibleData.ImageURL)
for i := range ids {
if col.CollectibleData.ID.Same(&ids[i]) {
if entriesForIds[i].payloadType == MultiTransactionPT {
data.ID = w_common.NewAndSet(entriesForIds[i].id)
if !col.CollectibleData.ID.Same(&ids[i]) {
continue
}
entryList, ok := entriesForIds[ids[i].HashKey()]
if !ok {
continue
}
for _, e := range entryList {
data := &EntryData{
NftName: nftName,
NftURL: nftURL,
}
if e.payloadType == MultiTransactionPT {
data.ID = w_common.NewAndSet(e.id)
} else {
data.Transaction = entriesForIds[i].transaction
}
data.PayloadType = entriesForIds[i].payloadType
}
data.Transaction = e.transaction
}
data.PayloadType = e.payloadType
res = append(res, data)
}
}
}
return res, nil
}

View File

@ -175,17 +175,35 @@ func TestService_UpdateCollectibleInfo(t *testing.T) {
}).Return([]thirdparty.FullCollectibleData{
{
CollectibleData: thirdparty.CollectibleData{
ID: thirdparty.CollectibleUniqueID{
ContractID: thirdparty.ContractID{
ChainID: args[4].chainID,
Address: *args[4].tokenAddress},
TokenID: &bigint.BigInt{Int: args[4].tokenID},
},
Name: "Test 4",
ImageURL: "test://url/4"},
CollectionData: nil,
}, {
CollectibleData: thirdparty.CollectibleData{
ID: thirdparty.CollectibleUniqueID{
ContractID: thirdparty.ContractID{
ChainID: args[1].chainID,
Address: *args[1].tokenAddress},
TokenID: &bigint.BigInt{Int: args[1].tokenID},
},
Name: "Test 1",
ImageURL: "test://url/1"},
CollectionData: nil,
},
{
CollectibleData: thirdparty.CollectibleData{
ID: thirdparty.CollectibleUniqueID{
ContractID: thirdparty.ContractID{
ChainID: args[0].chainID,
Address: *args[0].tokenAddress},
TokenID: &bigint.BigInt{Int: args[0].tokenID},
},
Name: "Test 0",
ImageURL: "test://url/0"},
CollectionData: nil,
@ -216,14 +234,17 @@ func TestService_UpdateCollectibleInfo(t *testing.T) {
}
}
// FetchAssetsByCollectibleUniqueID will receive only unique ids, while number of entries can be bigger
require.Equal(t, 1, filterResponseCount)
require.Equal(t, 3, len(updates))
require.Equal(t, 4, len(updates))
require.Equal(t, "Test 4", *updates[0].NftName)
require.Equal(t, "test://url/4", *updates[0].NftURL)
require.Equal(t, "Test 1", *updates[1].NftName)
require.Equal(t, "test://url/1", *updates[1].NftURL)
require.Equal(t, "Test 0", *updates[2].NftName)
require.Equal(t, "test://url/0", *updates[2].NftURL)
require.Equal(t, "Test 1", *updates[2].NftName)
require.Equal(t, "test://url/1", *updates[2].NftURL)
require.Equal(t, "Test 0", *updates[3].NftName)
require.Equal(t, "test://url/0", *updates[3].NftURL)
sub.Unsubscribe()
}