forked from harshjv/ethereum-scraper
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathscraper.js
More file actions
292 lines (240 loc) · 7.47 KB
/
scraper.js
File metadata and controls
292 lines (240 loc) · 7.47 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
const Sentry = require('@sentry/node')
const Bluebird = require('bluebird')
const { ethers, BigNumber, logger } = require('ethers')
const eventList = require('./eventList')
const Transaction = require('./models/Transaction')
const { logParser, isSwapTransaction, EVENT_SIG_MAP } = require('./utils')
const {
WEB3_URI,
MAX_BLOCK_BATCH_SIZE,
MAX_TRANSACTION_BATCH_SIZE,
START_BLOCK,
END_BLOCK,
REORG_GAP,
BLOCKTIME,
SWAP_ONLY_MODE
} = process.env
if (!MAX_BLOCK_BATCH_SIZE) throw new Error('Invalid MAX_BLOCK_BATCH_SIZE')
if (!MAX_TRANSACTION_BATCH_SIZE) throw new Error('Invalid MAX_TRANSACTION_BATCH_SIZE')
if (!START_BLOCK) throw new Error('Invalid START_BLOCK')
if (!REORG_GAP) throw new Error('Invalid REORG_GAP')
const SUPPORTS_WS = WEB3_URI.startsWith('ws')
let ethersProvider
let syncing = true
let latestBlockNumber = null
process.on('unhandledRejection', error => { throw error })
function handleError (e) {
console.error(e)
process.exit(1)
}
if (SUPPORTS_WS) {
ethersProvider = new ethers.providers.WebSocketProvider(WEB3_URI)
ethersProvider.on('error', handleError)
ethersProvider.on('end', handleError)
} else {
ethersProvider = new ethers.providers.StaticJsonRpcProvider(WEB3_URI)
}
async function sleep (duration) {
return new Promise(resolve => setTimeout(resolve, duration))
}
async function getTransactionReceipt (hash, attempts = 1) {
const receipt = await ethersProvider.getTransactionReceipt(hash)
if (receipt) return receipt
if (attempts <= 3) {
await sleep(5000)
return getTransactionReceipt(hash, attempts + 1)
}
throw new Error('Unable to fetch transaction receipt')
}
async function handleBlock (blockNum) {
if (!blockNum) return
const exist = await Transaction.findOne({
blockNumber: blockNum
}).exec()
if (exist) return
const block = await ethersProvider.getBlockWithTransactions(blockNum)
if (!block) return
const blockNumber = block.number
const blockHash = block.hash
const timestamp = block.timestamp
const events = {}
let transactions = []
let blockTransactions = block.transactions.map(tx => ({ ...tx, input: tx.data }))
if (SWAP_ONLY_MODE === 'true') {
const eventTopics = Object.keys(EVENT_SIG_MAP)
const blockEvents = await ethersProvider.getLogs({ topics: [eventTopics], fromBlock: blockNum, toBlock: blockNum })
const blockTransactionsWithEvents = blockEvents.map(e => e.transactionHash)
blockTransactions = blockTransactions.filter(tx => isSwapTransaction(tx) || blockTransactionsWithEvents.includes(tx.hash))
}
await Bluebird.map(blockTransactions, async ({ hash, from, to, input, value }) => {
try {
const { status, contractAddress, logs } = await getTransactionReceipt(hash)
logs
.map(logParser)
.filter(l => !!l)
.forEach(({ model, contractAddress, data }) => {
const commons = { hash, blockHash, blockNumber, status, timestamp }
if (!events[model.modelName]) events[model.modelName] = []
events[model.modelName].push({
...commons,
...data,
contractAddress
})
})
transactions.push({
from,
to,
hash,
blockHash,
blockNumber,
status,
input,
contractAddress,
timestamp,
value
})
} catch (e) {
Sentry.withScope(scope => {
scope.setTag('blockNumber', blockNumber)
scope.setTag('blockHash', blockHash)
scope.setTag('hash', hash)
scope.setTag('from', from)
scope.setTag('to', to)
scope.setExtra('input', input)
scope.setExtra('value', value)
Sentry.captureException(e)
})
throw e
}
}, { concurrency: Number(MAX_TRANSACTION_BATCH_SIZE) })
if (transactions.length === 0) {
transactions = [{
blockHash,
blockNumber
}]
}
await Transaction.insertMany(transactions, { ordered: false })
const eventEntries = Object.entries(events)
await Bluebird.map(eventEntries, async ([modelName, _events]) => {
if (_events.length > 0) {
const event = eventList.find(event => event.model.modelName === modelName)
if (!event) throw new Error(`Unknown event model: ${modelName}`)
await event.model.insertMany(_events, { ordered: false })
}
}, { concurrency: 1 })
const log = [
`#${blockNumber}[${block.transactions.length}]`
]
const compareWith = Number(END_BLOCK) || latestBlockNumber
if (compareWith) {
const diff = compareWith - blockNum
const progress = Math.floor((1 - (diff / compareWith)) * 10000) / 100
log.push(`${progress}%`)
}
console.log(log.join(' '))
}
async function sync () {
const lastBlockInRange = await Transaction.getLastBlockInRange(START_BLOCK, END_BLOCK)
let startFrom
if (lastBlockInRange) {
startFrom = lastBlockInRange + 1
} else {
startFrom = Number(START_BLOCK)
}
let batch = []
for (let i = startFrom; ; i++) {
batch.push(handleBlock(i))
if (batch.length === Number(MAX_BLOCK_BATCH_SIZE)) {
await Promise.all(batch)
batch = []
}
if (END_BLOCK && i >= Number(END_BLOCK)) {
console.log('Reached END_BLOCK', END_BLOCK)
break
}
if (latestBlockNumber && i >= latestBlockNumber) {
console.log('Reached latestBlockNumber', latestBlockNumber)
break
}
}
if (batch.length !== 0) {
await Promise.all(batch)
}
syncing = false
console.log('Synced!')
}
async function getLatestBlock () {
latestBlockNumber = await ethersProvider.getBlockNumber()
}
function onNewBlock (blockNumber) {
latestBlockNumber = blockNumber
if (!syncing && !END_BLOCK) {
handleBlock(latestBlockNumber - Number(REORG_GAP))
}
}
function subscribe () {
ethersProvider.on('block', (blockNumber) => {
onNewBlock(blockNumber)
})
ethersProvider.on('error', (error) => {
handleError(error)
})
}
async function poll () {
if (!BLOCKTIME) throw new Error('Invalid BLOCKTIME')
while (true) {
const blockNumber = await ethersProvider.getBlockNumber()
if (latestBlockNumber === blockNumber) {
await sleep(Number(BLOCKTIME))
} else {
await onNewBlock(latestBlockNumber + 1)
}
}
}
//Patch for RSK Support
ethersProvider.formatter.receipt = function (value) {
const result = check(ethersProvider.formatter.formats.receipt, value)
if (result.root != null) {
if (result.root.length <= 4) {
result.root = result.root == '0x' ? '0x0' : result.root
const tx_root = BigNumber.from(result.root).toNumber()
if (tx_root === 0 || tx_root === 1) {
if (result.status != null && (result.status !== tx_root)) {
logger.throwArgumentError("alt-root-status/status mismatch", "value", { root: result.root, status: result.status })
}
result.status = tx_root
delete result.root
}
else {
logger.throwArgumentError("invalid alt-root-status", "value.root", result.root)
}
}
else if (result.root.length !== 66) {
logger.throwArgumentError("invalid root hash", "value.root", result.root)
}
}
return result
}
function check(format, object) {
const result = {}
for (const key in format) {
try {
let value = format[key](object[key])
if (value !== undefined) {
result[key] = value
}
}
catch (error) {
error.checkKey = key
error.checkValue = object[key]
throw error
}
}
return result
}
;(async () => {
await ethersProvider.ready
await getLatestBlock()
SUPPORTS_WS ? subscribe() : poll()
sync()
})()