-
Notifications
You must be signed in to change notification settings - Fork 656
/
MonitorCli.js
111 lines (96 loc) · 2.97 KB
/
MonitorCli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { cli, Log } from 'decentraland-commons'
import { Handlers } from './handlers'
import { EventMonitor } from './EventMonitor'
import { processEvents } from './processEvents'
import { BlockchainEvent } from '../../src/BlockchainEvent'
const log = new Log('MonitorCli')
export class MonitorCli {
constructor(handlers, contractEvents = {}, processDelay) {
this.handlers = new Handlers(handlers)
this.contractEvents = contractEvents
this.processDelay = processDelay
this.processTimeout = null
this.isProcessRunning = false
}
run() {
cli.runProgram([this])
}
addCommands(program) {
program
.command('index')
.option(
'--args [args]',
'JSON string containing args to filter by. Defaults to {}'
)
.option(
'--from-block [fromBlock]',
'The number of the earliest block. Defaults to 0'
)
.option(
'--to-block [toBlock]',
'The number of the latest block. Defaults to `latest`'
)
.option(
'--address [address]',
'An address to only get logs from particular account(s).'
)
.option(
'-w, --watch',
'Keep watching the blockchain for new events after --to-block.'
)
.option(
'--skip-process',
'Only restore the stored events, without processing each one'
)
.allowUnknownOption()
.action(this.index)
}
index = options => {
for (const contractName in this.contractEvents) {
const eventNames = this.contractEvents[contractName]
this.monitor(contractName, eventNames, options)
}
}
processStoredEvents = fromBlock => {
if (this.isProcessRunning) {
return setTimeout(
() => this.processStoredEvents(fromBlock),
this.processDelay
)
}
clearTimeout(this.processTimeout)
this.processTimeout = setTimeout(() => {
this.isProcessRunning = true
this.processEvents(fromBlock).then(() => (this.isProcessRunning = false))
}, this.processDelay)
}
processEvents(fromBlock) {
return processEvents(fromBlock)
}
async monitor(contractName, eventNames, options) {
const eventMonitor = new EventMonitor(contractName, eventNames)
const handler = this.handlers.get('index', contractName, eventNames)
if (!handler) throw new Error('Could not find a valid handler')
const fromBlock = await this.getFromBlock(options)
eventMonitor.run(options, async (error, logs) => {
if (error) {
log.error(`Error monitoring "${contractName}" for "${eventNames}"`)
log.error(error)
} else {
if (Array.isArray(logs)) {
await Promise.all(logs.map(log => handler(log)))
} else {
await handler(logs)
}
if (!options.skipProcess) {
this.processStoredEvents(fromBlock)
}
}
})
}
async getFromBlock(options) {
return options.fromBlock === 'latest'
? await BlockchainEvent.findLastBlockNumber()
: options.fromBlock
}
}