forked from MetaMask/eth-block-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
37 lines (29 loc) · 921 Bytes
/
example.js
File metadata and controls
37 lines (29 loc) · 921 Bytes
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
const https = require('https')
const createConcatStream = require('mississippi').concat
const RpcBlockTracker = require('./lib/index')
const provider = { sendAsync }
const blockTracker = new RpcBlockTracker({ provider })
blockTracker.start()
blockTracker.on('block', (newBlock) => {
console.log(`block #${Number(newBlock.number)}`)
})
blockTracker.on('sync', ({ newBlock, oldBlock }) => {
if (oldBlock) {
console.log(`sync #${Number(oldBlock.number)} -> #${Number(newBlock.number)}`)
} else {
console.log(`first sync #${Number(newBlock.number)}`)
}
})
function sendAsync(payload, cb){
const data = new Buffer(JSON.stringify(payload))
const req = https.request({
host: 'mainnet.infura.io',
method: 'POST',
}, (res) => {
res.setEncoding('utf8')
res.pipe(createConcatStream((result) => cb(null, JSON.parse(result))))
res.on('error', cb)
})
req.write(data)
req.end()
}