I tried to catch all pending tx hashes and then find them inside new blocks. But it has some sync issue. Here is my simplified code example:
const provider = new ethers.providers.WebSocketProvider(
process.env.PROVIDER_WS_URL
);
const txs = new Map();
provider.on("pending", (hash) => {
txs.set(hash, Date.now());
});
provider.on("block", (blockNum) => {
provider.getBlock(blockNum).then((block) => {
const lastTx = block.transactions.pop();
// Expected to find block tx inside previousle recorded pending txs
console.log(block.number, lastTx, txs.get(lastTx));
});
});