6

I need to check the balance for multiple addresses (i.e. 100) a eth_getbalance seems to accept only one address.

2

As far as I know, retrieving balances for multiple addresses is not supported yet by the RPC standard.

However, what I've came up with so far is this: I pack all balance calls into promises and do Promise.all by chunks of 50 requests for instance. It waits for the first 50 balances, then queries next 50 and so on. Code:

async function getAllAddressBalances (addresses, batch = 50) {
    const map = new Map();
    let processed = 0;
    process.stdout.write(`Processed: ${ processed }/${ addresses.length }`);
    for (const batch of splitArray(addresses, batch)) {
        let retry = true,
            pss = processed;
        while (retry) {
            try {
                await Promise.all(batch.map(address => (async function () {
                    let balance = 0;
                    balance = await web3.eth.getBalance(address);
                    process.stdout.write(`\rProcessed: ${ ++processed }/${ addresses.length }`);
                    if (balance === 0)
                        return;
                    map.set(address, balance);
                })()));
            } catch (e) { // Sometimes rate limit may apply, this tries over and over again
                console.log(`Exceptions caught (possibly node rate limit), retrying in 5 seconds...`);
                retry = true;
                await delay(5);
                processed = pss;
                continue;
            }
            retry = false;
        }
    }
    process.stdout.write(`\n`);
    return map;
};

function splitArray (array, len) {
    const arr = [];
    for (let i = 0; i < array.length; i += len) {
        arr.push(array.slice(i, Math.min(i + len, array.length)));
    }
    return arr;
}

Alternatively, you can rely on third-party APIs like Etherscan, there are a plenty of those. As for Etherscan, they have an endpoint that enables you to get Ether Balance for multiple Addresses in a single call.

| improve this answer | |
0

You might write and deploy a smart contract that has a single method that will check the balance of multiple accounts and return you multiple balances concatenated. Then call this method and decode result.

Here the solution for a similar problem: https://github.com/pooltogether/etherplex

A problem that etherplex solves is similar but not exactly the same: You have a single address but multiple tokens, special smart contract + JS wrapper that uses ether.js returns you unpacked response from this dedicated balance checker contract

| improve this answer | |
-3

geth is using jsonrpc which is an open specification.

| improve this answer | |

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.