This script uses web3.js to evaluate whether a specific block within a blockchain network has been integrated into the main chain or if it was a result of a reorg.
Prerequisites
Environment setup
npm install web3
Initialize a provider instance
index.js
, import the web3.js library, and initialize a new provider instance using your Chainstack node URL.Paste your Chainstack node URL in the NODE_URL
const.Create the function with the logic
eth_getBlockByNumber
method, and compares its hash with the parent hash of the next block.This function returns true
if the block is forked and false
if it is not.You can remove the console.log
statements from the function and only use its boolean returns.Call the function
const
to specify which block to analyze.Understand the logic