I have a rather large list of accounts (HD wallets) I want to sweep into one.
They've all been imported into geth, and all have the same password.
I wrote a script to do this in node but get this error:
Error: Insufficient funds for gas * price + value
Here is the node script:
//web3
var Web3 = require('web3');
var web3 = new Web3();
//starting index... main account is 0... don't sweep that one ^.^
var startSweep = 1;
//account shell
var accounts = [];
//transaction cost... i think?
var gas = 21000;
//geth stuff
var sys = require('util');
var exec = require('child_process').execSync;
function puts(error, stdout, stderr) { sys.puts(stdout) };
//connect to node
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
//get accounts
var accounts = web3.eth.accounts;
//set default account for gas
web3.eth.defaultAccount = accounts[0];
//loop through accounts and sweep balances
for(var x = startSweep; x<accounts.length; x++){
//get current account balance
var balance = web3.eth.getBalance(accounts[x]);
//if balance is greater than 0 lets transfer...
if(balance > gas){
//command to unlock account
var execCommand = 'geth --exec "personal.unlockAccount(eth.accounts['+x+'], \'password\', 1000)" attach';
console.log(execCommand);
//execute command
exec(execCommand, puts);
console.log(' Unlocked account ' + accounts[x] + ' with a balance of ' + balance + '! ' + 'TRANSFERING!');
var transaction = {
"to" : accounts[0],
"from" : accounts[x],
"value" : balance - gas,
"gas": gas
};
//send transaction
web3.eth.sendTransaction(transaction);
console.log('Sent transaction from : '+ accounts[x]);
}
}
I read that the cost of a transaction was 21000, but I get that error.... So I just kept bumping the gas up until I received the error:
Error: Exceeds block gas limit
So I guess I'm doing something wrong. Any suggestions?
value: balance - (gas * gas price)
, as per ethereum.stackexchange.com/a/5766/52. Also see ether.fund/tool/calculator