Skip to content

Commit

Permalink
feat: triggers history sync on new tx
Browse files Browse the repository at this point in the history
  • Loading branch information
kenodressel authored and mmpetarpeshev committed Jun 25, 2024
1 parent 5c6b99e commit 138d45c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
12 changes: 11 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@ import { AppController } from '@/app.controller';
import { ClientsModule } from '@/clients/clients.module';
import { MdwWsClientService } from '@/clients/mdw-ws-client.service';
import { DatabaseModule } from '@/database/database.module';
import { PairLiquidityInfoHistoryImporterService } from '@/tasks/pair-liquidity-info-history-importer/pair-liquidity-info-history-importer.service';
import { PairPathCalculatorService } from '@/tasks/pair-path-calculator/pair-path-calculator.service';
import { PairSyncService } from '@/tasks/pair-sync/pair-sync.service';
import { TasksModule } from '@/tasks/tasks.module';

@Module({
imports: [ApiModule, ClientsModule, DatabaseModule, TasksModule],
controllers: [AppController],
providers: [MdwWsClientService, PairsService, TokensService, PairSyncService],
providers: [
MdwWsClientService,
PairsService,
TokensService,
PairSyncService,
// FIXME probably not the right place but did not get it to work any otherway
PairLiquidityInfoHistoryImporterService,
PairPathCalculatorService,
],
})
export class AppModule {}
7 changes: 1 addition & 6 deletions src/tasks/pair-sync/pair-sync.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,7 @@ describe('PairSyncService', () => {
};
const { onFactory, refreshPairsLiquidity, getAllAddresses } = mock<Ev>();
const logger = mock<Logger>();
const eventHandler = service['createOnEventReceived'](
logger,
onFactory,
refreshPairsLiquidity,
getAllAddresses,
);
const eventHandler = service['createOnEventReceived'];
return {
getAllAddresses,
logger,
Expand Down
18 changes: 13 additions & 5 deletions src/tasks/pair-sync/pair-sync.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { SdkClientService } from '@/clients/sdk-client.service';
import { PairDbService } from '@/database/pair/pair-db.service';
import { TokenDbService } from '@/database/token/token-db.service';
import { nonNullable } from '@/lib/utils';
import { PairLiquidityInfoHistoryImporterService } from '@/tasks/pair-liquidity-info-history-importer/pair-liquidity-info-history-importer.service';
import { PairPathCalculatorService } from '@/tasks/pair-path-calculator/pair-path-calculator.service';
import {
Aex9Methods,
Context,
Expand All @@ -28,6 +30,8 @@ export class PairSyncService implements OnModuleInit {
private readonly pairDb: PairDbService,
private readonly mdwWsClient: MdwWsClientService,
private readonly sdkClient: SdkClientService,
private readonly pairLiquidityInfoHistoryImporterService: PairLiquidityInfoHistoryImporterService,
private readonly pairPathCalculatorService: PairPathCalculatorService,
) {}

readonly logger = new Logger(PairSyncService.name);
Expand Down Expand Up @@ -249,11 +253,15 @@ export class PairSyncService implements OnModuleInit {
// the factory event handler was also involved here and will take care of the
// newly created pair
else if (addresses[contract]) {
// TODO trigger history sync here as well
return this.refreshPairLiquidityByAddress(
contract,
event.payload.block_height,
);
return Promise.all([
this.refreshPairLiquidityByAddress(
contract,
event.payload.block_height,
),
this.pairLiquidityInfoHistoryImporterService.import().then(() => {
this.pairPathCalculatorService.sync();
}),
]);
}
return Promise.resolve();
});
Expand Down
10 changes: 6 additions & 4 deletions src/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Injectable } from '@nestjs/common';
import { Cron, CronExpression } from '@nestjs/schedule';
import { Cron } from '@nestjs/schedule';

import { PairLiquidityInfoHistoryImporterService } from '@/tasks/pair-liquidity-info-history-importer/pair-liquidity-info-history-importer.service';
import { PairLiquidityInfoHistoryValidatorService } from '@/tasks/pair-liquidity-info-history-validator/pair-liquidity-info-history-validator.service';
import { PairPathCalculatorService } from '@/tasks/pair-path-calculator/pair-path-calculator.service';

const EVERY_5_MINUTES_STARTING_AT_01_30 = '30 1-56/5 * * * *';
const EVERY_5_MINUTES_STARTING_AT_02_30 = '30 2-57/5 * * * *';
const EVERY_5_MINUTES_STARTING_AT_03_30 = '30 3-58/5 * * * *';

@Injectable()
export class TasksService {
Expand All @@ -25,7 +27,7 @@ export class TasksService {
this._isRunning = isRunning;
}

@Cron(CronExpression.EVERY_5_MINUTES)
@Cron(EVERY_5_MINUTES_STARTING_AT_01_30)
async runPairLiquidityInfoHistoryImporter() {
try {
if (!this.isRunning) {
Expand All @@ -41,7 +43,7 @@ export class TasksService {
}
}

@Cron(CronExpression.EVERY_10_SECONDS)
@Cron(EVERY_5_MINUTES_STARTING_AT_02_30)
async runPairLiquidityInfoHistoryExchangeRateCalculator() {
try {
if (!this.isRunning) {
Expand All @@ -58,7 +60,7 @@ export class TasksService {
}
}

@Cron(EVERY_5_MINUTES_STARTING_AT_02_30)
@Cron(EVERY_5_MINUTES_STARTING_AT_03_30)
async runPairLiquidityInfoHistoryValidator() {
try {
if (!this.isRunning) {
Expand Down

0 comments on commit 138d45c

Please sign in to comment.