Optimal Routing in AMM-based Ecosystems
Convex optimization, equilibrium prices and arbitrage in networks of Constant Function Market Makers.
Overview
Automated Market Makers replace traditional order books with deterministic trading rules defined by the reserves of liquidity pools. When several pools connect the same assets, a trader must decide not only which route to use, but how to split an order across several routes in order to minimize price impact.
This project develops a mathematical framework for optimal routing across networks of Constant Function Market Makers, with Uniswap V2 as the main example.
The routing problem
In a CFMM, the execution price depends on trade size. Large trades move the reserves of the pool and therefore suffer from slippage.
As a result, optimal execution is not a shortest-path problem: the cost of a route changes with the amount of flow sent through it. Splitting a trade across several pools can therefore improve execution by combining their available liquidity.
Convex formulation
The network is represented by a collection of CFMMs connected through a common set of tokens. Each pool is described by a concave trading function, local reserve vectors and proportional transaction fees.
The original routing problem contains nonlinear equality constraints representing the trading invariants of the pools. Relaxing these equalities into superlevel inequalities produces a convex optimization problem.
Under suitable monotonicity and saturation assumptions, the convex relaxation is exact: an optimal solution can be chosen to saturate the original CFMM trading constraints.
Main results
- Models routing across a network of CFMMs as a nonlinear flow problem with global token-balance constraints.
- Shows that relaxing the CFMM trading equalities yields a convex program when the trading functions are concave.
- Establishes conditions under which the convex relaxation recovers solutions satisfying the original trading invariants.
- Uses convex duality to interpret the dual variables as a global system of token prices and marginal values of pool liquidity.
- Connects optimal routing and arbitrage detection within the same optimization framework.
- Characterizes no-arbitrage through the existence of a global price system compatible with all local pool prices up to transaction-fee bands.
Duality and equilibrium prices
Convex duality gives the optimization variables a natural economic interpretation.
The multiplier associated with the global balance constraint behaves like a system of token prices, while the multipliers associated with the individual CFMM constraints measure the marginal value of liquidity in each pool.
Without transaction fees, local marginal prices must be compatible with a common global price vector, up to a pool-specific scale factor. With fees, this equality becomes a band of admissible prices.
Arbitrage and price consistency
Arbitrage detection appears as a special case of the routing problem. A network is arbitrage-free when there exists a strictly positive global price vector compatible with the marginal prices of every pool inside their transaction-fee bands.
This condition can also be read in terms of cycles: a sequence of swaps forming a closed cycle is profitable only when the product of local exchange rates exceeds the cumulative cost imposed by transaction fees.
In logarithmic coordinates, the set of compatible no-arbitrage price certificates forms a convex polyhedron, with the remaining degree of freedom corresponding to the choice of numeraire.
Numerical experiment
The numerical application considers a triangular Uniswap V2 network connecting WETH, USDC and DAI, with synthetic but economically realistic reserves and a fee parameter of 0.997.
A WETH-to-DAI order can be executed either directly through the WETH/DAI pool or indirectly through WETH/USDC and USDC/DAI. The optimizer determines how much of the order should be sent through each route.
10 WETH example
For an order of 10 WETH, the optimal allocation sends approximately 9.82 WETH through the direct route and 0.18 WETH through the indirect route.
The optimal split produces approximately 29,547.53 DAI, compared with 29,541.83 DAI for the best single route.
The gain is small for this order size — around 1.93 basis points — but increases as the trade becomes larger and price impact becomes more important.
For a 100 WETH order, optimal routing produces approximately 266,538.87 DAI, compared with 265,955.32 DAI through the best single route: an execution improvement of approximately 21.94 bps.
General solver
The simplified numerical example can be reduced to a one-dimensional allocation problem because the relevant routes are known in advance. To verify the general framework, I also solve the full convex routing problem directly using CVXPY and the CLARABEL conic solver.
The general program recovers the same optimum without assuming which routes should be used beforehand, up to numerical solver tolerance.
No-arbitrage certificate
The same network is constructed with mutually consistent marginal prices: WETH is valued at 3,000 units of either stablecoin and USDC and DAI are valued at parity.
The global price vector (3000, 1, 1) satisfies the no-arbitrage inequalities for all three pools. The two triangular swap cycles also have fee-adjusted multiplicative factors below one, confirming the absence of triangular arbitrage.
Interpretation
The main object is not the best route, but the best allocation of flow across the entire liquidity network.
Convex optimization makes this allocation tractable even when several pools and assets interact, while duality provides a direct connection between optimal execution, equilibrium prices and arbitrage.
Methods
The project combines convex analysis, nonlinear optimization, Lagrangian duality, KKT conditions, graph-based market modelling and numerical optimization.
The numerical implementation uses Python and CVXPY, with Uniswap V2 constant-product pools as the main empirical illustration.
Research context
This project was completed as a Master 1 research thesis in Mathematics and Applications at Aix-Marseille University under the supervision of Sebastian Müller.