Skip to content

Commit 12f9d66

Browse files
committed
refactor applications for use accounts index
1 parent baba741 commit 12f9d66

8 files changed

Lines changed: 107 additions & 148 deletions

File tree

src/common/indexer/elastic/elastic.indexer.service.ts

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -969,14 +969,11 @@ export class ElasticIndexerService implements IndexerInterface {
969969
async getApplications(filter: ApplicationFilter, pagination: QueryPagination): Promise<any[]> {
970970
const elasticQuery = this.indexerHelper.buildApplicationFilter(filter)
971971
.withPagination(pagination)
972-
.withFields(['address', 'deployer', 'currentOwner', 'initialCodeHash', 'timestamp'])
973-
.withSort([{ name: 'timestamp', order: ElasticSortOrder.descending }]);
974-
975-
return await this.elasticService.getList('scdeploys', 'address', elasticQuery);
976-
}
972+
.withMustExistCondition('currentOwner')
973+
.withSort([{ name: 'api_transfersLast24h', order: ElasticSortOrder.descending }])
974+
.withFields(['address', 'currentOwner', 'timestamp', 'api_transfersLast24h', 'balance']);
977975

978-
async getApplication(address: string): Promise<any> {
979-
return await this.elasticService.getItem('scdeploys', 'address', address);
976+
return await this.elasticService.getList('accounts', 'address', elasticQuery);
980977
}
981978

982979
async getApplicationCount(filter: ApplicationFilter): Promise<number> {
@@ -985,23 +982,6 @@ export class ElasticIndexerService implements IndexerInterface {
985982
return await this.elasticService.getCount('scdeploys', elasticQuery);
986983
}
987984

988-
async getApplicationsBulkBalance(addresses: string[], pagination: QueryPagination): Promise<Record<string, { address: string, balance: string }>> {
989-
const elasticQuery = ElasticQuery.create()
990-
.withMustMultiShouldCondition(addresses, address => QueryType.Match('address', address, QueryOperator.AND))
991-
.withFields(['address', 'balance', 'balanceNum'])
992-
.withPagination(pagination);
993-
994-
const results = await this.elasticService.getList('accounts', 'address', elasticQuery);
995-
996-
return results.reduce((acc: Record<string, { address: string, balance: string }>, item: any) => {
997-
acc[item.address] = {
998-
address: item.address,
999-
balance: item.balance || '0',
1000-
};
1001-
return acc;
1002-
}, {});
1003-
}
1004-
1005985
async getAddressesWithTransfersLast24h(): Promise<string[]> {
1006986
const elasticQuery = ElasticQuery.create()
1007987
.withFields(['address'])

src/common/indexer/indexer.interface.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,8 @@ export interface IndexerInterface {
186186

187187
getApplications(filter: ApplicationFilter, pagination: QueryPagination): Promise<any[]>
188188

189-
getApplicationsBulkBalance(addresses: string[], pagination: QueryPagination): Promise<Record<string, { address: string, balance: string }>>
190-
191189
getApplicationCount(filter: ApplicationFilter): Promise<number>
192190

193-
getApplication(address: string): Promise<any>
194-
195191
getAddressesWithTransfersLast24h(): Promise<string[]>
196192

197193
getEvents(pagination: QueryPagination, filter: EventsFilter): Promise<Events[]>

src/common/indexer/indexer.service.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -446,15 +446,6 @@ export class IndexerService implements IndexerInterface {
446446
return await this.indexerInterface.getApplications(filter, pagination);
447447
}
448448

449-
@LogPerformanceAsync(MetricsEvents.SetIndexerDuration)
450-
async getApplicationsBulkBalance(addresses: string[], pagination: QueryPagination): Promise<Record<string, { address: string, balance: string }>> {
451-
return await this.indexerInterface.getApplicationsBulkBalance(addresses, pagination);
452-
}
453-
454-
@LogPerformanceAsync(MetricsEvents.SetIndexerDuration)
455-
async getApplication(address: string): Promise<any> {
456-
return await this.indexerInterface.getApplication(address);
457-
}
458449

459450
@LogPerformanceAsync(MetricsEvents.SetIndexerDuration)
460451
async getApplicationCount(filter: ApplicationFilter): Promise<number> {

src/endpoints/applications/application.controller.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Controller, DefaultValuePipe, Get, Param, Query } from "@nestjs/common";
1+
import { Controller, DefaultValuePipe, Get, NotFoundException, Param, Query } from "@nestjs/common";
22
import { ApiOkResponse, ApiOperation, ApiQuery, ApiTags } from "@nestjs/swagger";
33
import { ApplicationService } from "./application.service";
44
import { QueryPagination } from "src/common/entities/query.pagination";
@@ -54,7 +54,11 @@ export class ApplicationController {
5454
@ApiOkResponse({ type: Application })
5555
async getApplication(
5656
@Param('address', ParseAddressPipe) address: string,
57-
): Promise<Application> {
57+
): Promise<Application | undefined> {
58+
if (!address) {
59+
throw new NotFoundException('Application not found');
60+
}
61+
5862
return await this.applicationService.getApplication(address);
5963
}
6064
}

src/endpoints/applications/application.service.ts

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,15 @@ export class ApplicationService {
4343

4444
const assets = await this.assetsService.getAllAccountAssets();
4545
const verifiedAccounts = await this.cacheService.get<string[]>(CacheInfo.VerifiedAccounts.key) || [];
46-
const balances = await this.elasticIndexerService.getApplicationsBulkBalance(elasticResults.map(item => item.address), pagination);
4746

4847
const applications = elasticResults.map(item => new Application({
4948
contract: item.address,
50-
deployer: item.deployer,
5149
owner: item.currentOwner,
52-
codeHash: item.initialCodeHash,
5350
timestamp: item.timestamp,
5451
assets: assets[item.address],
55-
balance: balances[item.address]?.balance || '0',
52+
balance: item.balance || '0',
5653
isVerified: verifiedAccounts.includes(item.address),
54+
transfersLast24h: item.api_transfersLast24h || 0,
5755
...(filter.withTxCount && { txCount: 0 }),
5856
}));
5957

@@ -71,29 +69,17 @@ export class ApplicationService {
7169
return await this.elasticIndexerService.getApplicationCount(filter);
7270
}
7371

74-
async getApplication(address: string): Promise<Application> {
75-
const indexResult = await this.elasticIndexerService.getApplication(address);
76-
const assets = await this.assetsService.getAllAccountAssets();
77-
const verifiedAccounts = await this.cacheService.get<string[]>(CacheInfo.VerifiedAccounts.key) || [];
78-
72+
async getApplication(address: string): Promise<Application | undefined> {
7973
const pagination = new QueryPagination({ from: 0, size: 1 });
80-
const balances = await this.elasticIndexerService.getApplicationsBulkBalance([address], pagination);
81-
82-
const result = new Application({
83-
contract: indexResult.address,
84-
deployer: indexResult.deployer,
85-
owner: indexResult.currentOwner,
86-
codeHash: indexResult.initialCodeHash,
87-
timestamp: indexResult.timestamp,
88-
assets: assets[address],
89-
balance: balances[address]?.balance || '0',
90-
txCount: 0,
91-
isVerified: verifiedAccounts.includes(address),
92-
});
93-
94-
result.txCount = await this.getApplicationTxCount(result.contract);
95-
96-
return result;
74+
const filter = new ApplicationFilter({ address });
75+
76+
const applications = await this.getApplicationsRaw(pagination, filter);
77+
78+
if (!applications || !applications.length) {
79+
return undefined;
80+
}
81+
82+
return applications.at(0);
9783
}
9884

9985
private async getApplicationTxCount(address: string): Promise<number> {

src/endpoints/applications/entities/application.filter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export class ApplicationFilter {
55
Object.assign(this, init);
66
}
77

8+
address?: string;
89
after?: number;
910
before?: number;
1011
withTxCount?: boolean;
@@ -18,6 +19,7 @@ export class ApplicationFilter {
1819
isSet(): boolean {
1920
return this.after !== undefined ||
2021
this.before !== undefined ||
21-
this.withTxCount !== undefined;
22+
this.withTxCount !== undefined ||
23+
this.address !== undefined;
2224
}
2325
}

src/endpoints/applications/entities/application.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ export class Application {
99
@ApiProperty({ type: String })
1010
contract: string = '';
1111

12-
@ApiProperty({ type: String })
13-
deployer: string = '';
14-
1512
@ApiProperty({ type: String })
1613
owner: string = '';
1714

@@ -32,4 +29,7 @@ export class Application {
3229

3330
@ApiProperty({ type: Boolean, required: false, description: 'Whether the application is verified' })
3431
isVerified?: boolean;
32+
33+
@ApiProperty({ type: Number, required: false, description: 'Number of transfers in the last 24 hours' })
34+
transfersLast24h?: number;
3535
}

0 commit comments

Comments
 (0)