I'm trying to iterate through an array of address/key pairs and send a transaction from each one. When I do this on testrpc with an array of the account info for two different accounts, one transaction succeeds (ie I get the transaction information, both in the console log and in my ganache-cli) and the other fails (returns an empty error, no transaction hash).

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');
var numWallets = 0;

wallets.forEach(function(account) {
  var addr = account.address;
  var key;
  web3.eth.getBalance(addr).then( (result) => {

    .then((gasPrice) => {
      var gasLimit = 25000;
      web3.eth.getTransactionCount(addr).then( (nonce) => {
        var nonce = nonce.toString();
        addr = account.address;
        key = account.key;
        key = key.slice(2);

        var rawTransaction = {
          "from": addr,
          "nonce": web3.utils.toHex(nonce),
          "gasPrice": web3.utils.toHex(gasPrice),
          "gasLimit": web3.utils.toHex(gasLimit),
          "to": destinationAddr,
          "value": result - (gasPrice * gasLimit),


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

        var tx = new Tx(rawTransaction); // This object has the same output in both iterations

        var serializedTx = tx.serialize();

        var transaction = web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
        transaction.on('receipt', receipt => {
            if (numWallets == wallets.length) {
                // further logic
                console.log("finished iteration");
        transaction.on('error', console.log);
  • Is this even valid JavaScript syntax? var numWallets++; doesn't work for me. – user19510 Sep 1 '18 at 21:35
  • Sorry, I re-typed out my code and that was a typo, fixed now. – king_geedorah Sep 1 '18 at 21:58
  • Just a guess, but have you tried logging key from right before you use it? My guess it that this is just a variable capture issue and you're not really using both keys. – user19510 Sep 1 '18 at 21:59
  • Just checked, the key is correct :( – king_geedorah Sep 1 '18 at 22:02
  • To be extra clear, you're seeing two different values logged for key? And you're logging from within the getTransactionCount callback? – user19510 Sep 1 '18 at 22:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.