Skip to content

Commit

Permalink
Merge pull request #1554 from multiversx/MEX-662-trading-activity-for…
Browse files Browse the repository at this point in the history
…-tokens-and-pairs

[MEX-662]: Filter by pair address in ES query
  • Loading branch information
claudiulataretu authored Jan 31, 2025
2 parents 39a66ce + 6841761 commit 8490787
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 45 deletions.
56 changes: 20 additions & 36 deletions src/modules/analytics/services/analytics.compute.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,8 @@ export class AnalyticsComputeService {
@ErrorLoggerAsync()
@GetOrSetCache({
baseKey: 'analytics',
remoteTtl: Constants.oneMinute() * 2,
localTtl: Constants.oneMinute(),
remoteTtl: Constants.oneMinute() * 5,
localTtl: Constants.oneMinute() * 3,
})
async pairTradingActivity(
pairAddress: string,
Expand All @@ -295,8 +295,8 @@ export class AnalyticsComputeService {
@ErrorLoggerAsync()
@GetOrSetCache({
baseKey: 'analytics',
remoteTtl: Constants.oneMinute() * 2,
localTtl: Constants.oneMinute(),
remoteTtl: Constants.oneMinute() * 5,
localTtl: Constants.oneMinute() * 3,
})
async tokenTradingActivity(
tokenID: string,
Expand Down Expand Up @@ -385,40 +385,24 @@ export class AnalyticsComputeService {
async computeTokenTradingActivity(
tokenID: string,
): Promise<TradingActivityModel[]> {
const filteredEvents: RawElasticEventType[] = [];
const pairsAddresses = await this.routerAbi.pairsAddress();
let latestTimestamp = Math.floor(Date.now() / 1000);
const size = 50;
let filteredEvents: RawElasticEventType[] = [];
const pairsMetadata = await this.routerAbi.pairsMetadata();

const createUniqueIdentifier = (event: RawElasticEventType) => {
return `${event.txHash}-${event.shardID}-${event.order}`;
};
const pairsAddresses = pairsMetadata
.filter(
(pair) =>
pair.firstTokenID === tokenID ||
pair.secondTokenID === tokenID,
)
.map((pair) => pair.address);

filteredEvents = await this.elasticEventsService.getTokenTradingEvents(
tokenID,
pairsAddresses,
10,
);

while (filteredEvents.length < 10) {
const events =
await this.elasticEventsService.getTokenTradingEvents(
tokenID,
latestTimestamp,
size,
);
filteredEvents.push(
...events
.filter((event) => pairsAddresses.includes(event.address))
.reduce((unique, event) => {
const eventId = createUniqueIdentifier(event);
return unique.some(
(e) => createUniqueIdentifier(e) === eventId,
)
? unique
: [...unique, event];
}, [] as RawElasticEventType[])
.slice(0, 10),
);
if (events.length < size) {
break;
}
latestTimestamp = filteredEvents[0].timestamp;
}
filteredEvents = filteredEvents.slice(0, 10);

return filteredEvents.map((event) => {
const eventConverted = convertEventTopicsAndDataToBase64(event);
Expand Down
8 changes: 4 additions & 4 deletions src/modules/analytics/services/analytics.setter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export class AnalyticsSetterService extends GenericSetterService {
return await this.setData(
this.getCacheKey('pairTradingActivity', pairAddress),
value,
Constants.oneMinute() * 2,
Constants.oneMinute(),
Constants.oneMinute() * 5,
Constants.oneMinute() * 3,
);
}

Expand All @@ -92,8 +92,8 @@ export class AnalyticsSetterService extends GenericSetterService {
return await this.setData(
this.getCacheKey('tokenTradingActivity', tokenID),
value,
Constants.oneMinute() * 2,
Constants.oneMinute(),
Constants.oneMinute() * 5,
Constants.oneMinute() * 3,
);
}

Expand Down
15 changes: 15 additions & 0 deletions src/services/elastic-search/entities/terms.query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { AbstractQuery } from '@multiversx/sdk-nestjs-elastic';

export class CustomTermsQuery extends AbstractQuery {
constructor(private readonly key: string, private readonly values: any) {
super();
}

getQuery(): any {
return {
terms: {
[this.key]: this.values,
},
};
}
}
12 changes: 7 additions & 5 deletions src/services/elastic-search/services/es.events.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Logger } from 'winston';
import { RawElasticEventType } from '../entities/raw.elastic.event';
import { SwapEvent } from '@multiversx/sdk-exchange';
import { convertEventTopicsAndDataToBase64 } from 'src/utils/elastic.search.utils';
import { CustomTermsQuery } from '../entities/terms.query';

@Injectable()
export class ElasticSearchEventsService {
Expand Down Expand Up @@ -227,7 +228,7 @@ export class ElasticSearchEventsService {

async getTokenTradingEvents(
tokenID: string,
timestamp: number,
pairsAddresses: string[],
size: number,
): Promise<RawElasticEventType[]> {
const pagination = new ElasticPagination();
Expand All @@ -248,11 +249,12 @@ export class ElasticSearchEventsService {
SWAP_IDENTIFIER.SWAP_FIXED_OUTPUT,
),
]),
QueryType.Range('timestamp', {
key: 'lte',
value: timestamp,
}),
];

elasticQueryAdapter.filter = [
new CustomTermsQuery('address', pairsAddresses),
];

elasticQueryAdapter.sort = [
{ name: 'timestamp', order: ElasticSortOrder.descending },
];
Expand Down

0 comments on commit 8490787

Please sign in to comment.