From 9a7b560fa024c06680c9bb2abe9f093c11c9f69a Mon Sep 17 00:00:00 2001 From: artemkolodko Date: Sat, 14 Dec 2024 08:01:01 +0200 Subject: [PATCH] Fix bug in competition update --- src/app.service.ts | 4 ++-- src/indexer/indexer.service.ts | 23 ++++++++++++++--------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/app.service.ts b/src/app.service.ts index fdc4d5d..fb3badc 100644 --- a/src/app.service.ts +++ b/src/app.service.ts @@ -111,10 +111,10 @@ export class AppService { }) } - async getCompetitions(dto: GetCompetitionsDto = {}) { + async getCompetitions(dto: GetCompetitionsDto = {}, entityManager?: EntityManager) { const {offset = 0, limit = 100, competitionId} = dto - return await this.dataSource.manager.find(CompetitionEntity, { + return await (entityManager || this.dataSource.manager).find(CompetitionEntity, { relations: ['winnerToken'], where: { competitionId diff --git a/src/indexer/indexer.service.ts b/src/indexer/indexer.service.ts index efd436c..efc3dc1 100644 --- a/src/indexer/indexer.service.ts +++ b/src/indexer/indexer.service.ts @@ -28,7 +28,7 @@ export class IndexerService { private readonly accountAddress: string private readonly tokenFactoryContract: Contract private readonly maxBlocksRange = 1000 - private readonly maxBlocksBatchSize = 5 + private readonly maxBlocksBatchSize = 10 constructor( private configService: ConfigService, @@ -386,15 +386,20 @@ export class IndexerService { const competitionId = Number(values['competitionId'] as bigint) const timestamp = Number(values['timestamp'] as bigint) - const competitions = await this.appService.getCompetitions({ limit: 1 }) - if(competitions.length > 0) { - const currentCompetition = competitions[0] - if(currentCompetition) { - currentCompetition.isCompleted = true - currentCompetition.timestampEnd = timestamp - await transactionalEntityManager.save(currentCompetition) - } + const prevCompetitionId = competitionId - 1 + const [prevCompetition] = await this.appService.getCompetitions({ + competitionId: prevCompetitionId + }, transactionalEntityManager) + if(prevCompetition) { + prevCompetition.isCompleted = true + prevCompetition.timestampEnd = timestamp + await transactionalEntityManager.save(prevCompetition) + } else { + if(competitionId > 2) { + this.logger.error(`Failed to get prev competition=${prevCompetitionId}, new competitionId=${competitionId}, exit`) + process.exit(1) + } } await transactionalEntityManager.insert(CompetitionEntity, {