0

I am using web3 and an infura node to broadcast transactions.

    let result = await web3.eth.sendTransaction({
        to:receiveAddress, 
        from:web3.eth.defaultAccount, 
        value:BNAmount, 
        chainId:chainid, 
        gas:21000, 
        gasPrice:currentGasPrice,
        nonce:nonce,
    })
    .on('transactionHash', function(hash){
        let info = {transactionHash : hash};
        console.log(hash);
        nonce++;
    })

I am receiving a transaction hash from this code, but the hash is not related to any transaction, it is not pending on etherscan, and the transaction never resolves. Why could this be? Could it be a failure by Infura? How can I debug this?

Example transaction hash: 0x3e0e5fcf1eef6a0fe05a338ebff7162144db43ad4829aaf8777842289138a8e0

1

You should get the transaction receipt after an execution. Try this:

let options = {
    to:receiveAddress, 
    from:web3.eth.defaultAccount, 
    value:BNAmount, 
    chainId:chainid, 
    gas:21000, 
    gasPrice:currentGasPrice,
    nonce:nonce
}


// using the promise
web3.eth.sendTransaction(options)
.then(function(receipt){
    console.log(receipt)
});


// using the event emitter
web3.eth.sendTransaction(options)
.on('transactionHash', function(hash){
    console.log(hash)
})
.on('receipt', function(receipt){
    console.log(receipt)
})
.on('confirmation', function(confirmationNumber, receipt){ ... })
.on('error', console.error); // If a out of gas error, the second parameter is the receipt.

Referene - https://web3js.readthedocs.io/en/v1.2.11/web3-eth.html?highlight=sendtransaction#id86

2
  • thanks, but if I don't get a receipt, what does the .on('transactionhash') hash represent? Oct 2 '21 at 14:35
  • 1
    It's just the Transaction hash generated from your txn. The receipt is the outcome of the transaction. It shows both standardized information about what occurred (status, txn hash, contract address, etc) and also custom information from the events emitted in the smart contract.
    – GaryB
    Oct 3 '21 at 21:01

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.