1

The usual workflow to submit an EVM transaction seems to be:

  1. Call the send(Raw)Transaction RPC method.
  2. Use the returned transaction hash to query the status of your transaction.

My question is, is it possible to precompute the transaction hash before submitting the transaction to the node? I believe this would simplify the transaction tracking as it would allow me to start listening for the transaction right from the start. If instead I have to wait for the hash first, then once I receive the hash I first need to check the history whether the transaction has been mined already, and only then can I start looking for the transaction in new blocks.

2
  • 1
    Yes, it is possible. Check the web3 framework for ways to do it. For example web3.js provides a "transactionHash" event when the hash becomes available.
    – Ismael
    Commented Apr 29, 2022 at 0:45
  • I did that but can't find anything useful. I would expect that there should be a function transactionHash(tx) which takes a transaction object and computes its hash, but neither web3.js nor ethers.js seem to have such a function.
    – gTcV
    Commented Apr 29, 2022 at 4:04

1 Answer 1

1

For web3 there's web3.eth.signTransaction returns the hash of the transaction in the 'hash' field.

{
    raw: '0xf86c...',
    tx: {
        nonce: '0x0',
        ...
        hash: '0xda3be87732110de6c1354c83770aae630ede9ac308d9f7b399ecfba23d923384'
    }
}

Similarly web3.eth.sendTransaction returns a PromiEvent. This object provide several events that you can listen to, one of them is transactionHash

// using the event emitter 
web3.eth.sendTransaction({
    from: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe',
    to: '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe',
    value: '1000000000000000' })
})
.on('transactionHash', function(hash) {
    // hash is available
})
.on('receipt', function(receipt){
    // tx was mined
})
2
  • signTransaction seems to only return the raw transaction for me :-(
    – gTcV
    Commented Apr 29, 2022 at 6:40
  • 1
    @gTcV Which library are you using? My answer is for latest web3.js v1.7.x, for other languages there should be a similar library. If I remember correctly hash = keccak256(rawTransaction). See here ethereum.stackexchange.com/questions/47978/….
    – Ismael
    Commented Apr 30, 2022 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.