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?

2 Answers 2

  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.
  • 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, 2018 at 3:26
  • in the input field - what exactly am I looking for? Each transaction is different it seems.
    – adam
    May 25, 2018 at 3:50
  • 2
    getTransaction() doesn't work with pending transactions unfortunatly. I tried with 4 different versions of Web3. Sep 14, 2020 at 4:23
  • @PhilipRego, Were you able to resolve this issue? How to get the pending transaction details using transaction Hash? May 21, 2022 at 18:39

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.