6

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

1
  • is your geth client already 100% sync with the main network?
    – Majd TL
    Mar 12, 2020 at 10:10

2 Answers 2

14

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"
      }
    }
  }
}
3
  • 1
    Thank you for your reply. I could use TxPool. However, I don't know why there is a difference in the number of pending transactions between my geth node and etherscan.io. For example, The number from my geth node is 4281. But that on etherscan is 72385.
    – Pierogi
    Mar 13, 2020 at 8:23
  • Make sure to increase the number of txpool slots with --txpool.globalslots. Mar 14, 2020 at 17:04
  • 1
    Now number of pending transactions is 67862 on my geth node. Thank you!
    – Pierogi
    Mar 18, 2020 at 0:23
2

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

In order to get pending transactions using web3.js, the required steps are:

npm i web3

Create a file getPendingTx.js

const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
const getPendingTransaction = () => {
  web3.eth.getPendingTransactions((err, pendingTxs) => {
    console.log('Pending transactions are: ', pendingTxs);
    // In case if you want to return pendingTxs
    return pendingTxs;
  });
}

Example

Web3 Pending Transactions Reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.