8

Using web3 I can query the balance of any fixed account with web3.eth.getBalance. Assuming I have the entire blockchain downloaded, how can I recover the list of all addresses that have a positive balance at this moment?

  • You want to check that for all address or just the addresses on your node? – Prashant Prabhakar Singh Oct 5 '17 at 14:21
  • @PrashantPrabhakarSingh I'd like all address that have a balance on the network I'm connected to (mainnet, testrpc, etc...). I think the solution involves iterating through all the blocks but I'm not sure if that's possible. – Hooked Oct 5 '17 at 19:58
  • This question may give you an idea ethereum.stackexchange.com/questions/8978/… – Ismael Oct 8 '17 at 22:42
7

simple:

  1. get all the blocks
  2. from each block get all the transactions
  3. filter all the transactions with a value > 0
  4. record the list of all the to addresses
  5. filter out duplicates
  6. filter out addresses with balance 0

    const Web3 = require('web3')
    
    const web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/<InfuraToken>'))
    
    let blockNum = 0
    
    let run = async () => {
      let addresses = {}
    
      while (true) {
        let blck = blockNum++
        let block = await web3.eth.getBlock(blck)
        if (!block)
          break
    
        console.log('block', blck, 'transactions', block.transactions.length)
        for(let i = 0; i < block.transactions.length; i++) {
          let tx = await web3.eth.getTransaction(block.transactions[i])
          if (parseInt(tx.value) > 0) {
            addresses[tx.to] = true
          }
        }
      }
    
      let positiveAddresses = []
      for (address in addresses) {
        try {
          let balance = await web3.eth.getBalance(address)
          if (balance > 0) {
            positiveAddresses.push(address)
          }
        } catch (err) {
          console.log(err)
        }
      }
      console.log(positiveAddresses)
    }
    
    run()
    

let this script run for a week or two and you'll have your result. The script requires the 1.0 version of web3 npm i web3 should do the trick. No other dependencies.

to run this simply use node scriptName.js

improvements may include finding out which addresses are contracts with eth.getCode. You could also write the result to disk instead of doing it all in memory.

| improve this answer | |
  • 2
    New addresses are created also when a message call is made inside some contract. To list these calls every transaction has to be executed or transaction traces must be inspected github.com/ethereum/go-ethereum/wiki/… – medvedev1088 Feb 23 '18 at 19:51
6

You can get it from BigQuery https://medium.com/google-cloud/how-to-query-balances-for-all-ethereum-addresses-in-bigquery-fb594e4034a7

There are 3 ways by which an address can come into existence:

  • a transaction is sent to this address
  • a message call is made to this address within some contract
  • a block mined where the address is specified as the coinbase (receiver of the block reward)
  • a SELFDESTRUCT is called with recipient being the address

To list all existing addresses you need to (in the corresponding order):

  • list all transactions, get the destination addresses from these transactions
  • execute all transactions, intercept all message calls, get the destination addresses from calls
  • list all blocks, get the coinbase addresses from these blocks

Scanning the Patricia trie of an Ethereum client wouldn't work because the addresses are hashed before they are written to the trie: Getting complete state of a smart contract

| improve this answer | |
1

Run the following BigQuery:

with double_entry_book as (
    -- debits
    select to_address as address, value as value
    from `bigquery-public-data.ethereum_blockchain.traces`
    where to_address is not null
    and status = 1
    and (call_type not in ('delegatecall', 'callcode', 'staticcall') or call_type is null)
    union all
    -- credits
    select from_address as address, -value as value
    from `bigquery-public-data.ethereum_blockchain.traces`
    where from_address is not null
    and status = 1
    and (call_type not in ('delegatecall', 'callcode', 'staticcall') or call_type is null)
    union all
    -- transaction fees debits
    select miner as address, sum(cast(receipt_gas_used as numeric) * cast(gas_price as numeric)) as value
    from `bigquery-public-data.ethereum_blockchain.transactions` as transactions
    join `bigquery-public-data.ethereum_blockchain.blocks` as blocks on blocks.number = transactions.block_number
    group by blocks.miner
    union all
    -- transaction fees credits
    select from_address as address, -(cast(receipt_gas_used as numeric) * cast(gas_price as numeric)) as value
    from `bigquery-public-data.ethereum_blockchain.transactions`
)
select address, sum(value) as balance
from double_entry_book
group by address
order by balance desc
limit 10 -- Remove this line for all balances.

to show top 10 ethereum addresses with a positive balance. You can run this at GCP BigQuery portal. Then export results into CSV file. Remove last limit line to find all addresses, however there are some export limits, E.g. you can export up to 16k rows to local CSV file, or up to 1G to Google Drive, or save all results as BigQuery table. Run it only few times, to avoid exceeding your free quota.

Source: https://github.com/blockchain-etl/ethereum-etl-airflow

| improve this answer | |
0

You can pay a lot of money in Google cloud, or download it directly here -> https://blocklancer.net/job/476/1

Someone has made this for you. It takes a LOT of time to get the correct list of ETH addresses with full balance.

| 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.