@@ -4,33 +4,110 @@ import { Application } from './entities/application';
44import { QueryPagination } from 'src/common/entities/query.pagination' ;
55import { ApplicationFilter } from './entities/application.filter' ;
66import { AssetsService } from '../../common/assets/assets.service' ;
7+ import { GatewayService } from 'src/common/gateway/gateway.service' ;
8+ import { TransferService } from '../transfers/transfer.service' ;
9+ import { TransactionFilter } from '../transactions/entities/transaction.filter' ;
10+ import { TransactionType } from '../transactions/entities/transaction.type' ;
11+ import { Logger } from '@nestjs/common' ;
12+ import { CacheService } from '@multiversx/sdk-nestjs-cache' ;
13+ import { CacheInfo } from 'src/utils/cache.info' ;
714
815@Injectable ( )
916export class ApplicationService {
17+ private readonly logger = new Logger ( ApplicationService . name ) ;
18+
1019 constructor (
1120 private readonly elasticIndexerService : ElasticIndexerService ,
1221 private readonly assetsService : AssetsService ,
22+ private readonly gatewayService : GatewayService ,
23+ private readonly transferService : TransferService ,
24+ private readonly cacheService : CacheService ,
1325 ) { }
1426
1527 async getApplications ( pagination : QueryPagination , filter : ApplicationFilter ) : Promise < Application [ ] > {
28+ filter . validate ( pagination . size ) ;
29+
30+ if ( ! filter . isSet ) {
31+ return await this . cacheService . getOrSet (
32+ CacheInfo . Applications ( pagination ) . key ,
33+ async ( ) => await this . getApplicationsRaw ( pagination , filter ) ,
34+ CacheInfo . Applications ( pagination ) . ttl
35+ ) ;
36+ }
37+
38+ return await this . getApplicationsRaw ( pagination , filter ) ;
39+ }
40+
41+ async getApplicationsRaw ( pagination : QueryPagination , filter : ApplicationFilter ) : Promise < Application [ ] > {
1642 const elasticResults = await this . elasticIndexerService . getApplications ( filter , pagination ) ;
1743 const assets = await this . assetsService . getAllAccountAssets ( ) ;
1844
1945 if ( ! elasticResults ) {
2046 return [ ] ;
2147 }
2248
23- return elasticResults . map ( item => ( {
49+ const applications = elasticResults . map ( item => new Application ( {
2450 contract : item . address ,
2551 deployer : item . deployer ,
2652 owner : item . currentOwner ,
2753 codeHash : item . initialCodeHash ,
2854 timestamp : item . timestamp ,
2955 assets : assets [ item . address ] ,
56+ balance : '0' ,
57+ ...( filter . withTxCount && { txCount : 0 } ) ,
3058 } ) ) ;
59+
60+ const balancePromises = applications . map ( application =>
61+ this . getApplicationBalance ( application . contract )
62+ . then ( balance => { application . balance = balance ; } )
63+ ) ;
64+ await Promise . all ( balancePromises ) ;
65+
66+ if ( filter . withTxCount ) {
67+ for ( const application of applications ) {
68+ application . txCount = await this . getApplicationTxCount ( application . contract ) ;
69+ }
70+ }
71+
72+ return applications ;
3173 }
3274
3375 async getApplicationsCount ( filter : ApplicationFilter ) : Promise < number > {
3476 return await this . elasticIndexerService . getApplicationCount ( filter ) ;
3577 }
78+
79+ async getApplication ( address : string ) : Promise < Application > {
80+ const indexResult = await this . elasticIndexerService . getApplication ( address ) ;
81+ const assets = await this . assetsService . getAllAccountAssets ( ) ;
82+
83+ const result = new Application ( {
84+ contract : indexResult . address ,
85+ deployer : indexResult . deployer ,
86+ owner : indexResult . currentOwner ,
87+ codeHash : indexResult . initialCodeHash ,
88+ timestamp : indexResult . timestamp ,
89+ assets : assets [ address ] ,
90+ balance : '0' ,
91+ txCount : 0 ,
92+ } ) ;
93+
94+ result . txCount = await this . getApplicationTxCount ( result . contract ) ;
95+ result . balance = await this . getApplicationBalance ( result . contract ) ;
96+
97+ return result ;
98+ }
99+
100+ private async getApplicationTxCount ( address : string ) : Promise < number > {
101+ return await this . transferService . getTransfersCount ( new TransactionFilter ( { address, type : TransactionType . Transaction } ) ) ;
102+ }
103+
104+ private async getApplicationBalance ( address : string ) : Promise < string > {
105+ try {
106+ const { account : { balance } } = await this . gatewayService . getAddressDetails ( address ) ;
107+ return balance ;
108+ } catch ( error ) {
109+ this . logger . error ( `Error when getting balance for contract ${ address } ` , error ) ;
110+ return '0' ;
111+ }
112+ }
36113}
0 commit comments