Search code examples
ethereumweb3js

How can I filter incoming transactions for a given address?


using Infura as my Ethereum node I have subscribed to the pendingTransactions item in the web socket. I receive a transaction ID for every transaction that occurs. in order to determine whether it is relevant to me, I must call the getTransaction() method

I'm therefore having to call that method 100,000 times, which makes me exceed the Infura quota

to be fair, I don't even want pending transactions. I would prefer completed transactions, but the API doesn't seem to offer that

I have come across this article: https://medium.com/coinmonks/monitoring-an-ethereum-address-with-web3-js-970c0a3cf96d which seems to support the notion that Ethereum doesn't make this easy

can anyone recommend a better approach?

TIA, ekkis


Solution

  • You can subscribe to any websocket channel that your node provider supports. However, most providers (including Infura) use either go-ethereum or openethereum client software that by default only implement the four channels listed in the web3js docs: pendingTransactions, newBlockHeaders, syncing, and logs.

    So in order to subscribe only to mined transactions to/from a specified address, you'll need to find a provider that implements a transactions channel with a filter (unlikely). Or to run your own node and extend its code with a custom implementation of the channel (technically possible, but it's not a small scope).

    Workaround: Use an external service that collects all transactions, stores them in a searchable DB and allows their users to request filtered data. For example Etherscan has a limited free endpoint for this case.