Skip to content

Commit

Permalink
Fix collections retrieve
Browse files Browse the repository at this point in the history
  • Loading branch information
danielailie committed Jan 18, 2024
1 parent 2421d26 commit df84a08
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
44 changes: 26 additions & 18 deletions src/modules/nftCollections/collections-getter.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Injectable } from '@nestjs/common';
import { orderBy } from 'lodash';
import { Address } from '@multiversx/sdk-core';
import { genericDescriptions } from 'src/config';
import { constants, genericDescriptions } from 'src/config';
import { Collection, CollectionAsset } from './models';
import { CollectionQuery } from './collection-query';
import { CollectionApi, MxApiService, MxIdentityService } from 'src/common';
import { MxApiService, MxElasticService, MxIdentityService } from 'src/common';
import { CacheInfo } from 'src/common/services/caching/entities/cache.info';
import { CollectionsNftsCountRedisHandler } from './collection-nfts-count.redis-handler';
import { CollectionsNftsRedisHandler } from './collection-nfts.redis-handler';
Expand All @@ -17,6 +17,9 @@ import { CollectionNftTrait } from '../nft-traits/models/collection-traits.model
import { DocumentDbService } from 'src/document-db/document-db.service';
import { BlacklistedCollectionsService } from '../blacklist/blacklisted-collections.service';
import { TrendingCollectionsService } from '../analytics/trending/trending-collections.service';
import { ELASTIC_TOKENS_INDEX } from 'src/utils/constants';
import { ElasticQuery, QueryType, ElasticSortOrder } from '@multiversx/sdk-nestjs-elastic';
import { NftTypeEnum } from '../assets/models';

@Injectable()
export class CollectionsGetterService {
Expand All @@ -31,6 +34,7 @@ export class CollectionsGetterService {
private analyticsService: TrendingCollectionsService,
private documentDbService: DocumentDbService,
private blacklistedCollectionsService: BlacklistedCollectionsService,
private elasticService: MxElasticService,

Check warning on line 37 in src/modules/nftCollections/collections-getter.service.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/nftCollections/collections-getter.service.ts#L37

Added line #L37 was not covered by tests
) {}

async getCollections(
Expand Down Expand Up @@ -238,25 +242,19 @@ export class CollectionsGetterService {
}

public async getFullCollectionsRaw(): Promise<[Collection[], number]> {
const size = 25;
let from = 0;

const totalCount = await this.apiService.getCollectionsCount('?type=NonFungibleESDT,SemiFungibleESDT');
const query = this.getCollectionQuery();

Check warning on line 245 in src/modules/nftCollections/collections-getter.service.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/nftCollections/collections-getter.service.ts#L245

Added line #L245 was not covered by tests
let collectionsResponse: Collection[] = [];
do {
let mappedCollections = await this.getMappedCollections(from, size);
await this.elasticService.getScrollableList(ELASTIC_TOKENS_INDEX, 'token', query, async (items) => {
let mappedCollections = await this.getMappedCollections(items);

Check warning on line 248 in src/modules/nftCollections/collections-getter.service.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/nftCollections/collections-getter.service.ts#L247-L248

Added lines #L247 - L248 were not covered by tests

mappedCollections = await this.mapCollectionNftsCount(mappedCollections);

mappedCollections = await this.mapCollectionNfts(mappedCollections);

collectionsResponse.push(...mappedCollections);
});

from = from + size;
} while (from < totalCount && from <= 9975);
let uniqueCollections = [...new Map(collectionsResponse.map((item) => [item.collection, item])).values()];
uniqueCollections = orderBy(uniqueCollections, ['verified', 'creationDate'], ['desc', 'desc']);

return [uniqueCollections, uniqueCollections?.length];
}

Expand Down Expand Up @@ -309,21 +307,20 @@ export class CollectionsGetterService {
return [groupedCollections, groupedCollections.length];
}

private async getMappedCollections(page: number, size: number): Promise<Collection[]> {
const collections = await this.apiService.getCollections(new CollectionQuery().addPageSize(page, size).build());
const promisesCollections = collections?.map((collection): Promise<Collection> => this.mapCollection(collection));
private async getMappedCollections(items): Promise<Collection[]> {
const promisesCollections = items?.map((collection): Promise<Collection> => this.mapCollection(collection));
return await Promise.all(promisesCollections);
}

private async mapCollection(collection: CollectionApi): Promise<Collection> {
private async mapCollection(collection: any): Promise<Collection> {
const ownerAddress = new Address(collection.owner);
if (ownerAddress.isContractAddress()) {
const artist = await this.smartContractArtistService.getOrSetArtistForScAddress(collection.owner);
const followersCount = await this.idService.getFollowersCount(artist?.value?.owner);
return Collection.fromCollectionApi(collection, artist?.value?.owner, followersCount?.count);
return Collection.fromCollectionElastic(collection, artist?.value?.owner, followersCount?.count);
}
const followersCount = await this.idService.getFollowersCount(collection.owner);
return Collection.fromCollectionApi(collection, collection.owner, followersCount?.count);
return Collection.fromCollectionElastic(collection, collection.owner, followersCount?.count);
}

private async mapCollectionNfts(localCollections: Collection[]) {
Expand Down Expand Up @@ -422,4 +419,15 @@ export class CollectionsGetterService {
}
return CollectionNftTrait.fromCollectionTraits(traitSummary.traitTypes);
}

private getCollectionQuery(): ElasticQuery {
return ElasticQuery.create()

Check warning on line 424 in src/modules/nftCollections/collections-getter.service.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/nftCollections/collections-getter.service.ts#L424

Added line #L424 was not covered by tests
.withMustNotExistCondition('identifier')
.withMustMultiShouldCondition([NftTypeEnum.NonFungibleESDT, NftTypeEnum.SemiFungibleESDT], (type) => QueryType.Match('type', type))

Check warning on line 426 in src/modules/nftCollections/collections-getter.service.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/nftCollections/collections-getter.service.ts#L426

Added line #L426 was not covered by tests
.withPagination({ from: 0, size: constants.getCollectionsFromElasticBatchSize })
.withSort([
{ name: 'api_isVerified', order: ElasticSortOrder.descending },
{ name: 'timestamp', order: ElasticSortOrder.descending },
]);
}
}
23 changes: 23 additions & 0 deletions src/modules/nftCollections/models/Collection.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,29 @@ export class Collection {
scamInfo: ScamInfo.fromScamInfoApi(collectionApi.scamInfo),
});
}

static fromCollectionElastic(collectionElastic: any, artistAddress?: string, followersCount?: number) {
if (!collectionElastic) return null;
return new Collection({

Check warning on line 127 in src/modules/nftCollections/models/Collection.dto.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/nftCollections/models/Collection.dto.ts#L127

Added line #L127 was not covered by tests
collection: collectionElastic.token,
artistAddress: artistAddress,
type: NftTypeEnum[collectionElastic.type],
ticker: collectionElastic.ticker,
ownerAddress: collectionElastic.currentOwner,
creationDate: collectionElastic.timestamp,
name: collectionElastic.name,
canTransferRole: collectionElastic.properties.canTransferNFTCreateRole,
canPause: collectionElastic.properties.canPause,
canBurn: collectionElastic.properties.canBurn,
canFreeze: collectionElastic.properties.canFreeze,
canWipe: collectionElastic.properties.canWipe,
canAddQuantity: collectionElastic.properties.canAddQuantity,
canCreate: collectionElastic.properties.canMint,
artistFollowersCount: followersCount,
nftsCount: collectionElastic.api_nftCount,
verified: collectionElastic.api_isVerified ?? 0,
});
}
}

@ObjectType()
Expand Down

0 comments on commit df84a08

Please sign in to comment.