Getting Familiar With web3.js

Published by Mario Oettler on

First, we want to make ourself familiar with web3.js.

Install Node.js

Create a new folder

Go to your new folder and install web3.js with

npm install web3

Open your editor and create a file web3_server01.js

Check your version by typing

npm list web3

It should return a version of 4.x.x

Now, we include the web3.js library.

We do this with the following lines:

const Web3 = require(“web3”);
const web3 = new Web3(‘https://sepolia.infura.io/v3/YOUR PERSONAL API-KEY’);

Replace YOUR PERSONAL API-KEY with your api key from Infura. To get this api key, select the Goerli testnet. (We choose Goerli testnet because it is available in MetaMask by default.)

Next, we want to check, if the library is successfully included and your Infura endpoint works correctly. We do this by looking up the block number of the Sepolia testnet. Add the following line to your code:

web3.eth.getBlockNumber().then(console.log);

You can find the complete code here:

const {Web3} = require("web3");

const web3 = new Web3JS('https://sepolia.infura.io/v3/YOUR PERSONAL API-KEY');
//const web3 = new Web3('https://eth.llamarpc.com');

console.log(web3.eth.getBlockNumber().then(console.log));

Go to your terminal (console), navigate to your folder and run the file with node by entering:

node web3_server01.js

It should run without error and give you the actual block number.

Now, we want to make a few requests to the full node and find out a few pieces of information about the client and the blockchain. Our program should display them on the console.

You can try to answer the following questions by using the documentation of the web3.js library. If you should get stuck, you will find the solution in the source code below.

  • What is the actual block number?
  • What version of the Geth client do you use?
  • Calculate the SHA3 hash (Keccak-256) from “Hello World”.
  • What is the chain Id you are connected to?
  • How many Wei are three Eth?
  • How many Eth are 1500000000000000 Wei?
  • What is the actual gas price in Ether?
  • What is the actual gas limit?
  • What is the hash of the genesis block?
  • How many Ethers does the following address hold? 0x1ac222d2ad71f4daaec0ad719375b472b294a7377031450c6da63087782c0bcb
  • How many transactions does block 0xaf7877939e25d806eab7a157da2383e3328f58c7217079c1217efcffb7f1d178 include?
  • What is the sender in this transaction 0x1ac222d2ad71f4daaec0ad719375b472b294a7377031450c6da63087782c0bcb?

The following code contains the solution.

const {Web3} = require("web3");

const web3 = new Web3('https://sepolia.infura.io/v3/Your Personal Key Here');


web3.eth.getBlockNumber().then(function(res){
    console.log("Block Number: " + res)
});

web3.eth.getNodeInfo().then(function(res){
    console.log("Node Info: " + res);
});

console.log("SHA3: " + web3.utils.sha3("Hello World"));


web3.eth.getChainId().then(function(res){
    console.log("Chain ID: " + res);
});

console.log("Wei: " + web3.utils.toWei("3", "ether"));

console.log("Ether: " + web3.utils.fromWei("1500000000000", "ether"));

web3.eth.getGasPrice().then(function(res){
    console.log("Gas Price: " + res);
});

web3.eth.getBlock('latest').then(function(res){
    console.log("Gas Limit: " + res.gasLimit);
})

web3.eth.getBlock(0).then(function(res){
    console.log("Genesis: " + res.hash);
})

web3.eth.getBalance("0x1268AD189526AC0b386faF06eFfC46779c340eE6").then(function(res){
    console.log("Balance: " + res);
})

web3.eth.getBlockTransactionCount("0xaf7877939e25d806eab7a157da2383e3328f58c7217079c1217efcffb7f1d178").then(function(res){
    console.log("TX Count: " + res);
})

web3.eth.getTransaction("0x1ac222d2ad71f4daaec0ad719375b472b294a7377031450c6da63087782c0bcb").then(function(res){
    console.log("TX sender: " + res.from);
});
Categories: