4

I have an issue with transactions on my local ethereum network - at some point, transaction hangs & spends a lot of ETH from my account.

Here's a sample code:

async function send(toAccount, weiVal) {
  let account = await w3.getDefAccount();

  for (let i = 0; i < 100; i++) {
    let res = await web3.eth.sendTransaction({
      from: account,
      to: toAccount,
      value: weiVal
    });
    await helper.timeout(2000);
  }
}

send('0x5648...', 100000000000000);

It hangs at sendTransaction call (promise is never resolved) on some random iteration.

The situation remains the same after script restart - transaction passes a few times and then hangs.

geth version: 1.7.3

TylerH
  • 20,799
  • 66
  • 75
  • 101
Dmitry T.
  • 464
  • 4
  • 12
  • Do you get errors if you put a try/catch inside your for loop? One thing that stands out is you're not specifying a gas limit or gas price, so your transaction may be failing on some iteration either due to insufficient funds, or some `revert` action occuring which then consumes all of your remaining gas. It's hard to tell without more debug information. – Adam Kipnis Mar 26 '18 at 17:30

1 Answers1

3

If you are sending transactions back-to-back from the same account, you need to manually set the nonce, because the node will not keep track of it correctly.

Example code

async function send(toAccount, weiVal) {
  const account = await web3.getDefAccount();
  // the transaction count does not include *pending* transactions
  // but is a good starting point for the nonce
  const nonce = await web3.eth.getTransactionCount(account);

  let promises =  [];
  for (let i = 0; i < 100; i++) {
    promises.push(web3.eth.sendTransaction({
      from: account,
      to: toAccount,
      nonce: nonce++, // increment the nonce for every transaction
      value: weiVal
    }));
  }

  return Promise.all(promises);
}

await send('0x5648...', 100000000000000);
gaiazov
  • 1,908
  • 14
  • 26