I am wondering what the best way is to watch several (hundreds or thousands) Ethereum addresses at once for incoming transactions and send out the amount deposited once a transaction is detected. I came across similar questions on this forum where a few solutions have been proposed.

For instance, one answer proposed here: How to monitor all your addresses and send payments out immediately to a main address

  var target = web3.eth.accounts[10];
  for (var account = 0; account < 10; account++) {
   var balance = web3.eth.getBalance(web3.eth.accounts[account]);
   if(balance > 0)
     web3.eth.sendTransaction({from: web3.eth.accounts[account], to: target, value: balance});
}, 10000);

However, this does not seem very scalable, because you'd have to unlock all hundreds or thousands of addresses to keep watch.

Another solution proposed is to watch for all transactions on the blockchain, and check if the address match any of your watched addresses: https://ethereum.stackexchange.com/a/35818/27511

Again, I don't think this is scalable if you have hundreds or even thousands of addresses. Let's say you have 10,000 addresses to watch, and 1,000 incoming transactions, that would be an n^2 operation to check each transaction against each address.

Is there a more efficient way, or are these the only options?

  • 2
    No unlocking is required to check an account's balance. And no need for n^2 unless you need to know which transaction adjusted which balance. (If you're just monitoring the balances, no need to loop over each transaction.) – user19510 Apr 6 '18 at 19:21
  • You can use a hash list so you don't need to iterate over all accounts and you don't have n^2 anymore – Daniel Luca CleanUnicorn Apr 6 '18 at 20:43

You only have to go through transactions in the block and check if one of your relevant addresses is target of a transfer. See also "How to scan ether transfer from smart contract to my addresses programmatically?" for value transfers triggered during contract executions (a.k.a. internal transactions).

This approach scales well for the number of accounts to watch, since you only have to iterate over transactions (currently ~150/block in average).

However, this approach might run into trouble when the transaction throughput increases in the future (see PoS).

The most efficient solution would be to have an extension to JSON RPC, which would allow us to subscribe for changes on account values ("don't call us, we will call you" - principle), but this is currently not possible.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.