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 use a pending transaction as an example to demonstrate how to decode transaction details.
Pending Transaction
A pending transaction is a transaction sent by a user but has not been included in a block by a miner yet. It appears in the mempool (transaction memory pool). For more information about the mempool, you can refer to the # WTF Ethers 19: Listening to the Mempool.
Below is an example of a pending transaction that transafers an ERC20
token. You can view the transaction details on etherscan:

The box at the bottom indicates the input data
of this transaction, which seems like random hexadecimal data. In fact, it encodes the calldata of the transaction, including the called function and the input parameters. By clicking the Decode Input Data button on etherscan, we can decode this data:

After decoding, we can see the function called by this transaction and its input parameters.
Interface Class
ethers.js
provides the Interface
class for decoding transaction data. Declaring a Interface
is similar as declaring a abi
, for example:
Decoding Transaction Data
Now let's write a script to decode the data of a pending transaction.
-
Create a
provider
and awallet
. It is recommended to use awss
connection instead ofhttp
when listening to transactions. -
Create an
Interface
object for decoding transaction details. -
Get the function selector.
-
Listen for pending
ERC20
transfer transactions, retrieve the transaction details, and decode them: -
Decoding transaction parameters:
Summary
In this lesson, we use Interface
object to decode pending transactions on Uniswap.