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:
- 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?
- Once my wallet is found from the transaction hash I set
foundWallet
totrue
so the!foundWallet
check stops it from callinggetTransaction(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
provider.off("pending")
- docs.ethers.io/v5/api/providers/provider/#Provider-offprovider.off("pending", ....)