99
1010import { ethers } from 'ethers' ;
1111import dotenv from 'dotenv'
12- import TaskManagerAbi from './abi/TaskManagerAbi.json' with { type : 'json' } ; // compiled ABI
12+ import TaskManagerAbi from '../../.. /abi/TaskManagerAbi.json' with { type : 'json' } ; // compiled ABI
1313import Db from '../db/Db.js' ;
1414
1515// Load env variables
@@ -95,15 +95,28 @@ private eventQueue: Map<number, { blockHash: string, handlers: (() => Promise<vo
9595 txHash : log . transactionHash ,
9696 blockNumber : log . blockNumber
9797 } )
98- // Save to Postgres
99- await this . db . saveTask ( {
100- taskId : Number ( taskId ) ,
101- name,
102- difficulty,
103- time : Number ( time ) ,
104- uri : URI ,
105- txHash : log . transactionHash
106- } ) ;
98+ // If log.blockNumber >= currentBlock - FINALITY_BLOCKS => Save to Postgres
99+ if ( ( log . blockNumber - this . FINALITY_BLOCKS ) >= 0 ) {
100+ await this . db . saveTask ( {
101+ taskId : Number ( taskId ) ,
102+ name,
103+ difficulty,
104+ time : Number ( time ) ,
105+ uri : URI ,
106+ txHash : log . transactionHash
107+ } ) ;
108+ } else { // Else add it to eventQueue
109+ this . _queueEvent ( log . blockNumber , log . blockHash , async ( ) => {
110+ await this . db . saveTask ( {
111+ taskId : Number ( taskId ) ,
112+ name,
113+ difficulty,
114+ time : Number ( time ) ,
115+ uri : URI ,
116+ txHash : log . transactionHash ,
117+ } ) ;
118+ } )
119+ }
107120 } ) ;
108121
109122 const filterDeactivated = this . contract . filters . TaskDeactivated ! ( )
@@ -114,10 +127,16 @@ private eventQueue: Map<number, { blockHash: string, handlers: (() => Promise<vo
114127 taskId : taskId . toString ( ) ,
115128 txHash : log . transactionHash ,
116129 } )
117- await this . db . deactivateTask ( Number ( taskId ) , log . transactionHash )
130+ if ( ( log . blockNumber - this . FINALITY_BLOCKS ) ) {
131+ await this . db . deactivateTask ( Number ( taskId ) , log . transactionHash )
132+ } else {
133+ this . _queueEvent ( log . blockNumber , log . blockHash , async ( ) => {
134+ await this . db . deactivateTask ( Number ( taskId ) , log . transactionHash ) ;
135+ } ) ;
136+ }
118137 } )
119138
120- await this . db . updateLastProcessedBlock ( toBlock ) ;
139+ await this . db . updateLastProcessedBlock ( toBlock - this . FINALITY_BLOCKS ) ;
121140 }
122141
123142 // Backfill past events from the deployed block to the latest block
@@ -126,7 +145,7 @@ private eventQueue: Map<number, { blockHash: string, handlers: (() => Promise<vo
126145 const currentBlock = await this . provider . getBlockNumber ( ) ;
127146 let fromBlock = ( await this . db . getLastProcessedBlock ( ) ) ?? this . START_BLOCK ;
128147 if ( fromBlock > currentBlock ) fromBlock = currentBlock ;
129- await this . _fetchPastEvents ( fromBlock , currentBlock - this . FINALITY_BLOCKS ) ;
148+ await this . _fetchPastEvents ( fromBlock , currentBlock ) ;
130149 }
131150
132151 /// Adds emmited event into the processing queue - handler executes the action once after the blockNumber fill be considerd final
0 commit comments