4

How would I go about monitoring pending transactions in real-time? Would I be able to monitor only one function?

I have a full node running and have a nodejs (express server) that I want to have this info available to.

I am guessing that a websocket would be the way to go, in which case what would I be listening for?

Vignesh Karthikeyan
  • 1,950
  • 1
  • 12
  • 40
adam
  • 97
  • 1
  • 7

2 Answers2

6
  1. You can create a stream of pending transactions using web3.eth.subscribe('pendingTransactions' [, callback]);, which currently returns a transaction hash.
  2. You can turn into the actual transaction object using web3.eth.getTransaction(transactionHash [, callback]), which will return a transaction object.
  3. You can then filter the returned object by to and input to find only the transactions that are being sent to a specific contract address and are calling a specific function in that contract.
Shawn Tabrizi
  • 8,008
  • 4
  • 19
  • 38
  • 3
    Thanks. I ended up coming to the same conclusion myself. It would be nice if pendingTransactions subscription return the full object as per the documentation ;) – adam May 25 '18 at 03:26
  • in the input field - what exactly am I looking for? Each transaction is different it seems. – adam May 25 '18 at 03:50
  • https://ethereum.stackexchange.com/questions/11144/how-to-decode-input-data-from-a-transaction?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Shawn Tabrizi May 25 '18 at 05:44
  • 2
    getTransaction() doesn't work with pending transactions unfortunatly. I tried with 4 different versions of Web3. – Philip Rego Sep 14 '20 at 04:23
  • @PhilipRego, Were you able to resolve this issue? How to get the pending transaction details using transaction Hash? – Sai Chander May 21 '22 at 18:39
  • I don't remember but maybe see if ethers.js has it https://docs.ethers.io/v5/ – Philip Rego May 25 '22 at 03:46
  • Not all web3 providers support streaming mempool transactions, if nothing shows up, check your provider – Aaron Ash Jun 19 '22 at 10:15
0

Bit late to the show, but to resolve the getTransaction you need to create a buffer, wait till it gets a few confirmations that way the data is more available.

I would suggest creating a batch call, adding them all then rotate through the batch call for the ones you need.