Introduction to ethers.js – Complete Guide (2022)
![Introduction to ethers.js – Complete Guide (2022)](https://wsrv.nl/?url=https%3A%2F%2Fcodedamn-blog.s3.amazonaws.com%2Fwp-content%2Fuploads%2F2022%2F11%2F29200641%2Fwhat-is-nft.png&w=1280&q=82&output=webp)
Ether.js is one of the fastest-growing libraries for building unstoppable web 3.0 applications. It’s become a strong preference by many experienced developers as their go-to library for building javascript-based blockchain applications because it is elegant and it’s easy to use. You can get up to speed pretty fast.
In this blog, I will give you an introduction, how to get started with ether.js, and the complete guide you need. This blog will cover all the things like the prerequisite of ether.js and the proper steps to follow so that you can get up to speed and start building your own blockchain applications in no time.
About Ether.js
Before learning about ether.js, let’s discuss Blockchain
Blockchain is a worldwide computer or it’s a peer-to-peer network of nodes that all talk to one another. Consider it like a web server in the sense that each of the nodes on the network is a computer but each node keeps a redundant copy of all the information and all the programs on the blockchain.
![](https://wsrv.nl/?url=https%3A%2F%2Fcodedamn-blog.s3.amazonaws.com%2Fwp-content%2Fuploads%2F2022%2F11%2F30231449%2Fimage-5-615.png&w=1920&q=75&output=webp)
Ether.js is one of the most popular javascript-based tooling kits that allow us to interact with different blockchains and it has all the wrappers that help to make the APIs call and do all sorts of things with Ethereum and polygon and avalanche and any EVM-compatible chain.
Ether.js is a javascript library that enables you to work with Ethereum-based virtual machines i.e EVM. Basically, we all use web applications through browsers like chrome, brave, safari, and many others out there, when you are browsing the browsers are talking directly to the websites.
Overview of Web 3.0
Those websites are usually written in HTML, CSS, and Javascript which are called the frontend part, and then that website talks to a backend typically that in on a central server, it has some APIs that help it to talk to the backend and get the data. That’s how a typical web 2.0 website works where you have a website and all the information is stored in a central server.
![](https://wsrv.nl/?url=https%3A%2F%2Fcodedamn-blog.s3.amazonaws.com%2Fwp-content%2Fuploads%2F2022%2F11%2F22104254%2Fimage-5-583-1024x507.png&w=1920&q=75&output=webp)
Let’s look at a web 3.0 way of doing things which is a very similar scenario where you have your web browsers like chrome, safari, or brave and then you talk to front-end websites where you might be interacting with a decentralized application or adapt.
Web 3.0 got the same website in HTML, CSS, and Javascript, and then instead of talking to a backend that might directly talk to a blockchain. Like normal websites, it doesn’t have any central server, and all the code and data of the application is on a server that is present on a blockchain replacing the typical backend from the web 2 scenario and it uses smart contracts to deal with it.
![](https://wsrv.nl/?url=https%3A%2F%2Fcodedamn-blog.s3.amazonaws.com%2Fwp-content%2Fuploads%2F2022%2F11%2F29171935%2Fimage-5-609-1024x516.png&w=1920&q=75&output=webp)
To create such websites that actually talk to a blockchain then the critical piece to understand is how to get the frontend to talk to the backend in the website to a blockchain so you can interact with smart contracts and also get information to the blockchain itself and write information to the blockchain.
How Ether.js works?
If you want to create a website that talks to the nodes of the blockchain so that you can interact with the smart contracts, you know to get information from the blockchain and write new transactions that you need to connect to a node.
![](https://wsrv.nl/?url=https%3A%2F%2Fcodedamn-blog.s3.amazonaws.com%2Fwp-content%2Fuploads%2F2022%2F11%2F29174218%2Fimage-5-610-1024x539.png&w=1920&q=75&output=webp)
Initializing connection to the blockchain nodes can achieve in many ways like directly from your web browser with a website, or you could do it with a script on your computer or you could do ti with a back-end server.
Any of these things can wire up a connection to an individual node on a blockchain to start using it so that it can get information about cryptocurrencies, and NFTS, and write new transactions. It can help you to create a website that powers the full-stack decentralized applications.
You need something that connects your client to one of these nodes of blockchain so they can start using the full power of the blockchain, that’s exactly what ether.js is it’s a library written in javascript that lets you create any javascript application that talks to the blockchain so you can use and create many applications which are going to be based n decentralized application (daps). It’s the bridge that essentially that takes your client and it allows it to connect to the blockchain.
Ether.js Modules
Ether.js consists of some important modules that can be used to interact with blockchain nodes easily, and get the transaction data as required. To get started with the modules of Ehter.js, let’s take an overview of all the Ether.js modules.
- Ethers.Provider: In this module it lets you initialize a connection with the Ethereum blockchain, and it provides you the features to issue queries and send signed transactions. Managing the state of the blockchain is also possible through this module.
- Ethers.Contract: In this module, you can deploy and interact with smart contracts, during the deployment part of smart contracts and to make it successful is the part of Ethers.Contract module. It also offers some unique packs of functions that let the developer “listen” to smart contract events and after listening to the contracts you can also get information about them.
- Ethers.Utils: This module lets you process the user data inputs and format them according to your requirements. Ether. utils make it easy to build decentralized applications.
- Ethers.Wallet: As the name suggests it provides a way to connect to any co-existing Ethereum address to a proper wallet. It also has important features like it lets you create new wallets and also make sign transactions too.
Where Ether.js are used?
The Ether.js library provides many sets of tools as we saw in the above context to interact with the Ethereum Nodes along with Javascript or Typescript. It was originally designed for use in some other field but turns out into a more general-purpose library.
Because of Ether.js we now connect to the blockchain with ease and get all the information required like transaction details, block updates, and many other things.
One of the blockchain products which uses ether.js as their solely reliable communication to blockchain nodes is uniswap.org
Getting started with Ether.js
Starting with Ether.js is fairly easy. You can follow these steps in order to start using Ether.js
.
Installing
Before installing Ether.js, you need Nodejs installed on your computer, to install node js just click on this link . After setting up the Nodejson your computer type this command in the terminal.
// this will install ether libray on your particualr folder
npm install --save ethers
Code language: JavaScript (javascript)
Importing
We will see how to import ether.js after installing it in our folder. There are two ways for javascript and react.
Javascript:
// this will import ether.js in your project
const { ethers } = require("ethers");
Code language: JavaScript (javascript)
React:
// this will import ether.js in your project
import { ethers } from "ethers";
Code language: JavaScript (javascript)
Metamask
Metamask is a cryptocurrency wallet that lets you enable users to store Ethereum and other tokes which lies in ERC-20. It is a browser plugin that helps you to store or transfer Ethereum or any other ERC-20 tokens.
The quickest and easiest way to develop any Ethereum-based product is to use Metamask which is a browser extension that provides:
- Connection to the Ethereum network
- Reserved your private keys and can use them as a signer.
- Interact with faucets like virtual coins with the help of a test network.
Start Developing the Project
After you have successfully executed the above code, you have successfully performed the installation and setup of Ether.js
.
Now, you can install and use truffle
framework in order to create a project that will help you use Ether.js with React, which is one of the most popular frameworks for building a front end.
Also, you should install ganache-cli,
which will help you to set up an Ethereum-like blockchain on your machine. It will also provide you with some wallet addresses with some fake testing, which you can use to perform some operations and pay the required gas fee (fee required to create the transaction block on the chain)
Truffle provides some of the features and many tools which will help you to compile and deploy the smart contract Ethereum or other Ethereum-based blockchains (testnet/mainnet) and many more. These will help you create a fully-fledged decentralized app like NFT(Non-Fungible Token) Marketplace
based on ERC721 Standards
, a token-minting app based on ERC20 Tokens
, and many more.
Conclusion
Ether.js is essentially one of the lightest libraries compared with web3.js. It is a small, compact library, with a very good number of test cases. The popularity of a library is crucial because if the demand is less then the project will not get updated day by day and the error or bugs will not get fixed. Ether.js is really popular so this will never arrive in the future for sure.
The performance of ether.js is more compared to web3.js and the ether.js library also claims to be 284kb uncompressed, and on NPM it is listed at 3.5 MB unpacked. Web.js is quite heavy with respect to ether.js. The documentation is great and it still getting updated as per the new features roll out.
We saw about Ether.js and what features it provides. And learning it would be really beneficial for your career in web3 if you are thinking of starting. Codedamn provides a whole web3 full-fledged curated hands-on learning and practicing course check it out too .
Try to explore all the stuff in ether.js and have great learning!😊
Sharing is caring
Did you like what Anas Khan wrote? Thank them for their work by sharing it on social media.
No comments so far
Curious about this topic? Continue your journey with these coding courses: