New Here? Claim Your 30% Discount on Your First Blockchain Node Access!

banner image

Web3 Libraries for development on Ethereum

Vance Wood

Vance Wood

October 2, 2023

10 min read

article cover

In a rapidly approaching era of Web3.0, developers are on the lookout for tools that will help them build blockchain-enabled applications while also being able to apply existing skills in software engineering. Web3 libraries cater to the need.

In this beginner-friendly guide, we will explore key Web3 libraries, namely Web3.js and Ethers.js, compare their functionality, and see how they work in practice.

Top Web3 libraries: Highlights

Ready-made libraries boost the speed of decentralized application development by significantly saving time and effort.

  • Developers do not need to learn a new framework for the blockchain, familiar Web2 frameworks like JavaScript are still relevant for dApp development;
  • Web3 developers favor Web3.js and Ethers.js libraries, each providing all the necessary toolings to perform any blockchain-related task;
  • They support various Ethereum Virtual Machine (EVM)-compatible chains expanding their utility way beyond Ethereum.

If you’re a blockchain developer or an enthusiast, understanding these libraries is essential to harness the true potential of Web3.

Libraries for Web 3.0 development on Ethereum

The world right now is experiencing a transition from a closed and centralized Web 2.0 to an open Web 3.0, powered by blockchain technology, which serves as the foundation for trust, security, and transparency in this new decentralized web.

RELATED: Web3 and Web2, Changing The Narrative in Tech: Brief Guide By GetBlock

Web3 is the driving force behind the development of decentralized applications (dApps) as opposed to traditional Web apps. While frontend development of dApps will be strikingly familiar to traditional software engineers, the backend of Web3.0 is tied to the blockchain technology and encompasses the following components:

  • Blockchain nodes as a communication endpoint for applications to read on-chain data and submit transactions to the ledger;
  • Smart contracts that enable building programs on top of blockchain protocols like Ethereum where most blockchain applications are built to operate autonomously within a decentralized ecosystem;
  • Cryptocurrencies that not only serve as a medium of exchange but also fuel decentralized finance and applications, power smart contracts, and incentivize developers to build Web3 products and people to use them;
  • Decentralized storage based on IPFS to keep data on the P2P network of computers rather than corporation-owned storage;

Finally, Web3 libraries are the most important tools in the developer stack that bridge communication with any blockchain of choice and smart contracts executed on these networks. In essence, that is what injects any software with Web3 functionality.

What is a library?

A library is a group of code packed together that you can fit into your projects and reuse across different applications. It is a very powerful tool that helps avoid having to manually write frequently used functions. Developers can run one of their own or refer to existing resources by simply importing them and calling methods written in these libraries.

As a rule, each coding library is designed to solve a specific problem. For Web3, they will help bring blockchain functionality into applications.

What is a Web3 library?

Web3 library is a set of prewritten codes that helps glue the front end to the Ethereum blockchain. Namely, it is an add-on required for interaction with smart contracts deployed to Ethereum and a wide selection of EVM-compatible chains, including zkEVMs.

The main objective of various Web3 libraries is to enable web and mobile applications to “talk” to any blockchain. For doing so, libraries specifically designed for Web3 application development allow operating intuitive and concise instruction. To name a few:

  • Interact with nodes through various communication protocols such as HTTP or WebSocket;
  • Help create Solidity contracts and trigger actions codified into smart contracts;
  • Query any type of Ethereum network data;
  • Manage all kinds of transactions.

Existing libraries integrate far more extensive functionalities and cover every need a dApp developer may have.

How do Web3 libraries work?

Operating with JSON RPC requests, Web3 libraries send calls to ETH nodes, which can either be self-run or obtained from node service providers. The frontend code will specify the provider RPC URL or point to a local node, and pass necessary functions and parameters to get a JSON-formatted response.

how_dApps_communcate_via_Web3_libraries

Image from GitHub

Key Libraries for Ethereum Development

Most developers building Web3-enabled applications use Web3.js or Ethers.js as go-to external JavaScript libraries. Similar in many ways, the two however have their own perks and shortcomings, which we’ll further expand on.

Web3.js

The oldest and most reputable library for interacting with Ethereum, Web3.js has been around since 2015. As such, many projects are making extensive use of this tool to connect smart contracts to JavaScript frontend code, execute transactions within contracts, and fetch on-chain data to bring it back to a frontend.

Key Features

Web3.js comprises several modules, each designed for different purposes. To better understand the range of functions, let’s quickly outline them:

eth – help establish connection to Ethereum mainnet or testnets, manage transactions, construct and further interact with Solidity contracts;

shh – for dApps to communicate and exchange data;

utils – handles data formatting and conversion between different units of Ether;

bzz – enables using Swarm as a decentralized storage system.

The full list of Web3.js functions, however, is vast and can be well integrated into almost any existing framework, which makes it a highly-adopted tool.

First steps with Web3.js

The library can be easily downloaded using a desired package manager like NPM: npm i web3, or YARN: yarn add web3. From there, it’s ready to be further integrated into various consoles such as Node.js.

Example

To demonstrate the use of the library, we’ll set up a provider and request the balance of a random ETH address converted into Ether using both eth and utils modules within Web3.js 4x version:


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

const web3 = new Web3('Url');

//Submit your local Geth node or URL provided by node service in place of ‘Url’

//You can get a free ETH RPC endpoint by registering a GetBlock account

  

(async () => {

const balance = await web3.eth.getBalance('0xe688b84b23f322a994A53dbF8E15FA82CDB71127');

console.log('Balance in ETH:', web3.utils.fromWei(balance, 'ether'));

})();

Here’s the result of running the code above:

code result.png

Ethers.js

Richard Moore as an author and sole contributor initiated Ethers.js project back in 2016 as a more neatly organized alternative to Web3.js. Since then it quickly gained in popularity and is now sharing the reputation of top Web3 libraries with its more famous competitor.

Key Features

The scope of possibilities within Ethers is on par with Web3.js as it offers similar functionalities. However, the newer library boasts some added capabilities:

  • Out-of-box support for ENS domain names to use instead of lengthy contract and wallet address strings;
  • Open-source MIT licensed software that authorizes reusing and revising the code with little to no limitations;
  • Thoroughly tested codes with over 26k entries and counting;
  • Compact package size to avert negative implications on the front end performance;
  • Ethers is built on TypeScript with enhanced security in mind to eliminate the probability of bugs and errors.

Similar to Web3.js, the library is constantly updated and enriched. For instance, the newest sixth version adds capabilities to reduce the code size by using built-in big numbers and compact signature representations along with other improvements.

Installing Ethers.js

The installation process for the Ethers JS library is not different from the previous example: npm i ethers or yarn add ethers will do the job. However, unlike Web3.js, there’s no need to download the whole package if you only need to use select modules.

Example of use

Let’s run the same getBalance command and configure the provider within Ethers.js v6 to see how it compares to Web3.js:

const { ethers } = require("ethers");

const provider = new ethers.JsonRpcProvider("RpcUrl");

  

(async () => {

const balance = await provider.getBalance('0xe688b84b23f322a994A53dbF8E15FA82CDB71127');

console.log('Balance in ETH:',ethers.formatEther(balance));

})();

Since we passed the same address, the method will resolve with the same result as in the previous example. However, the keywords will slightly differ.

Web3.js vs Ethers.js

Generally speaking, JS libraries in question have overlapping functionality which makes them interchangeable. Therefore, when picking sides, it’s necessary to additionally assess other characteristics that are given in the table below.

Web3.js Ethers.js
Contributors Community of developers backed by Ethereum Foundation Richard Moore
License LGPL-3.0 MIT
Package Size 1.13 MB* * minified version 113kB* *mini zipped version
Downloads per month 2.7M 5M
Object Types Provider for both reading and writing Provider for fetching data Signer for operations that use Private Keys
Documentation Well-maintained guides with examples Well-laid and reader-friendly guides with test suit of over 26k scenarios

The reason why Web3.js is often considered superior over its younger contender is that it has been long used by Ethereum developers, therefore the community around it is more active and projects using it are numerous.

At the same time, Ethers.js has been increasingly utilized by newer projects. The benefits users notice are easier and more logical syntax along with compact package size which affect the overall performance of a dApp.

For visual learners, it might be useful to check out the video by Dapp University comparing the two libraries.

In general, it’s worth testing both to see which library suits separate use cases. For instance, Ethers.js is often referred to in tutorials for writing your first smart contracts and it also has a dedicated ‘playground’ for testing before use.

Context Banner

Best practices when using a Web 3.0 library

Since mastering Web3 libraries is not an easy task, some beginners in Ethereum development could use some tips.

  1. Pay attention to the versions of a library that you’re planning to use as methods may vary depending on different installments which can result in errors. Older versions are often considered to provide a smoother experience, while upgraded packages are more feature-rich;

  2. Seek community help: Ethereum has a solid ecosystem and the biggest community, if you encounter a problem you are more likely to find a solution on Stack Exchange;

  3. Choose a reliable provider to ensure a safe and stable connection to the blockchain.

If you’re new to blockchain development, try using GetBlock to test your first blockchain-based software. GetBlock, as a prominent infrastructure provider, exposes endpoints that will allow users to talk to an Ethereum node in addition to 50 more networks.

The service provides free 40,000 requests per day and is easy to use: just register a GetBlock account and generate URL endpoints ready to be integrated into your application code and immediately talk to the ETH node and a library installed.

Wrapping up

As blockchain technology continues to reshape our digital world, Web3 libraries will remain at the forefront, empowering developers to craft the decentralized future of the internet. So, if you're embarking on your first blockchain journey, remember that Web3 libraries are your gateway to building the next generation of innovative and trust-driven applications on Ethereum.

FAQ

  • What is Web3.0 and why is it important for Ethereum development?

    plus
  • What criteria should developers use to choose the best Web3 library for their project?

    plus
  • What tips can you offer to beginners who want to learn how to use Web3 libraries with Ethereum?

    plus
  • What are the differences between Web3.js and Ethers.js libraries?

    plus
Vance Wood

Vance Wood

October 2, 2023

10 min read

twittertwittertelegramtelegramLinkedinLinkedin