0

I am trying to code something that empties an address and sends that amount to my main-wallet. In order to get familiar with sending raw transactions and manually setting the eth-amount, I've tried some testprogram. My logic is to send balance from Account1 <---> Account2 in an infinite loop.

However that works many many times, but at some point either "ERROR!Error: insufficient funds for gas * price + value" or an error with a duplicate transaction happens.

I don't understand why that is the case, any ideas?

var Web3 = require('web3');
var web3 = new Web3(new 
Web3.providers.HttpProvider('https://ropsten.infura.io/'));
const ethereumjs = require('ethereumjs-tx');
var BigNumber = require('bignumber.js');

var account2 = {
'address': '0xaCC9a59A4266F40846664992bE9bACD607716b7c',
'privateKey': '0x26c1999d1b6e51eda8ed8912cc678fe48879b8cefea1af11835d9d64669a818a'
}

var account = {
'address': '0x379ccad8923B1e4CF79E9dB125d70f0843cb2C35',
'privateKey': '0xc129caaa7a963e1c67bd8bf0586dcf4e47b6ae7e9294962886178a9acb842290'
}


recBalanceCheck(account,account2);


function recBalanceCheck(account, account2) {
setTimeout(function () {
    var balance = web3.fromWei(web3.eth.getBalance(account.address));
    // var balance =(web3.eth.getBalance(account.address));
    console.log('awaiting payment... current balance is: ' + balance.toNumber());
    if (balance.toNumber() > 0) {
        console.log('payment received. Balance is now ' + balance.toNumber());
        console.log('sending ether to main account...');
        sendEther(account, account2.address, balance.toNumber(), 3);
        recBalanceCheck(account2, account);
    } else {
        recBalanceCheck(account, account2);
    }
}, 10000);
}


function sendEther(senderAccount, receiveraddress, amount, gwei) {
var account = senderAccount.address;
var privateKey = senderAccount.privateKey;
privateKey = privateKey.slice(2, privateKey.length);
privateKey = Buffer.from(privateKey, 'hex');
var gas = 21000;
var gasPrice = new BigNumber(web3.toWei(gwei, 'gwei'));
var cost = gasPrice.multipliedBy(gas);

web3.eth.getTransactionCount(account, function (err, nonce) {
    var tx = new ethereumjs({
        nonce: nonce,
        gasPrice: web3.toHex(web3.toWei(gwei, 'gwei')),
        gasLimit: 21000,
        to: receiveraddress,
        value: web3.toHex(new BigNumber(web3.toWei(amount, 'ether')) - cost)
    });
    tx.sign(privateKey);
    var raw = '0x' + tx.serialize().toString('hex');
    web3.eth.sendRawTransaction(raw, function (err, transactionHash) {
        if (err) {
            console.log('ERROR!' + err);
            process.exit();
        }
        console.log(transactionHash);

    });
});
}
0

I found my own mistake. I did not use BigNumber for every calculation. The sendEther(...)-Function needs to be called with a BigNumber already, otherwise something gets rounded or lost, and that causes the transaction to be invalide.

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.