1

I am deploying a smart contract using web3js and it works perfectly. But i was made aware of my incorrect use of callbacks. I got some basic understanding of promievents from https://web3js.readthedocs.io/en/v1.2.1/callbacks-promises-events.html and rewrote my code as below based on the samples here : https://web3js.readthedocs.io/en/v1.2.0/web3-eth.html#sendsignedtransaction :

web3.eth.getTransactionCount(account, (err, txCount) => {
    if (err!=null) {console.log('error executing web3.eth.getTransactionCount: ', err)}
    else{
        console.log('txCount: ',txCount)

        const txObject = {
            nonce: web3.utils.toHex(txCount),
            gasLimit: web3.utils.toHex(1000000),
            gasPrice: web3.utils.toHex(web3.utils.toWei('0', 'gWei')),
            data: contractByteCode
        }

        const tx = new EthereumTx(txObject,{common: customCommon})

        tx.sign(privateKey)

        const serializedTransaction = tx.serialize()
        const raw = '0x' + serializedTransaction.toString('hex')

        web3.eth.sendSignedTransaction(raw)
        .on('transactionHash',(hash) => {
            console.log('txHash:', hash)
        })
        .on('receipt',(receipt) => {
            console.log('receipt', receipt)
        })
        .on('error', console.error)
    }

})

I did this based on the principle that the callback in getTransactionCount() might execute at any future time, so i should use the return value txCount only within the callback. That is why the entire code is inside the callback function.

But i am not sure about the sendsignedtransaction part. I am assuming that the ".on('transactionHash')" function will execute whenever a transactionHash event is emitted by the sendsignedtransaction function. Similarly the ".on('receipt')" function will execute whenever a receipt event is emitted by the sendsignedtransaction function. And the ".on('error')" function will execute whenever a error event is emitted by the sendsignedtransaction function. So everything works asynchronously without blocking my thread.

If my understanding is correct, then i am confused why this code just hangs in node when i intentionally put in an bug(by removing the "{common: customCommon}" parameter). Shouldn't it return immediately and do "console.error" ?

1 Answer 1

2

Here is the general scheme:

  • transactionHash is fired right after the transaction is sent (almost immediately)
  • receipt is fired when the transaction receipt is available
  • error is fired if an error occurs at any point during this process
  • confirmation is fired for every confirmation up to the 12th confirmation

Note that the more confirmations you wait for, the higher the probability of your transaction to remain in the blockchain forever is.

See the official documentation (web3.js v1.2.0) for more details.

11
  • 1
    i got the confirmation 24 times in fact. Maybe it is decided by some configuration, though i did not change any configuration myself. I am using the Quorum network in Azure blockchain service.
    – faizal
    Jan 21, 2020 at 7:18
  • 2
    @faizal: Yeah, that's a known issue in Web3.js. I thought they've fixed it in v1.2.0 but apparently they haven't (so the official documentation says 12, but the actual implementation yields 24). Jan 21, 2020 at 7:24
  • 1
    I said asynchronous, not async. A function can be asynchronous without using the keyword async (which was in fact introduced in later versions of Javascript). For a function which is asynchronous but not declared async, there is no point in using await because it will not really make it run synchronously until completion. But this topic in general is not related to Ethereum, so you might wanna search it on other forums as well (e.g., Stack Overflow). Jan 22, 2020 at 7:55
  • 1
    @faizal: Yes, it should return immediately. Add a console.log statement right before the line web3.eth.sendSignedTransaction(raw) and right after the line .on('error', console.error) in order to verify this. Jan 22, 2020 at 11:57
  • 1
    Of course, because the entire process (your script executed by NodeJS) is blocked until those callbacks are executed. Jan 22, 2020 at 12:30

Your Answer

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

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