How to stream pending transactions with ethers.js

April 15, 2022

Overview

Here is a video representation of this guide if you prefer to watch instead of read



On ethereum, before being included in a block, transactions remain in what is called a pending transaction queue, tx pool, or mempool - they all mean the same thing. Miners then select a subset of all pending transactions from this queue to mine - there are a lot of benefits to being able to access and analyze this information for traders, people who want to save fees on gas, and more.

In this guide, we will learn how to stream pending transactions from Ethereum and similar chains with ethers.js.

Prerequisites
  • NodeJS installed on your system.
  • A text editor
  • Terminal aka Command Line
  • An Ethereum node

What is a Pending Transaction?

To write or update any on the Ethereum network, someone needs to create, sign and send a transaction. Transactions are how the external world communicates with the Ethereum network. When sent to the Ethereum network, a transaction stays in a queue known as mempool where transactions wait to be processed by miners - the transactions in this waiting state are known as pending transactions. The small fee required to send a transaction is known as a gas; Transactions get included in a block by miners, and they are prioritized based on the amount of gas price they include which goes to the miner.

You can get more information on mempool and pending transactions here.

Why do we want to see pending transactions?
By examining pending transactions, one can do the following:

  • Estimate gas: We can theoretically look at pending transactions to predict the next block's optimal gas price.
  • For Trading analytics: We can analyze pending transactions on decentralized exchanges. To predict market trends using the analysis.
  • Front running: In DeFi, you can preview upcoming oracle related transactions related to price and potentially issue a liquidation for a vault on MKR, COMP, and other protocols.

There can be many use cases for streaming pending transactions - we won't cover them all here.

We’ll use ethers.js to stream these pending transactions with WebSockets. Let’s see how to install ethers.js before writing our code.

Installing ethers.js

Our first step here would be to check if node.js is installed on the system or not. To do so, copy-paste the following in your terminal/cmd:

installing ethers js

Copy
$ node -v

If not installed, you can download the LTS version of NodeJS from the official website.

Now that we have node.js installed let’s install the ethers.js library using npm (Node Package Manager), which comes with node.js.

installing ethers js

Copy
$ npm i ethers

The most common issue at this step is an internal failure with `node-gyp.` You can follow node-gyp installation instructions here.

Note: You will need to have your python version match one of the compatible versions listed in the instructions above if you encounter the node-gyp issue. 

Another common issue is a stale cache; clear your npm cache by simply typing the below into your terminal:

installing ethers js

Copy
$ npm cache clean

If everything goes right, ethers.js will be installed on your system.

Booting our Ethereum node

We could use pretty much any Ethereum client, such as Geth or OpenEthereum (fka Parity), for our purposes today. Since to stream incoming new pending transactions, a node connection must be stable and reliable; it’s a challenging task to maintain a node, we'll just grab a free endpoint from QuickNode to make this easy. After you've created your free ethereum endpoint, copy your WSS (WebSocket) Provider endpoint:

Screenshot of QuickNode Ethereum Endpoint

You'll need this later, so copy it and save it.

Streaming pending transactions

Create a short script file pending.js, which will have a transaction filter on incoming pending transactions. Copy-paste the following in the file:

streaming pending transactions

Copy
var ethers = require("ethers");
var url = "ADD_YOUR_ETHEREUM_NODE_WSS_URL";

var init = function () {
  var customWsProvider = new ethers.providers.WebSocketProvider(url);
  
  customWsProvider.on("pending", (tx) => {
    customWsProvider.getTransaction(tx).then(function (transaction) {
      console.log(transaction);
    });
  });

  customWsProvider._websocket.on("error", async () => {
    console.log(`Unable to connect to ${ep.subdomain} retrying in 3s...`);
    setTimeout(init, 3000);
  });
  customWsProvider._websocket.on("close", async (code) => {
    console.log(
      `Connection lost with code ${code}! Attempting reconnect in 3s...`
    );
    customWsProvider._websocket.terminate();
    setTimeout(init, 3000);
  });
};

init();

So go ahead and replace `ADD_YOUR_ETHEREUM_NODE_WSS_URL` with the WSS  (WebSocket) provider from the section above. 

Explanation of the code above.

Line 1: Importing the ethers library.

Line 2: Setting our Ethereum node URL.

Line 4: Creating the init function.

Line 5: Instantiating an ethers WebSocketProvider instance.

Line 7: Creating an event listener for pending transactions that will run each time a new transaction hash is sent from the node.

Line 8-10: Getting the whole transaction using the transaction hash obtained from the previous step and printing the transaction in the console.

Line 13-16: A function to restart the WebSocket connection if the connection encounters an error.

Line 17-21: A function to restart the WebSocket connection if the connection ever dies.

Line 24: Calling the init function.

Now, let’s run our script.

streaming pending transactions

Copy
$ node pending

If everything goes right, you must see incoming pending transactions. Something like this



Use Ctrl+c to stop the script.

Conclusion

Here we saw how to get pending transactions from the Ethereum network using ethers,js. Learn more about Event filters and Transaction filters in ethers.js in their documentation.

Subscribe to our newsletter for more articles and guides on Ethereum. If you have any feedback, feel free to reach out to us via Twitter. You can always chat with us on our Discord community server, featuring some of the coolest developers you’ll ever meet :)

Related articles 12

How to Interact with Uniswap using Javascript
Apr 12, 2022

Uniswap is one of the most discussed and important projects in the DeFi space. It’s a pretty popular project for many reasons - in this guide, we will learn how to interact with the Uniswap smart contracts using a JavaScript library called

Continue reading
How to setup a Chainlink node
Apr 12, 2022

Smart-contracts are the heart and soul of all the development happening on the Ethereum blockchain, and as more and more people develop on Ethereum, smart contracts are becoming more complex.Sometimes a smart contract wants information about the real world, like...

Continue reading
How to Access Bitcoin Mempool
Apr 12, 2022

Bitcoin is the father of blockchain technology. With Bitcoin started a new era of blockchain and decentralization. Bitcoin enabled everyone to make the peer-to-peer transactions they enjoy today; this guide will teach you how to get these transactions from the Bitcoin...

Continue reading
How to Make a Flash Loan using Aave
Dec 27, 2021

Aave, previously known as ETHLender, has catapulted to the forefront of the DeFi space. Aave was the first in the space to come up with the idea of a Flash Loan. Before flash loans, you would have to stake an over-collateralized...

Continue reading
Cómo realizar un Préstamo Flash en Aave
Jan 10, 2022

Aave, anteriormente conocido como ETHLender, se ha catapultado hacia la delantera en el espacio DeFi. Aave fue el primero de todos en aparecer con la idea de los Prestamos Flash. Antes de los Préstamos Flash, tenías que tener...

Continue reading
Interacting with 0x API using JavaScript
Mar 23, 2022

We have seen tremendous growth in trade volume in DEXs. With many of these coming to the market, it is tough to decide which DEX to choose when you want to swap your token for another. That's where 0x and its APIs come into the picture. It helps us to fetch the DEX with...

Continue reading
How to access Ethereum Mempool
Apr 12, 2022

On Ethereum, when a transaction is sent, before being added to a block, it resides in what is called a Mempool. To receive information about this transaction, the Mempool must be queried. This guide will demonstrate how to query a node’s mempool using QuickNode Ethereum...

Continue reading
How to Listen For Newly Minted Tokens on PancakeSwap
Apr 12, 2022

Hello reader! Today we will be diving into details about making a bot that buys newly minted tokens on the PancakeSwap DEX. Today may be your first time making a trading bot or using the BSC network, and that is okay! By the time you have read through and completed this...

Continue reading
How to Swap Tokens on Uniswap with Ethers.js
Apr 12, 2022

Not all users interact via the front-end UI when trading tokens on a decentralized exchange. Some users (or entities) trade programmatically via a smart contract or server-side scripts. This guide will demonstrate how to swap tokens on Uniswap using Javascript and the...

Continue reading