How to get pending transactions in ethereum using web3?

12,613

Solution 1

Did you try using web3.eth.filter?

Following code should work. (unable to test myself at the moment)

var options = {
  fromBlock: "pending",
  toBlock: "latest",
  address: "0xabc123...",
}

web3.eth.filter(options, (error, result) => {
  if (!error)
    console.log(result);
});

Solution 2

This is a known issue# 1741, maybe you can better wait for the transactions to get cleared as a work around.

Solution 3

using web3js 1.0 you can use getPendingTransactions

 web3.eth.getPendingTransactions().then(console.log);
 >  [
     {
         hash: '0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b',
         nonce: 2,
         blockHash: '0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46',
         blockNumber: 3,
         transactionIndex: 0,
         from: '0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b',
         to: '0x6295ee1b4f6dd65047762f924ecd367c17eabf8f',
         value: '123450000000000000',
         gas: 314159,
         gasPrice: '2000000000000',
         input: '0x57cb2fc4'
         v: '0x3d',
         r: '0xaabc9ddafffb2ae0bac4107697547d22d9383667d9e97f5409dd6881ce08f13f',
         s: '0x69e43116be8f842dcd4a0b2f760043737a59534430b762317db21d9ac8c5034'
     },....,
Share:
12,613
Abhishek Kumawat
Author by

Abhishek Kumawat

New Technologies attract me more than New Girls in the city! I love hanging out with Javascript mostly.

Updated on June 21, 2022

Comments

  • Abhishek Kumawat
    Abhishek Kumawat almost 2 years

    I need to calculate the nonce for successive transactions using web3 in Ethereum, but getTransactionCount does not return pending transactions.

    Is there a way to get all transactions including both pending and completed transactions using web3?? If not web3, is there some other way to do that??

  • Abhishek Kumawat
    Abhishek Kumawat over 5 years
    filter didn't work for me, also, is it even a function of web3? If yes, then why it isn't mentioned in this doc
  • Abhishek Kumawat
    Abhishek Kumawat over 5 years
    Perhaps yes, I queued transactions. Now, I wait for the receipt and then execute the next one in the queue.
  • Atu
    Atu over 5 years
    @AbhishekKumawat my friend, the doc you're referring is for web3.js v1.0 the one I've referred to was the web3 0.x.x
  • Abhishek Kumawat
    Abhishek Kumawat over 5 years
    Yea I figured that out. Perhaps, v1.0 has no functionality for this issue. Thanks.
  • user2284570
    user2284570 over 3 years
    @Atu How to do the same thing but by just listening instead of constantly polling?
  • Atu
    Atu over 3 years
    @user2284570 i don't think there's a way to do that for pendingTransactions... afaik there's event listener for contract events
  • user2284570
    user2284570 over 3 years
    @Atu but contract events d ont happens before a transaction is mined?
  • Atu
    Atu over 3 years
    @user2284570 oh i'm not entirely sure about that. But i'm guessing that they don't.
  • user2284570
    user2284570 over 3 years
    @Atu so this isn t an alternative option. Please read stackoverflow.com/q/65095482 for more details.