Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get real caller from ES for events #19

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
IndexerService, JoinExitEvent, LatestBlockResponse,
MultiversXApiService, PairResponse, SwapEvent, XExchangeService,
} from "@mvx-monorepo/common";
import { OriginLogger } from "@multiversx/sdk-nestjs-common";
import { AddressUtils, BatchUtils, OriginLogger } from "@multiversx/sdk-nestjs-common";
import { IProviderService } from "@mvx-monorepo/common/providers/interface";
import { GeneralEvent } from "@mvx-monorepo/common/providers/entities/general.event";
import { OneDexService } from "@mvx-monorepo/common/providers";
Expand Down Expand Up @@ -83,6 +83,8 @@ export class DataIntegrationService {
allEvents.push(...event);
}

await this.updateEventsCaller(allEvents);

const sortedEvents = allEvents.sort((a, b) => {
if (a.block.blockTimestamp !== b.block.blockTimestamp) {
return a.block.blockTimestamp - b.block.blockTimestamp;
Expand Down Expand Up @@ -136,4 +138,38 @@ export class DataIntegrationService {
}
return events;
}

private async updateEventsCaller(events: ({ block: Block } & (SwapEvent | JoinExitEvent))[]) {
const filteredEvents = events.filter(event => AddressUtils.isSmartContractAddress(event.maker));

const txHashes = filteredEvents.map(event => event.txnId);

const transactions = await this.getTxDetailsInBatches(txHashes, 200);

const txToCallerMap = new Map<string, string>(
transactions.map(transaction => [transaction.txHash, transaction.sender])
);

for (const event of events) {
if (AddressUtils.isSmartContractAddress(event.maker)) {
const callerFromMap = txToCallerMap.get(event.txnId);
if (callerFromMap) {
event.maker = callerFromMap;
}
}
}
}

private async getTxDetailsInBatches(txHashes: string[], batchSize: number) {
const transactions: any[] = [];
const txHashesBatches = BatchUtils.splitArrayIntoChunks(txHashes, batchSize);

for (const txHashesBatch of txHashesBatches) {
const transactionsBatch = await this.indexerService.getTxDetails(txHashesBatch);

transactions.push(...transactionsBatch);
}

return transactions;
}
}
2 changes: 1 addition & 1 deletion libs/common/src/providers/onedex/onedex.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export class OneDexService implements IProviderService {
public async getPairsMetadata(): Promise<any[]> {
return await this.cacheService.getOrSet(
CacheInfo.OneDexPairsMetadata().key,
() => this.getPairsMetadataRaw(),
async () => await this.getPairsMetadataRaw(),
CacheInfo.OneDexPairsMetadata().ttl,
);
}
Expand Down
4 changes: 2 additions & 2 deletions libs/common/src/providers/xexchange/xexchange.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class XExchangeService implements IProviderService {
public async getPairsMetadata(): Promise<PairMetadata[]> {
return await this.cacheService.getOrSet(
CacheInfo.PairsMetadata().key,
() => this.getPairsMetadataRaw(),
async () => await this.getPairsMetadataRaw(),
CacheInfo.PairsMetadata().ttl,
);
}
Expand Down Expand Up @@ -115,7 +115,7 @@ export class XExchangeService implements IProviderService {
public async getPairFeePercent(pairAddress: string): Promise<number> {
return await this.cacheService.getOrSet(
CacheInfo.PairFeePercent(pairAddress).key,
() => this.getPairFeePercentRaw(pairAddress),
async () => await this.getPairFeePercentRaw(pairAddress),
CacheInfo.PairFeePercent(pairAddress).ttl,
);
}
Expand Down
20 changes: 20 additions & 0 deletions libs/common/src/services/indexer/indexer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,24 @@ export class IndexerService {

return logs;
}

@LogPerformanceAsync(MetricsEvents.SetIndexerDuration)
public async getTxDetails(txHashes: string[]) {
try {
const query = ElasticQuery.create()
.withPagination({ from: 0, size: 10000 })
.withMustCondition([
QueryType.Should(txHashes.map(txHash => new MatchQuery("_id", txHash))),
]);

const transactions = await this.elasticService.getList('operations', 'txHash', query);

return transactions;
} catch (error) {
this.logger.error(`Failed to get txDetails`);
this.logger.error(error);

return [];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class MultiversXApiService {
public async getToken(identifier: string): Promise<Token | null> {
return await this.cacheService.getOrSet(
CacheInfo.Token(identifier).key,
() => this.getTokenRaw(identifier),
async () => await this.getTokenRaw(identifier),
CacheInfo.Token(identifier).ttl,
);
}
Expand All @@ -42,7 +42,7 @@ export class MultiversXApiService {
public async getContractDeployInfo(address: string): Promise<{ deployTxHash?: string, deployedAt?: number }> {
return await this.cacheService.getOrSet(
CacheInfo.ContractDeployInfo(address).key,
() => this.getContractDeployInfoRaw(address),
async () => await this.getContractDeployInfoRaw(address),
CacheInfo.ContractDeployInfo(address).ttl,
);
}
Expand Down
Loading