Ethereum Asked by Alexxosipov on December 13, 2020
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
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.
Answered by Mikhail Vladimirov on December 13, 2020
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.
Answered by Mikhail Vladimirov on December 13, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP