How to listen to pending transactions using Ethers.js
3 April, 2022
9
9
1
Contributors
Before transactions are recorded into blocks on the blockchain, they will exist in a "pool" commonly known as a transaction pool, txpool, or mempool. All transactions in txpool will be selected by the miner to be recorded in the blockchain. Before being recorded in the blockchain, of course, these transactions had a "pending" status. These are all pending transactions. This article will discuss how we can see what transactions are in the txpool and how to filter pending transactions based on the input data using the Ethers.js library. Ethers.js is a JavaScript library used to interact with Ethereum Virtual Machine based blockchain ecosystem.
Before we start, there may be a question, what's the point of seeing transactions that aren't even recorded on the blockchain when I'm not a miner myself?
There are several things that we can do by listening to pending transactions. One example is that we can predict what will happen to the state of a DeFi if pending transactions against the Smart Contract in question will then be recorded in the blockchain. For example, imagine if we put our life savings in a particular token. To prevent bad things, we need to monitor if the Smart Contract owner disables trading via a function or if liquidity provider(s) will remove the liquidity so we can't swap it back to native coins. By listening to the txpool, we can send an "emergency transaction" with higher gas that will race (front-run) any malicious transaction.
What do you need to be able to listen to pending transactions?
- Fast internet connection.
- Node.js installed
- Ethers.js library.
- WebSocket Provider endpoint (WSS).
How to connect to the blockchain via WebSocket
Assuming you have already installed Node.js and Ethers.js on your machine and have access to the WebSocket provider, you need to create a script like this.
It would be best to put the WSS inside the .env
in your project directory.
How to listen to pending transactions
The above code will bring up all transaction hashes with the pending status that you can retrieve from the connected node. A Provider in ethers is a read-only abstraction to access the blockchain data. provider.on( eventName, listener )
will add a listener to be triggered for each event. We can listen to seven events, one of which is the "pending" event. Hence, in our code, we use the provider.on("pending", txHash)
.
How to filter pending transactions using Ethers.js
Before discussing how to filter pending transactions, please note that in Ethers.js, we can listen for certain events that have occurred (already recorded in the blockchain) using Log Filter, Topic-set Filter, and Transaction Filter. Here's the example from Ethers.js documentation:
Then, can we use these methods to filter pending transactions? The answer is, no, we can't.
The value returned by provider.on("pending", (tx) => {});
is the transaction hash. So to be able to filter certain events or functions to be executed, we have to check all transaction data.
It scans data or input from ALL existing pending transactions and looks for which data contains the hash signature "0xbaa2abde", the Uniswap V2 removeLiquidity event. Because the filter method requires us to process all the data, we should use our own private WSS.
That's roughly how we can listen to pending transactions on Ethereum based blockchains and how to filter to get only certain events.
Next, I will write about how to decode the pending transaction input data to know what the transaction sender will do with the Smart Contract.
blockchain
web3
ethereum
defi
ethers.js