Skip to content

Commit

Permalink
Merge pull request #1506 from multiversx/SERVICES-2603-refactor-es-fu…
Browse files Browse the repository at this point in the history
…nctionality-to-use-events-index

[SERVICES-2603] Refactor ES integration to use events index
  • Loading branch information
mad2sm0key authored Jan 13, 2025
2 parents ce30f18 + 101710e commit 89d56b7
Show file tree
Hide file tree
Showing 16 changed files with 483 additions and 456 deletions.
98 changes: 0 additions & 98 deletions src/helpers/elastic.service.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/modules/governance/governance.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { GovernanceAbiFactory } from './services/governance.abi.factory';
import { GovernanceServiceFactory } from './services/governance.factory';
import { GovernanceOldEnergyAbiService } from './services/governance.old.energy.abi.service';
import { LockedAssetModule } from '../locked-asset-factory/locked-asset.module';
import { ElasticService } from 'src/helpers/elastic.service';
import { ElasticSearchModule } from 'src/services/elastic-search/elastic.search.module';

@Module({
imports: [
Expand All @@ -38,6 +38,7 @@ import { ElasticService } from 'src/helpers/elastic.service';
TokenModule,
EnergyModule,
LockedAssetModule,
ElasticSearchModule,
],
providers: [
GovernanceTokenSnapshotService,
Expand All @@ -57,7 +58,6 @@ import { ElasticService } from 'src/helpers/elastic.service';
GovernanceEnergyContractResolver,
GovernanceTokenSnapshotContractResolver,
GovernanceProposalResolver,
ElasticService,
],
exports: [
GovernanceTokenSnapshotAbiService,
Expand Down
88 changes: 37 additions & 51 deletions src/modules/governance/services/governance.compute.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,56 @@ import { CacheTtlInfo } from '../../../services/caching/cache.ttl.info';
import { GovernanceSetterService } from './governance.setter.service';
import { Address } from '@multiversx/sdk-core/out';
import { decimalToHex } from '../../../utils/token.converters';
import { ElasticQuery, ElasticSortOrder, QueryType } from '@multiversx/sdk-nestjs-elastic';
import { ElasticService } from 'src/helpers/elastic.service';
import { toVoteType } from '../../../utils/governance';
import { ElasticSearchEventsService } from 'src/services/elastic-search/services/es.events.service';

@Injectable()
export class GovernanceComputeService {
constructor(
private readonly elasticService: ElasticService,
private readonly elasticEventsService: ElasticSearchEventsService,
private readonly governanceSetter: GovernanceSetterService,
) {
}
) {}

async userVotedProposalsWithVoteType(scAddress: string, userAddress: string, proposalId: number): Promise<VoteType> {
const currentCachedProposalVoteTypes = await this.userVoteTypesForContract(scAddress, userAddress);
const cachedVoteType = currentCachedProposalVoteTypes.find((proposal) => proposal.proposalId === proposalId);
async userVotedProposalsWithVoteType(
scAddress: string,
userAddress: string,
proposalId: number,
): Promise<VoteType> {
const currentCachedProposalVoteTypes =
await this.userVoteTypesForContract(scAddress, userAddress);
const cachedVoteType = currentCachedProposalVoteTypes.find(
(proposal) => proposal.proposalId === proposalId,
);
if (cachedVoteType) {
return cachedVoteType.vote;
}

const log = await this.getVoteLog('vote', scAddress, userAddress, proposalId);
const voteEvents = await this.elasticEventsService.getGovernanceVotes(
scAddress,
Address.fromString(userAddress).hex(),
decimalToHex(proposalId),
);

let voteType = VoteType.NotVoted;
if (log.length > 0) {
const voteEvent = log[0]._source.events.find((event) => event.identifier === 'vote');
voteType = toVoteType(atob(voteEvent.topics[0]));
if (voteEvents.length > 0) {
const voteEvent = voteEvents.find(
(event) => event.identifier === 'vote',
);
voteType = toVoteType(
Buffer.from(voteEvent.topics[0], 'hex').toString(),
);
}

const proposalVoteType = {
proposalId,
vote: voteType,
}
};
currentCachedProposalVoteTypes.push(proposalVoteType);
await this.governanceSetter.userVoteTypesForContract(scAddress, userAddress, currentCachedProposalVoteTypes);
await this.governanceSetter.userVoteTypesForContract(
scAddress,
userAddress,
currentCachedProposalVoteTypes,
);
return proposalVoteType.vote;
}

Expand All @@ -46,43 +65,10 @@ export class GovernanceComputeService {
remoteTtl: CacheTtlInfo.ContractState.remoteTtl,
localTtl: CacheTtlInfo.ContractState.localTtl,
})
async userVoteTypesForContract(scAddress: string, userAddress: string): Promise<{ proposalId: number, vote: VoteType }[]> {
return [];
}

private async getVoteLog(
eventName: string,
async userVoteTypesForContract(
scAddress: string,
callerAddress: string,
proposalId: number,
): Promise<any[]> {
const elasticQueryAdapter: ElasticQuery = new ElasticQuery();
const encodedProposalId = Buffer.from(decimalToHex(proposalId), 'hex').toString('base64');
const encodedCallerAddress = Buffer.from(Address.fromString(callerAddress).hex(), 'hex').toString('base64');
elasticQueryAdapter.condition.must = [
QueryType.Match('address', scAddress),
QueryType.Nested('events', [
QueryType.Match('events.address', scAddress),
QueryType.Match('events.identifier', eventName),
]),
QueryType.Nested('events', [
QueryType.Match('events.topics', encodedProposalId),
]),
QueryType.Nested('events', [
QueryType.Match('events.topics', encodedCallerAddress),
]),
];

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


const list = await this.elasticService.getList(
'logs',
'',
elasticQueryAdapter,
);
return list;
userAddress: string,
): Promise<{ proposalId: number; vote: VoteType }[]> {
return [];
}
}
10 changes: 4 additions & 6 deletions src/modules/pair/services/pair.compute.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import { FarmComputeServiceV2 } from 'src/modules/farm/v2/services/farm.v2.compu
import { StakingComputeService } from 'src/modules/staking/services/staking.compute.service';
import { CacheService } from '@multiversx/sdk-nestjs-cache';
import { getAllKeys } from 'src/utils/get.many.utils';
import { ESTransactionsService } from 'src/services/elastic-search/services/es.transactions.service';
import moment from 'moment';
import { ElasticSearchEventsService } from 'src/services/elastic-search/services/es.events.service';

@Injectable()
export class PairComputeService implements IPairComputeService {
Expand All @@ -50,7 +50,7 @@ export class PairComputeService implements IPairComputeService {
private readonly farmCompute: FarmComputeServiceV2,
private readonly stakingCompute: StakingComputeService,
private readonly cachingService: CacheService,
private readonly elasticTransactionsService: ESTransactionsService,
private readonly elasticEventsService: ElasticSearchEventsService,
) {}

async getTokenPrice(pairAddress: string, tokenID: string): Promise<string> {
Expand Down Expand Up @@ -729,9 +729,7 @@ export class PairComputeService implements IPairComputeService {
}

async computeTradesCount(pairAddress: string): Promise<number> {
return await this.elasticTransactionsService.computePairSwapCount(
pairAddress,
);
return await this.elasticEventsService.getPairSwapCount(pairAddress);
}

@ErrorLoggerAsync({
Expand All @@ -750,7 +748,7 @@ export class PairComputeService implements IPairComputeService {
const end = moment.utc().unix();
const start = moment.unix(end).subtract(1, 'day').unix();

return await this.elasticTransactionsService.computePairSwapCount(
return await this.elasticEventsService.getPairSwapCount(
pairAddress,
start,
end,
Expand Down
2 changes: 0 additions & 2 deletions src/modules/pair/specs/pair.compute.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { EsdtToken } from 'src/modules/tokens/models/esdtToken.model';
import { AssetsModel } from 'src/modules/tokens/models/assets.model';
import { RolesModel } from 'src/modules/tokens/models/roles.model';
import { PairAbiService } from '../services/pair.abi.service';
import { ElasticService } from 'src/helpers/elastic.service';
import { RemoteConfigGetterServiceProvider } from 'src/modules/remote-config/mocks/remote-config.getter.mock';
import { StakingProxyAbiServiceProvider } from 'src/modules/staking-proxy/mocks/staking.proxy.abi.service.mock';
import { FarmAbiServiceProviderV2 } from 'src/modules/farm/mocks/farm.v2.abi.service.mock';
Expand Down Expand Up @@ -66,7 +65,6 @@ describe('PairService', () => {
ContextGetterServiceProvider,
ApiConfigService,
MXApiServiceProvider,
ElasticService,
FarmAbiServiceProviderV2,
RemoteConfigGetterServiceProvider,
StakingProxyAbiServiceProvider,
Expand Down
2 changes: 0 additions & 2 deletions src/modules/router/router.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { CommonAppModule } from 'src/common.app.module';
import { ContextModule } from 'src/services/context/context.module';
import { WrappingModule } from '../wrapping/wrap.module';
import { RemoteConfigModule } from '../remote-config/remote-config.module';
import { ElasticService } from 'src/helpers/elastic.service';
import { SwapEnableConfigResolver } from './swap.enable.config.resolver';
import { SimpleLockModule } from '../simple-lock/simple.lock.module';
import { ESTransactionsService } from 'src/services/elastic-search/services/es.transactions.service';
Expand All @@ -35,7 +34,6 @@ import { AnalyticsModule } from '../analytics/analytics.module';
RouterComputeService,
RouterTransactionService,
ESTransactionsService,
ElasticService,
SwapEnableConfigResolver,
RouterResolver,
],
Expand Down
15 changes: 8 additions & 7 deletions src/modules/tokens/services/token.compute.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import {
QueryType,
} from '@multiversx/sdk-nestjs-elastic';
import moment from 'moment';
import { ESLogsService } from 'src/services/elastic-search/services/es.logs.service';
import { PendingExecutor } from 'src/utils/pending.executor';
import { CacheService } from '@multiversx/sdk-nestjs-cache';
import { TokenService } from './token.service';
import { computeValueUSD } from 'src/utils/token.converters';
import { getAllKeys } from 'src/utils/get.many.utils';
import { ElasticSearchEventsService } from 'src/services/elastic-search/services/es.events.service';

@Injectable()
export class TokenComputeService implements ITokenComputeService {
Expand All @@ -53,8 +53,8 @@ export class TokenComputeService implements ITokenComputeService {
private readonly dataApi: MXDataApiService,
private readonly analyticsQuery: AnalyticsQueryService,
private readonly elasticService: ElasticService,
private readonly logsElasticService: ESLogsService,
private readonly cachingService: CacheService,
private readonly elasticEventsService: ElasticSearchEventsService,
) {
this.swapCountExecutor = new PendingExecutor(
async () => await this.allTokensSwapsCount(),
Expand Down Expand Up @@ -717,11 +717,12 @@ export class TokenComputeService implements ITokenComputeService {
): Promise<{ tokenID: string; swapsCount: number }[]> {
const pairAddresses = await this.routerAbi.pairsAddress();

const allSwapsCount = await this.logsElasticService.getTokenSwapsCount(
start,
end,
pairAddresses,
);
const allSwapsCount =
await this.elasticEventsService.getTokenSwapsCount(
start,
end,
pairAddresses,
);

const result = [];

Expand Down
4 changes: 0 additions & 4 deletions src/modules/tokens/token.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import { MXCommunicationModule } from 'src/services/multiversx-communication/mx.
import { NftCollectionResolver } from './nftCollection.resolver';
import { NftTokenResolver } from './nftToken.resolver';
import { AnalyticsModule } from 'src/services/analytics/analytics.module';
import { ElasticService } from 'src/helpers/elastic.service';
import { TokenFilteringService } from './services/token.filtering.service';
import { ElasticSearchModule } from 'src/services/elastic-search/elastic.search.module';
import { ESLogsService } from 'src/services/elastic-search/services/es.logs.service';
import { TokenLoader } from './services/token.loader';

@Module({
Expand All @@ -41,9 +39,7 @@ import { TokenLoader } from './services/token.loader';
TokensResolver,
NftCollectionResolver,
NftTokenResolver,
ElasticService,
TokenFilteringService,
ESLogsService,
],
exports: [
TokenRepositoryService,
Expand Down
3 changes: 1 addition & 2 deletions src/private.app.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Module } from '@nestjs/common';
import { CommonAppModule } from './common.app.module';
import { MetricsController } from './endpoints/metrics/metrics.controller';
import { ElasticService } from './helpers/elastic.service';
import { PairModule } from './modules/pair/pair.module';
import { RemoteConfigController } from './modules/remote-config/remote-config.controller';
import { RemoteConfigModule } from './modules/remote-config/remote-config.module';
Expand All @@ -19,6 +18,6 @@ import { ESTransactionsService } from './services/elastic-search/services/es.tra
DynamicModuleUtils.getCacheModule(),
],
controllers: [MetricsController, TokenController, RemoteConfigController],
providers: [ElasticService, ESTransactionsService],
providers: [ESTransactionsService],
})
export class PrivateAppModule {}
Loading

0 comments on commit 89d56b7

Please sign in to comment.