-
Notifications
You must be signed in to change notification settings - Fork 0
/
Monitor.js
35 lines (28 loc) · 1.41 KB
/
Monitor.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
// To read incoming blocks, transactions and events happening on the blockchain.
let latestKnownBlockNumber = -1;
let blockTime = 5000;
// Our function that will triggered for every block
async function processBlock(blockNumber) {
console.log("We process block: " + blockNumber);
let block = await web3.eth.getBlock(blockNumber);
console.log("new block :", block)
for (const transactionHash of block.transactions) {
let transaction = await web3.eth.getTransaction(transactionHash);
let transactionReceipt = await web3.eth.getTransactionReceipt(transactionHash);
transaction = Object.assign(transaction, transactionReceipt);
console.log("Transaction: ", transaction);
// Do whatever you want here
}
latestKnownBlockNumber = blockNumber;
}
// This function is called every blockTime, check the current block number and order the processing of the new block(s)
const checkCurrentBlock = async function checkCurrentBlock() {
const currentBlockNumber = await web3.eth.getBlockNumber()
console.log("Current blockchain top: " + currentBlockNumber, " | Script is at: " + latestKnownBlockNumber);
while (latestKnownBlockNumber == -1 || currentBlockNumber > latestKnownBlockNumber) {
await processBlock(latestKnownBlockNumber == -1 ? currentBlockNumber : latestKnownBlockNumber + 1);
}
setTimeout(checkCurrentBlock, blockTime);
}
checkCurrentBlock()
*/