Go Ethereum – How to Get Pending Transactions Using Geth or Other Clients

clientsgo-ethereumnodejspending-transactionsweb3js

I would like to get pending transactions with using geth.

And I tried to use 'web3.eth.pendingTransactions' on geth console.
So the geth returned '[]'.

> web3.eth.pendingTransactions
[]

But I visited Etherscan's pending transactions page.

They could display pending transactions.
It was displayed 'A total of 74,945 pending transactions found' and the transactions list.

Why can they gain pending transactions with elapsed time ?
Is this possible with using geth or other client ?
Or is this the software of their own developing ?

I'd like to get pending transactions with using geth via web3.js (or with using other client via other node.js library).

Best Answer

To get the pending transactions you need a node that you're running on your own because this type of action requires a lot of resources.

You can run your own Geth node and wait until it is synchronized. When I want to run a Geth node on the main net and query for the transaction pool, I usually start it like this.

Download the Geth binary and rename it as geth.bin.

Then run:

./geth.bin --datadir=./chaindata \
    --rpc --rpcapi="eth,net,rpc,web3,txpool,personal,debug,account" \
    --rpccorsdomain='*' --port 30303 \
    --txpool.globalslots=250000 \
    --txpool.globalqueue=50000 \
    console

The important parts are:

  • --rpc enables rpc requests, so you can connect with web3.js / web3py / ...
  • --rpcapi defines what APIs are available over RPC; the important one in this case is txpool which enables a Geth specific API to query for the transaction pool; this is usually faster than the standard one. Documented here as a non-standard RPC method;
  • --txpool.globalslots and --txpool.globalqueue increases the number of slots available for the pending transactions; otherwise it will ignore other pending transactions once it reaches a specific number (by default 4096)
  • console it will open a console where you can run commands in.

After you start Geth like that and wait for it to be synchronized, you can run commands inside the open console.

txpool.status doc will display a statistic of pending and queued transactions. Pending are the ones that you care about, might be included in the next blocks if the miners choose to add them.

> txpool.status
{
  pending: 1234,
  queued: 4321
}

txpool.content doc will display details about each transaction in the current lists.

> txpool.content
{
  pending: {
    0x0216d5032f356960cd3749c31ab34eeff21b3395: {
      806: {
        blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
        blockNumber: null,
        from: "0x0216d5032f356960cd3749c31ab34eeff21b3395",
        gas: "0x5208",
        gasPrice: "0xba43b7400",
        hash: "0xaf953a2d01f55cfe080c0c94150a60105e8ac3d51153058a1f03dd239dd08586",
        input: "0x",
        nonce: "0x326",
        to: "0x7f69a91a3cf4be60020fb58b893b7cbb65376db8",
        transactionIndex: null,
        value: "0x19a99f0cf456000"
      }
    }
  },
  queued: {
    0x976a3fc5d6f7d259ebfb4cc2ae75115475e9867c: {
      3: {
        blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
        blockNumber: null,
        from: "0x976a3fc5d6f7d259ebfb4cc2ae75115475e9867c",
        gas: "0x15f90",
        gasPrice: "0x4a817c800",
        hash: "0x57b30c59fc39a50e1cba90e3099286dfa5aaf60294a629240b5bbec6e2e66576",
        input: "0x",
        nonce: "0x3",
        to: "0x346fb27de7e7370008f5da379f74dd49f5f2f80f",
        transactionIndex: null,
        value: "0x1f161421c8e0000"
      }
    }
  }
}
Related Topic