How to get pending transactions in ethereum using web3?
Asked Answered
C

4

6

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

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

Costello answered 11/12, 2018 at 6:58 Comment(0)
T
2

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'
     },....,
Thalassography answered 10/7, 2019 at 22:50 Comment(0)
L
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);
});
Lowboy answered 12/12, 2018 at 4:40 Comment(8)
filter didn't work for me, also, is it even a function of web3? If yes, then why it isn't mentioned in this docCostello
@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.xLowboy
Yea I figured that out. Perhaps, v1.0 has no functionality for this issue. Thanks.Costello
@Lowboy How to do the same thing but by just listening instead of constantly polling?Crossbreed
@Crossbreed i don't think there's a way to do that for pendingTransactions... afaik there's event listener for contract eventsLowboy
@Lowboy but contract events d ont happens before a transaction is mined?Crossbreed
@Crossbreed oh i'm not entirely sure about that. But i'm guessing that they don't.Lowboy
@Lowboy so this isn t an alternative option. Please read https://mcmap.net/q/1913587/-how-to-listen-for-incoming-transactions-not-yet-mined-for-a-single-address for more details.Crossbreed
T
1

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

Thrave answered 12/12, 2018 at 15:55 Comment(1)
Perhaps yes, I queued transactions. Now, I wait for the receipt and then execute the next one in the queue.Costello
T
0

I made a script in python using web3.py to retrieve pending transactions! it's on my GitHub: https://github.com/soos3d/Retrive-and-display-pending-transactions-Web3.py. Feel free to check it out! But the main code to do that is this.

# retrive pending transactions hash

pending_tx_filter = web3.eth.filter('pending')
pending_tx = pending_tx_filter.get_new_entries()

then you can loop through the "pending_tx" variable and extract the transactions hash.

Tabbie answered 14/4, 2022 at 1:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.