|
|
@ -34,8 +34,7 @@ export class TxListener { |
|
|
|
_listenOnNetwork:boolean |
|
|
|
_listenOnNetwork:boolean |
|
|
|
_loopId |
|
|
|
_loopId |
|
|
|
blocks |
|
|
|
blocks |
|
|
|
lastBlock |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor (opt, executionContext) { |
|
|
|
constructor (opt, executionContext) { |
|
|
|
this.event = new EventManager() |
|
|
|
this.event = new EventManager() |
|
|
|
// has a default for now for backwards compatability
|
|
|
|
// has a default for now for backwards compatability
|
|
|
@ -123,9 +122,7 @@ export class TxListener { |
|
|
|
if (this._loopId) { |
|
|
|
if (this._loopId) { |
|
|
|
clearInterval(this._loopId) |
|
|
|
clearInterval(this._loopId) |
|
|
|
} |
|
|
|
} |
|
|
|
if (this._listenOnNetwork) { |
|
|
|
this._listenOnNetwork ? this._startListenOnNetwork() : this.stopListening() |
|
|
|
this._startListenOnNetwork() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -133,7 +130,6 @@ export class TxListener { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
init () { |
|
|
|
init () { |
|
|
|
this.blocks = [] |
|
|
|
this.blocks = [] |
|
|
|
this.lastBlock = -1 |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -164,26 +160,33 @@ export class TxListener { |
|
|
|
this._isListening = false |
|
|
|
this._isListening = false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_startListenOnNetwork () { |
|
|
|
async _startListenOnNetwork () { |
|
|
|
|
|
|
|
let lastSeenBlock = this.executionContext.lastBlock?.number |
|
|
|
this._loopId = setInterval(() => { |
|
|
|
this._loopId = setInterval(() => { |
|
|
|
const currentLoopId = this._loopId |
|
|
|
const currentLoopId = this._loopId |
|
|
|
this.executionContext.web3().eth.getBlockNumber((error, blockNumber) => { |
|
|
|
if (this._loopId === null) return |
|
|
|
if (this._loopId === null) return |
|
|
|
if (!lastSeenBlock) { |
|
|
|
if (error) return console.log(error) |
|
|
|
lastSeenBlock = this.executionContext.lastBlock?.number // trying to resynchronize
|
|
|
|
if (currentLoopId === this._loopId && blockNumber > this.lastBlock) { |
|
|
|
console.log('listen on blocks, resynchronising') |
|
|
|
let current = this.lastBlock + 1 |
|
|
|
return |
|
|
|
this.lastBlock = blockNumber |
|
|
|
} |
|
|
|
while (blockNumber >= current) { |
|
|
|
const current = this.executionContext.lastBlock?.number |
|
|
|
try { |
|
|
|
if (!current) { |
|
|
|
this._manageBlock(current) |
|
|
|
console.error(new Error('no last block found')) |
|
|
|
} catch (e) { |
|
|
|
return |
|
|
|
console.log(e) |
|
|
|
} |
|
|
|
} |
|
|
|
if (currentLoopId === this._loopId && lastSeenBlock < current) { |
|
|
|
current++ |
|
|
|
while (lastSeenBlock <= current) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
this._manageBlock(lastSeenBlock) |
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
|
|
console.log(e) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
lastSeenBlock++ |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
lastSeenBlock = current |
|
|
|
}, 2000) |
|
|
|
} |
|
|
|
|
|
|
|
}, 10000) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_manageBlock (blockNumber) { |
|
|
|
_manageBlock (blockNumber) { |
|
|
|