Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
feat: add timeout for emitting reorgs
Browse files Browse the repository at this point in the history
  • Loading branch information
nduchak committed Aug 26, 2020
1 parent 754b2bc commit b6badf2
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/blockchain/reorg-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ const DEFAULT_DEBOUNCE_TIME = 5000
const REORG_EVENT = 'reorg-event'

export class ReorgEmitterService implements Partial<ServiceMethods<any>> {
private readonly debounceTime: number = DEFAULT_DEBOUNCE_TIME
private readonly debounceTime: number = DEFAULT_DEBOUNCE_TIME // ms
private reorgContract: string[] = []
private lastProcessedBlockNumber = 0
private timeoutStarted = false
emit?: Function
events: string[]

Expand All @@ -22,7 +25,21 @@ export class ReorgEmitterService implements Partial<ServiceMethods<any>> {
if (!this.emit) {
throw new Error('ReorgEmitterService invalid setup. Missing \'emit\' function')
}

if (!this.timeoutStarted) {
setTimeout(() => {
if (this.emit) {
this.emit({ contracts: this.reorgContract, lastProcessedBlockNumber: this.lastProcessedBlockNumber })
}
this.reorgContract = []
this.lastProcessedBlockNumber = 0
})
this.timeoutStarted = true
}

this.reorgContract = [...this.reorgContract, contractName]
this.lastProcessedBlockNumber = lastProcessedBlockNumber

logger.info(`Reorg happens on block number ${lastProcessedBlockNumber} for ${contractName} contract`)
this.emit(REORG_EVENT, { lastProcessedBlockNumber, contractName })
}
}

0 comments on commit b6badf2

Please sign in to comment.