0

I don't understand what .on event handler does in Web3. Take this example for signing a transaction:

    web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
        .on('transactionHash', function () {
            console.log('*** Transaction Hash ***')
        })
        .on("transactionHash", console.log)
        .on('transactionHash', function () {
            console.log('*** Waiting for Confirmation ***')
        })
        .on('receipt', function () {
            console.log('*** Transaction Submitted ***')
        })
        .on("confirmation", function() {
            console.log('*** Transaction Confirmed ***')
        })
        .on("error", function() {
            console.log('*** Error ***')
        })
}

It waits for various things to happen, but when they do, I get 10-20 console log lines every confirmation or error. Is this an infura thing maybe, sending back events asynchronously from multiple nodes, or is there something else going on?

Nothing bad is happening in my script, I just get spammed with the same events that have already happened.

  • You have the .on("transactionHash" part duplicated 3 times dude! You should use a single handler for each type. – goodvibration Jul 31 at 14:22
  • Regarding the other question, you should be getting 24 confirmation events. What is it good for? The more confirmation events you wait for - the higher the possibility that the transaction will be a part of the main chain (and not of an orphan branch). – goodvibration Jul 31 at 14:26
  • I don't use Tx hash, it's just in this example for discussion, but ironically, no duplication of that. Can you please make an answer explaining how things work? :-) I can't find any reference to this. Do you mean the first 24 confirmation events are coming from infura as a matter of their design? I get at least 15 confirmations in the first minute. Is that normal? Seems like it is something other than confirmations triggering this event. – TrumpPaiPence Jul 31 at 14:42
  • 1
    No, the events are implemented in web3.js, see here (BTW, I was wrong about 24, it's only 12 according to that link, though I vaguely recall seeing 24 events when I was using this function). – goodvibration Jul 31 at 14:47
  • 1
    They had 24 written there in the past, so perhaps it changes from one version of web3 to another, while the official documentation is sometimes forgotten behind. You could simply check it in the source code of web3 and know for sure. It's right there on your machine, under the node_modules folders if you're installing your npm packages locally. – goodvibration Jul 31 at 20:20
3

Is not an infura thing is the way that promievents behave

promievents_docs

On your code every line with .on('transactionHash', function () will return a the same hash

but specially this line .on("confirmation", function() will return you up to 12 confirmations. To evade this, try to change it to .once("confirmation", function() or use off

Its explained in docs too promievents_docs2

  • The only unanswered thing is why it is between 15 and 30 responses coming back, but I will put that down as tolerance and/or a bug. If you are counting confirmations, better to have a few more in case some events go missing I guess. – TrumpPaiPence Aug 1 at 8:59

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.