Guarantee Execution Order of Transactions

Say you have two separate transaction groups: txGroup-A and txGroup-B.
Additionally, txGroup-B depends on txGroup-A, and txGroup-B is created after txGroup-A has been submitted to the mempool

Is it possible to enforce txGroup-B is executed after txGroup-A in the same block? How is this ordering enforced?

No, the only way to enforce transactions’ ordering and single block execution is grouping them in an Atomic Transfer. So you should join transactions in Group A and transactions Group B in a single Atomic Transfer, if possible. Otherwise you will have no 100% certainty that Group A is executed before Group B (or in the same block).

1 Like

Officially, no - there is no way to enforce that.
But there is a trick i’ve used before:
On txn group 1, move 5 algo to acct A.( new account )
On txn group 2, move 5 algo from acct A to itself.

The second txn group would fail if the first one didn’t took place.

1 Like

This solution works as long as nothing bad can happen if group 2 is executed before group 1.
If something bad can happen (e.g., loss of funds, ways for an attacker to steal funds, moving the contract in a dangerous state, …), then you should absolutely not use the above. Indeed, an attacker (who controls the block proposer, or the correct relays, or the node you used, or …) can easily drop group 1 and then fund the account A themselves.

If you need group 1 to happen before group 2, the best solution I know of is to use a smart contract for that purpose. There are many ways to implement it, but a very naive way is to have a counter you increment.

1 Like

Yes, @fabrice is correct, of course.
My method is not secured enough for any mission critical application.
Usage of local/global state could help resolve these issues.

1 Like