Hi,
I’m utilizing ether.js for my project and have established a Websocket connection through Infura. I couldn’t find a straightforward way to directly listen to transactions for a specific address. To work around this, I’ve incorporated a filter within the pending hook. This approach functions to some extent, the performance isn’t optimal, and for a while only. Because each transaction are requested within the hook, the daily request limit is rapidly exceeded.
If anyone has insights on how to properly configure a Websocket connection to listen to pending transactions for a specific address, I’d greatly appreciate the help!
const wsProvider = new InfuraWebSocketProvider('sepolia', API_KEY);
export const initTransactionsListener = (address: string): WebSocketListener => {
wsProvider.on('pending', async (tx: string) => {
const transaction = await wsProvider.getTransaction(tx);
if ([null, undefined].includes(transaction)) {
return;
}
const { from, to } = transaction;
if (![from, to].includes(address)) {
return;
}
console.log('Transaction', transaction)
});
return {
destroy: async () => await wsProvider.destroy()
};
};