[Ethereum] Get the list of pending transactions with Web3 for the given address/wallet

pending-transactionsweb3js

I'm trying to get information whether there are pending transactions for the given address. My code is as follows:

web3.eth.getTransactionCount(‘ADDRESS’, ‘pending’, function(error, result){
  if(!error)
      console.log(JSON.stringify(result));
  else
      console.error(error);
})

This works, however it returns the count of ALL of the transactions I see in Metamask, not just the ones in "pending" state.

Any clues how can I get only the pending ones?

Thanks!

Best Answer

pending is a special state in ethereum, containing transactions that will likely be in the next block. If you make a transaction with a very low gas price, you will not end up in the pending state.

Since pending is a defined state, when you query the pending tx count, you are querying the expected tx count after one more block has been mined, which will naturally contain the count of transactions before it.

If you wish to just list unmined transactions, your best bet is to retrieve the txpool, and parse that for your address.