Skip to content

Commit c3690e5

Browse files
Improved step 29
1 parent f792645 commit c3690e5

File tree

10 files changed

+9868
-3713
lines changed

10 files changed

+9868
-3713
lines changed
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
# Node packages
12
node_modules
2-
*.js
3+
4+
# Build files created by TypeScript Compiler
5+
*.js
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import Web3 from "web3";
2+
3+
const network = "mainnet";
4+
const INFURA_PROJECT_ID = "INFURA_PROJECT_ID";
5+
const RPC_ENDPOINT = `https://${network}.infura.io/v3/${INFURA_PROJECT_ID}`;
6+
7+
export class EthNetwork {
8+
private web3: Web3;
9+
10+
/**
11+
* Creates an instance of EthNetwork object.
12+
*/
13+
constructor() {
14+
this.web3 = new Web3(RPC_ENDPOINT);
15+
}
16+
17+
/**
18+
* Get the number of the latest block on Ethereum Mainnet.
19+
* @returns latest block number.
20+
*/
21+
public getLatestBlockNumber = async () => {
22+
return await this.web3.eth.getBlockNumber();
23+
};
24+
25+
/**
26+
* Fetch a block of given number or hash from the Ethereum Mainnet.
27+
* @param block number or hash of the block.
28+
* @returns a block of given number or Hash.
29+
*/
30+
public getBlock = async (block: number | string) => {
31+
return await this.web3.eth.getBlock(block);
32+
};
33+
34+
/**
35+
* Get the number of transaction in a block of given number or Hash.
36+
* @param block (Optional) Hash or number of the block- default is "latest".
37+
* @returns number of transactions in the block.
38+
*/
39+
public getBlockTransactionCount = async (
40+
block: string | number = "latest"
41+
) => {
42+
return await this.web3.eth.getBlockTransactionCount(block);
43+
};
44+
45+
/**
46+
* Get a specific transaction from a block of given number.
47+
* @param trxIndex index of the transaction in the block.
48+
* @param block (Optional) Hash or number of the block- default is "latest".
49+
* @returns transactions object for the required transaction.
50+
*/
51+
public getTransactionFromBlock = async (
52+
trxIndex: number,
53+
block: string | number = "latest"
54+
) => {
55+
return await this.web3.eth.getTransactionFromBlock(block, trxIndex);
56+
};
57+
58+
/**
59+
* Fetch a given number of latest blocks from Ethereum Mainnet.
60+
* @param numberOfBlocks (Optional) number of blocks to fetch. Default is `10`.
61+
*/
62+
public getLatestXBlocks = async (numberOfBlocks: number = 10) => {
63+
const latestBlock = await this.getLatestBlockNumber();
64+
65+
let blocks: Array<any> = [];
66+
for (let i = 0; i < numberOfBlocks; ++i) {
67+
blocks.push(await this.getBlock(latestBlock - i));
68+
}
69+
return blocks;
70+
};
71+
}
Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1-
import { InspectBlocks } from './inspectBlocks';
2-
3-
const blocks : InspectBlocks = new InspectBlocks();
4-
5-
// get latest block number
6-
blocks.getBlockNumber().then((res)=>console.log('latest Block Number -------',res))
7-
8-
// you can also write the block number or hash of the block to get info about a block
9-
10-
// I am only logging the block hash here. Feel free to log other info about the block
11-
blocks.fetchBlock('latest').then((res)=>console.log('Block hash -------',res.hash))
12-
13-
// get latest 10 blocks
14-
blocks.getBlockNumber().then((latest)=>{
15-
for (let i=0; i< 10; i++){
16-
blocks.fetchBlock(latest-i).then((block)=>console.log(block.number))
17-
}
18-
})
19-
20-
21-
// number of transaction in a block
22-
blocks.getBlockTransactionCount('latest').then((res)=>console.log('Block transaction count -------',res))
23-
24-
// get a particular transaction from a block. In this case we are getting the 3rd transaction from the latest block (transactions' index start from 0)
25-
blocks.getTransactionFromBlock('latest',2).then((res)=>console.log('Transaction info -------',res))
1+
import { EthNetwork } from "./EthNetwork";
2+
3+
(async () => {
4+
const network = new EthNetwork();
5+
6+
console.log("Latest Block Number:", await network.getLatestBlockNumber());
7+
8+
const blockNumber = 13101095;
9+
const block = await network.getBlock(blockNumber);
10+
// I am only logging the block hash here.
11+
// Feel free to log other info about the block
12+
console.log(`Block Hash of Block #${blockNumber}:`, block.hash);
13+
14+
console.log(
15+
`Number of Txns in Block #${blockNumber}:`,
16+
await network.getBlockTransactionCount(blockNumber)
17+
);
18+
19+
console.log(
20+
`First Transaction in Block #${blockNumber}:`,
21+
(await network.getTransactionFromBlock(0, blockNumber)).hash
22+
);
23+
24+
console.log("Number of Transactions in 10 Latest Blocks:");
25+
(await network.getLatestXBlocks()).forEach(block => {
26+
console.log(`Block Number ${block.number} ==>`, block.transactions.length);
27+
});
28+
})();

step29_web3_inspect_blocks/inspectBlocks.ts

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)