0

I've set up a private network and generated around 100 addresses. Exactly 80 of those addresses have a balance of 10 or more ETH, while the rest of the addresses have balances of 0. I've also preallocated 10,000 ETH to a separate address (let's call it base) in genesis.json.

I would like to send 5 ETH from base address to every address that has a balance >= 10 ETH. I've seen examples for ERC20 tokens around but couldn't find anything using the actual currency of a chain instead of a token.

Also, is it possible to check balances and send the desired amount to all the addresses created on the network without me having to save every one addresses in a separate file and load them up when sending?

Any help would be greatly appreciated. Thank you!

0

You can definitely send Ethers (the base currency) to any address. And you can impose any conditions before sending.

You only need the address of the receiver and not their keystore file or anything. A sample code is given for refrence.

_self.SENDETHER=function(from,to,password,amount){
    return new Promise(function(fullfill,reject){
      web.eth.getBalance(to)
        .then(function(recepientEthBal){
          if(recepientEthBal > 10){ // any condition can be imposed here
            web3.eth.personal.unlockAccount(from, password).then(function (result) {
              web3.eth.sendTransaction({from:from,to:to,value:web3.utils.toWei(amount,"ether")})
                .on('transactionHash', function(txHash){
                    console.log("Transaction hash : "+txHash);
                    fullfill(txHash);
                }).catch(function(err){
                    console.log(err);
                    reject("Unable to send transaction");
                });
              }).catch(function(err){ //catch of unlock account
                  console.log(err);
                  reject("Unable to unlock sender's account with given passphrase");
              });
          }
        })
        .catch(function(err){
          console.log(err);
          reject(err);
       })     
  });
}
  • Thanks for your answer. However what I'm trying to do is, first to collect all the addresses ever created on the network and second filter out the ones with a balance >=10. I know i can do that using web3.js connected to a node i have exposed the RPC. My question is, if there are several nodes on the network and let's say one of them doesn't expose the RPC can I still get the accounts created on that node? – Pewpew May 6 '18 at 22:47
0

You need to scan the whole blockchain to get all the addresses that may have ETHs in them, that is, other than those pre-allocated addresses. For pre-allocated addresses in the genesis file, I guess you can just read and parse the genesis.json for those addresses if you want to automate the process.

For other addresses, you need to scan all the blocks for transactions and record all the addresses in the "to" fields of the transactions, and also record the miner address of the blocks. That way you will have all addresses/accounts ever existed on the blockchain.

And as long as your local node has synced the whole blockchain, you can get all the addresses on the chain, as all nodes should sync to reach the exact same blockchain. Of course, if your local node has some network problems and lags some blocks behind the other nodes, then you won't have the relevant address info stored in those newer blocks until your local node catches up.

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.