Last active
May 24, 2023 04:43
-
-
Save leozc/8db5b9e5abe234d637477997b18ef749 to your computer and use it in GitHub Desktop.
Verify data consistency of Block and Transaction of a Web3 Provider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Web3 = require('web3'); | |
// Connect to Ethereum node (use your own provider URL here) | |
const web3 = new Web3('https://xxx.quiknode.pro/xxx/'); | |
const web3s = new Web3(new Web3.providers.WebsocketProvider('wss://xxx.quiknode.pro/yyy/')); | |
web3s.eth.subscribe('newBlockHeaders', async (error, blockHeader) => { | |
if (error) { | |
console.error(`Error: ${error}`); | |
return; | |
} | |
console.log(`New block received. Block # ${blockHeader.number}`); | |
// Get full block details | |
const block = await web3.eth.getBlock(blockHeader.number); | |
// Fetch the transaction details for each transaction in the block | |
//get random 10 transactions | |
const trx = block.transactions.slice(0, 10); | |
for (let txHash of trx) { | |
const tx = await web3.eth.getTransaction(txHash); | |
console.log("OK"); | |
} | |
}).on('error', error => console.error(error)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "ethereum-block-fetcher", | |
"version": "1.0.0", | |
"description": "A simple Node.js script to fetch Ethereum block and transaction details", | |
"main": "index.js", | |
"scripts": { | |
"start": "node index.js" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"web3": "^1.5.3" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment