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

Now, we include the web3.js library.

We do this with the following lines:

var Web3JS = require("web3");
web3 = new Web3JS('https://goerli.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 by checking its version

Add the following line to your code:

console.log(web3.version);

You can find the complete code here:

var Web3JS = require("web3");

web3 = new Web3JS('https://goerli.infura.io/v3/YOUR PERSONAL API-KEY');
console.log(web3.version);

Let’s test if our code works.

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 version number of your web3.js library.

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 version of the Geth client do you use?
  • Calculate the SHA3 hash (Keccak-256) from “Hello World”.
  • Determine to what network the client is connected.
  • How many Wei are three Shannon?
  • How many Finney 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 Ether does the following address hold? 0xb589178adfb6c6915c55b08B284bdBbbcf4f452e
  • How many transactions does block ‘0xbf6e6e8b2fc05a44e1c685f2f2de0e6434de777ce643fd0de68fc631e3ca367d’ include?
  • What is the sender in this transaction 0xfbfae15e019c4d945a80b3bb7432dc414ba6be685f510fa380a97d9b43d45acf?

The following code contains the solution.

var Web3JS = require("web3");

web3 = new Web3JS('https://goerli.infura.io/v3/YOUR PERSONAL API-KEY');

console.log("Web3 version: " + web3.version);

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

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

web3.eth.net.getId().then(function(res){
    console.log("Network ID: " + res);
});

web3.eth.net.getNetworkType().then(function(res){
    console.log("Network Name: " + res);
})

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

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

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("0xb589178adfb6c6915c55b08B284bdBbbcf4f452e").then(function(res){
    console.log("Balance: " + res);
})

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

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