Created
April 1, 2023 04:16
-
-
Save ZacharyCouchman/cbcd31fe676d171943cd550ee478bb9d to your computer and use it in GitHub Desktop.
Inspect the information and transactions within a block
This file contains 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
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