Knotenskript zum Sweeping von Konten

Ich habe eine ziemlich große Liste von Konten (HD-Geldbörsen), die ich in eine zusammenführen möchte.

Sie wurden alle in Geth importiert und haben alle das gleiche Passwort.

Ich habe ein Skript geschrieben, um dies im Knoten zu tun, bekomme aber diesen Fehler:

Error: Insufficient funds for gas * price + value

Hier ist das Knotenskript:

//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]);
    }
}

Ich habe gelesen, dass die Kosten einer Transaktion 21000 waren, aber ich bekomme diesen Fehler .... Also habe ich einfach weiter Gas gegeben, bis ich den Fehler erhalten habe:

Error: Exceeds block gas limit

Also mache ich wohl etwas falsch. Irgendwelche Vorschläge?

Ist es möglich, dass auf einem oder mehreren Konten nicht genügend Guthaben vorhanden ist, um die Transaktionskosten zu decken? (dh Saldo < 21000)
Ich führe gerade einen Test mit nur drei Konten durch, alle haben Guthaben von mehr als 21000. Soweit ich weiß, kommt das Gas sowieso vom Hauptkonto (Index 0), das ein paar Eth enthält. Bitte korrigiert mich jemand, wenn ich hier falsch liege.
Möglicherweise habe ich Ihren Kommentar @RichardHorrocks versehentlich gelöscht. Verzeihung!
Entschuldigung, das war ich. Gemäß dem gelöschten Kommentar müssen Sie den Gaspreis sowie die Gasmenge berücksichtigen . In diesem Fall value: balance - (gas * gas price)gemäß ethereum.stackexchange.com/a/5766/52 . Siehe auch ether.fund/tool/calculator
Nach einigen cowboyartigen Bearbeitungen habe ich versehentlich Gas auf 210.000 eingestellt . Ich habe es auf 21000 zurückgesetzt und alles funktioniert wie es sollte. Ich werde OP mit dem geänderten Code aktualisieren, falls es jemandem helfen kann. Danke @RichardHorrocks

Antworten (1)

Ich habe das Gas nicht richtig berechnet. Hier ist der aktualisierte Code, falls jemand etwas Ähnliches tun möchte.

//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.
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'));

//current gas price
var gasPrice = web3.eth.gasPrice.toNumber();

//get accounts
var accounts = web3.eth.accounts;

//set default account
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]);
    //see if this is worth sending ^.^
    var balanceMinusFee = balance - (gas * gasPrice);
    if(balanceMinusFee > 0){
        //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 ' + balanceMinusFee + '! ' + 'TRANSFERING!');
        var transaction = {
            "to" : accounts[0],
            "from" : accounts[x],
            "value" : balanceMinusFee,
            "gas": gas
        };

        //send transaction
        web3.eth.sendTransaction(transaction);
        console.log('Sent transaction from : '+ accounts[x]);
    }
}