const Web3 = require("web3");

const NODE_URL = "YOUR_CHAINSTACK_ENDPOINT"
const web3 = new Web3(NODE_URL)

// If the hash of the block to inspect is === to the parent hash of the next block, then this is not a forked block.
async function isForked(block) {

    const nextBlock = blockToInspect + 1
    console.log(`Block to inspect: ${block}`)
    console.log(`Next block: ${nextBlock} \n`)

    const blockToInspectHash = (await web3.eth.getBlock(block, false)).hash
    console.log(`Block hash of the block you are inspecting: ${blockToInspectHash} \n`)

    const nextBlockParentHash = (await web3.eth.getBlock(nextBlock, false)).parentHash
    console.log(`Parent hash of the next block: ${nextBlockParentHash} \n`)

    if (String(nextBlockParentHash) == String(nextBlockParentHash)) {
        console.log(`You are inspecting the correct block`)
        return false

    } else {
        console.log(`You are inspecting a forked block!`)
        return true
    }
}

const blockToInspect = 16394103
isForked(blockToInspect)
$ node index

  'Block to inspect: 16394103
  'Next block: 16394104

  'Block hash of the block you are inspecting: 0x819f79970003b8c7d51071001509406d9f51aa734aaf69aff506959abdcc2d55

  'Parent hash of the next block: 0x819f79970003b8c7d51071001509406d9f51aa734aaf69aff506959abdcc2d55

  'You are inspecting the correct block
const Web3 = require("web3");

const NODE_URL = "YOUR_CHAINSTACK_ENDPOINT"
const web3 = new Web3(NODE_URL)

// If the hash of the block to inspect is === to the parent hash of the next block, then this is not a forked block.
async function isForked(block) {

    const nextBlock = blockToInspect + 1
    console.log(`Block to inspect: ${block}`)
    console.log(`Next block: ${nextBlock} \n`)

    const blockToInspectHash = (await web3.eth.getBlock(block, false)).hash
    console.log(`Block hash of the block you are inspecting: ${blockToInspectHash} \n`)

    const nextBlockParentHash = (await web3.eth.getBlock(nextBlock, false)).parentHash
    console.log(`Parent hash of the next block: ${nextBlockParentHash} \n`)

    if (String(nextBlockParentHash) == String(nextBlockParentHash)) {
        console.log(`You are inspecting the correct block`)
        return false

    } else {
        console.log(`You are inspecting a forked block!`)
        return true
    }
}

const blockToInspect = 16394103
isForked(blockToInspect)
$ node index

  'Block to inspect: 16394103
  'Next block: 16394104

  'Block hash of the block you are inspecting: 0x819f79970003b8c7d51071001509406d9f51aa734aaf69aff506959abdcc2d55

  'Parent hash of the next block: 0x819f79970003b8c7d51071001509406d9f51aa734aaf69aff506959abdcc2d55

  'You are inspecting the correct block