July 06, 2021

How to access Ethereum Mempool

Overview

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 nodes.

What is Ethereum Mempool?

In blockchain terminology, a mempool is a waiting area for the transactions that haven't been added to a block and are still unconfirmed. This is how a Blockchain node deals with transactions that have not yet been included in a block.

When an Ethereum node receives a transaction, it will propagate the transaction to peer nodes until a miner approves the transaction and adds it to a new block. Before it’s added to the next block, the transaction remains in a staging/waiting area called mempool or txpool. It is also known as “pending transactions” to some people.

This waiting area or buffer zone functionality is needed because transactions aren't immediately added to the Blockchain. Nodes also run a series of validity checks on these transactions. These checks include ensuring that the funds are still available, the output is not exceeding the input, the signature is valid, etc. The transaction is rejected if it fails any of these tests. Note that the Mempool cannot be considered as a master reference shared universally by all nodes. Each node configures its own rules for the node’s mempool. Additionally, a node may be the first to receive a transaction but may not have propagated the transaction to the rest of the network.

For example, the default settings for slots and memory on Geth and OpenEthereum dictate their behavior with mempool transactions. When a new pending transaction is allowed into the mempool, but all the node spaces are full -- the transaction with the lowest gas fee will be dropped from the pool. The rules for accepting and dropping transactions vary for different nodes and depend on their settings.

Even the term itself differs depending on clients:

However, “mempool” is a widely used and accepted term.

What is the use of Ethereum Mempool?
As stated above, transactions need to be stored before they are confirmed and picked up by a miner. Usually, the transactions with lower gas prices spend more time in a mempool. Such transactions may be overwritten with replacement transactions (commonly known as 'cancel and speed up transaction'). One of the most prominent uses of the Mempool is to get pending transactions. Here’s why you may want to do that:

  • In Trading, to obtain trading prices on DEX like Uniswap, Balancer, etc.
  • To analyze the gas prices and adjust the transaction price to avoid getting dropped.
  • To analyze and simulate pending transactions, which can help reduce rejection rates.

Let’s get started!

Booting our Ethereum node

For the purpose of this guide, any Ethereum client can be used, such as Geth or OpenEthereum (fka Parity). To boot a new Ethereum node - first, select a client and configure it. Note that syncing and maintaining an Ethereum node is a challenging task and could take days.

To make things easy, we'll grab a node from QuickNode, which is a lot easier to set up. After a QuikNode Ethereum node is up, copy your HTTP Provider endpoint: 

This guide uses an OpenEthereum (Parity) dedicated node and a Geth dedicated node to illustrate the difference between their Mempool approach. But, we can use pretty much any node from QuikNode API plans or a QuikNode Dedicated node with OpenEthereum client to access TX-QUEUE, or QuikNode Dedicated node with Geth client to access TX-POOL.



Copy and save your HTTP provider endpoint.

How to access and query Ethereum Mempool with QuickNode?

First, query the OpenEthereum node's Mempool. OpenEthereum calls its mempool a Transaction Queue or TX-QUEUE. An OpenEthereum client listens to the network for transactions to include in the blocks (if mining/validating) or for transaction broadcasting. These transactions (emitted locally or externally) are verified and ordered into a transaction queue. 

Make a cURL request to the transaction queue/mempool of your node:

$ curl --data '{"method":"parity_pendingTransactions","params":[],"id":1,"jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST ADD_YOUR_ETHEREUM_NODE_URL

Replace `ADD_YOUR_ETHEREUM_NODE_URL` with the HTTP provider you saved earlier.

The above command is making a cURL JSON-RPC request to your node's transaction queue/mempool.

The `parity_pendingTransactions` method returns the pending transactions. In the params section, the number of transactions to return is set with the `limit` parameter. Transactions can also be filtered based on the `to` and `from` address, gas, gasPrice, value, and nonce.

A successful output should look like this (a single transaction is highlighted):
 


The output contains the stream of transactions arranged by priority. Each transaction includes the hash, nonce, block hash, block number, value, gas price, gas, raw transaction data, `to` and `from` address, public key, chain id, etc.

The other txqueue methods are `parity_localTransactions,` which returns 25 local transactions that may be mined, pending, or dropped, and `parity_allTransactions` to return the transactions to be processed in the future as well as dropped transactions.

To query a Geth node's Mempool or TX-POOL or Transaction Pool

Make a cURL request to the transaction pool/mempool of your node:

$ curl --data '{"method":"txpool_content","id":1,"jsonrpc":"2.0"}' -H "Content-Type: application/json" -X POST ADD_YOUR_ETHEREUM_NODE_URL

Replace `ADD_YOUR_ETHEREUM_NODE_URL` with the HTTP provider you saved earlier.

This will make a cURL JSON-RPC request to your Geth node's transaction pool/mempool by using the `txpool_content` method, which will return the pending transactions and those queued for future execution. The output stream will have two fields: pending and queued.

A successful output should look like this (a single transaction is highlighted):



This output features the same elements as in the previous example.

The other txpool methods are `txpool_inspect,` which returns a listed summary of pending transactions in text form for a quick view, and `txpool_status,` which returns the current number of pending and queued transactions.

Querying with Web3

Let’s examine how to subscribe to pending transactions from the mempool using Web3.js. 

Prerequisites
  • NodeJS installed on your system.
  • A text editor
  • CLI

Check our guide on 'How to connect to Ethereum network with Web3.js' to learn how to install and work with the Web3.js library.

Step 1.
Install web3.js by executing the following command: 

$ npm install web3

Step 2.
Now, create a text script file and call it index.js

var Web3 = require("web3");
var url = "ADD_YOUR_ETHEREUM_NODE_WSS_URL";

var options = {
  timeout: 30000,
  clientConfig: {
    maxReceivedFrameSize: 100000000,
    maxReceivedMessageSize: 100000000,
  },
  reconnect: {
    auto: true,
    delay: 5000,
    maxAttempts: 15,
    onTimeout: false,
  },
};

var web3 = new Web3(new Web3.providers.WebsocketProvider(url, options));
const subscription = web3.eth.subscribe("pendingTransactions", (err, res) => {
  if (err) console.error(err);
});

var init = function () {
  subscription.on("data", (txHash) => {
    setTimeout(async () => {
      try {
        let tx = await web3.eth.getTransaction(txHash);
        console.log(tx)
      } catch (err) {
        console.error(err);
      }
    });
  });
};

init();

Explanation of the code above:

Line 1-2: Importing the web3 library, and adding QuickNode's wss URL to the variable url.

Line 4-16: Adding a reconnect logic, so that if the connection is dropped for some reason the script will reattempt to establish a connection.

Line 18-21: Instantiating a WebSocketProvider instance, and assigning it to the web3 variable, subscribing to pending transactions. 

Line 23-34: Getting the transaction body by hash, and printing the transactions. 

Step3.
When we run this file, it should output a stream of pending transactions currently in the mempool.

$ node index



This can be useful for mempool tracking, tracking specific transactions, trading bots, and much more.

Conclusion

This short guide showed you how to easily query a mempool of an Ethereum node using QuikNode as an example. Understanding pending (in-flight) transactions provide valuable insights into your (& others’)  transactions, so you can make valuable adjustments.

Further reading: How to stream pending transactions with ethers.js

Subscribe to our newsletter for more articles and guide on Ethereum. If you have any feedback, please feel free to reach out to us via Twitter, and you can always chat with us if you have a question via our community server on Discord; thanks :)
Ready to try QuickNode? Start your 7-day free trial

Related articles 10

Aug 20, 2021 How to Access Bitcoin Mempool

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
Jun 21, 2021 How to Make a Flash Loan using Aave

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
Jun 9, 2021 How to setup a Chainlink node

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
Jun 9, 2021 How to run a keeper bot for MKR, DAI & ETH auctions

At a high level, keeper auctions allow speculators to automatically buy assets at a discount, like the $4m+ of ETH that was bought for near-zero DAI on Black Thursday. You can see the results...

Continue reading
Jun 9, 2021 How to stream pending transactions with ethers.js

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...

Continue reading
Jun 9, 2021 How to Interact with Uniswap using Javascript

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
Jun 9, 2021 Creating a RESTful API for Compound Finance Smart Contracts

Compound finance are early pioneers in the decentralized finance space, as one of the first defi lenders. Compound offers a way to earn interest on several tokens: ETH, BAT, DAI, REP, WBTC, USDC & a few others. Compound makes this possible by locking your assets in a...

Continue reading
Jul 26, 2021 How to Listen For Newly Minted Tokens on PancakeSwap

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
Aug 6, 2021 Interacting with 0x API using JavaScript

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
Aug 31, 2021 How to run Trade Butler Bot with QuickNode

Bots are often made to automate manual workflows; one such type is trading bots. Trade Butler is a very secure and popular trading bot so let’s see how we can run Trade Butler Bot backed by robust

Continue reading