@@ -69,44 +69,92 @@ export class NftService {
6969
7070 const nfts = await this . getNftsInternal ( { from, size } , filter ) ;
7171
72- for ( const nft of nfts ) {
73- await this . applyAssetsAndTicker ( nft ) ;
74- }
72+ await Promise . all ( [
73+ this . batchApplyAssetsAndTicker ( nfts ) ,
74+ this . conditionallyApplyOwners ( nfts , queryOptions ) ,
75+ this . conditionallyApplySupply ( nfts , queryOptions ) ,
76+ this . batchProcessNfts ( nfts ) ,
77+ ] ) ;
7578
76- if ( queryOptions && queryOptions . withOwner ) {
77- const nftsIdentifiers = nfts . filter ( x => x . type === NftType . NonFungibleESDT ) . map ( x => x . identifier ) ;
79+ await this . batchApplyUnlockFields ( nfts ) ;
7880
79- const accountsEsdts = await this . getAccountEsdtByIdentifiers ( nftsIdentifiers , { from : 0 , size : nftsIdentifiers . length } ) ;
81+ return nfts ;
82+ }
8083
81- for ( const nft of nfts ) {
82- if ( nft . type === NftType . NonFungibleESDT ) {
83- const accountEsdt = accountsEsdts . find ( ( accountEsdt : any ) => accountEsdt . identifier == nft . identifier ) ;
84- if ( accountEsdt ) {
85- nft . owner = accountEsdt . address ;
86- }
84+ private async batchProcessNfts ( nfts : Nft [ ] , fields ?: string [ ] ) {
85+ await Promise . all ( [
86+ this . batchApplyMedia ( nfts , fields ) ,
87+ this . batchApplyMetadata ( nfts , fields ) ,
88+ ] ) ;
89+ }
90+
91+ private async batchApplyAssetsAndTicker ( nfts : Nft [ ] , fields ?: string [ ] ) : Promise < void > {
92+ if ( fields && fields . includesNone ( [ 'ticker' , 'assets' ] ) ) {
93+ return ;
94+ }
95+
96+ await Promise . all (
97+ nfts . map ( async ( nft ) => {
98+ nft . assets = await this . assetsService . getTokenAssets ( nft . identifier ) ??
99+ await this . assetsService . getTokenAssets ( nft . collection ) ;
100+
101+ if ( nft . assets ) {
102+ nft . ticker = nft . collection . split ( '-' ) [ 0 ] ;
103+ } else {
104+ nft . ticker = nft . collection ;
87105 }
88- }
106+ } )
107+ ) ;
108+ }
109+
110+ private async conditionallyApplyOwners ( nfts : Nft [ ] , queryOptions ?: NftQueryOptions ) : Promise < void > {
111+ if ( ! queryOptions ?. withOwner ) {
112+ return ;
89113 }
90114
91- if ( queryOptions && queryOptions . withSupply ) {
92- const supplyNfts = nfts . filter ( nft => nft . type . in ( NftType . SemiFungibleESDT , NftType . MetaESDT ) ) ;
93- await this . batchApplySupply ( supplyNfts ) ;
115+ const nftsIdentifiers = nfts . filter ( x => x . type === NftType . NonFungibleESDT ) . map ( x => x . identifier ) ;
116+
117+ if ( nftsIdentifiers . length === 0 ) {
118+ return ;
94119 }
95120
96- await this . batchProcessNfts ( nfts ) ;
121+ const accountsEsdts = await this . getAccountEsdtByIdentifiers ( nftsIdentifiers , {
122+ from : 0 ,
123+ size : nftsIdentifiers . length ,
124+ } ) ;
125+
126+ const ownerMap = accountsEsdts . reduce ( ( acc : Record < string , string > , accountEsdt : any ) => {
127+ acc [ accountEsdt . identifier ] = accountEsdt . address ;
128+ return acc ;
129+ } , { } ) ;
97130
98131 for ( const nft of nfts ) {
99- await this . applyUnlockFields ( nft ) ;
132+ if ( nft . type === NftType . NonFungibleESDT && ownerMap [ nft . identifier ] ) {
133+ nft . owner = ownerMap [ nft . identifier ] ;
134+ }
135+ }
136+ }
137+
138+ private async conditionallyApplySupply ( nfts : Nft [ ] , queryOptions ?: NftQueryOptions ) : Promise < void > {
139+ if ( ! queryOptions ?. withSupply ) {
140+ return ;
100141 }
101142
102- return nfts ;
143+ const supplyNfts = nfts . filter ( nft => nft . type . in ( NftType . SemiFungibleESDT , NftType . MetaESDT ) ) ;
144+
145+ if ( supplyNfts . length > 0 ) {
146+ await this . batchApplySupply ( supplyNfts ) ;
147+ }
103148 }
104149
105- private async batchProcessNfts ( nfts : Nft [ ] , fields ?: string [ ] ) {
106- await Promise . all ( [
107- this . batchApplyMedia ( nfts , fields ) ,
108- this . batchApplyMetadata ( nfts , fields ) ,
109- ] ) ;
150+ private async batchApplyUnlockFields ( nfts : Nft [ ] , fields ?: string [ ] ) : Promise < void > {
151+ if ( fields && fields . includesNone ( [ 'unlockSchedule' , 'unlockEpoch' ] ) ) {
152+ return ;
153+ }
154+
155+ await Promise . all (
156+ nfts . map ( nft => this . applyUnlockFields ( nft , fields ) )
157+ ) ;
110158 }
111159
112160 private async applyNftOwner ( nft : Nft ) : Promise < void > {
@@ -219,23 +267,29 @@ export class NftService {
219267 return undefined ;
220268 }
221269
222- if ( nft . type && nft . type . in (
223- NftType . SemiFungibleESDT , NftType . MetaESDT ,
224- NftSubType . DynamicSemiFungibleESDT , NftSubType . DynamicMetaESDT
225- ) ) {
226- await this . applySupply ( nft ) ;
227- }
228-
229- await this . applyNftOwner ( nft ) ;
230-
231- await this . applyNftAttributes ( nft ) ;
270+ const types = [
271+ NftType . SemiFungibleESDT ,
272+ NftType . MetaESDT ,
273+ NftSubType . DynamicSemiFungibleESDT ,
274+ NftSubType . DynamicMetaESDT ,
275+ ] ;
232276
233- await this . applyAssetsAndTicker ( nft ) ;
277+ await Promise . all ( [
278+ ( async ( ) => {
279+ if ( nft . type && types . includes ( nft . type as NftType | NftSubType ) ) {
280+ await this . applySupply ( nft ) ;
281+ }
282+ } ) ( ) ,
283+ this . applyAssetsAndTicker ( nft ) ,
284+ this . processNft ( nft ) ,
285+ ( async ( ) => {
286+ await this . applyNftOwner ( nft ) ;
287+ await this . applyNftAttributes ( nft ) ;
288+ } ) ( ) ,
289+ ] ) ;
234290
235291 await this . applyUnlockFields ( nft ) ;
236292
237- await this . processNft ( nft ) ;
238-
239293 return nft ;
240294 }
241295
0 commit comments