6

Reading through the web3.js transaction documentation, what is an optimal approach of waiting for a transaction to confirm?

The method sendTransaction() returns a

String - The 32 Bytes transaction hash as HEX string.

But when it does is the transaction confirmed in the blockchain? If not, is an approach using a loop, a wait and a hash transaction optimal?

  • btw I could swear this is a duplicate but I couldn't find a duplicate... – Paul S Jul 6 '16 at 21:09
4

Example here using filters: How do I know when I've run out of gas programmatically?

and also here by busy polling: https://github.com/barkthins/ether-pudding/blob/master/index.js#L375

I use busy polling, because I've found the filters to be unreliable (fails about 1:1000 times)

EDIT: Also, how MANY blocks to wait for confirmation is debatable, here's the Q for that: What number of confirmations is considered secure in Ethereum?

2

We can subscribe to events from the send method. For example we need to wait for 5 confirmations before considering a transaction mined we can do the following:

return new Promise((resolve, reject) => {
  contract.methods.doWork(1, 2, 3).send({from: account}) 
    .on('confirmation', (confirmationNumber) => {
      if (confirmationNumber === 5) {
        resolve()
      }
    })
    .on('error', (error) => {
      reject(error)
    })
})
1

With web3 1.0 methods you can now listen for the receipt when you send a transaction.

So, you could fire a send method on a contract and then listen for the receipt by chaining .on to the end like so:

_contract.methods.someSendMethod( parameters, go, here ).send().on('receipt', receipt => console.log('receipt', receipt))

web3 1.0 docs => https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html?highlight=send#id12

0

We will do a loop checking on the sending node, see if txpool.status.pending is zero.

If there are no transactions in pending status, we can make sure that all the transactions from this node has already been included in the blockchain.

If you need to consider double-spending which is out of my interest, you may need to check more.

0

Synchronized transactions are one of the primary features of the truffle framework, in particular the "truffle-contract" abstraction that wraps web3.eth.Contract.

Example:

MyContract.deployed().then(function(instance) {
  var deployed = instance;
  return instance.someFunction(5);
}).then(function(result) {
  // Do something with the result or continue with more transactions.
});

See https://github.com/trufflesuite/truffle-contract#full-example for an example and more docs.

0

Made a npm module called await-transaction-mined for this specific problem.

const awaitTransactionMined = require ('await-transaction-mined');
(async function() {
   var txHash = '0x6ee5d58c314d183f3ca70e2292b39dca5ae46141fe4e6b1da5b106dd506e589a';
   const minedTxReceipt = await awaitTransactionMined.await(web3, txHash);
})();

It polls the blockchain every 500ms to check if the transaction has been mined. Once mined it returns the transaction receipt.

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.