There’s been a lot of hype around web3.js.
In this article, we’ll learn what the technologies web3.js and ethers.js are, and how they’re used to interact with the Ethereum blockchain.
We’ll also learn how to get started using the web3.js library, the main JavaScript library to interact with the Ethereum blockchain.
Overview of the article
-
What is web3.js?
-
Web3.js vs ethers.js.
-
How to run JavaScript code in web3.
-
What is web3.js used for?
Without further delay, let’s dive into the world of web3.js; let’s thoroughly understand what it means and how to implement it in our smart contracts programs.
What is Web3.js?
Web3.js is a collection of libraries that allow you to interact with a local or remote Ethereum node using HTTP, IPC, or WebSocket.
Web3.js allows you to develop websites or clients that interact with the blockchain. For example, it allows you to send ether from one account to another, read and write data from smart contracts, create smart contracts, and so much more!
The following documentation will guide you through installing and running web3.js, as well as providing API reference documentation with examples.
There are a few different aspects to developing blockchain applications with Ethereum:
-
Smart contract development – writing code that gets deployed to the blockchain with the Solidity programming language.
-
Developing websites or clients that interact with the blockchain – writing code that reads and writes data from the blockchain with smart contracts.
If you have a web development background, you might have used jQuery to make Ajax calls to a web server. That’s a good starting point to understand the function of web3.js. Instead of using jQuery to read and write data from a web server, you can use web3.js to read and write to the Ethereum blockchain.
Below is a diagram to illustrate how web3.js interacts with the Ethereum blockchain:
Image credit: iotbl.
Web3.js vs ethers.js
Both web3.js and ethers.js are Ethereum JavaScript Libraries.
Ether.js is developed and maintained by Rick Moore – A Canadian developer.
Web3.js is developed and maintained by the Ethereum Foundation. So, there is wider support for web3.js, as more developers are behind it.
One major difference between ethers.js and web3.js is how they handle key management and interaction with the Ethereum blockchain.
Web3.js assumes that there is a local node connected to the application. It’s assumed that the node to stores keys, signs transactions, and reads and interacts with the Ethereum blockchain. In reality, this is not often the case — most users are not running geth locally. Metamask effectively emulates that environment through a browser application, so most web3 apps require Metamask to hold keys, sign transactions, and interact with the ethereum mainnet.
On the other hand, ethers.js takes a different approach that we believe gives developers more flexibility. Ethers.js separates the “node” into two separate roles:
- A “wallet” that holds keys and signs transactions, and
- A “provider” that serves as an anonymous connection to the ethereum network, checking state and sending transactions.
The 2 main advantages of ethers.js that comes to mind are:
- ENS names are first-class citizens.
- Key management and state – separation of concerns.
How to run JavaScript code in Web3.js
To run Javascript code in web3.js, you need to get web3.js into your project. This can be done using the following methods:
Dependencies
There are a few dependencies that will help you start developing with web3.js.
Node Package Manager (NPM)
The first dependency we need is Node Package Manager, or NPM, which comes with Node.js. To check if you have Node already installed, go to your terminal and type:
node -v
Web3.js Library
You can install the Web3.js library with NPM in your terminal like this:
npm install web3
Or using Yarn:
yarn add web3
Infura RPC URL
To connect to an Ethereum node with JSON RPC on the mainnet, we need access to an Ethereum node….
Read More: web3.hashnode.com