Optimal Routing in AMM-based Ecosystems
A mathematical study of optimal trade execution in networks of Automated Market Makers.
Overview
This project studies optimal routing in ecosystems of Automated Market Makers, with a focus on Constant Function Market Makers such as Uniswap V2. The central question is how to split a trade across several liquidity pools in order to obtain the best execution when prices move with trade size.
The project begins with the basic mechanics of an AMM: reserves, constant-product invariants, marginal price, effective price, slippage, and local arbitrage. It then moves from a single pool to a network of pools, where a trade can be executed directly, indirectly, or split across several routes.
Main idea
In an AMM, the price of a trade is not fixed. It depends on the size of the order relative to the reserves of the pool. This creates slippage: large orders receive a worse average execution price than small orders. Routing is therefore not only a graph problem, but a nonlinear optimization problem.
The key mathematical point is that, under suitable assumptions on the trading functions of the pools, the routing problem can be relaxed into a convex optimization problem. This makes it possible to characterize optimal trade execution and to connect routing with arbitrage detection.
Contributions
- Introduces the mechanics of Uniswap V2 through marginal price, effective price, slippage, and constant-product reserves.
- Formulates optimal routing in a network of CFMMs as an optimization problem over local pool transactions and global token balances.
- Shows how relaxing the trading constraints yields a convex program under standard concavity and monotonicity assumptions.
- Uses convex duality to interpret Lagrange multipliers as equilibrium token prices and marginal values of pool liquidity.
- Explains arbitrage detection as a special case of the routing framework.
- Provides a reproducible numerical experiment on a triangular WETH / USDC / DAI network.
Methods
The project combines mathematical finance, convex optimization, and decentralized market microstructure. It uses CFMM trading functions, convex relaxations, KKT conditions, duality, price certificates, and cycle-based no-arbitrage conditions. The numerical section implements the routing problem in Python and compares direct routing, indirect routing, and optimal split routing.
Numerical illustration
The numerical experiment considers a synthetic but realistic network of three Uniswap V2 pools: WETH/USDC, WETH/DAI, and USDC/DAI. For a WETH-to-DAI trade, the optimal solution sends most of the order through the direct route and a small fraction through the indirect route. This split slightly reduces total slippage and becomes more valuable as the trade size increases.
For a 10 WETH order, the optimal split improves execution relative to the best single route. For larger orders, the benefit of splitting grows, reaching around 22 basis points in the 100 WETH example. The experiment also verifies the absence of arbitrage by exhibiting a compatible global price certificate.
Current status
This project was developed as a Master 1 research project in mathematics. A natural continuation would be to extend the numerical implementation to larger pool networks, include gas costs and fixed execution costs, and test the routing algorithm on real on-chain liquidity data.
Read the paper