Best practice: How to handle 120sec timeout of contract callbacks?

857 views Asked by At

With Truffle we get a nice wrapper for contracts. But it has one feature making me headaches:

Example from documentation:

MetaCoin.at(contract_address).then(function(instance) {
  coin = instance;
  return coin.sendCoin(account_two, 3, {from: account_one});
}).then(function(result) {
  // This code block will not be executed until truffle-contract has verified
  // the transaction has been processed and it is included in a mined block.
  // truffle-contract will error if the transaction hasn't been processed in 120 seconds.
})

This raises four questions:

  1. How to prevent the user getting a 120sec timeout as his transaction still might be mined a couple minutes later (depending on gas price and network conditions)? This is especially important if we're talking about Ether-sending transactions. I don't want to rip-off my users by alleging the transaction failed (and suggesting them to repeat).
  2. Where will it error? In the callback with a 2nd parameter (function(result, error)) or the whole thing (.then( function(result) {...} ).catch(e))? I can't test it locally with Ganache.
  3. How do you inform your users about the transaction status? Are you displaying a 'Transaction successfull' in any case or do you use some kind of throbber (that's what I'm considering right now) during this 120sec period?
  4. How to get the tx-hash as fast as possible? By sending with MEW I instantly get a transaction link to etherscan.io - even if the transaction hasn't propagated yet.

Especially problem #1 is causing me a headache.

1

There are 1 answers

2
Raghav Sood On

The timeout is solely a truffle thing. The network does not time out, and when developing an app with web3 or similar wrappers, you simply keep listening until the tx is miner, or the page is closed.

When you make a transaction in web3, you will receive the txhash as part of the response, even before it is mined. You can display that to the user, along with a pending status, and use Web3's filters to set a listener for a callback when it is mined, without running into timeout issues.