I'm trying to write a simple function to send a transaction using web3 and ethereumjs-tx that empties the wallet of an account (call this account A) into another account (account B). However, no matter what I initialize the balance of account A at using ganache-cli, ie with ganache-cli --account="0x4482be4e4e36d5521d70dafa57818eed38a4d0c69562bffebf24901c3199b271, 222222222222222222222", I get the error:

Error: Returned error: sender doesn't have enough funds to send tx. The upfront cost is: 73361650815041401384341826897131069221944849241050 and the sender's account only has: 222222222222222222222

Furthermore, if I increase the amount I initialize the wallet at, the 'upfront cost' increase proportionally:

Error: Returned error: sender doesn't have enough funds to send tx. The upfront cost is: 22704331223003175573249212746801550559464702875615796870481879467237868556850 and the sender's account only has: 22222222222222222222222222222222

Can anyone tell me what I'm doing wrong? Here's my code:

var Web3 = require('web3');
var web3 = new Web3(new 
const Tx = require('ethereumjs-tx');

  addr = account..address;
  key = account.key;
  key = key.slice(2);
  web3.eth.getBalance(addr).then( (result) => {
    evmBalance += parseInt(result, 10);

    .then((gasPrice) => {
      var gasLimit = 25000;
      web3.eth.getTransactionCount(addr).then( (nonce) => {
        var nonce = nonce.toString();
        var rawTransaction = {
          "from": addr,
          "nonce": web3.utils.toHex(nonce),
          "gasPrice": web3.utils.toHex(gasPrice * 1e9),
          "gasLimit": web3.utils.toHex(gasLimit),
          "to": destinationAddr,
          "value": result,


        var privKey = Buffer.from(key, 'hex');

        var tx = new Tx(rawTransaction);

        var serializedTx = tx.serialize();

        var transaction = web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
        transaction.on('receipt', console.log);
        transaction.on('error', console.log);
  • I've also noticed strange behaviour of Ganache's upfront cost estimation - Ganache (or maybe Truffle?) estimated function's call's cost to about 20 times higher, than it later took from sender's balance. What's more interesting - I also wrote a function to clear user's account (to test my autorefill functionality). Dec 11 '18 at 20:09

You're calling getBalance, which returns the amount of ether (in wei) that the account holds.

You're then passing that amount in the value field of the transaction, so the transaction is going to attempt to send the entire balance of the account. This is why the amount goes up when you add more funds to the account.

Such a transaction will never succeed, because the account sending the transaction must have enough ether to cover both the value being transmitted and the amount required to pay for gas. The maximum you can transfer should be result - (gasPrice * 1e9 * gasLimit).


You also are choosing an extremely high gas price, because you're multiplying the gas price returned from getGasPrice by 1 billion.


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.