This repository was archived by the owner on Mar 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathscraper.js
191 lines (151 loc) · 4.24 KB
/
scraper.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
const Sentry = require('@sentry/node')
const Bluebird = require('bluebird')
const Web3 = require('web3')
const Transaction = require('./Transaction')
const {
WEB3_URI,
MAX_BLOCK_BATCH_SIZE,
MAX_TRANSACTION_BATCH_SIZE,
START_BLOCK,
END_BLOCK,
REORG_GAP
} = 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')
let web3
let syncing = true
let latestBlockNumber = null
function handleError (e) {
console.error(e)
process.exit(1)
}
if (WEB3_URI.startsWith('ws')) {
const provider = new Web3.providers.WebsocketProvider(WEB3_URI, {
clientConfig: {
maxReceivedFrameSize: 100000000,
maxReceivedMessageSize: 100000000
}
})
provider.on('error', handleError)
provider.on('end', handleError)
web3 = new Web3(provider)
} else {
web3 = new Web3(WEB3_URI)
}
function sleep (duration) {
return new Promise((resolve, reject) => setTimeout(resolve, duration))
}
async function getTransactionReceipt (hash, attempts = 1) {
const receipt = await web3.eth.getTransactionReceipt(hash)
if (receipt) return receipt
if (!receipt && 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 web3.eth.getBlock(blockNum, true)
if (!block) return
const blockNumber = block.number
const blockHash = block.hash
const timestamp = block.timestamp
let transactions = await Bluebird.map(block.transactions, async ({ hash, from, to, input, value }) => {
try {
const { status, contractAddress } = await getTransactionReceipt(hash)
return {
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 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 latest () {
const gap = Number(REORG_GAP)
const subscription = web3.eth.subscribe('newBlockHeaders')
subscription.on('data', block => {
if (block) {
latestBlockNumber = block.number
if (!syncing && !END_BLOCK) {
handleBlock(latestBlockNumber - gap)
}
}
})
subscription.on('error', handleError)
}
sync()
latest()