The Distinctions between Ethers.js and Web3.js

Jordan Type
Coinmonks
4 min readDec 2, 2022

--

Introduction

You have probably come across ethers.js, more so web3.js as a frontend blockchain developer or still as a backend smart contract developer while testing your smart contract. I believe these two libraries are the most important node packages that offer different ways of interacting with your smart contract and processing transaction in an EVM-compatible blockchain.

In this article, I will cover the differences between the two and how they interact with the network.

Prerequisites

To get more out of this article you will need to have:

  1. Knowledge of JavaScript, and Node.js
  2. Basic understanding of smart contracts, web3 and blockchain technology

What is a library?

A library is a packed, reusable section of code that executes a single function or a group of closely related functions, to put it simply. So rather than writing the code from scratch, you may add a library to your program and access it whenever you need to implement that function.

Suppose that instead of an application, you are building a house. One of the things you’ll need for that house is a stove, but building one from scratch when you could just buy one off the shelf won’t be particularly practical. What a library is to a program, a stove is to a home. There are some libraries and tools that you are already familiar with.

What is Ethers?

Ethers.js library aims to be a complete and compact library for interacting with the Ethereum Blockchain and its ecosystem. It was originally designed for use with ethers.io and has since expanded into a more general-purpose library.

Features of Ethers.js

  1. Keep your private keys in your client, safe and sound
  2. Import and export JSON wallets (Geth, Parity and crowdsale)
  3. Import and export BIP 39 mnemonic phrases (12 word backup phrases) and HD Wallets (English, Italian, Japanese, Korean, Simplified Chinese, Traditional Chinese; more coming soon).
  4. Meta-classes create JavaScript objects from any contract ABI, including ABIv2 and Human-Readable ABI.
  5. Connect to Ethereum nodes over JSON-RPC, INFURA, Etherscan, Alchemy, Cloudflare or MetaMask.
  6. ENS names are first-class citizens; they can be used anywhere an Ethereum addresses can be used.
  7. Tiny (~88kb compressed; 284kb uncompressed).
  8. Complete functionality for all your Ethereum needs
  9. Extensive documentation.
  10. Large collection of test cases which are maintained and added to.
  11. Fully TypeScript ready, with definition files and full TypeScript source.
  12. MIT License (including ALL dependencies); completely open source to do with as you please

What is Web3.js?

Web3.js is a collection of libraries that allow developers to interact with a remote or local Ethereum node using HTTP, IPC, or WebSocket.

You can create websites or clients that communicate with the blockchain using this library. This can include, among other things, transmitting ether from one user to another, examining data from smart contracts, and designing smart contracts.

Distinctions between Ethers.js and Web3.js

Unlike web3.js, which provides a single instantiated web3 object with methods for interacting with the blockchain, ethers.js divides the API into two distinct roles. The provider is an anonymous connection to the Ethereum network, while the signer has access to the private key and can sign transactions. The ethers team intended for this separation of concerns to give developers more flexibility.

Comparisons with examples

The following are some common functions that a developer might include in their dapp. You’ll notice that they provide the same functionality, with minor API differences.

Creating a provider with MetaMask wallet

web3

const web3 = new Web3(Web3.givenProvider);

ethers

const provider = new ethers.providers.Web3Provider(window.ethereum)

Obtaining the account balance

web3

const balance = await web3.eth.getBalance(“0x0”)

ethers (supports ENS!)

const balance = await provider.getBalance(“ethers.eth”)

Instantiating contract

web3

const myContract = new web3.eth.Contract(ABI, contractAddress);

ethers

const myContract = new ethers.Contract(contractAddress, ABI, provider.getSigner());

Calling contract method

web3

const balance = await myContract.methods.balanceOf(“0x0”).call()

ethers

const balance = await myContract.balanceOf(“ethers.eth”)

Conclusion

In this read we have learned:

1. what is a web3.js and ethers.js library

2. How both are used to intaract with the smart contract

3. And how they are distict form each other

Links

Docs: https://docs.ethers.io/v5/getting-started/

Docs: https://github.com/web3/web3.js/tree/v1.8.1

Youtube:https://www.youtube.com/watch?v=yk7nVp5HTCk

New to trading? Try crypto trading bots or copy trading

--

--

Jordan Type
Coinmonks

Jordan Muthemba - Tech Enthusiast & Blockchain Advocate. Unraveling tech's potential in society and business. Embracing change, one byte at a time.