[Ethereum] Why does a transaction trigger 12 or 24 confirmation events

confirmationsreceiptstransactionsweb3js

Web3.js docs state that a confirmation event will be fired:

  • 12 times when calling web3.eth.sendTransaction (see here)
  • 24 times when calling myContract.methods.myMethod.send (see here)

Why is the difference in the number of times that the event is fired in each case?

More importantly, what exactly should I use these events for?

At present, I simply await for the function to complete, and then use the returned value, which is a resolved Promise containing the transaction receipt (unless we're dealing with a contract deployment, in which case it will contain the new contract's instance).

To my understanding, this receipt can also be obtained using:

myContract.methods.myMethod().send().on('receipt', function(receipt){...});

I assume that this additional way of obtaining the receipt is for when you also want to handle other things, such as those confirmation events, which from what I've observed, do not necessarily occur before the receipt is available.

Do I need to count up 12 (or 24) events in order to be sure that the information receipt reflects the block-chain?

Thank you!

Best Answer

When a transaction is mined, the receipt is available for use, at this point confirmation number is 0, as more block are added to the blockchain, confirmation number increases. With higher confirmation number, we will have more confidence that transaction is on the longest path and can’t be undone.

Suppose you accept transactions with a certain number of confirmations, you can filter that confirmation number on confirmation event.

.on('confirmation', function(confirmationNumber,receipt){})

Regarding why the event is emitted up to 12 and 24 confirmations in different scenarios, I do not have much idea.

Related Topic