4

I run a testnet Ropsten (revival) ethereum node. I would like to get the content of txpool. With a geth console, I can access the variable txpool.

My question is how can I access this variable within a web3 script ?

Web3 = require("web3");
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

Someting like the following:

pendingTx = web3.txpool ?

pendingTx = web3.eth.txpool ?

they are all undefined...

4
  • you are right web3.txpool is returning undefined. Looks like this API is unavailable.
    – Sanchit
    Commented Aug 11, 2017 at 14:50
  • 1
    When we launch geth with rpcapi and ipcapi, we can specify txpool as an option: --rpcapi "eth,web3,txpool" --ipcapi "admin,db,eth,miner,net,shh,txpool,web3" Are they useful for something ?
    – jfjobidon
    Commented Aug 11, 2017 at 18:40
  • Yes, I defined txpool while starting geth node but still web3.txpool returns undefined.
    – Sanchit
    Commented Aug 11, 2017 at 18:46
  • The --XXXapi only enables the server side api to be available. On the client side you can use npmjs.com/package/web3admin, to extend the normal web3 with extra methods. But some methods are still missing and you have to add them by hand.
    – Ismael
    Commented Aug 12, 2017 at 0:45

1 Answer 1

4

With Web3 1.0.0 it's easy to implement it yourself:

var Web3 = require('web3');
var web3 = new Web3('ws://127.0.0.1:8546');
web3.eth.extend({
  property: 'txpool',
  methods: [{
    name: 'content',
    call: 'txpool_content'
  },{
    name: 'inspect',
    call: 'txpool_inspect'
  },{
    name: 'status',
    call: 'txpool_status'
  }]
});

Then use it normally:

web3.eth.txpool.status().then(console.log).catch(console.error)

Output:

{pending: "0x0", queued: "0x0"}

The same way you can extend Web3 to invoke any other "missing" JSON RPC.

3
  • Found this strange behavior, Method name content is not working, while others are working.
    – Ayushya
    Commented Jun 15, 2018 at 19:22
  • I'm using infura and it can't resolve these functions. Commented Sep 14, 2020 at 3:49
  • Can you tell, is there any way to subscribe to txpool? so, not just one-time polling event, but got a websocket/realtime one (considering that we have a standard WS provider).
    – T.Todua
    Commented Dec 1, 2021 at 22:33

Your Answer

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

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