Skip to content

Instantly share code, notes, and snippets.

@0xlxy
Created April 19, 2023 23:41
Show Gist options
  • Save 0xlxy/586eefd40b0307d61c508d6d1ae2895f to your computer and use it in GitHub Desktop.
Save 0xlxy/586eefd40b0307d61c508d6d1ae2895f to your computer and use it in GitHub Desktop.
Ethereum Blockchain Indexer
const { ethers } = require("ethers");
const provider = new ethers.providers.WebSocketProvider(
"wss://mainnet.infura.io/ws/v3/992c9dccdfd849dabf68ab78d689595d"
);
console.log(provider);
provider.on("block", (blockNumber) => {
provider
.getBlockWithTransactions(blockNumber)
.then((block) => {
const transactions = block.transactions;
console.log(
`New block received: ${blockNumber}, ${transactions.length} transactions`
);
console.log(transactions);
})
.catch((error) => {
console.error(error);
});
});
{
"name": "block-indexer",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"ethers": "^5.7.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment