A central part of Web3 development revolves around listening to and interacting with decentralized networks. One way of doing so is through blockchain listeners, which receive real-time data regarding important on-chain events. However, setting this up has been relatively cumbersome – until just recently. Now, thanks to ethers.js or Moralis’ Web3 Streams API, you can easily set up blockchain listeners. But, out of these two options, which is the best alternative for blockchain developers? If you want to learn more about this, join us in this guide as we compare ethers.js vs Web3 streams!
This article will start by briefly covering the intricacies of ethers.js and Web3 streams. From there, the article compares the two, highlighting their similarities and differences. Lastly, to top things off, we dive into practical examples of setting up blockchain listeners with ethers.js vs Web3 streams. In doing so, we further explore how they differ and what makes Moralis the preferred option!
Throughout the article, you will familiarize yourself with Moralis’ Streams API. However, this is only one of many Web3 APIs offered by Moralis. Other prominent tools include the Auth API, NFT API, EVM API, etc. These tools contribute to a more seamless developer experience and define why Moralis is the fastest way to build a Web3 app! Accordingly, if you are looking to get into Web3 development, sign up with Moralis. You can create your account for free and receive access to the various Web3 development tools immediately!
Ethers.js – What is it?
Some of the core features of ethers.js are the library’s extensive documentation, user-friendly API structure, small bundle size, and that it is written in TypeScript. What’s more, it is a relatively intuitive and straightforward library to use. In turn, it’s one of the top choices among Web3 developers to, for example, set up blockchain listeners.
Ethers.js consists of four central modules: “ethers.contract“, “ethers.utils“, “ethers.wallets“, and ”ethers.provider”. Each module serves a different function and is essential to the library’s application programming interface (API). Nevertheless, if you want to learn more about these modules, and the library in general, check out our other article answering the question, ”what is Ethers.js?”.
What are Web3 Streams?
A Web3 stream is essentially an advanced blockchain listener, allowing you to monitor blockchain wallets or smart contract events, and you can set one up easily with Moralis’ Web3 Streams API! Furthermore, with this API, you can seamlessly stream blockchain data into the backend of your applications or Web3 projects via Moralis webhooks.
Additionally, with the cross-chain capabilities of Moralis, you can set up Web3 streams for multiple chains. This includes Polygon, Avalanche, BNB Chain, Ethereum, and others. What’s more, the Streams API is actively maintained by our team here at Moralis, and they are continuously working on adding more networks!
You can set up Web3 streams with the API to receive webhooks whenever an address sends, receives, swaps, or stakes an asset. Also, you can receive webhooks whenever a battle starts in a Web3 game; someone partakes in a token sale, or any other smart contract events fire based on your filters.
Now, with a better understanding of ethers.js and Web3 streams, we will compare the two in the following section to highlight the similarities and differences between them when it comes to monitoring on-chain events!
Comparison of Ethers.js vs Web3 Streams
Ethers.js is a good alternative for setting up blockchain listeners to receive information about on-chain events in real-time. Unfortunately, if you start working with ethers.js, you will quickly notice that the library has limitations. Therefore, you should consider Moralis’ Web3 Streams API as an option instead. However, you do not have to take our word for it. Join us in the following section, where we compare ethers.js vs Web3 streams to highlight their differences and why Moralis is the preferred option!
The main differences between ethers.js and Web3 streams are summarized in the image above. At a glance, you instantly notice that Moralis provides everything that ethers.js offers and more. However, let us cover each point in further detail to give an in-depth analysis of ethers.js vs Web3 streams!
Why Choose Moralis? – Ethers.js vs Web3 Streams
As the table from the image in the previous section illustrates, you can use both ethers.js and Moralis’ Web3 streams to listen to blockchain events in real-time. Both alternatives feature multiple chains, meaning you can monitor events for various networks. However, this only covers the similarities between ethers.js and Moralis’ Web3 streams; now, let us jump into the differences.
First up, with Moralis, you get 100% reliability, which is not always the case with ethers.js. When you set up a blockchain listener with ethers.js, you must add separate node providers, which in some instances, can be problematic. For example, the provider might not support the chains you want, and it will be challenging to be 100% certain that the nodes stay operational. Instead, with Moralis, you have a single, straightforward tech stack and always get real-time alerts via Web3 webhooks.
Furthermore, you can add filters with Moralis when setting up Web3 streams. As such, you can target particular events and only receive webhooks for the data you are interested in. For example, you can set up streams to solely receive data regarding transactions exceeding a particular transfer amount.
Additionally, with Moralis, you can pool several contract addresses into a single stream, and the webhooks inform you from where an event is emitted. This is not the case with ethers.js, where you must create separate listeners for all contracts you want to monitor.
Finally, you can also use Moralis to listen to wallet addresses. As such, you can receive webhooks when wallets perform a specific action. Moreover, the data received from Web3 streams get parsed, meaning you do not have to worry about additional processing. As such, you receive all the data you need ready for use straight out of the box!
Ethers.js vs Web3 Streams – Which is the Best Option for Developers?
With a comparison of ethers.js vs Web3 streams, you are now familiar with the differences between the two options. However, to make the distinction even more straightforward and highlight the benefits of working with Moralis compared to ethers.js, the following sections explore practical examples based on these two alternatives. As such, let us start by taking a closer look at setting up a blockchain listener with ethers.js!
Ethers.js Practical Example
In the following ethers.js example, we will briefly cover the code for setting up a blockchain listener for monitoring the USD coin smart contract. Specifically, we are going to monitor the contract’s transfer events. Nevertheless, you can find what the entirety of the code looks like in the following image:
The main section is the ”getTransfer()” function, where we initially add the contract address. From there, we specify the ”provider” using one of the ethers.js library’s modules. Since we need to add the node provider when working with ethers.js, we lose some reliability as we cannot know with 100% certainty that they can keep their nodes fully operational at all times. We also need to ensure that the provider supports our interested chains.
From there, we create a new ”contract” object with ”usdcAddress” and ”provider” as parameters. Next, we set the listener to monitor the contract’s ”Transfer” events. Lastly, we finally console-log the results.
Now, once you run the code from the screenshot above, it will return information regarding USDC transfers, and it can look something like this:
The ethers.js blockchain listener returns various information, such as the ”to” and ”from” addresses and event data, such as the ”blockNumber“, ”blockHash”, ”transactionHash”, etc. However, this does not contain parsed data, and we cannot, for example, directly determine exactly from which contract the data originates.
This is a great start, but you get much more with Moralis! Thus, let us dive into the Moralis Web3 Streams API example to explore what makes this a superior alternative!
Moralis’ Web3 Streams Practical Example
For this example, we have the same blockchain listener; however, this time, it has been set up using Moralis’ Web3 Streams API:
To begin with, as illustrated in the screenshot above, we first create an ”options” object with a few parameters. These include the chain(s), a description, a tag, a webhook URL, etc. From there, we additionally add a Moralis API key and create a new ”stream” object by calling the ”Moralis.Streams.add()” function with ”options” an argument.
Lastly, with the ”Moralis.Streams.addAddress()” function, we can add all the addresses we want to monitor. Note that, thanks to Moralis, you can include multiple smart contract addresses by adding them to the ”address” array.
By running the code from above, it autonomously creates a new Web3 stream, adding it to the Moralis admin panel. As such, by logging in to Moralis and clicking on the ”Streams” tab, we should now find a new stream:
From there, it is possible to manage the stream directly through the Moralis UI. With the click of a few buttons, we can add additional contracts, select more chains, and apply filters, for example!
Web3 Streams Response
Now, with a brief overview of how a stream can be structured, let us look closer at the results and the data we receive. As such, we can open the console for the server receiving the webhooks to see what the responses look like:
Above, you can see one of the responses sent to our webhooks server containing parsed data. This response is part of a more extensive array of other transactions with similar structures. Nevertheless, the data includes a transaction hash, log index, contract address, etc. What’s more, since the data has already been formatted, it is possible to use it directly in dapps and other Web3 projects!
As the ether.js vs Web3 streams comparison demonstrates, you get everything that ethers.js offers with Moralis’ Web3 streams and much more! Consequently, it should be a no-brainer to go with Moralis when setting up blockchain listeners!
However, in our example, we did not cover how to add filters. If you want to learn more about this, check out the official Web3 Streams API documentation page. You can also watch the video below from the Moralis YouTube channel. This clip compares ethers.js vs Web3 streams, along with providing a tutorial for adding filters:
Ethers.js vs Web3 Streams – Summary
In this article, you learned that a vital part of Web3 development revolves around interacting and listening to blockchain networks. Accordingly, it’s important for developers to, for example, be able to easily set up blockchain listeners to monitor on-chain events. Moreover, you additionally found out that two prominent methods for doing so are ethers.js and Moralis’ Streams API. However, which alternative should you go for?
To clarify the distinction between the two alternatives, the article compared ethers.js vs Web3 streams. In doing so, you discovered that Web3 streams provide everything that ethers.js offers and more. As such, if you are looking to set up blockchain listeners, then the best choice is Moralis’ Web3 Streams API!
If you found this article helpful, consider checking out additional Moralis content here on the Web3 blog. For instance, learn more about the Streams API by checking out our articles on Ethereum webhooks and Polygon webhooks! What’s more, if you want to become a more skilled Web3 developer, consider enrolling in Moralis Academy. Moralis Academy offers great blockchain development courses for beginners and more experienced developers. For instance, check out the following course covering the fundamentals of Ethereum development: ”Ethereum 101”.
Nevertheless, if you want to create your own Web3 streams in the future, sign up with Moralis right now! Creating an account is free, and you receive access to the various tools of Moralis immediately. As a result, you can leverage blockchain technology to its fullest and build more sophisticated Web3 projects!