I have the following code scanning the pending transaction pool until my wallet is found:

var scanTxPool = async () => {
    var myWallet = '0x0000000000000000000000000000000000000000';
    var foundWallet = false;
    provider.on("pending", async (txHash) => {
        if (!foundWallet) {
            provider.getTransaction(txHash).then(async (tx) => {
                if (tx && tx.to) {
                    if (tx.from === myWallet) {
                        console.log('Found my wallet!');
                        foundWallet = true;
    // do other stuff 

The code is working great but I have two issues:

  1. While the pending transactions are run in the background, execution continues to "do other stuff." How can I make sure that the pending transaction check is awaited before do other stuff is executed?
  2. Once my wallet is found from the transaction hash I set foundWallet to true so the !foundWallet check stops it from calling getTransaction(txHash). However, the pending txHash check is still executed indefinitely. How can I cleanly end the pending transaction check once my condition has been met?

Edit: Please see a solution from zemse at the ethers-io repo that still didn't work for me: https://github.com/ethers-io/ethers.js/discussions/2176#discussioncomment-1487598

  • It's watching the pending transactions because you set a listener... why don't you remove the listener? provider.off("pending") - docs.ethers.io/v5/api/providers/provider/#Provider-off
    – Hart Simha
    Oct 23 '21 at 3:25
  • why don't you try it... there are two listeners.......
    – John
    Oct 23 '21 at 8:14
  • In that case, you can assign the listener you want to remove to a variable and pass it as the second argument to provider.off("pending", ....)
    – Hart Simha
    Oct 23 '21 at 13:13

Your concept is bit wrong. provider.on("pending", ...) is called listener and it is should be called globaly. So it will listen for pending transactions and when find one it would notify you by executing the function passed as second parameter.

const completion = (tx) => {
   /* do some stuff */

const checkTxHash = async (txHash) => {
    const tx = await provider.getTransaction(txHash)
    if (!tx || !tx.to) return

    if (tx.from === myWallet) {
        console.log('Found my wallet!')

provider.on("pending", checkTxHash);
  • this looks like a clean solution Hristo. However, the provider.on() call needs to be declared after the checkTxHash function to be able to run this and unfortunately once the myWallet condition is met the code gets stuck and keeps executing without exiting. The code also is never awaited and constantly runs in the background. Please check the solution from the ethers-io repo that resolves a promise and uses provider.removeAllListeners() instead.
    – John
    Oct 25 '21 at 19:22
  • By constantly runs in the background you are meaning you want first to get the transaction then to do some other stuff? Oct 25 '21 at 19:43
  • I mean after the wallet is found I want the checkTxHash function to complete so I can execute code after provider.on("pending", checkTxHash); right now there are two issues. 1. provider.off("pending") does not exit the checkTxHash function, it keeps running indefinitely and 2. provider.on() isn't awaited so code defined after will immediately execute at the same time checkTxHash is running
    – John
    Oct 25 '21 at 21:01
  • 1
    I you want your method to be awaited you should implement it in promise and resolve this promis like in the github you provided. If you don't want to use promise you can have simple completion closure/callback which to handle the success of the function. Oct 26 '21 at 5:15
  • 1
    Something like in the answer, I've updated it. Oct 26 '21 at 5:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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