@@ -3,7 +3,7 @@ import {Contract, ContractAbi, EventLog, Web3} from "web3";
33import { TokenMetadata , TradeEventLog , TradeType } from "../types" ;
44import axios from "axios" ;
55import process from "process" ;
6- import { IndexerState , Token , TokenBalance , Trade } from "../entities" ;
6+ import { IndexerState , Token , TokenBalance , TokenWinner , Trade } from "../entities" ;
77import { ConfigService } from "@nestjs/config" ;
88import { UserService } from "../user/user.service" ;
99import { DataSource } from "typeorm" ;
@@ -83,6 +83,42 @@ export class IndexerService {
8383 }
8484 }
8585
86+ private async processSetWinnerEvents ( events : EventLog [ ] ) {
87+ for ( const event of events ) {
88+ const txnHash = event . transactionHash . toLowerCase ( )
89+ const blockNumber = Number ( event . blockNumber )
90+ const values = event . returnValues
91+ const winnerAddress = ( values [ 'winner' ] as string ) . toLowerCase ( )
92+ const timestamp = String ( values [ 'timestamp' ] as bigint )
93+
94+ const existedWinner = await this . dataSource . manager . findOne ( TokenWinner , {
95+ where : {
96+ token : {
97+ address : winnerAddress
98+ } ,
99+ timestamp
100+ }
101+ } )
102+
103+ if ( ! existedWinner ) {
104+ const token = await this . appService . getTokenByAddress ( winnerAddress )
105+ if ( ! token ) {
106+ this . logger . error ( `Winner token entry not found in database, winnerAddress=${ winnerAddress } , exit` )
107+ process . exit ( 1 )
108+ }
109+ await this . dataSource . manager . insert ( TokenWinner , {
110+ token,
111+ timestamp,
112+ txnHash,
113+ blockNumber
114+ } )
115+ this . logger . log ( `Added new token winner=${ winnerAddress } , timestamp=${ timestamp } ` )
116+ } else {
117+ this . logger . warn ( `Token winner=${ winnerAddress } , timestamp=${ timestamp } already exists, skip` )
118+ }
119+ }
120+ }
121+
86122 private async processTradeEvents ( events : TradeEventLog [ ] ) {
87123 for ( const event of events ) {
88124 const { type, data } = event
@@ -208,6 +244,10 @@ export class IndexerService {
208244 }
209245
210246 if ( toBlock - fromBlock >= 1 ) {
247+ const setWinnerEvents = await this . tokenFactoryContract . getPastEvents ( 'allEvents' , {
248+ fromBlock, toBlock, topics : [ this . web3 . utils . sha3 ( 'SetWinner(address,uint256)' ) ] ,
249+ } ) as EventLog [ ] ;
250+
211251 const tokenCreatedEvents = await this . tokenFactoryContract . getPastEvents ( 'allEvents' , {
212252 fromBlock,
213253 toBlock,
@@ -289,9 +329,10 @@ export class IndexerService {
289329 this . logger . log ( `Create token: address=${ tokenAddress } , name=${ name } , symbol=${ symbol } , uri=${ uri } , creator=${ creatorAddress } , txnHash=${ txnHash } ` ) ;
290330 }
291331
332+ await this . processSetWinnerEvents ( setWinnerEvents )
292333 await this . processTradeEvents ( tradeEvents )
293334
294- this . logger . log ( `[${ fromBlock } -${ toBlock } ] (${ ( ( toBlock - fromBlock + 1 ) ) } blocks), new tokens=${ tokenCreatedEvents . length } , trade=${ [ ...buyEvents , ...sellEvents ] . length } (buy=${ buyEvents . length } , sell=${ sellEvents . length } )` )
335+ this . logger . log ( `[${ fromBlock } -${ toBlock } ] (${ ( ( toBlock - fromBlock + 1 ) ) } blocks), new tokens=${ tokenCreatedEvents . length } , trade=${ [ ...buyEvents , ...sellEvents ] . length } (buy=${ buyEvents . length } , sell=${ sellEvents . length } ), setWinner= ${ setWinnerEvents . length } ` )
295336 } else {
296337 // Wait for blockchain
297338 toBlock = fromBlockParam - 1
0 commit comments