Ganache-cli: sendSignedTransaction return promise is never fulfilled

Created on 28 Apr 2019  路  7Comments  路  Source: trufflesuite/ganache-cli

Expected Behavior

Call to sendSignedTransaction return a promise that is fulfilled as soon as the transaction is settled.

Current Behavior

sendSignedTransaction hangs forever.

Also, it seems from the logs that I'm stuck in a loop.

eth_sendRawTransaction

  Transaction: 0x33244e50ce07f80112de5cb56bc64616d85078350ba9c8df0f883182a9580470
  Gas usage: 21000
  Block Number: 5
  Block Time: Sun Apr 28 2019 17:37:47 GMT-0300 (GMT-03:00)

eth_getTransactionReceipt
eth_getBlockByNumber
eth_getTransactionReceipt
eth_getBlockByNumber
eth_getTransactionReceipt
eth_getBlockByNumber
eth_getTransactionReceipt
eth_getBlockByNumber
eth_getTransactionReceipt
eth_getBlockByNumber
eth_getTransactionReceipt
...

Steps to Reproduce (for bugs)

const Web3 = require('web3')
const EthTxn = require('ethereumjs-tx')

const web3 = new Web3('http://localhost:8545')
const { getBalance, sendSignedTransaction } = web3.eth

const sender = {
  address: '...',
  privateKey: '...',
}

const receiver = {
  address: '...',
  privateKey: '...',
}

;(async () => {
  const rawTxn = {
    nonce: 5,
    to: receiver.address,
    gasPrice: 20000000,
    gasLimit: 30000,
    value: 100,
    data: '',
  }
  const senderPrivateKeyHex = Buffer.from(sender.privateKey, 'hex')
  const txn = new EthTxn(rawTxn)
  txn.sign(senderPrivateKeyHex)

  const serializedTxn = txn.serialize()

  await sendSignedTransaction(serializedTxn)

  console.log('here') // <--- never executed
})()

Environment

  • Version used: Ganache CLI v6.4.3 (ganache-core: 2.5.5)
  • Web3 Version: [email protected]
  • NodeJS Version:

    • [x] 11.x

    • [x] 10.x

    • [x] 8.x

  • Operating System and version: Linux 4.19.34-1-MANJARO
bug needs validation priority-high

Most helpful comment

The reason is that Web3 will by default wait for 24 confirmations before resolving, but ganache-cli will only give you one, causing it to hang indefinitely.

The easiest solution is to change options for web3 to only require one confirmation:

const Web3 = require('web3')
const ganache = require('ganache-cli')
const web3 = new Web3(ganache.provider(), null, { transactionConfirmationBlocks: 1 /* This is the critical part */ })

// ... send your transaction using web3

All 7 comments

Same here.

pragma solidity >=0.5.0 <0.6.0;

  contract DigitalRights {
      string data = "0x123123123";
      string copyright = "bogdan";
      string[] producers = ["prod1","prod2"];
      string[] productionAssistance = ["as1","as2"];
      string[] guests = ["guest1","guest2"];
      string[] sponsors = ["Sponsor1","Sponsor2"];
  }

const contract = new this.web3.eth.Contract(abi)
        .deploy({ data: '0x' + bytecode })
        .send({ from: owner, gas: this.gas })
        .on('error', error => {
          log(`error: ${error}`);
          reject(error);
        })
        .on('transactionHash', transactionHash => {
          log(`transactionHash: ${transactionHash}`);
        })
        .on('receipt', receipt => {
          log(`receipt: ${receipt}`);
        })
        .on('confirmation', (confirmationNumber, receipt) => {
          log(`confirmation: ${confirmationNumber} ${JSON.stringify(receipt)}`);
          //gets 1 configmation
        })
        .then((newContract: Contract) => {
          // never fulfilled
          log(`successfully deployed at ${newContract.options.address}`);
          resolve({
            contract: newContract,
            owner,
            accounts,
          });
        });

Try setting https://web3js.readthedocs.io/en/1.0/web3-shh.html#transactionconfirmationblocks
and/or set fixed blocktime using -b 1 for ganache.

I had the same problem !

The reason is that Web3 will by default wait for 24 confirmations before resolving, but ganache-cli will only give you one, causing it to hang indefinitely.

The easiest solution is to change options for web3 to only require one confirmation:

const Web3 = require('web3')
const ganache = require('ganache-cli')
const web3 = new Web3(ganache.provider(), null, { transactionConfirmationBlocks: 1 /* This is the critical part */ })

// ... send your transaction using web3

Thanks @bergkvist! If time permits, I would definitely encourage you to open a PR! Or, let me know and I'll make sure this lands in our documentation! :+1: :+1:

@nicholasjpaterno I might look into it in the end of next week! I won't have time until then.

Documentation fixed by: https://github.com/trufflesuite/ganache-cli/pull/656 Thanks everyone!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

leopoldjoy picture leopoldjoy  路  3Comments

zatsepinvl picture zatsepinvl  路  5Comments

gskerry picture gskerry  路  3Comments

zweicoder picture zweicoder  路  3Comments

xavierlepretre picture xavierlepretre  路  5Comments