Websocket pending transaction for a particular address

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()
	};
};
2 Likes

Hi @user165,

You could subscribe to pending txs and do it that way or you could subscribe to newHeads so you get a batch of tx hashes each time a block is added to the chain, but ether way you’d have to request the details of each transaction and examine them if you are looking only for ETH transfers.

If you are interested in events emitted by contracts you can subscribe efficiently to them but you seem to want to be notified each time there’s a simple eth transfer.

Hope this helps a bit.

Warm regards,
Chris | Infura | ConsenSys

1 Like

The suggestions you provided might not align with the specific goal of my feature, which is to observe transactions for a specific list of addresses. Both of the options you proposed seem to share the same issue I mentioned earlier—suboptimal performance and added costs. However, I could be misinterpreting the solution.

Anyway, in the meantime, I’ve migrated to Alchemy, which offers a built-in solution for this scenario.

I appreciate your response nonetheless—thanks!

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.