|
|
@ -1,5 +1,4 @@ |
|
|
|
'use strict' |
|
|
|
'use strict' |
|
|
|
import { each } from 'async' |
|
|
|
|
|
|
|
import { ethers } from 'ethers' |
|
|
|
import { ethers } from 'ethers' |
|
|
|
import { toBuffer, addHexPrefix } from 'ethereumjs-util' |
|
|
|
import { toBuffer, addHexPrefix } from 'ethereumjs-util' |
|
|
|
import { EventManager } from '../eventManager' |
|
|
|
import { EventManager } from '../eventManager' |
|
|
@ -34,8 +33,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
|
|
|
@ -107,8 +105,7 @@ export class TxListener { |
|
|
|
addExecutionCosts(txResult, tx, execResult) |
|
|
|
addExecutionCosts(txResult, tx, execResult) |
|
|
|
tx.envMode = this.executionContext.getProvider() |
|
|
|
tx.envMode = this.executionContext.getProvider() |
|
|
|
tx.status = txResult.receipt.status // 0x0 or 0x1
|
|
|
|
tx.status = txResult.receipt.status // 0x0 or 0x1
|
|
|
|
this._resolve([tx], () => { |
|
|
|
this._resolve([tx]) |
|
|
|
}) |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
@ -123,9 +120,7 @@ export class TxListener { |
|
|
|
if (this._loopId) { |
|
|
|
if (this._loopId) { |
|
|
|
clearInterval(this._loopId) |
|
|
|
clearInterval(this._loopId) |
|
|
|
} |
|
|
|
} |
|
|
|
if (this._listenOnNetwork) { |
|
|
|
this._listenOnNetwork ? this.startListening() : this.stopListening() |
|
|
|
this._startListenOnNetwork() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -133,7 +128,6 @@ export class TxListener { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
init () { |
|
|
|
init () { |
|
|
|
this.blocks = [] |
|
|
|
this.blocks = [] |
|
|
|
this.lastBlock = -1 |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -164,34 +158,54 @@ export class TxListener { |
|
|
|
this._isListening = false |
|
|
|
this._isListening = false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_startListenOnNetwork () { |
|
|
|
async _startListenOnNetwork () { |
|
|
|
this._loopId = setInterval(() => { |
|
|
|
let lastSeenBlock = this.executionContext.lastBlock?.number - 1 |
|
|
|
|
|
|
|
let processingBlock = false |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const processBlocks = async () => { |
|
|
|
|
|
|
|
if (!this._isListening) return |
|
|
|
|
|
|
|
if (processingBlock) return |
|
|
|
|
|
|
|
processingBlock = true |
|
|
|
const currentLoopId = this._loopId |
|
|
|
const currentLoopId = this._loopId |
|
|
|
this.executionContext.web3().eth.getBlockNumber((error, blockNumber) => { |
|
|
|
if (this._loopId === null) { |
|
|
|
if (this._loopId === null) return |
|
|
|
processingBlock = false |
|
|
|
if (error) return console.log(error) |
|
|
|
return |
|
|
|
if (currentLoopId === this._loopId && blockNumber > this.lastBlock) { |
|
|
|
} |
|
|
|
let current = this.lastBlock + 1 |
|
|
|
if (!lastSeenBlock) { |
|
|
|
this.lastBlock = blockNumber |
|
|
|
lastSeenBlock = this.executionContext.lastBlock?.number // trying to resynchronize
|
|
|
|
while (blockNumber >= current) { |
|
|
|
console.log('listen on blocks, resynchronising') |
|
|
|
try { |
|
|
|
processingBlock = false |
|
|
|
this._manageBlock(current) |
|
|
|
return |
|
|
|
} catch (e) { |
|
|
|
} |
|
|
|
console.log(e) |
|
|
|
const current = this.executionContext.lastBlock?.number |
|
|
|
} |
|
|
|
if (!current) { |
|
|
|
current++ |
|
|
|
console.log(new Error('no last block found')) |
|
|
|
|
|
|
|
processingBlock = false |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (currentLoopId === this._loopId && lastSeenBlock < current) { |
|
|
|
|
|
|
|
while (lastSeenBlock <= current) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
if (!this._isListening) break |
|
|
|
|
|
|
|
await this._manageBlock(lastSeenBlock) |
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
|
|
console.log(e) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
lastSeenBlock++ |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
lastSeenBlock = current |
|
|
|
}, 2000) |
|
|
|
} |
|
|
|
|
|
|
|
processingBlock = false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
this._loopId = setInterval(processBlocks, 20000) |
|
|
|
|
|
|
|
processBlocks() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_manageBlock (blockNumber) { |
|
|
|
async _manageBlock (blockNumber) { |
|
|
|
this.executionContext.web3().eth.getBlock(blockNumber, true, (error, result) => { |
|
|
|
try { |
|
|
|
if (!error) { |
|
|
|
const result = await this.executionContext.web3().eth.getBlock(blockNumber, true) |
|
|
|
this._newBlock(Object.assign({ type: 'web3' }, result)) |
|
|
|
return await this._newBlock(Object.assign({ type: 'web3' }, result))
|
|
|
|
} |
|
|
|
} catch (e) {} |
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -215,31 +229,37 @@ export class TxListener { |
|
|
|
return this._resolvedTransactions[txHash] |
|
|
|
return this._resolvedTransactions[txHash] |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_newBlock (block) { |
|
|
|
async _newBlock (block) { |
|
|
|
this.blocks.push(block) |
|
|
|
this.blocks.push(block) |
|
|
|
this._resolve(block.transactions, () => { |
|
|
|
await this._resolve(block.transactions) |
|
|
|
this.event.trigger('newBlock', [block]) |
|
|
|
this.event.trigger('newBlock', [block]) |
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_resolve (transactions, callback) { |
|
|
|
_resolveAsync (tx) { |
|
|
|
each(transactions, (tx, cb) => { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
this._api.resolveReceipt(tx, (error, receipt) => { |
|
|
|
this._api.resolveReceipt(tx, (error, receipt) => { |
|
|
|
if (error) return cb(error) |
|
|
|
if (error) return reject(error) |
|
|
|
this._resolveTx(tx, receipt, (error, resolvedData) => { |
|
|
|
this._resolveTx(tx, receipt, (error, resolvedData) => { |
|
|
|
if (error) cb(error) |
|
|
|
if (error) return reject(error) |
|
|
|
if (resolvedData) { |
|
|
|
if (resolvedData) { |
|
|
|
this.event.trigger('txResolved', [tx, receipt, resolvedData]) |
|
|
|
this.event.trigger('txResolved', [tx, receipt, resolvedData]) |
|
|
|
} |
|
|
|
} |
|
|
|
this.event.trigger('newTransaction', [tx, receipt]) |
|
|
|
this.event.trigger('newTransaction', [tx, receipt]) |
|
|
|
cb() |
|
|
|
resolve({}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}, () => { |
|
|
|
|
|
|
|
callback() |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async _resolve (transactions) { |
|
|
|
|
|
|
|
for (const tx of transactions) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
if (!this._isListening) break |
|
|
|
|
|
|
|
await this._resolveAsync(tx) |
|
|
|
|
|
|
|
} catch (e) {} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
_resolveTx (tx, receipt, cb) { |
|
|
|
_resolveTx (tx, receipt, cb) { |
|
|
|
const contracts = this._api.contracts() |
|
|
|
const contracts = this._api.contracts() |
|
|
|
if (!contracts) return cb() |
|
|
|
if (!contracts) return cb() |
|
|
|