Skip to content

Commit

Permalink
Merge branch 'development' into SERVICES-2694-remove-support-for-grap…
Browse files Browse the repository at this point in the history
…hql-service
  • Loading branch information
cfaur09 committed Nov 4, 2024
2 parents 396b554 + 0bbe190 commit d8d62db
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 19 deletions.
20 changes: 16 additions & 4 deletions src/common/indexer/elastic/elastic.indexer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ export class ElasticIndexerService implements IndexerInterface {
return await this.elasticService.getList('operations', 'hash', elasticQuery);
}

async getAccounts(queryPagination: QueryPagination, filter: AccountQueryOptions): Promise<any[]> {
async getAccounts(queryPagination: QueryPagination, filter: AccountQueryOptions, fields?: string[]): Promise<any[]> {
let elasticQuery = this.indexerHelper.buildAccountFilterQuery(filter);
const sortOrder: ElasticSortOrder = !filter.order || filter.order === SortOrder.desc ? ElasticSortOrder.descending : ElasticSortOrder.ascending;
const sort: AccountSort = filter.sort ?? AccountSort.balance;
Expand All @@ -429,6 +429,10 @@ export class ElasticIndexerService implements IndexerInterface {

elasticQuery = elasticQuery.withPagination(queryPagination);

if (fields && fields.length > 0) {
elasticQuery = elasticQuery.withFields(fields);
}

return await this.elasticService.getList('accounts', 'address', elasticQuery);
}

Expand Down Expand Up @@ -805,8 +809,6 @@ export class ElasticIndexerService implements IndexerInterface {
}
}

console.log({ subType: filter.subType });

const elasticQuery = ElasticQuery.create()
.withMustExistCondition('identifier')
.withMustMatchCondition('address', address)
Expand Down Expand Up @@ -888,7 +890,7 @@ export class ElasticIndexerService implements IndexerInterface {
async getAllFungibleTokens(): Promise<any[]> {
const query = ElasticQuery.create()
.withMustMatchCondition('type', TokenType.FungibleESDT)
.withFields(["name", "type", "currentOwner", "numDecimals", "properties", "timestamp"])
.withFields(["name", "type", "currentOwner", "numDecimals", "properties", "timestamp", "ownersHistory"])
.withMustNotExistCondition('identifier')
.withPagination({ from: 0, size: 1000 });

Expand Down Expand Up @@ -978,6 +980,16 @@ export class ElasticIndexerService implements IndexerInterface {
return await this.elasticService.getCount('scdeploys', elasticQuery);
}

async getAddressesWithTransfersLast24h(): Promise<string[]> {
const elasticQuery = ElasticQuery.create()
.withFields(['address'])
.withPagination({ from: 0, size: 10000 })
.withMustExistCondition('api_transfersLast24h');

const result = await this.elasticService.getList('accounts', 'address', elasticQuery);
return result.map(x => x.address);
}

async getEvents(pagination: QueryPagination, filter: EventsFilter): Promise<Events[]> {
const elasticQuery = this.indexerHelper.buildEventsFilter(filter)
.withPagination(pagination)
Expand Down
4 changes: 3 additions & 1 deletion src/common/indexer/indexer.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export interface IndexerInterface {

getAccount(address: string): Promise<Account>

getAccounts(queryPagination: QueryPagination, filter: AccountQueryOptions): Promise<Account[]>
getAccounts(queryPagination: QueryPagination, filter: AccountQueryOptions, fields?: string[]): Promise<Account[]>

getAccountDeploys(pagination: QueryPagination, address: string): Promise<ScDeploy[]>

Expand Down Expand Up @@ -188,6 +188,8 @@ export interface IndexerInterface {

getApplicationCount(filter: ApplicationFilter): Promise<number>

getAddressesWithTransfersLast24h(): Promise<string[]>

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

getEvent(txHash: string): Promise<Events>
Expand Down
8 changes: 6 additions & 2 deletions src/common/indexer/indexer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ export class IndexerService implements IndexerInterface {
}

@LogPerformanceAsync(MetricsEvents.SetIndexerDuration)
async getAccounts(queryPagination: QueryPagination, filter: AccountQueryOptions): Promise<Account[]> {
return await this.indexerInterface.getAccounts(queryPagination, filter);
async getAccounts(queryPagination: QueryPagination, filter: AccountQueryOptions, fields?: string[]): Promise<Account[]> {
return await this.indexerInterface.getAccounts(queryPagination, filter, fields);
}

@LogPerformanceAsync(MetricsEvents.SetIndexerDuration)
Expand Down Expand Up @@ -452,6 +452,10 @@ export class IndexerService implements IndexerInterface {
}

@LogPerformanceAsync(MetricsEvents.SetIndexerDuration)
async getAddressesWithTransfersLast24h(): Promise<string[]> {
return await this.indexerInterface.getAddressesWithTransfersLast24h();
}

async getEvents(pagination: QueryPagination, filter: EventsFilter): Promise<Events[]> {
return await this.indexerInterface.getEvents(pagination, filter);
}
Expand Down
24 changes: 16 additions & 8 deletions src/crons/cache.warmer/cache.warmer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { PoolService } from "src/endpoints/pool/pool.service";
import * as JsonDiff from "json-diff";
import { QueryPagination } from "src/common/entities/query.pagination";
import { StakeService } from "src/endpoints/stake/stake.service";
import { ApplicationMostUsed } from "src/endpoints/accounts/entities/application.most.used";

@Injectable()
export class CacheWarmerService {
Expand Down Expand Up @@ -377,21 +378,28 @@ export class CacheWarmerService {
async handleUpdateAccountTransfersLast24h() {
const batchSize = 100;
const mostUsed = await this.accountService.getApplicationMostUsedRaw();
const mostUsedIndexedAccounts = await this.indexerService.getAddressesWithTransfersLast24h();

const batches = BatchUtils.splitArrayIntoChunks(mostUsed, batchSize);
const allAddressesToUpdate = [...mostUsed.map(item => item.address), ...mostUsedIndexedAccounts].distinct();
const mostUsedDictionary = mostUsed.toRecord<ApplicationMostUsed>(item => item.address);

const batches = BatchUtils.splitArrayIntoChunks(allAddressesToUpdate, batchSize);
for (const batch of batches) {
const accounts = await this.indexerService.getAccounts(
new QueryPagination({ from: 0, size: batchSize }),
new AccountQueryOptions({ addresses: batch.map(item => item.address) }),
new AccountQueryOptions({ addresses: batch }),
['address', 'api_transfersLast24h'],
);

const accountsDictionary = accounts.toRecord<Account>(account => account.address);
const accountsDictionary = accounts.toRecord<Pick<Account, 'address' | 'api_transfersLast24h'>>(account => account.address);

for (const address of batch) {
const account = accountsDictionary[address];
const newTransfersLast24h = mostUsedDictionary[address]?.transfers24H ?? 0;

for (const item of batch) {
const account = accountsDictionary[item.address];
if (account && account.api_transfersLast24h !== item.transfers24H) {
this.logger.log(`Setting transferLast24h to ${item.transfers24H} for account with address '${item.address}'`);
await this.indexerService.setAccountTransfersLast24h(item.address, item.transfers24H);
if (account && account.api_transfersLast24h !== newTransfersLast24h) {
this.logger.log(`Setting transferLast24h to ${newTransfersLast24h} for account with address '${address}'`);
await this.indexerService.setAccountTransfersLast24h(address, newTransfersLast24h);
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/endpoints/esdt/esdt.address.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,7 @@ export class EsdtAddressService {
}
}

if ([NftType.SemiFungibleESDT, NftType.MetaESDT].includes(nft.type)) {
nft.balance = dataSourceNft.balance;
}

nft.balance = dataSourceNft.balance;
nftAccounts.push(nft);
}

Expand Down
1 change: 1 addition & 0 deletions src/endpoints/esdt/esdt.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ export class EsdtService {
type: elasticProperties.type as EsdtType,
subType: elasticProperties.type as EsdtSubType,
owner: elasticProperties.currentOwner,
ownersHistory: elasticProperties.ownersHistory,
decimals: elasticProperties.numDecimals,
canUpgrade: elasticProperties.properties?.canUpgrade ?? false,
canMint: elasticProperties.properties?.canMint ?? false,
Expand Down
8 changes: 8 additions & 0 deletions src/endpoints/tokens/entities/token.owner.history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export class TokenOwnersHistory {
constructor(init?: Partial<TokenOwnersHistory>) {
Object.assign(this, init);
}

address: string = '';
timestamp: number = 0;
}
4 changes: 4 additions & 0 deletions src/endpoints/tokens/entities/token.properties.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ApiProperty } from "@nestjs/swagger";
import { EsdtType } from "../../esdt/entities/esdt.type";
import { EsdtSubType } from "src/endpoints/esdt/entities/esdt.sub.type";
import { TokenOwnersHistory } from "./token.owner.history";

export class TokenProperties {
constructor(init?: Partial<TokenProperties>) {
Expand Down Expand Up @@ -75,4 +76,7 @@ export class TokenProperties {

@ApiProperty()
timestamp: number = 0;

@ApiProperty()
ownersHistory: TokenOwnersHistory[] = [];
}
5 changes: 5 additions & 0 deletions src/endpoints/tokens/entities/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ApiProperty } from "@nestjs/swagger";
import { TokenType } from "src/common/indexer/entities";
import { TokenAssets } from "../../../common/assets/entities/token.assets";
import { MexPairType } from "src/endpoints/mex/entities/mex.pair.type";
import { TokenOwnersHistory } from "./token.owner.history";

export class Token {
constructor(init?: Partial<Token>) {
Expand Down Expand Up @@ -125,4 +126,8 @@ export class Token {

@ApiProperty({ type: Number, nullable: true })
tradesCount: number | undefined = undefined;

@Field(() => TokenOwnersHistory, { description: 'Token owners history.', nullable: true })
@ApiProperty({ type: TokenOwnersHistory, nullable: true })
ownersHistory: TokenOwnersHistory[] = [];
}
12 changes: 12 additions & 0 deletions src/test/unit/services/tokens.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,12 @@ describe('Token Service', () => {
canTransferNFTCreateRole: false,
NFTCreateStopped: false,
timestamp: 1643824710,
ownersHistory: [
{
address: 'erd1qqqqqqqqqqqqqpgq0lzzvt2faev4upyf586tg38s84d7zsaj2jpsglugga',
timestamp: 1643824710,
},
],
};

it('should returns undefined if getTokenProperties returns undefined', async () => {
Expand Down Expand Up @@ -903,6 +909,12 @@ describe('Token Service', () => {
canTransferNFTCreateRole: false,
NFTCreateStopped: false,
timestamp: 1643824710,
ownersHistory: [
{
address: 'erd1qqqqqqqqqqqqqpgq0lzzvt2faev4upyf586tg38s84d7zsaj2jpsglugga',
timestamp: 1643824710,
},
],
};

const assets = {
Expand Down

0 comments on commit d8d62db

Please sign in to comment.