-
Notifications
You must be signed in to change notification settings - Fork 17
/
subscribe.js
37 lines (31 loc) · 1.27 KB
/
subscribe.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
'use strict'
const config = require('config')
const db = require('./models/mongodb')
const q = require('./queues')
const web3 = require('./models/blockchain/chain')
let sleep = (time) => new Promise((resolve) => setTimeout(resolve, time))
async function crawlProcess() {
while (true) {
let block = await web3.eth.getBlock('latest');
let b = await db.Block.findOne({blockNumber: block.number})
if (b && b.isFinish) {
continue
}
await db.Block.findOneAndUpdate({blockNumber: block.number}, {
hash: block.hash,
blockNumber: block.number,
transactionCount: block.transactions.length,
parentHash: block.parentHash,
timestamp: block.timestamp,
}, { upsert: true, new: true })
console.log("Process block number: " + block.number);
let listTransactions = await block.transactions
if (listTransactions != null && block != null) {
await q.create('newTransaction', {transactions: listTransactions.toString(), blockNumber: block.number})
.attempts(5).backoff({delay: 10000})
.priority('low').removeOnComplete(true).save()
}
await new Promise(done => setTimeout(done, 5000));
}
}
crawlProcess()