-
Notifications
You must be signed in to change notification settings - Fork 0
/
5-inspect-blocks.js
25 lines (19 loc) · 1002 Bytes
/
5-inspect-blocks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { ethers } from "ethers"
import { ALCHEMY_API_KEY_MAINNET } from './constants.js';
const rpcUrl = `https://eth-mainnet.g.alchemy.com/v2/${ALCHEMY_API_KEY_MAINNET}`;
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();