-
Notifications
You must be signed in to change notification settings - Fork 7
/
chainsaw.js
193 lines (173 loc) · 6.08 KB
/
chainsaw.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import p from 'es6-promisify'
const abiDecoder = require('abi-decoder')
const wait = ms => new Promise(resolve => setTimeout(resolve, ms))
// Lives inside a long-running process
// Usage patterns:
// 1. Polling - given a set of contract addresses, emit all events logged for that contract
// 2. Querying - given a block range and an address, return all events logged
/**
** Chainsaw is a ethereum log extractor and log decoding library.
**/
class Chainsaw {
constructor (web3, contractAddresses = [], pollingInterval = 1000) {
this.web3 = web3
this.eth = web3.eth
this.contractAddresses = contractAddresses
this.isPolling = false
this.lastReadBlockNumber = this.eth.blockNumber
this.generator = null
this.pollingInterval = pollingInterval
}
/**
** Given the blocknumber return the array of logs for each transaction.
** blockNumber -> Block: [ txHash1, txHash2] -> Logs: [logs1, logs2]
**/
getLogsByBlockNumber (blockNumber) {
if (blockNumber < 0 || blockNumber > this.eth.blockNumber) {
throw new Error('Invalid blockNumber : blockNumber should be greater than zero and less than latestBlock.')
}
let block = this.eth.getBlock(blockNumber)
if (block && block['hash']) {
let transactionHashes = block['transactions']
return transactionHashes.map(tx => {
// We need to get the transactions sender and receiver
let transactionData = this.eth.getTransaction(tx)
// If no logs for a transaction it is omitted
let receipt = this.eth.getTransactionReceipt(tx)
if (receipt && receipt['logs']) {
receipt['logs']['timestamp'] = block['timestamp']
receipt['logs']['sender'] = transactionData['from']
receipt['logs']['receiver'] = transactionData['to']
return receipt['logs']
}
}).filter(a => a.length > 0)
} else {
return []
}
}
/**
** Add abi for decoder before being able to decode logs
** in the block .
**/
addABI (abi) {
abiDecoder.addABI(abi)
}
/*
** Utility function to construct
** decoded logs to return
**/
constructLogs (dLog, i, transactionIndex, decodedLogs, logsInTheBlock) {
dLog['logIndex'] = decodedLogs[i]['logIndex']
dLog['blockHash'] = decodedLogs[i]['blockHash']
dLog['blockNumber'] = decodedLogs[i]['blockNumber']
dLog['contractAddress'] = decodedLogs[i]['address']
dLog['sender'] = logsInTheBlock[transactionIndex]['sender']
dLog['receiver'] = logsInTheBlock[transactionIndex]['receiver']
dLog['eventType'] = dLog['name']
dLog['fields'] = dLog['events']
dLog['ts'] = logsInTheBlock[transactionIndex]['timestamp']
return dLog
}
/**
** Given an startBlock and endBlock range, decoded logs are returned.
** Params -
** startBlock: Starting block to read the block. (default: latest block)
** endBlock: End block to read the block.(default: latest block)
**/
getLogs (startBlock = this.eth.blockNumber, endBlock = this.eth.blockNumber) {
if (startBlock > this.eth.blockNumer || startBlock < 0) {
throw new Error('Invalid startBlock: Must be below web3.eth.blockNumber or startBlock cannot be below 0')
}
if (startBlock > endBlock) {
throw new Error('Invalid startBlock: Must be below endBlock')
}
if (endBlock > this.eth.blockNumber) {
throw new Error('Invalid endBlock: Must be less than or equal to latest block')
}
let logs = []
for (let i = startBlock; i <= endBlock; i++) {
const logsInTheBlock = this.getLogsByBlockNumber(i)
let transactionIndex = 0
logs.push(logsInTheBlock.map(log => {
log = log.filter(a => this.contractAddresses.indexOf(a.address) >= 0)
let decodedLogs = abiDecoder.decodeLogs(log)
// Formating decoded logs to add extra data needed.
decodedLogs = decodedLogs.map((dLog, i) => {
return this.constructLogs(dLog, i, transactionIndex, log, logsInTheBlock)
})
transactionIndex += 1
return decodedLogs
}).filter(a => a.length > 0))
}
// Flatten the logs array
logs = logs.reduce((prev, curr) => {
return prev.concat(curr)
}, []).filter(a => a.length > 0)
return logs
}
getLogsAsync (startBlock = this.eth.blockNumber, endBlock = this.eth.blockNumber) {
return new Promise((resolve, reject) => {
resolve(this.getLogs(startBlock, endBlock))
})
}
/**
** Generator function which yields logs from the blockchain based on last
** read block and the latest block.
**/
* getLogsGenerator () {
let strictlyLess = true
while (this.lastReadBlockNumber <= this.eth.blockNumber) {
if (strictlyLess) {
yield this.getLogs(this.lastReadBlockNumber, this.eth.blockNumber)
}
if (this.lastReadBlockNumber === this.eth.blockNumber) {
strictlyLess = false
// If there is no new block , yield empty list
yield []
} else {
strictlyLess = true
this.lastReadBlockNumber = this.eth.blockNumber
}
}
}
/**
** Recursive polling function. (Recommended not to call this function
** directly.) Best usage pattern would be call turnOnPolling(handler).
** Termination condition for runPolling is when isPolling = false.
**/
async runPolling (handler) {
if (!this.isPolling) {
// End polling if isPolling is set to false.
return
}
if (!this.generator) {
this.generator = this.getLogsGenerator()
}
// console.log('blocknumbers', this.lastReadBlockNumber, this.eth.blockNumber)
const logsP = this.generator.next()
if (logsP.value) {
handler(null, logsP.value)
}
await wait(this.pollingInterval)
this.runPolling(handler)
}
/**
** Turns polling on for reading logs.
** Params -
** handler: Expects an function handler to send the logs back.
**/
turnOnPolling (handler, startBlock = this.eth.blockNumber) {
this.isPolling = true
this.lastReadBlockNumber = startBlock
this.runPolling(handler)
}
/**
** Turns polling off.(Logs can still be manually read)
**/
turnOffPolling () {
this.isPolling = false
}
}
module.exports = {
Chainsaw
}