question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to guarantee the order of transactions?

See original GitHub issue

I read #435, but following the instructions don’t guarantee that the transactions sent to an end-user wallet, such as MetaMask, will prompt the user to sign them in the same order.

Here’s what I do:

const promises = [];
promises.push(createTokenApprovalTx());
promises.push(createMyTx());
await Promise.all(promises);

The 2nd transaction depends on the first one, that is, the token approval must be sufficient for the contract to be able to move tokens around. In the transaction creation functions, I call getTransactionCount to get the latest nonce and increment it for the 2nd transaction.

But the order is not enforced in the MetaMask popups. I couldn’t identify a pattern for when it works and when it doesn’t - it seems to be a stochastic process. I tried swapping the order of adding the transactions in the promises array, but that didn’t move the needle either.

I find this a basic example and arguably the safer alternative to approve(MAX_UINT256). What’s the best way to do this with ethers?

Capture d’écran 2019-12-05 à 15 16 27 Capture d’écran 2019-12-05 à 15 16 33

As you can see in the screenshots, the token approval comes 2nd, even if it should come 1st.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ricmoocommented, Jun 4, 2020

I think this is resolved, so I’m going to close it now. If not, please re-open.

Thanks! 😃

1reaction
ricmoocommented, Jan 31, 2020

The 1 means wait for 1 block confirmation. The default is one, so it is not necessary, but if you pass in 0, then wait will return null if the transaction is not mined, and for Example, 2 would indicate to wait until there were two confirmations.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Guarantee Execution Order of Transactions - General - Algorand
This solution works as long as nothing bad can happen if group 2 is executed before group 1. If something bad can happen...
Read more >
Order of Transactions and How Blockchain avoids Double ...
Now there is a tie. This tie is resolved when the second block is mined. If the second block is added to path...
Read more >
Lecture 13: Transaction Ordering and Fairness
Majority rule is then employed to achieve a consensus on the ordering of the transactions in the fair-ordered ledger. Fair Ordering Guarantees.
Read more >
blockchain - Chronologically sorting transactions within a block
There's a minor case where correct ordering is guaranteed: If transaction Y spends coins from transaction X, and if they're both included in...
Read more >
Order of transactions on database - Stack Overflow
Short answer is yes. Do you have a timestamp in your transaction? You can verify it with that. Depends on what database you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found