[Ethereum] web3.eth.sendSignedTransaction is not returning promise or promised event emiter

sendsignedtransactionweb3js

I am testing whether transactions that I'm signing offline are accepted by the Kovan network. I'm submitting the transactions using Web3.js (web3@1.20) as follows:

I connect to my Open Ethereum 3.0.1 node, and then use web3.eth.sendSignedTransaction. According to the documentation, this should return a PromiEvent. However, the events emitted by the submission are not captured and the promise never resolves. The transaction is submitted to the network nonetheless and is valid (it's a simple transaction sending funds from one non-contract account to another). One can find it in any blockexplorer, like for instance etherscan.

The code below does not behave as expected:

function sendTrans(_rawData,_txHash) {

    try {
            
            console.log("Before Asynch call");
                                    
              web3.eth.sendSignedTransaction(_rawData)
                .once('transactionHash', function(hash){ console.log("txHash", hash) })
                .once('receipt', function(receipt){ console.log("receipt", receipt) })
                .on('confirmation', function(confNumber, receipt){ console.log("confNumber",confNumber,"receipt",receipt) })
                .on('error', function(error){ console.log("error", error) })
                .then(function(receipt){
                    console.log("trasaction mined!", receipt);
                });
                
          console.log("After Asynch Call");
         }
    catch (error) {
      
      console.log("Error Sending Transaction", error.message);
         
    }
      return { response: "OK", transHash: _txHash };
  }

// result: the code does not trigger any emitter event. No logging to the console is made.

The code above just hangs, because the promise returned by sendSignedTransaction is never resolved. No event is received either, so the .on('receipt') is never triggered. However the transaction is successfully submitted to the network and mined.
So the problem is not with the submission, but rather with the PromiEvent that web3.eth.sendSignedTransaction returns.

Anyone has any idea of why this behavior is happening?

Best Answer

From the official documentation:

// using the promise
web3.eth.sendTransaction({
    from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe',
    to: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
    value: '1000000000000000'
})
.then(function(receipt){
    ...
});


// using the event emitter
web3.eth.sendTransaction({
    from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe',
    to: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
    value: '1000000000000000'
})
.on('transactionHash', function(hash){
    ...
})
.on('receipt', function(receipt){
    ...
})
.on('confirmation', function(confirmationNumber, receipt){ ... })
.on('error', console.error); // If a out of gas error, the second parameter is the receipt.

In your code, you seem to be attempting to use both the promise and the event emitter.

In addition to that, for the event emitter, you seem to be using once instead of on.

I'd start by fixing these two (getting rid of the then, and replacing once with on).

Related Topic