@@ -2,15 +2,14 @@ import {Injectable, Logger} from '@nestjs/common';
22import { Contract , ContractAbi , EventLog , Web3 } from "web3" ;
33import { TokenMetadata , TradeType } from "../types" ;
44import axios from "axios" ;
5- import process from "process" ;
5+ import * as process from "process" ;
66import {
77 CompetitionEntity ,
88 IndexerState ,
99 LiquidityProvision ,
1010 Token ,
1111 TokenBalance ,
1212 TokenBurn ,
13- TokenWinner ,
1413 Trade
1514} from "../entities" ;
1615import { ConfigService } from "@nestjs/config" ;
@@ -107,37 +106,27 @@ export class IndexerService {
107106 return
108107 }
109108
110- const existedWinner = await transactionalEntityManager . findOne ( TokenWinner , {
111- where : {
112- token : {
113- address : winnerAddress ,
114- } ,
115- competitionId ,
116- timestamp
117- }
109+ const token = await this . appService . getTokenByAddress ( winnerAddress , transactionalEntityManager )
110+ if ( ! token ) {
111+ this . logger . error ( `Failed to add winner: winner token not found in database, winnerAddress= ${ winnerAddress } , exit` )
112+ process . exit ( 1 )
113+ }
114+
115+ const competition = await transactionalEntityManager . findOne ( CompetitionEntity , {
116+ where : { competitionId }
118117 } )
118+ if ( ! competition ) {
119+ this . logger . error ( `Failed to add winner: competition=${ competitionId } not found in database, exit` )
120+ process . exit ( 1 )
121+ }
119122
120- if ( ! existedWinner ) {
121- const token = await this . appService . getTokenByAddress ( winnerAddress , transactionalEntityManager )
122- if ( ! token ) {
123- this . logger . error ( `Failed to add winner: winner token not found in database, winnerAddress=${ winnerAddress } , exit` )
124- process . exit ( 1 )
125- }
123+ token . isWinner = true
124+ await transactionalEntityManager . save ( token )
126125
127- token . isWinner = true
126+ competition . winnerToken = token
127+ await transactionalEntityManager . save ( competition )
128128
129- await transactionalEntityManager . save ( token )
130- await transactionalEntityManager . insert ( TokenWinner , {
131- token,
132- timestamp,
133- competitionId,
134- txnHash,
135- blockNumber
136- } )
137- this . logger . log ( `Added new token winner=${ winnerAddress } , competitionId=${ competitionId } , timestamp=${ timestamp } ` )
138- } else {
139- this . logger . warn ( `Token winner=${ winnerAddress } , competitionId=${ competitionId } , timestamp=${ timestamp } already exists, skip` )
140- }
129+ this . logger . log ( `Added new token winner=${ winnerAddress } , competitionId=${ competitionId } , timestamp=${ timestamp } ` )
141130 }
142131
143132 private async processCreateTokenEvent ( event : EventLog , transactionalEntityManager : EntityManager ) {
@@ -170,17 +159,32 @@ export class IndexerService {
170159 }
171160 }
172161
162+ const competition = await transactionalEntityManager . findOne ( CompetitionEntity , {
163+ where : { } ,
164+ order : {
165+ competitionId : 'DESC'
166+ }
167+ } )
168+ if ( ! competition ) {
169+ this . logger . error ( `Create token: current competition is missing in DB; exit` )
170+ process . exit ( 1 )
171+ }
172+ if ( competition . isCompleted ) {
173+ this . logger . error ( `Create token: current competition is completed, new competitions has not started yet; exit` )
174+ process . exit ( 1 )
175+ }
176+
173177 await transactionalEntityManager . insert ( Token , {
174178 txnHash,
175179 address : tokenAddress ,
176180 blockNumber : Number ( event . blockNumber ) ,
177181 name,
178182 symbol,
179- competitionId,
180183 timestamp,
181184 user,
182185 uri,
183186 uriData,
187+ competition
184188 } ) ;
185189 this . logger . log ( `Create token: address=${ tokenAddress } , name=${ name } , symbol=${ symbol } , uri=${ uri } , creator=${ creatorAddress } , competitionId=${ competitionId } , txnHash=${ txnHash } ` ) ;
186190 }
@@ -370,11 +374,26 @@ export class IndexerService {
370374 const values = event . returnValues
371375 const competitionId = Number ( values [ 'competitionId' ] as bigint )
372376 const timestamp = Number ( values [ 'timestamp' ] as bigint )
377+
378+ const competitions = await this . appService . getCompetitions ( )
379+ if ( competitions . length > 0 ) {
380+ const currentCompetition = competitions [ 0 ]
381+ if ( currentCompetition ) {
382+ currentCompetition . isCompleted = true
383+ currentCompetition . timestampEnd = timestamp
384+ await transactionalEntityManager . save ( currentCompetition )
385+ }
386+
387+ }
388+
373389 await transactionalEntityManager . insert ( CompetitionEntity , {
374390 txnHash,
375391 blockNumber : Number ( event . blockNumber ) ,
376392 competitionId,
377- timestamp,
393+ timestampStart : timestamp ,
394+ timestampEnd : null ,
395+ isCompleted : false ,
396+ winnerToken : null ,
378397 } ) ;
379398 this . logger . log ( `NewCompetitionStarted: competitionId=${ competitionId } , timestamp=${ timestamp } , txnHash=${ txnHash } ` ) ;
380399 }
@@ -413,6 +432,10 @@ export class IndexerService {
413432 }
414433
415434 if ( toBlock - fromBlock >= 1 ) {
435+ const newCompetitionEvents = await this . tokenFactoryContract . getPastEvents ( 'allEvents' , {
436+ fromBlock, toBlock, topics : [ this . web3 . utils . sha3 ( 'NewCompetitionStarted(uint256,uint256)' ) ] ,
437+ } ) as EventLog [ ] ;
438+
416439 const setWinnerEvents = await this . tokenFactoryContract . getPastEvents ( 'allEvents' , {
417440 fromBlock, toBlock, topics : [ this . web3 . utils . sha3 ( 'SetWinner(address,uint256,uint256)' ) ] ,
418441 } ) as EventLog [ ] ;
@@ -421,10 +444,6 @@ export class IndexerService {
421444 fromBlock, toBlock, topics : [ this . web3 . utils . sha3 ( 'WinnerLiquidityAdded(address,address,address,address,uint256,uint128,uint256,uint256,uint256)' ) ] ,
422445 } ) as EventLog [ ] ;
423446
424- const newCompetitionEvents = await this . tokenFactoryContract . getPastEvents ( 'allEvents' , {
425- fromBlock, toBlock, topics : [ this . web3 . utils . sha3 ( 'NewCompetitionStarted(uint256,uint256)' ) ] ,
426- } ) as EventLog [ ] ;
427-
428447 const tokenCreatedEvents = await this . tokenFactoryContract . getPastEvents ( 'allEvents' , {
429448 fromBlock,
430449 toBlock,
0 commit comments