Financial applications on blockchains are highly sensitive to the order in which their transactions execute. For example, a PropAMM wants price updates to execute before swaps to avoid adverse selection, while an order book wants cancellations to execute before market orders for the same reason. Yet today, blockchains do not give applications a general way to get the order they want.

Instead, on most blockchains, the proposer has full control over the order. Applications that desire a specific order can rely on off-chain auctions and builders, or launch their own app chain and lose composability.

To give applications a general way to express their ordering preferences, we propose Application-Preferred Execution Sequencing (APES) for Monad. APES replaces the proposer's free choice of order with an execution order computed from the applications' preferences and from priority fees, hence unlocking a broader design space for applications. Combining preferences is not a trivial problem, however, and an attacker can try to manipulate the resulting order. In the following, we explain how APES works, what attacks are possible, what guarantees it offers, and how applications and users can protect themselves.

Key takeaways when using APES:

For users: if ordering matters to you, keep your transaction simple. Interact with only one application where ordering matters.

For applications: set your preferences carefully to ensure that an attacker cannot cheaply get ahead of your users. We walk through sample configurations for common applications below.


APES at a High Level

At a high level, APES has four parts:

  • Preference registration: applications register their ordering preferences.
  • Interaction declaration: each transaction declares the applications it interacts with.
  • Protocol-controlled ordering: the protocol orders transactions by their declarations, following application preferences where possible.
  • Execution check: the protocol and applications check during execution that the declarations were correct.

We outline these parts below in more detail using a PropAMM as a running example. If you are interested in the full implementation details, you can check our forum post.

Preference Registration

Applications with ordering preferences register them in an on-chain registry, becoming opinionated. An application's preferences are a set of clauses, one for each method it wants to rank. A clause is a function that assigns a score to a transaction that wants to access a given method at that application, and a higher score means earlier execution.

Each clause scores a transaction as a · v + b, where v is a parameter the transaction declares for the respective method, and the application chooses a and b. The application can also use different coefficients for different senders, and can distinguish between transactions interacting with a single opinionated application and those interacting with several.

For example, a PropAMM wants its oracle updates to execute before swaps. It defines two clauses without a parameter v (a = 0). The update clause scores 1 when the sender is one of its keepers and 0 otherwise, and the swap clause scores 0 for every swap. We call this a uniform PropAMM, as it ranks swaps equally.

A fee-ranked PropAMM instead executes swaps that pay a higher swap fee first. Each swap transaction declares the swap fee it pays as v, and the swap clause scores it v (a = 1, b = 0). The update clause gives keepers a score of LARGE_VALUE, above any possible swap fee, so updates still execute first.

Interaction Declaration

A transaction that interacts with an opinionated application must declare which of the application's ranked methods it may call. For each method, it can also declare a parameter v, which the application uses to compute the transaction's score. These declarations go in a new transaction field, the interaction_list.

For example, a transaction that swaps on a uniform PropAMM declares the PropAMM's swap method. It needs no parameter, since here every swap gets the same score. A swap on a fee-ranked PropAMM additionally declares its swap fee as the parameter, which the PropAMM uses to score it.

If a transaction declares several methods at the same application, it gets the lowest of their scores there. For example, a transaction that declares both update and swap is scored as a swap.

There are two options for a transaction with an unpredictable execution path. It can either over-declare its interactions, listing every application and method it might call, or be sent as a wildcard transaction. A wildcard transaction skips the declaration and may call any application. However, wildcard transactions execute after all declaring transactions. Existing transaction types are treated as wildcard transactions by default.

Protocol-Controlled Ordering

The protocol computes the execution order from the scores using an algorithm called unanimity override, which we introduced in an earlier post. At a high level, the algorithm is based on the idea that when all applications that score two transactions agree on how to order them, the protocol should respect that order. Everything else is decided by priority fee, as long as it does not contradict these preferences. Consequently, the proposer has no discretion over the ordering of transactions.

For example, consider two uniform PropAMMs, P and Q, with preferences registered as in the previous examples. Each PropAMM receives a keeper update (t1 and t2) and a swap (t3 and t5). A third swap, t4, trades on both PropAMMs, so it executes after both updates. Among the swaps, the PropAMMs have no preference, so priority fees decide, and t4 executes after t3 and t5, which pay more. Finally, t6 is a transfer that does not interact with any opinionated application. It carries an empty interaction_list and, unlike a wildcard transaction, is not pushed to the end of the block. Instead, it executes according to its priority fee, between t5 and t1.

Two uniform PropAMMs P and Q with keeper updates t1 and t2, swaps t3, t4 and t5, and a transfer t6, and the execution order the protocol computes from their preferences and priority fees
Each PropAMM wants its keeper's update before all of its swaps and scores all swaps equally. t4 swaps on both PropAMMs, so it follows both updates. Wherever the preferences leave the order open, priority fees decide: t3 and t5 go before t4, and t6, a transfer with no preferences, lands between t5 and t1.

Note that the unanimity ranking can contain cycles. In that case, a fallback rule we refer to as demotion breaks them. Consider three fee-ranked PropAMMs, P, Q and R, which favor swaps with higher swap fees. In the example below, three transactions each swap on two of the PropAMMs, with swap fee 3 on one and swap fee 1 on the other. Thus, P prefers t1 before t3, Q prefers t2 before t1, and R prefers t3 before t2. To break the cycle, the protocol demotes the transaction with the lowest priority fee among those that interact with several opinionated applications, t3, and places it after the others in the execution order.

Three fee-ranked PropAMMs P, Q and R whose preferences over t1, t2 and t3 form a cycle; t3 is demoted
P, Q and R are fee-ranked PropAMMs. Each transaction swaps on two of them, with a different swap fee on each, which it declares. P prefers t1 before t3, Q prefers t2 before t1, and R prefers t3 before t2, closing a cycle. The protocol demotes t3, which has the lowest priority fee.
Execution Check

The protocol builds the order from each transaction's static declaration, so execution has to check that these declarations were truthful. Thus, the protocol makes sure that a declaring transaction never calls a ranked method that it didn't declare and reverts the call otherwise. Each application is responsible for checking that the declared value, such as the swap fee, matches what the call actually does. For example, an application can revert calls that do not match their declaration.


Attacks and Guarantees

Applications can disagree, and even when they do not, the unanimity ranking can form cycles, so unanimity override cannot always respect every preference. An attacker can try to exploit this to manipulate the execution order.

We consider a strong attacker who sees every transaction in the block, injects any transactions it likes, and deploys its own opinionated applications. However, it cannot impersonate other senders, such as a PropAMM's keepers, and cannot censor transactions. Below, we show what such an attacker can do and what APES guarantees nonetheless.

Note that today, Monad's block proposers choose the order of transactions freely, so every manipulation described below is already possible. With APES, the protocol enforces the ordering instead, which removes most of this freedom. However, some ways to manipulate the order remain.

Overturning a Preference

Consider two DEXs, A and B, that rank swaps by swap fee. A victim transaction V swaps on both, and an attacker's transaction X swaps on A with a lower swap fee. Since A prefers V before X, V should execute first. However, there are two ways in which the attacker can overturn the order.

First, the attacker can create a disagreement. X adds a phantom swap with a higher swap fee on B, that is, it declares the swap but never makes it. Thus, B now prefers X before V. As A and B disagree, priority fees decide, and X executes first by paying a slightly higher priority fee.

Two DEXs A and B; the attacker's X adds a phantom swap on B so A and B disagree about V and X, and X executes first on priority fee
Two DEXs, A and B, rank swaps by swap fee. A prefers V before X. The attacker adds a phantom swap with swap fee 5 on B, so B prefers X before V. A and B now disagree, so priority fees decide, and X executes first by paying a slightly higher priority fee.

Second, the attacker can create a cycle. It deploys its own application M and injects two helper transactions, Y1 and Y2, with phantom swaps, such that A prefers X before Y1, M prefers Y1 before Y2, and B prefers Y2 before V. This creates a cycle, and if V pays the lowest priority fee among the multi-opinion transactions on it, that is, those interacting with several opinionated applications, V is demoted and executes after X.

Two DEXs A and B plus the attacker's application M; helpers Y1 and Y2 close a cycle through V, which is demoted behind X
Two DEXs, A and B, rank swaps by swap fee, and A prefers V before X. The attacker deploys its own application M and injects two helpers with phantom swaps: A prefers X before Y1, M prefers Y1 before Y2, and B prefers Y2 before V, closing a cycle. V has the lowest priority fee on the cycle, so it is demoted and executes after X.

Observe that both attacks rely on the fact that the victim's transaction interacts with several opinionated applications. We call a transaction that interacts with only one opinionated application single-opinion. For a single-opinion transaction, only one application decides how it is ranked, so an attacker cannot create a disagreement. Further, the protocol first demotes multi-opinion transactions in cycles. Thus, single-opinion transactions are safe from both attacks.

Guarantee 1: single-opinion transactions are safe. Consider a transaction that interacts with only one opinionated application. No transaction that this application scores lower will execute before it.

Moreover, even a multi-opinion transaction is safe from both attacks if no attacker can score above it at any of its applications. In the first example, X scores above V at B, and in the second example, Y2 does. Similarly, the cycle attack fails if no attacker can rank below it, since the victim transaction then cannot be on a cycle. More generally:

Guarantee 2: gated transactions are safe. A transaction is top-gated if no transaction an attacker can craft outranks it in the unanimity ranking, either directly or through other transactions. Such a transaction executes as the applications unanimously prefer, however many applications it touches. Similarly, a transaction that no attacker can rank below is bottom-gated, and it executes after every transaction the applications unanimously rank above it.

Cheap Front-Running via Ties

Note that Guarantees 1 and 2 only protect the applications' preferences. Next, we see how an attacker can try to front-run a transaction while respecting these preferences.

Consider a strawman order book market that wants cancellations to execute before market orders and simply gives every cancel a score of 1 and every order a score of 0:

MethodAll senders
cancel1
order0

Now consider two such markets, A and B, and a transaction V that places orders on both markets with a priority fee of 10. This transaction can be cheaply front-run by an attacker with two transactions: X1 declares a phantom cancellation on A and places an order on B, and X2 places an order on A and declares a phantom cancellation on B, both with a priority fee of 0. The unanimity ranking places X1 before V and X2 before V. Thus, the attacker can cheaply front-run V.

Two strawman order book markets A and B; X1 and X2 each pair a phantom cancellation with an order and both execute before V despite a priority fee of 0
Both markets score every cancellation 1 and every order 0. X1 outranks V at A and ties with it at B, and X2 does the reverse. Thus, both execute before V, despite a priority fee of 0 against V's 10.

In the example above, the victim's transaction was multi-opinion. However, even a single-opinion transaction can be cheaply front-run if its application scores it the same as the attacker's transaction. For example, consider again a strawman order book market A. The victim transaction V places an order on A with a priority fee of 5, and an attacker's transaction Y places an order on A with a priority fee of 1. V is single-opinion, but it ties with Y at A.

For Y to execute before V, the attacker sends a helper transaction W2 with a phantom cancellation on A, so A prefers W2 before both Y and V. Using two applications it deploys, Z1 and Z2, and a second helper W1, the attacker also places Y before W2, which closes a cycle. W2 also interacts with one of these applications, so it is multi-opinion. It pays the lowest priority fee on the cycle, so it is demoted behind Y. However, W2 still executes before V, so Y also executes before V, despite its lower priority fee.

A single strawman market A; the attacker's Y ties with V, helper W2 outranks both with a phantom cancellation, and applications Z1 and Z2 close a cycle so W2 is demoted behind Y but stays before V
Market A scores every cancellation 1 and every order 0, so V and the attacker's Y tie there. W2's phantom cancellation outranks both, and the attacker's applications close a cycle through Y. W2, the cheapest on the cycle, is demoted behind Y but stays before V, so Y front-runs V despite its lower fee.

Notice that if A had ranked orders by a value it checks at execution, such as the priority fee, A would prefer V before Y, and Guarantee 1 would protect V. More generally, an application can protect its transactions from cheap front-running by ranking them by such a value:

Guarantee 3: simple transactions cannot be cheaply front-run. Consider a transaction that touches only one application where ordering matters. If this application either has no preferences or ranks the transaction by a value (which could be the priority fee), then an attacker who wants to front-run it has to outbid the victim on the relevant value.

Further, a transaction that no attacker can outrank is also safe from cheap front-running:

Guarantee 2 (continued): top-gated transactions are also safe from cheap front-running.


Guidance for Applications

Guided by the above guarantees, we now go through sample configurations that various applications could use to set their preferences safely.

PropAMM

Consider a strawman version of our uniform PropAMM that does not reserve updates for its keepers. It simply gives every update a score of 1 and every swap a score of 0:

MethodAll senders
update1
swap0

Just as with the strawman order book above, a swap across two such PropAMMs can be cheaply front-run. An attacker's transaction declares a phantom update on one PropAMM and swaps on the other. This outranks the victim's swap on the first PropAMM and ties it on the second. To keep swaps, including cross-PropAMM swaps, from being cheaply front-run, PropAMMs can configure their preferences as in our uniform PropAMM, where S is the set of their keepers:

MethodSender in SOther senders
update10
swap00

With this configuration, only trusted keepers can reach score 1. Any update an attacker declares scores 0, the same as a swap. Thus, the attacker cannot outrank the swap, so swaps are top-gated (Guarantee 2) and cannot be front-run cheaply.

We note an important subtlety: PropAMMs always guard their update methods to revert calls from non-keepers, so why is the strawman PropAMM still susceptible to attacks? The answer is that without explicitly distinguishing the keepers in the PropAMM's preferences, the ordering algorithm would be unaware of who can actually call update, and allow anybody to declare update calls that change the execution order. These updates would revert in execution, but at that point the execution order has already been fixed.

Order Book

Next, let us return to the order book that wants to order cancellations before market orders. As we showed above, the strawman configuration, which gives every cancel a score of 1 and every order a score of 0, can be cheaply front-run. Additionally, the solution we used for the PropAMM's preferences cannot be carried over to the order book, because cancellations do not come from a privileged set of keepers.

Instead, the order book can rank market orders by priority fee, while cancellations score highest for every sender. Each order declares its priority fee as its parameter v, which the market checks at execution, and LARGE_VALUE is larger than any possible priority fee:

MethodAll senders
cancelLARGE_VALUE
orderv

As no transaction can score above LARGE_VALUE, cancellations are top-gated (Guarantee 2) and execute before every market order on the markets they touch. Further, since market orders are ranked by priority fee, an order on a single market cannot be cheaply front-run (Guarantee 3).

However, orders across several markets are multi-opinion and thus exposed to cycle attacks. A designer can avoid this by putting all markets in one contract with a single set of preferences. Then, an order across several of these markets interacts with only one opinionated application, so Guarantees 1 and 3 protect it. Note that this only protects orders whose markets all belong to that one contract. An order that also trades on another opinionated application is again multi-opinion. Consequently, a cancellation on one market then executes before the market orders of every market. Thus, preferences become coarser.

Cross-Market Batch Exchange

Next, consider a batch exchange that wants to settle all orders across several markets at uniform clearing prices. In this example, each market is a contract, and the exchange can be thought of as the collection of these contracts (hence "cross-market").

Uniform clearing ensures that all trades in a market execute at the same price. However, the last order before the settlement is the only one that can compute that price from all the others and its own. It thus has a last look. For this reason, in this example we assume that orders bid for their position and are ordered with the highest bid last.

Building on the PropAMM guidance above, each market could reserve the lowest score for a set S containing only the exchange engine's address, so that only the engine's settlement executes after all orders. Here v is the order's bid and LARGE_VALUE is larger than any possible bid:

MethodSender in SOther senders
cancel11
order−v−v
settle−LARGE_VALUE0

The settlement, sent from the engine's address, touches every market and is therefore multi-opinion. Importantly, it is bottom-gated, since no other transaction can score below it, so it executes after every cancellation and order. Wildcard transactions still come after the settlement, and the exchange can choose to either reject them or roll them into the next batch.

However, this is not enough. In this exchange, executing late is an advantage, and demotion places a transaction after the rest of its cycle. Consider a victim transaction V on market A. An attacker can craft its transaction X that places its own order with a bid of 0 on A. The attacker also declares a high-bid order on another market Z2 that it controls. Together with two helper transactions H and W involving Z2 and yet another market Z1, the attacker can close a cycle. If X pays the lowest priority fee on the cycle, it is demoted behind the victim's order and gets the last look for free, as the figure below shows.

Batch exchange market A; the attacker's bid-0 order X ranks above V, helper transactions H and W close a cycle through markets Z1 and Z2, and X is demoted behind V to get the last look
Orders score −bid, so the attacker's order X with bid 0 ranks above the victim's order V at A. A helper transaction H with a phantom order with bid 9 ranks below V, and a second helper transaction W, through the attacker's applications Z1 and Z2, closes the cycle back to X. X pays the lowest priority fee on the cycle, so it is demoted behind V and executes just before the settlement, getting the last look without bidding.

To prevent this, the exchange only lets single-opinion orders bid:

MethodSingle-opinion, sender in SSingle-opinion, other sendersMulti-opinion, sender in SMulti-opinion, other senders
cancel1111
order−v−v00
settle−LARGE_VALUE0−LARGE_VALUE0

With multi-opinion orders scoring 0, no multi-opinion transaction from the attacker can rank below a single-opinion order, and such a cycle cannot form. However, this means that orders bidding for last look across several markets need to be split into separate transactions.

Liquidation Auction

Finally, consider a lending protocol that wants fresh oracle prices before any liquidation, and liquidations ordered by how much each liquidator bids to the protocol. It configures its preferences as follows, where v is the bid the liquidator declares and LARGE_VALUE is a value above any possible bid:

MethodSender in SOther senders
updateLARGE_VALUE0
liquidatevv

In this example, prices are updated through an oracle in set S calling update. Only senders in S get a high score, and the lending protocol reverts such a call for any other sender. Additionally, the application checks during execution that each liquidation pays the bid it declared.

Since only senders in S reach LARGE_VALUE, the update is top-gated and executes before every liquidation (Guarantee 2). Because liquidations are ordered by bid, liquidators compete through bids to the lending protocol instead of through priority fees. The lending protocol thus captures these payments and can pass them on to borrowers or lenders.

Importantly, a liquidator who bundles its liquidation with calls to other opinionated applications gives up this protection. Liquidators therefore have an incentive to send each liquidation as a single-opinion transaction, i.e., without interacting with opinionated liquidity sources in the same transaction.


Guidance for Users

Users who are sensitive to ordering should keep their transactions simple, that is, interact with only one application where ordering matters, whether it is opinionated or not. Then, they are protected by Guarantee 1 if the application is opinionated, and by Guarantee 3 if it has no preferences or ranks by a value. As the attacks above show, transactions that interact with several opinionated applications are more exposed. However, more complex transactions across several applications can also be safe, such as top-gated transactions (protected by Guarantee 2), or cross-PropAMM swaps on PropAMMs configured as outlined above.

Finally, users whose interactions are hard to predict ahead of time can either over-declare them or send wildcard transactions, to avoid unnecessary transaction failures.


Looking Forward

APES provides a general way for applications to express their ordering preferences natively on Monad. The protocol orders transactions such that they respect these preferences whenever possible. With APES, PropAMMs, order books, batch exchanges and liquidation auctions can get their desired order without having to trust an off-chain builder or move to their own app chain. Thus, they stay composable with everything else on Monad.

Not every preference can always be respected, so some transactions remain attackable. But as we showed above, a few simple practices go a long way in keeping applications and their users safe.

The design is not final, and we would like your input! The design, with its full implementation details, is up for discussion on the Monad forum (APES). If you build on Monad, tell us whether our design covers your application or whether there are extensions that you would require.

APES: Application-Preferred Execution Sequencing