April 12, 2022

How to Access Bitcoin Mempool

Overview

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

Prerequisites:
  • Terminal/cmd (CLI).
  • Bitcoin Node.
  • Love for decentralization.

What is Bitcoin Mempool?

The transactions which are sent on the Bitcoin Network are not added directly to the blockchain. All of the valid transactions have to enter a waiting area before they are accepted in a block. This waiting area is known as the mempool. If the size of the mempool is large, it indicates high traffic on the network which results in longer transaction confirmation times and higher transaction fees.

Note: In standard practice, Bitcoin is protocol/network, and bitcoin or BTC is the currency.

The Bitcoin transaction fee is measured in the number of satoshis paid for per byte of the transaction. Satoshi is the smallest unit of the native currency bitcoin of the Bitcoin network, where 1 bitcoin = 100 Million satoshis. Miners give priority to the transactions in the mempool with high transaction fees. But since the SegWit upgrade, a few things changed around Bitcoin transactions. It introduced a new transaction known as a SegWit transaction to increase the number of transactions per Bitcoin block. It fixed two issues: 1. It increased the number of transactions on Bitcoin per second, 2. It fixed the issue of transaction malleability.

Before SegWit, blocks or transactions were measured in bytes, and each block was limited to 1 MB (MegaByte) or 1 Million bytes. After SegWit, transactions, and blocks are measured in weight units; these weight units are used to measure Bitcoin data and compare transactions.

The next concept to cover is vByte. A vByte is equivalent to four 4 weight units. A Byte of data takes 1 vByte (4 weight units) for a legacy transaction. Whereas, for a SegWit transaction, a Byte of data takes 1/4 vByte (1 weight unit). This discount allows the blocks to be filled with 4 times more transactions. The current Bitcoin block size is limited to 1 vMegabyte/ 1 million vByte/ 4 million weight units. Most wallets calculate transaction fees in terms of satoshis/vByte, which is paid per vByte of data used.

Bitcoin mempool analysis use cases:

Mempool analysis helps us understand the congestion/traffic on the network, which ultimately helps us calculating transaction fees.

Let us now access the mempool and get some transactions data from the pool.

Booting a Bitcoin node

For our purpose today, we need a Bitcoin node to access the blockchain data. Launching our own node here will be very expensive and time-consuming, given the large amount of data on Bitcoin. We will instead get a free trial Bitcoin node from QuickNode.

Screenshot of Quicknode Bitcoin endpoint

Copy and save the HTTPS URL of your node as it will be needed later.

How to access Bitcoin Mempool/How to get pending transactions from Bitcoin

We will use Bitcoin RPC methods to get mempool data getrawmempool method to be specific Open terminal/cmd and copy-paste the following:

curl --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getrawmempool", "params": [true]}' -H 'content-type: application/json' <QUICKNODE_BITCOIN_URL>

Replace QUICKNODE_BITCOIN_URL with your Bitcoin node’s HTTPS URL, which we got in the last step. Above is a cURL request to our node along with the getrawmempool method. 

The output should be similar to this.


Now let us understand the output body. The above method gives out a list of pending transactions currently in the mempool. We will take out a single transaction and go through each field.

{
    "result": {
        "2d1228abf06836b1173936061fec0384e82e3b684d7950a27f06a06f587400d3": {
            "fees": {
                "base": 0.00111999,
                "modified": 0.00111999,
                "ancestor": 0.00111999,
                "descendant": 0.00111999
            },
            "size": 90826,
            "fee": 0.00111999,
            "modifiedfee": 0.00111999,
            "time": 1629052793,
            "height": 695936,
            "descendantcount": 1,
            "descendantsize": 90826,
            "descendantfees": 111999,
            "ancestorcount": 1,
            "ancestorsize": 90826,
            "ancestorfees": 111999,
            "wtxid": "b3b49bb3dcae483d579710a2e1f9c7ed585ab35e2a8cb941ff8cb27ea8adec20",
            "depends": [],
            "spentby": [],
            "bip125-replaceable": false
        }
    }
}

Explanation of the above JSON output:

Line 3: This is the transaction id by which a transaction on the blockchain is identified.

Line 4-9: Transaction fee breakdown.

Line 10: Size of the transaction as per BIP 141.

Line 11: Transaction fee in BTC (bitcoin).

Line 12: Transaction fee with fee deltas used for mining priority.

Line 13: Time at which the transaction entered in the pool in seconds since 1 Jan 1970 GMT.

Line 14: Height of the blockchain (latest block) when transaction entered the pool.

Line 15: Number of descendent transactions in mempool, including this one.

Line 16: Size of all the descendent transactions in mempool.

Line 17: Modified fees of all the descendent transactions in mempool.

Line 18: Number of ancestor transactions in mempool, including this one.

Line 19: Size of all the ancestor transactions in mempool.

Line 20: Modified fees of all the ancestor transactions in mempool.

Line 21: Transaction id along with witness data.

Line 22: If an unconfirmed transaction is used as input for this transaction, it is to be entered here.

Line 23: If another unconfirmed transaction is using data from this transaction, it must be entered here.

Line 24: A Boolean value showing that whether the transaction can be replaced due to BIP 125.

Now to get the actual transaction data we will have to use another method getrawtransaction which will return information about a particular transaction. Copy-paste the following in your terminal/cmd:

curl --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getrawtransaction", "params": ["2d1228abf06836b1173936061fec0384e82e3b684d7950a27f06a06f587400d3", true]}' -H 'content-type: application/json' <QUICKNODE_BITCOIN_URL>

Replace QUICKNODE_BITCOIN_URL with your Bitcoin node’s HTTPS URL, which we got in the last step. Above is a cURL request to our node along with the getrawtransaction method. To get this, we will ping our node to resolve the method. We have to pass two parameters for this method. The first is the transaction id of the transaction to be queried. The second is a boolean value which, if set false, returns a string; otherwise, it returns a JSON object.

We have set the second parameter as ‘true’, so the output should look like this.


Explanation of the transaction object pasted here:

Line 3: Transaction id by which a transaction on the blockchain is identified (same as provided).

Line 4: Transaction hash which from txid for witness transactions.

Line 5: Size of the transaction as per BIP 141.

Line 6: Virtual size of the transaction which from size for witness transactions.

Line 7: The version number of the transaction, currently 1 or 2. If 2, it means BIP 68 applies.

Line 8: Lock time is specified as a block number. If a lock time is mentioned, then that transaction can only be added to a block if the lock time block has been passed. For example, if the lock time is mentioned as 30, miners can only pick up the transaction after block number 30 has been mined.

Line 9-20: Vin shows the list of transactions in mempool this particular transaction is using as input.

Line 21-33: Vout shows the list of transactions in mempool using this particular transaction as input.

Line 34: Block hash, null here as the transaction is not confirmed yet.

Line 35: Number of confirmations the transaction has passed.

Line 36: Time at which the transaction entered in the pool in seconds since 1 Jan 1970 GMT.

Line 37: Time at which block was mined in which the transaction was added, null here since the transaction is pending.

Line 38: Serialized, hex-encoded data for 'txid'

By analyzing the above data we can understand in and outs of a Bitcoin transaction. Bitcoin transactions are pretty complex, but getrawtransaction method gives out the raw transaction information which is easy to look at and understand.

Conclusion

Congratulations on mastering the Bitcoin mempool. Today in this guide, we learned what Bitcoin mempool is, the use of Bitcoin mempool, how to access Bitcoin mempool/how to get Bitcoin pending transactions, and how to get Bitcoin pending transaction data.

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 :)
Ready to try QuickNode? Start your 7-day free trial

Related articles 12

Apr 12, 2022 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
Apr 12, 2022 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
Apr 15, 2022 How to stream pending transactions with ethers.js

Here is a video representation of this guide if you prefer to watch instead of read[yt:YjQj6uk9M98]On ethereum, before being included in a block, transactions remain in what is called a pending transaction queue, tx pool, or mempool -...

Continue reading
Dec 27, 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
Jan 10, 2022 Cómo realizar un Préstamo Flash en Aave

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
Apr 12, 2022 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 &amp; a few others. Compound makes this possible by locking your assets in a...

Continue reading
Dec 27, 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
Mar 23, 2022 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
Apr 12, 2022 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
Apr 12, 2022 How to access Ethereum Mempool

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
Apr 12, 2022 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
Apr 12, 2022 How to Swap Tokens on Uniswap with Ethers.js

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