[Ethereum] How to access to txpool within a web3 script

go-ethereumtxpool

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…

Best Answer

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.

Related Topic