How to get new transactions by addresses?

Hi there. I have an array of addresses on my server which I need to track. What method should I use to do that via websockets? I’m new to eth and cryptocurrency at all, and I don’t understand “blocks”, “gas price” meaning :frowning:
I expect some method which will return data like {from: "", to: "", amount: ""}, but didn’t find it for now

Welcome to the community @Alexxosipov! One way you can do this is setup a newHeads sub using eth_subscribe, whenever a new block comes in send an eth_getBalance request for each addr over the WSS connection.

Can you describe your implementation here a bit more as to why you want to follow those specific addrs?

Hi, @mike! That is my test task to get the job :slight_smile: I need to implement adding addresses, getting balance by address and track for any transactions from or to the address. And if first and second I did without any doubt, the third thing makes me stuck in reverse to implement it.

I’ll try to do that by the way you said now, but for now I don’t understand why Ethereum has no transactions API to get it by the addresses

Thanks for the feedback and good luck.

@mike may I ask you some questions here?

Of course, we will do our best to help!

1 Like

Thanks! After some practices with the API, I have a couple of questions.

My main goal is to track new transactions for addresses that exist in my database. I used the “eth_subscribe” => “newPendingTransactions” method and after receiving a new data I sent a request to the method “eth_getTransactionByHash” via http. But this is an expensive and not the best option.

Also there’s the “eth_subscribe” => “logs” method where I can pass an array or addresses which I want to track. There’s a field “transactionHash” — does it mean that I will get a data as in “newPendingTransactions” but with addresses filter?

No problem! I think the best way to do this would be the way I outlined above. Determine the latest block using newHeads sub and than request the balance of the address you care about against that block.

But I need to track (and put it in my database) not only balance but transactions too. Am I wrong about “newHeads” sub will not send transactions to me? And there’s no filter about addresses (I guess that’s bad because it will cause high load to the server), how to implement it with this way?

In this case newPendingTransaction sub is probably not your best solution because of the network functionality around the pending pool this won’t guarantee you would catch everything. Logs sub is also not going to help because logs are used to access events from contracts, unless you are looking for token transfers/generation/etc. this would not show you ETH transactions.

Your best bet is to have a newHeads sub, when a new block comes in, send eth_getBlockByHash, based on the hash provided in the newHeads event. You will need to set your show Transactions detail flag to true in your eth_getBlockByHash request. Then the response of the eth_getBlockByHash will include a transactions object which will list all of the transactions from that block. You can then process that based on the account addrs you care about.

Yes, thanks, Mike! It works like a charm. I have only one more question: how can I track changes in the number of block confirmations? Like it comes on etherscan.io (there it’s updated in real time)

Block confirmations is typically the number of blocks since the block which the transaction was added too. This data is not transmitted by the default client specs. A simple way to do this would be to count the number of newHeads events that have come in since the block that the transaction was a part of. Our newHeads sub will return information about re-orgs so make sure to watch for those and update your counter accordingly. More info on the newHeads functionality here, https://infura.io/docs/ethereum/wss/eth_subscribe