0

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 :(

I expect some method which will return data like {from: "", to: "", amount: ""}, but didn’t find it for now

| improve this question | | | | |
  • What kind of events regarding theses addresses are you interested in? – Mikhail Vladimirov Dec 10 '19 at 13:19
  • @MikhailVladimirov new transactions. I need to track it and write to the database in real-time – Alexxosipov Dec 10 '19 at 13:21
  • As I understand, you need to monitor new transactions related to theses addresses, right? A transaction may be related to an address in many different ways, thus my question. Do you need transactions, where origin ("from" attribute) is from your list? Or transactions where either origin or destination is from the list? Or transactions that somehow modify balance of any address from the list? Or what? – Mikhail Vladimirov Dec 10 '19 at 13:29
  • @MikhailVladimirov uhh, so many options.. I guess I need to track transactions which could modify balance. I mean if owner of the address makes a transfer to any other address, I need to get this data and in reverse (if someone makes a transfer to some addresses from my list). My goal is to have up-to-dated balances of these addresses, but from your comment I have doubts about it. Is there any kind of transactions which could modify the balance without any transfer from or to the address? – Alexxosipov Dec 10 '19 at 13:38
  • Sure, owner of address A sends transaction to smart contract B (e.g. wallet smart contract) telling it to transfer some ether to address C. In transaction from=A, to=B, amount=0, and this transaction modifies balance of C. – Mikhail Vladimirov Dec 10 '19 at 13:41
0

Geth has undocumented API function getModifiedAccounts that returns list of addresses whose state (balance is a part of address state) was modified between two given blocks. You may use this function to query the list of modified addresses for every new block (or several consequent blocks), then filter returned list dropping addresses that are not in your array, and then obtain balances of the addresses one by one.

However, this method is probably not available on Infura, so you will have to run your own Geth node to do the trick.

| improve this answer | | | | |
  • Can you check infura.io/docs/ethereum/wss/eth_subscribe method, please? There's a address parameter where I can pass my array, but will I get the needed info via this method? I've got chained to the Infura API due to technically description of the task (there's no any other ways and I have to do it only in this way) :( – Alexxosipov Dec 10 '19 at 14:16
  • Added another answer that should work for Infura. Left this answer as well, as it could be helpful for those who are not required to use Infura. – Mikhail Vladimirov Dec 10 '19 at 15:28
0

Deploy the following contract (you need to do this only once):

contract BalanceChecker {
  function checkBalances (address [] memory addresses, uint [] memory previousBalances)
  public view
  returns (address [] memory changedAddresses, uint [] memory currentBalances) {
    uint count = addresses.length;
    require (previousBalances.length == count);

    address [] memory tempAddresses = new address [] (count);
    uint [] memory tempBalances = new uint [] (count);

    uint changedCount = 0;
    for (uint i = 0; i < count; i++) {
      address a = addresses [i];
      uint pb = previousBalances [i];
      uint cb = a.balance;
      if (cb != pb) {
        tempAddresses [changedCount] = a;
        tempBalances [changedCount] = cb;
        changedCount += 1;
      }
    }

    changedAddresses = new address [] (changedCount);
    currentBalances = new uint [] (changedCount);

    for (uint i = 0; i < changedCount; i++) {
      changedAddresses [i] = tempAddresses [i];
      currentBalances [i] = tempBalances [i];
    }
  }
}

After every block, call the checkBalances function on the contract, passing array of addresses and array of previously observed balances for these addresses (the arrays must be of the same size). The function will return you array of addresses whose balance was changed and array of new balances for theses addresses. The call will be executed off-chain, so it will not cost you any gas.

This approach should work with Infura. If your array of addresses is too large and Infura will complain about this, just split the array into several chunks.

| improve this answer | | | | |
  • Do I understand it correctly that it just checks users balance? Without transactions? – Alexxosipov Dec 10 '19 at 15:38
  • Yes, as you said your goal is to have up-to-date balances of the addresses. – Mikhail Vladimirov Dec 10 '19 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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