In the initial sections of this article, we will explore the intricacies of ethers.js. In doing so, we cover some of the library’s most prominent features and modules. Once you gain a better understanding of ethers.js, we will briefly cover Web3.js and compare the two to highlight the differences and similarities between them. Lastly, to top things off, the last section covers a prominent ethers.js alternative: the Moralis Streams API!
The aforementioned interface is one of Moralis’ outstanding Web3 APIs, enabling anyone to easily stream blockchain data into the backend of any application via Web3 webhooks. Other noteworthy Moralis development tools include the Auth API, NFT API, Token API, etc. These tools contribute to a more accessible Web3 developer experience, which is why Moralis presents the fastest way to build a Web3 app!
If this sounds exciting and you want to access these Web3 development features, sign up with Moralis. You can create an account entirely for free and leverage blockchain technology’s full potential in no time. So, set up your account immediately; it only takes a couple of seconds!
What is Ethers.js?
Ethers.js was initially designed for “ethers.io. However, it has expanded to become a more general-purpose library. Furthermore, ethers.js features a small bundle size, extensive and straightforward documentation, a user-friendly API structure, and is written in TypeScript. Consequently, it is a highly attractive library for many Web3 developers as it is intuitive and straightforward to use.
What’s more, to better understand the utility of ethers.js, let us take a closer look at some of the library’s key features:
- Size – Ethers.js is tiny, only 88 KB compressed and 284 KB uncompressed.
- ENS – Ethereum Name Service (ENS) names are first-class citizens. Consequently, they can be used wherever Ethereum addresses can be used.
- Test Cases – Ethers.js features an extensive collection of test cases that are actively maintained and updated.
- MIT License – The library is entirely open-source, including all dependencies.
- Security – With ethers.js, you can keep your private keys safe and secure.
The features above are only five prominent examples, and ethers.js has much more to offer. As such, there is more for you to discover on your own. However, some essential components of ethers.js we need to explore further in this guide are its various modules. In turn, we’ll understand how the library works.
Before exploring ethers.js modules, let us briefly recap what JS modules are in the first place. A module is a function or a collection of similar functions created to better structure/organize a codebase. What’s more, modules are generally independent, specific, and reusable. Now, with a brief overview of what modules are, let us jump straight into ethers.js!
Ethers.js features four central modules: “ethers.contract“, “ethers.utils“, “ethers.wallets“, and “ethers.provider“. These modules are at the core of ethers.js’ application programming interface (API). Consequently, let us break down each module separately to get a quick overview of their purposes:
- “ethers.contract“ – The central purpose of the “ethers.contract” module is to provide the ability to deploy Ethereum smart contracts seamlessly. However, even though this is the module’s main functionality, it has more to offer. For example, you can use “ethers.contract“ to listen to smart contract events, call functions, query information regarding contracts, and more.
- “ethers.utils“ – You can use “ethers.utils” for both processing user inputs and formatting data. These are two vital Web3 development tasks, meaning that this module makes blockchain development more accessible.
- “ethers.wallet” – With the “ethers.wallet” module, you can easily connect to existing wallets (Ethereum addresses), create new wallets, and sign transactions.
- “ethers.provider” – With the “ethers.provide” module, you can seamlessly establish a connection with the Ethereum network. What’s more, you can utilize this module to query on-chain data and send signed transactions. As such, through this module, you can alter the state of the Ethereum blockchain with ethers.js!
That covers the essentials of ethers.js! In the following section, we will look closely at web3.js to learn how these libraries differ and explore what makes ethers.js unique!
What is Web3.js?
Web3.js is – just like ethers.js – an open-source JS library facilitating seamless interactions with the Ethereum blockchain. Furthermore, the Ethereum Foundation created Web3.js. As such, it has the backing of a rather extensive community, which adds value to this library. Moreover, Web3.js has been around since 2015, meaning that this is a battle-tested library used in many prominent projects.
- “web3.eth“ – This is the module for interacting with the Ethereum network. What’s more, it offers additional sub-modules like “web3.eth.contract“, “web3.eth.subscribe“, “web3.eth.accounts“, etc.
- “web3.net“ – You can use “web3.net“ to interact with network properties.
- “web3.shh“ – With the “web3.shh“ module, you can interact with the Whisper protocol.
- “web3.utils“ – The “web3.utils“ module provides utility functions for Ethereum dapps and other packages.
- “web3.bzz“ – You use “web3.bzz“ to interact with the Swarm network.
Based on the overviews of ethers.js and web3.js, you might have noticed that these libraries are quite alike and serve similar purposes. Consequently, the following section compares the two, highlighting the differences between them!
Ethers.js vs Web3.js
If you have followed along this far, you should now be familiar with ethers.js and Web3.js. In turn, you are now acquainted with the libraries’ various modules, key features, and how developers can use them. You also understand that Web3 libraries like these are vital in blockchain development as they allow developers to save vast amounts of time and resources.
However, you might also have noticed that they serve similar purposes and are quite alike. As such, this section compares ethers.js and Web3.js to highlight some significant differences. By doing so, you will hopefully have an easier time choosing which one to go for in your development endeavors.
There are various ways in which to compare the two libraries; however, for this guide, we will focus on community, documentation, and popularity:
- Underlying Community – Web3.js is, as was previously mentioned, an Ethereum Foundation project. Accordingly, it means that Web3.js is directly supported by the organization and is maintained by ChainSafe. On the contrary, ethers.js was built by a single individual called Richard Moore, and a much smaller team of developers maintains this library.
- Documentation – Ethers.js and Web3.js both have comprehensive documentation. However, in this aspect, it seems like ethers.js has a slight edge based on the extensive and beginner-friendly ”Getting Started” guide.
- Popularity – Web3.js is currently the largest of the two Ethereum JS libraries. However, ethers.js is presently growing at a rapid rate based on weekly downloads. However, in terms of GitHub stars, Web3.js exceeds ethers.js by more than 10,000.
Nevertheless, both ethers.js and Web3.js have their pros and cons. Consequently, it is up to you to choose between them depending on your needs as a developer!
Exploring Ethers.js Alternatives
In addition to the aforementioned libraries, there are other ways to interact with the Ethereum blockchain, and a great example is Moralis’ Web3 Streams API! With the API, you can seamlessly stream on-chain data into the backend of your applications via Moralis webhooks. What’s more, because of Moralis’ cross-chain capabilities, you don’t have to limit yourself to the Ethereum network. In fact, you can set up streams for multiple leading programable blockchains. This includes Polygon, Avalanche, BNB Chain, Cronos, Fantom, and many more! As a result, you can future-proof all of your blockchain projects!
From a conventional perspective, it has always been a cumbersome task to query blockchain networks for data. However, this is no longer the case, thanks to Moralis! With the Streams API, you can avoid tasks such as connecting and maintaining RPC nodes, wasting time building complicated data pipelines, setting up unnecessary abstractions, etc. Instead, you can save time and focus on creating more value for your customers.
As a result, you can easily set up streams to receive Web3 webhooks on several networks and layer-2 (L2) platforms whenever:
- Someone partakes in a token sale
- Particular assets are sent, swapped, received, or staked
- A certain address sends or receives assets
- A battle starts in an online Web3 game
- Or other smart contract events trigger based on your filters
What’s more, thanks to the accessibility of the Streams API, you can set up streams in only five straightforward steps:
- Provide an address
- Apply filters to specify when to receive webhooks
- Specify the chains you are looking to monitor
- Add your webhook URL
- Receive webhooks
If you want more information on how to set up Web3 streams with Moralis, check out our guides on Ethereum webhooks and Polygon webhooks. These articles provide comprehensive tutorials for setting up Web3 webhooks for various chains! In addition, If you’d like to explore how Moralis – the ultimate Web3 provider – enables developers to get wallet balance, including native and ERC20 token from address, make sure to read our article exploring the best ERC20 token balance API! Additionally, if you want to add scalable Web3 infrastructure to your project, Moralis is your go-to option. In fact, Moralis is trusted by industry leaders, including MetaMask, 1inch, SuperFarm, and many others! On average, projects save $86.4 million in engineering costs and a whopping 87% reduction in time-to-market!
Summary – What is Ethers.js?
An exciting ethers.js alternative is Moralis’ Web3 Streams API. With this interface, you can easily set up Web3 webhooks to easily stream on-chain data into the backend of any project. As such, with Moralis, you can create dapps more effectively and efficiently by fully leveraging the power of blockchain technology.
If you liked this tutorial, consider checking out more articles here at Morals’ Web3 blog. For instance, read our guides on the Goerli testnet, how to create an AWS Lambda function, what the next-gen L2 chain – the Palm network – entails, and much more! The blog features fresh and exciting Web3 development content for both new and experienced developers.
Moreover, if you want to become a more proficient Web3 developer, consider enrolling in Moralis academy. The academy offers a selection of informative and exciting blockchain development courses. For example, check out ”Blockchain & Bitcoin Fundamentals” to learn the basics of the blockchain industry!
Nevertheless, no matter your Web3 ambitions, sign up with Moralis to access the most seamless Web3 developer experience. Creating an account is free, so you have nothing to lose!