Skip to content

Instantly share code, notes, and snippets.

@ZacharyCouchman
Created April 1, 2023 04:16
Show Gist options
  • Save ZacharyCouchman/cbcd31fe676d171943cd550ee478bb9d to your computer and use it in GitHub Desktop.
Save ZacharyCouchman/cbcd31fe676d171943cd550ee478bb9d to your computer and use it in GitHub Desktop.
Inspect the information and transactions within a block
import { ethers } from "ethers"
const rpcUrl = `https://eth-mainnet.g.alchemy.com/v2/demo`;
async function inspectBlocks(){
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
const blockNumber = await provider.getBlockNumber();
console.log(blockNumber);
const previousBlock = await provider.getBlock(blockNumber - 1);
console.log(`Getting previous block: ${blockNumber - 1}`)
console.log(previousBlock);
const latestBlock = await provider.getBlock(blockNumber);
console.log(`Getting latest block: ${blockNumber}`)
console.log(latestBlock);
console.log(`Previous block's hash ${previousBlock.hash} is the parent hash of the latest block ${latestBlock.parentHash}`)
const getBlockWithTransactions = await provider.getBlockWithTransactions(blockNumber);
console.log('First transaction in the latest block:')
console.log(getBlockWithTransactions.transactions[0]);
}
inspectBlocks();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment