I've been revisiting ethers.js recently to refresh my understanding of the details and to write a simple tutorial called "WTF Ethers" for beginners.
Twitter: @0xAA_Science
Community: Website wtf.academy | WTF Solidity | discord | WeChat Group Application
All the code and tutorials are open-sourced on GitHub: github.com/WTFAcademy/WTF-Ethers
In this lesson, we will learn how to use ethers.js to read events emitted by smart contracts. If you are not familiar with events in Solidity, you can read about them in the "Event" section of the WTF Solidity Lesson 12. Note: This tutorial is based on ethers.js v6.
For more details, refer to the ethers.js documentation.
Event
Events emitted by smart contracts are stored in the logs of the Ethereum virtual machine. Logs consist of two parts: the topics and the data. The event hash and indexed variables are stored in the topics for efficient queries, while non-indexed variables are stored in the data and cannot be directly queried but can store more complex data structures.
Take the Transfer event in an ERC20 token contract as an example. It is declared in the contract as follows:
It records three variables: from, to, and amount, which correspond to the address the tokens are sent from, the address they are sent to, and the transfer amount, respectively. The from and to variables have the indexed keyword. When a transfer occurs, the Transfer event is logged and can be viewed on etherscan.

From the above image, you can see that the Transfer event is logged in the EVM log. The Topics contain three pieces of data corresponding to the event hash, the from address, and the to address. The Data contains only one piece of data, which is the transfer amount.
Retrieving Events
We can use the queryFilter() function of the contract type in Ethers to read events emitted by contracts.
queryFilter() has three parameters: the event name (required), the starting block (optional), and the ending block (optional). The retrieval results will be returned as an array.
Note: The event to be retrieved must be included in the contract's ABI.
Example: Retrieving Transfer Events from the WETH Contract
- 
Create a provider.
- 
Create an ABIthat includes the event to be retrieved.
- 
Declare an instance of the WETHcontract.
- 
Retrieve the Transferevents within the last 10 blocks and print one event. We can see that thetopicscontain three pieces of data corresponding to the event hash, thefromaddress, and thetoaddress, while thedataonly contains one piece of data, which is theamount. Additionally,ethersautomatically parses the event based on the ABI, and the parsed event is shown in theargsmember. 
- 
Read the parsed results of the event.  
Summary
In this lesson, we reviewed events in Solidity and learned how to retrieve events emitted by smart contracts using ethers. One important thing to note is that the event to be retrieved must be included in the contract's ABI.