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

[SERVICES-2155] context tracker #1327

Draft
wants to merge 30 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3fc3d54
SERVICES-2155: add interceptor for field resolver enhancers
claudiulataretu Mar 13, 2024
8e51d23
Merge branch 'development' into SERVICES-2155-context-tracker
claudiulataretu Mar 18, 2024
f88ff41
SERVICES-2155: fix timestamp duplicates
claudiulataretu Mar 18, 2024
633265c
Merge branch 'SERVICES-2204-fix-token-analytics' into SERVICES-2155-c…
claudiulataretu Mar 25, 2024
6e197b5
Merge branch 'SERVICES-2204-fix-token-analytics' into SERVICES-2155-c…
claudiulataretu Mar 26, 2024
4be4931
Merge branch 'SERVICES-2204-fix-token-analytics' into SERVICES-2155-c…
claudiulataretu Mar 26, 2024
8be0c21
Merge branch 'development' into SERVICES-2155-context-tracker
claudiulataretu Apr 1, 2024
f9305ad
Merge branch 'development' into SERVICES-2155-context-tracker
claudiulataretu Apr 10, 2024
aa1a6c9
SERVICES-2155: filter farms address based on deploy timestamp
claudiulataretu Apr 10, 2024
833e405
SERVICES-2155: use cache decorator for remote config service
claudiulataretu Apr 10, 2024
3c66cde
SERVICES-2155: add methods to get the staking and staking proxy addre…
claudiulataretu Apr 10, 2024
37735b9
SERVICES-2155: enable datadog tracing
claudiulataretu Apr 18, 2024
58ed85a
Merge branch 'development' into SERVICES-2155-context-tracker
claudiulataretu Apr 18, 2024
2019840
Merge branch 'development' into SERVICES-2155-context-tracker
claudiulataretu Apr 23, 2024
53d237f
Merge branch 'development' into SERVICES-2155-context-tracker
claudiulataretu Apr 23, 2024
53a9a80
Merge branch 'development' into SERVICES-2155-context-tracker
claudiulataretu Apr 29, 2024
21840f5
Merge branch 'development' into SERVICES-2155-context-tracker
claudiulataretu May 8, 2024
1e5e529
Merge branch 'SERVICES-2352-enable-mex-pairs' into SERVICES-2155-cont…
claudiulataretu May 8, 2024
bfb1dc0
Merge branch 'SERVICES-2352-enable-mex-pairs' into SERVICES-2155-cont…
claudiulataretu May 8, 2024
a198870
Merge branch 'development' into SERVICES-2155-context-tracker
claudiulataretu May 9, 2024
1f5b5ad
Merge branch 'SERVICES-2315-tokens-query-extra-fields' into SERVICES-…
claudiulataretu May 10, 2024
c45d8c2
Merge branch 'SERVICES-2315-tokens-query-extra-fields' into SERVICES-…
claudiulataretu May 10, 2024
80e8b2f
Merge branch 'development' into SERVICES-2155-context-tracker
claudiulataretu May 30, 2024
fd06c25
Merge branch 'development' into SERVICES-2155-context-tracker
claudiulataretu Jun 5, 2024
d377224
Merge branch 'development' into SERVICES-2155-context-tracker
claudiulataretu Jun 14, 2024
f14ea38
Merge branch 'main' into SERVICES-2155-context-tracker
claudiulataretu Jul 16, 2024
2aa67cc
Merge branch 'MEX-493-price-discovery-query-improvement' into SERVICE…
claudiulataretu Jul 30, 2024
e66af8c
Merge branch 'development' into SERVICES-2155-context-tracker
claudiulataretu Aug 6, 2024
3a25a60
Merge branch 'development' into SERVICES-2155-context-tracker
claudiulataretu Aug 7, 2024
8c92663
Merge branch 'development' into SERVICES-2155-context-tracker
claudiulataretu Aug 9, 2024
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
593 changes: 579 additions & 14 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"class-validator": "^0.14.0",
"config": "^3.3.7",
"cookie-parser": "^1.4.6",
"dd-trace": "^5.10.0",
"express": "^4.18.1",
"graphql": "^16.5.0",
"graphql-redis-subscriptions": "^2.4.2",
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import cookieParser from 'cookie-parser';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
import { LoggerService } from '@nestjs/common';
import { NestExpressApplication } from '@nestjs/platform-express';
import 'dd-trace/init';

async function bootstrap() {
BigNumber.config({ EXPONENTIAL_AT: [-30, 30] });
Expand Down
29 changes: 16 additions & 13 deletions src/modules/analytics/services/analytics.compute.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import {
FarmRewardType,
FarmVersion,
} from 'src/modules/farm/models/farm.model';
import { farmsAddresses, farmType, farmVersion } from 'src/utils/farm.utils';
import { farmType, farmVersion } from 'src/utils/farm.utils';
import { FarmComputeFactory } from 'src/modules/farm/farm.compute.factory';
import { AnalyticsQueryService } from 'src/services/analytics/services/analytics.query.service';
import { RemoteConfigGetterService } from '../../remote-config/remote-config.getter.service';
import { WeekTimekeepingAbiService } from 'src/submodules/week-timekeeping/services/week-timekeeping.abi.service';
import { WeeklyRewardsSplittingAbiService } from 'src/submodules/weekly-rewards-splitting/services/weekly-rewards-splitting.abi.service';
import { PairAbiService } from 'src/modules/pair/services/pair.abi.service';
Expand All @@ -21,21 +20,24 @@ import { FarmAbiFactory } from 'src/modules/farm/farm.abi.factory';
import { TokenComputeService } from 'src/modules/tokens/services/token.compute.service';
import { TokenService } from 'src/modules/tokens/services/token.service';
import { ErrorLoggerAsync } from '@multiversx/sdk-nestjs-common';
import { FarmFactoryService } from 'src/modules/farm/farm.factory';
import { StakingService } from 'src/modules/staking/services/staking.service';

@Injectable()
export class AnalyticsComputeService {
constructor(
private readonly routerAbi: RouterAbiService,
private readonly farmAbi: FarmAbiFactory,
private readonly farmCompute: FarmComputeFactory,
private readonly farmFactory: FarmFactoryService,
private readonly pairAbi: PairAbiService,
private readonly pairCompute: PairComputeService,
private readonly stakingService: StakingService,
private readonly stakingCompute: StakingComputeService,
private readonly tokenCompute: TokenComputeService,
private readonly tokenService: TokenService,
private readonly weekTimekeepingAbi: WeekTimekeepingAbiService,
private readonly weeklyRewardsSplittingAbi: WeeklyRewardsSplittingAbiService,
private readonly remoteConfigGetterService: RemoteConfigGetterService,
private readonly analyticsQuery: AnalyticsQueryService,
) {}

Expand All @@ -51,9 +53,9 @@ export class AnalyticsComputeService {

async computeLockedValueUSDFarms(): Promise<string> {
let totalLockedValue = new BigNumber(0);

const farmsAddresses = await this.farmFactory.getFarmsAddresses();
const promises: Promise<string>[] = [];
for (const farmAddress of farmsAddresses()) {
for (const farmAddress of farmsAddresses) {
promises.push(
this.farmCompute
.useCompute(farmAddress)
Expand Down Expand Up @@ -118,25 +120,26 @@ export class AnalyticsComputeService {
let totalValueLockedUSD = new BigNumber(0);

const stakingAddresses =
await this.remoteConfigGetterService.getStakingAddresses();
await this.stakingService.getStakingAddresses();
const promises = stakingAddresses.map((stakingAddress) =>
this.stakingCompute.stakedValueUSD(stakingAddress),
);

promises.push(this.computeTotalLockedMexStakedUSD());
const farmsAddresses = await this.farmFactory.getFarmsAddresses();

if (farmsAddresses()[5] !== undefined) {
if (farmsAddresses[5] !== undefined) {
promises.push(
this.farmCompute
.useCompute(farmsAddresses()[5])
.computeFarmLockedValueUSD(farmsAddresses()[5]),
.useCompute(farmsAddresses[5])
.computeFarmLockedValueUSD(farmsAddresses[5]),
);
}
if (farmsAddresses()[13] !== undefined) {
if (farmsAddresses[13] !== undefined) {
promises.push(
this.farmCompute
.useCompute(farmsAddresses()[13])
.computeFarmLockedValueUSD(farmsAddresses()[13]),
.useCompute(farmsAddresses[13])
.computeFarmLockedValueUSD(farmsAddresses[13]),
);
}

Expand All @@ -163,7 +166,7 @@ export class AnalyticsComputeService {
}

async computeTotalAggregatedRewards(days: number): Promise<string> {
const addresses: string[] = farmsAddresses();
const addresses: string[] = await this.farmFactory.getFarmsAddresses();
const promises = addresses.map(async (farmAddress) => {
if (
farmType(farmAddress) === FarmRewardType.CUSTOM_REWARDS ||
Expand Down
2 changes: 2 additions & 0 deletions src/modules/analytics/specs/analytics.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import { WinstonModule } from 'nest-winston';
import { ApiConfigService } from 'src/helpers/api.config.service';
import winston from 'winston';
import { DynamicModuleUtils } from 'src/utils/dynamic.module.utils';
import { FarmFactoryService } from 'src/modules/farm/farm.factory';
import { ElasticSearchModule } from 'src/services/elastic-search/elastic.search.module';
import { StakingFilteringService } from 'src/modules/staking/services/staking.filtering.service';

Expand Down Expand Up @@ -77,6 +78,7 @@ describe('AnalyticsService', () => {
FarmComputeServiceV1_2,
FarmComputeServiceV1_3,
FarmComputeServiceV2,
FarmFactoryService,
FarmServiceV1_2,
FarmServiceV1_3,
FarmServiceV2,
Expand Down
1 change: 0 additions & 1 deletion src/modules/farm/base-module/farm.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Resolver, ResolveField, Parent } from '@nestjs/graphql';
import { BaseFarmModel } from '../models/farm.model';
import { PairModel } from '../../pair/models/pair.model';
import { LockedAssetModel } from '../../locked-asset-factory/models/locked-asset.model';
import { EsdtToken } from '../../tokens/models/esdtToken.model';
import { NftCollection } from '../../tokens/models/nftCollection.model';
import { Address } from '@multiversx/sdk-core';
Expand Down
28 changes: 28 additions & 0 deletions src/modules/farm/base-module/services/farm.abi.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,4 +420,32 @@ export class FarmAbiService
async getFarmShardRaw(farmAddress: string): Promise<number> {
return (await this.apiService.getAccountStats(farmAddress)).shard;
}

@ErrorLoggerAsync({
logArgs: true,
})
@GetOrSetCache({
baseKey: 'farm',
remoteTtl: CacheTtlInfo.ContractInfo.remoteTtl,
localTtl: CacheTtlInfo.ContractInfo.localTtl,
})
async farmDeployedTimestamp(farmAddress: string): Promise<number> {
return await this.getFarmDeployedTimestampRaw(farmAddress);
}

async getFarmDeployedTimestampRaw(
farmAddress: string,
): Promise<number | undefined> {
try {
const addressDetails = await this.apiService.getAccountStats(
farmAddress,
);
return addressDetails.deployedAt ?? undefined;
} catch (error) {
if (error.message.includes('Account not found')) {
return undefined;
}
throw error;
}
}
}
1 change: 1 addition & 0 deletions src/modules/farm/base-module/services/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface IFarmAbiService {
lastErrorMessage(farmAddress: string): Promise<string>;
ownerAddress(farmAddress: string): Promise<string>;
farmShard(farmAddress: string): Promise<number>;
farmDeployedTimestamp(farmAddress: string): Promise<number>;
}

export interface IFarmComputeService {
Expand Down
12 changes: 9 additions & 3 deletions src/modules/farm/farm.abi.factory.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { Injectable } from '@nestjs/common';
import { farmVersion, farmsAddresses } from 'src/utils/farm.utils';
import { farmVersion } from 'src/utils/farm.utils';
import { FarmAbiService } from './base-module/services/farm.abi.service';
import { FarmVersion } from './models/farm.model';
import { FarmAbiServiceV1_2 } from './v1.2/services/farm.v1.2.abi.service';
import { FarmAbiServiceV1_3 } from './v1.3/services/farm.v1.3.abi.service';
import { FarmAbiServiceV2 } from './v2/services/farm.v2.abi.service';
import { CacheService } from '@multiversx/sdk-nestjs-cache';
import { Constants } from '@multiversx/sdk-nestjs-common';
import { FarmFactoryService } from './farm.factory';

@Injectable()
export class FarmAbiFactory {
constructor(
private readonly abiServiceV1_2: FarmAbiServiceV1_2,
private readonly abiServiceV1_3: FarmAbiServiceV1_3,
private readonly abiServiceV2: FarmAbiServiceV2,
private readonly farmFactory: FarmFactoryService,
private readonly cachingService: CacheService,
) {}

Expand All @@ -29,7 +31,8 @@ export class FarmAbiFactory {
}

async isFarmToken(tokenID: string): Promise<boolean> {
for (const farmAddress of farmsAddresses()) {
const farmsAddresses = await this.farmFactory.getFarmsAddresses();
for (const farmAddress of farmsAddresses) {
const farmTokenID = await this.useAbi(farmAddress).farmTokenID(
farmAddress,
);
Expand All @@ -49,7 +52,10 @@ export class FarmAbiFactory {
if (cachedValue && cachedValue !== undefined) {
return cachedValue;
}
for (const farmAddress of farmsAddresses()) {

const farmsAddresses = await this.farmFactory.getFarmsAddresses();

for (const farmAddress of farmsAddresses) {
const farmTokenID = await this.useAbi(farmAddress).farmTokenID(
farmAddress,
);
Expand Down
32 changes: 29 additions & 3 deletions src/modules/farm/farm.factory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { FarmVersion } from './models/farm.model';
import { farmsAddresses, farmType, farmVersion } from 'src/utils/farm.utils';
import { farmType, farmVersion, farmsAddresses } from 'src/utils/farm.utils';
import { FarmModelV1_2 } from './models/farm.v1.2.model';
import { FarmModelV1_3 } from './models/farm.v1.3.model';
import { FarmCustomModel } from './models/farm.custom.model';
Expand All @@ -10,18 +10,44 @@ import { FarmServiceV1_3 } from './v1.3/services/farm.v1.3.service';
import { FarmServiceV2 } from './v2/services/farm.v2.service';
import { FarmServiceBase } from './base-module/services/farm.base.service';
import { FarmModelV2 } from './models/farm.v2.model';
import { ContextTracker } from '@multiversx/sdk-nestjs-common';
import { FarmAbiServiceV1_2 } from './v1.2/services/farm.v1.2.abi.service';

@Injectable()
export class FarmFactoryService {
constructor(
private readonly farmServiceV1_2: FarmServiceV1_2,
private readonly farmServiceV1_3: FarmServiceV1_3,
private readonly farmServiceV2: FarmServiceV2,
private readonly farmAbi: FarmAbiServiceV1_2,
) {}

getFarms(): Array<typeof FarmsUnion> {
async getFarmsAddresses(versions?: string[]): Promise<string[]> {
let addresses = farmsAddresses(versions);

const context = ContextTracker.get();
if (context && context.deepHistoryTimestamp) {
const farmsDeployedTimestamps = await Promise.all(
addresses.map((address) =>
this.farmAbi.farmDeployedTimestamp(address),
),
);
addresses = addresses.filter((_, index) => {
return (
farmsDeployedTimestamps[index] !== undefined &&
farmsDeployedTimestamps[index] <=
context.deepHistoryTimestamp
);
});
}

return addresses;
}

async getFarms(): Promise<Array<typeof FarmsUnion>> {
const farms: Array<typeof FarmsUnion> = [];
for (const address of farmsAddresses()) {
const farmsAddresses = await this.getFarmsAddresses();
for (const address of farmsAddresses) {
const version = farmVersion(address);
switch (version) {
case FarmVersion.V1_2:
Expand Down
1 change: 0 additions & 1 deletion src/modules/farm/farm.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { FarmSetterFactory } from './farm.setter.factory';
ContextModule,
forwardRef(() => PairModule),
TokenModule,
FarmCustomModule,
FarmModuleV1_2,
FarmModuleV1_3,
FarmModuleV2,
Expand Down
4 changes: 4 additions & 0 deletions src/modules/farm/mocks/farm.abi.service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export class FarmAbiServiceMock implements IFarmAbiService {
async farmShard(farmAddress: string): Promise<number> {
return 1;
}

async farmDeployedTimestamp(farmAddress: string): Promise<number> {
return 1636895880;
}
}

export const FarmAbiServiceProvider = {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/farm/specs/farm.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe('FarmService', () => {

it('should get farms', async () => {
const factory = module.get<FarmFactoryService>(FarmFactoryService);
const farms = factory.getFarms();
const farms = await factory.getFarms();
expect(farms).toEqual([
{
address:
Expand Down
6 changes: 4 additions & 2 deletions src/modules/rabbitmq/rabbitmq.consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { RabbitMQProxyHandlerService } from './rabbitmq.proxy.handler.service';
import { CompetingRabbitConsumer } from './rabbitmq.consumers';
import { scAddress } from 'src/config';
import { RabbitMQEsdtTokenHandlerService } from './rabbitmq.esdtToken.handler.service';
import { farmsAddresses } from 'src/utils/farm.utils';
import { RouterHandlerService } from './handlers/router.handler.service';
import { RabbitMQMetabondingHandlerService } from './rabbitmq.metabonding.handler.service';
import { PriceDiscoveryEventHandler } from './handlers/price.discovery.handler.service';
Expand Down Expand Up @@ -64,6 +63,7 @@ import { RouterAbiService } from '../router/services/router.abi.service';
import { EscrowHandlerService } from './handlers/escrow.handler.service';
import { governanceContractsAddresses } from '../../utils/governance';
import { GovernanceHandlerService } from './handlers/governance.handler.service';
import { FarmFactoryService } from '../farm/farm.factory';

@Injectable()
export class RabbitMqConsumer {
Expand All @@ -72,6 +72,7 @@ export class RabbitMqConsumer {

constructor(
private readonly routerAbi: RouterAbiService,
private readonly farmFactory: FarmFactoryService,
private readonly liquidityHandler: LiquidityHandler,
private readonly swapHandler: SwapEventHandler,
private readonly wsFarmHandler: FarmHandlerService,
Expand Down Expand Up @@ -326,7 +327,8 @@ export class RabbitMqConsumer {
async getFilterAddresses(): Promise<void> {
this.filterAddresses = [];
this.filterAddresses = await this.routerAbi.getAllPairsAddressRaw();
this.filterAddresses.push(...farmsAddresses());
const farmsAddresses = await this.farmFactory.getFarmsAddresses();
this.filterAddresses.push(...farmsAddresses);
this.filterAddresses.push(scAddress.routerAddress);
this.filterAddresses.push(scAddress.metabondingStakingAddress);
this.filterAddresses.push(...scAddress.priceDiscovery);
Expand Down
Loading
Loading