-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetBlock.js
30 lines (29 loc) · 898 Bytes
/
getBlock.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
// 1. Access environmental Variables
require('dotenv').config()
var figlet = require('figlet');
figlet('477 scanning the mempool for new block', function(err, data) {
if (err) {
console.log('Something went wrong...');
console.dir(err);
return;
}
console.log(data)
});
//const { on } = require('events')
//const { default: Web3, default: Web3 } = require('web3')
// 2: Connect to network with web3
//https://web3js.readthedocs.io/en/v1.8.0/web3-eth.html#setprovider
const Web3 = require('web3')
const web3 = new Web3(
new Web3.providers.WebsocketProvider(process.env.INFURA_WSS)
)
//3. Grab block numbers
//https://web3js.readthedocs.io/en/v1.8.0/web3-eth-subscribe.html#id8
web3.eth.subscribe('newBlockHeaders')
.on('data', async block => {
console.log(`Block#: ${block.number}`)
})
//check for errors
.on('error', error => {
console.log(error)
})