Skip to content

Commit

Permalink
Fix bug in competition update
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtemKolodko committed Dec 14, 2024
1 parent 46ac11b commit 9a7b560
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 14 additions & 9 deletions src/indexer/indexer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class IndexerService {
private readonly accountAddress: string
private readonly tokenFactoryContract: Contract<ContractAbi>
private readonly maxBlocksRange = 1000
private readonly maxBlocksBatchSize = 5
private readonly maxBlocksBatchSize = 10

constructor(
private configService: ConfigService,
Expand Down Expand Up @@ -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, {
Expand Down

0 comments on commit 9a7b560

Please sign in to comment.