Skip to main content

An Efficient Algorithm for Optimal Routing Through Constant Function Market Makers

  • Conference paper
  • First Online:
Financial Cryptography and Data Security (FC 2023)

Abstract

Constant function market makers (CFMMs) such as Uniswap have facilitated trillions of dollars of digital asset trades and have billions of dollars of liquidity. One natural question is how to optimally route trades across a network of CFMMs in order to ensure the largest possible utility (as specified by a user). We present an efficient algorithm, based on a decomposition method, to solve the problem of optimally executing an order across a network of decentralized exchanges. The decomposition method, as a side effect, makes it simple to incorporate more complicated CFMMs, or even include ‘aggregate CFMMs’ (such as Uniswap v3), into the routing problem. Numerical results show significant performance improvements of this method, tested on realistic networks of CFMMs, when compared against an off-the-shelf commercial solver.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Subscribe and save

Springer+ Basic
$34.99 /Month
  • Get 10 units per month
  • Download Article/Chapter or eBook
  • 1 Unit = 1 Article or 1 Chapter
  • Cancel anytime
Subscribe now

Buy Now

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Similar content being viewed by others

References

  1. Adams, H., Zinsmeister, N., Salem, M., Keefer, R., Robinson, D.: Uniswap V3 Core (2021). https://uniswap.org/whitepaper-v3.pdf

  2. Angeris, G., Agrawal, A., Evans, A., Chitra, T., Boyd, S.: Constant Function market makers: multi-asset trades via convex optimization. In: Tran, D.A., Thai, M.T., Krishnamachari, B. (eds.) Handbook on Blockchain. Springer Optimization and Its Applications, vol. 194, pp. 415–444. Springer, Cham (2022). https://doi.org/10.1007/978-3-031-07535-3_13

  3. Angeris, G., Chitra, T.: Improved price Oracles: constant function market makers. In: Proceedings of the 2nd ACM Conference on Advances in Financial Technologies, pp. 80–91. ACM (2020). https://doi.org/10.1145/3419614.3423251

  4. Angeris, G., Chitra, T., Evans, A.: When does the tail wag the dog? Curvature and market making. Cryptoecon. Syst. 2(1) (2022)

    Google Scholar 

  5. Angeris, G., Evans, A., Chitra, T., Boyd, S.: Optimal routing for constant function market makers. In: Proceedings of the 23rd ACM Conference on Economics and Computation, pp. 115–128 (2022)

    Google Scholar 

  6. Angeris, G., Kao, H.T., Chiang, R., Noyes, C., Chitra, T.: An analysis of uniswap markets. Cryptoecon. Syst. (2020). https://doi.org/10.21428/58320208.c9738e64. https://cryptoeconomicsystems.pubpub.org/pub/angeris-uniswap-analysis

  7. ApS, M.: MOSEK Optimizer API for Python 9.1.5. https://docs.mosek.com/9.1/pythonapi/index.html

  8. Bertsekas, D.: Nonlinear Programming, 3rd edn. Athena Scientific (2016)

    Google Scholar 

  9. Bezanson, J., Edelman, A., Karpinski, S., Shah, V.: Julia: A fresh approach to numerical computing. SIAM Rev. 59(1), 65–98 (2017). https://doi.org/10.1137/141000671

  10. Boyd, S., Vandenberghe, L.: Convex Optimization, 1st edn. Cambridge University Press (2004)

    Google Scholar 

  11. Byrd, R.H., Lu, P., Nocedal, J., Zhu, C.: A limited memory algorithm for bound constrained optimization. SIAM J. Sci. Comput. 16(5), 1190–1208 (1995)

    Article  MathSciNet  MATH  Google Scholar 

  12. Coey, C., Kapelevich, L., Vielma, J.P.: Solving natural conic formulations with Hypatia.jl (2021)

    Google Scholar 

  13. Danos, V., Khalloufi, H.E., Prat, J.: Global order routing on exchange networks. In: Bernhard, M., et al. (eds.) FC 2021. LNCS, vol. 12676, pp. 207–226. Springer, Heidelberg (2021). https://doi.org/10.1007/978-3-662-63958-0_19

    Chapter  Google Scholar 

  14. Dantzig, G.B., Wolfe, P.: Decomposition principle for linear programs. Oper. Res. 8(1), 101–111 (1960)

    Article  MATH  Google Scholar 

  15. Dunning, I., Huchette, J., Lubin, M.: JuMP: a modeling language for mathematical optimization. SIAM Rev. 59(2), 295–320 (2017). https://doi.org/10.1137/15M1020575. https://epubs.siam.org/doi/10.1137/15M1020575

  16. Egorov, M.: StableSwap - efficient mechanism for Stablecoin liquidity, p. 6. https://www.curve.fi/stableswap-paper.pdf

  17. Legat, B., Dowson, O., Garcia, J., Lubin, M.: MathOptInterface: a data structure for mathematical optimization problems (2021). https://doi.org/10.1287/ijoc.2021.1067. http://pubsonline.informs.org/doi/10.1287/ijoc.2021.1067

  18. Martinelli, F., Mushegian, N.: Balancer: a non-custodial portfolio manager, liquidity provider, and price sensor (2019)

    Google Scholar 

  19. Morales, J.L., Nocedal, J.: Remark on “algorithm 778: L-BFGS-B: Fortran subroutines for large-scale bound constrained optimization”. ACM Trans. Math. Softw. (TOMS) 38(1), 1–4 (2011)

    Google Scholar 

  20. O’Donoghue, B., Chu, E., Parikh, N., Boyd, S.: Conic optimization via operator splitting and homogeneous self-dual embedding. J. Optim. Theor. Appl. 169(3), 1042–1068 (2016). https://doi.org/10.1007/s10957-016-0892-3

  21. Revels, J., Lubin, M., Papamarkou, T.: Forward-mode automatic differentiation in Julia. arXiv arXiv:1607.07892 [cs.MS] (2016)

  22. Wang, Y., Chen, Y., Wu, H., Zhou, L., Deng, S., Wattenhofer, R.: Cyclic arbitrage in decentralized exchanges. In: Companion Proceedings of the Web Conference 2022, Virtual Event, Lyon France, April 2022, pp. 12–19. ACM (2022). https://doi.org/10.1145/3487553.3524201

  23. Zhang, Y., Chen, X., Park, D.: Formal specification of constant product (xy = k) market maker model and implementation (2018)

    Google Scholar 

  24. Zhu, C., Byrd, R.H., Lu, P., Nocedal, J.: Algorithm 778: L-BFGS-B: Fortran subroutines for large-scale bound-constrained optimization. ACM Trans. Math. Softw. (TOMS) 23(4), 550–560 (1997)

    Article  MathSciNet  MATH  Google Scholar 

Download references

Acknowledgements

We thank Francesco Iannelli and Jiahao Song for contributing to the package’s documentation and the Financial Cryptography 2023 reviewers for helpful comments on this paper.

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Theo Diamandis .

Editor information

Editors and Affiliations

A Closed Form Solutions

A Closed Form Solutions

Here, we cover some of the special cases where it is possible to analytically write down the solutions to the arbitrage problems presented previously.

Geometric Mean Trading Function. Some of the most popular swap markets, for example, Uniswap v2 and most Balancer pools, which total over $2B in reserves, are geometric mean markets (5) with \(n=2\). This trading function can be written as

$$ \varphi (R) = R_1^wR_2^{1-w}, $$

where \(0 < w < 1\) is a fixed parameter. This very common trading function admits a closed-form solution to the arbitrage problem (8). Using (12), we can write

$$ f_1(\delta _1) = R_2\left( 1- \left( \frac{1}{1 + \gamma \delta _1/R_1}\right) ^\eta \right) $$

where \(\eta = w/(1-w)\). (A similar equation holds for \(f_2\).) Using (15) and (16), and defining

$$ \delta _1 = \frac{R_1}{\gamma }\left( \left( \eta \gamma \frac{\nu _2}{\nu _1}\frac{R_2}{R_1}\right) ^{1/(\eta +1)} - 1\right) , $$

we have that \(\delta _1^\star = \max \{\delta _1, 0\}\) is an optimal point for (14). Note that when we take \(w = 1/2\) then \(\eta = 1\) and we recover the optimal arbitrage for Uniswap given in [6, App. A].

Bounded Liquidity Variation. The bounded liquidity variation (4) of the product trading function satisfies the definition of bounded liquidity given in §4.1, whenever \(\alpha , \beta > 0\). We can write the forward exchange function for the bounded liquidity product function (4), using (12), as

$$ f_1(\delta ) = \min \left\{ R_2, \frac{\gamma \delta (R_2 + \beta )}{R_1 + \gamma \delta + \alpha }\right\} $$

The ‘min’ here comes from the definition of a CFMM: it will not accept trades which pay out more than the available reserves. The maximum amount that a user can trade with this market, which we will write as \(\delta _1^-\), is when \(f_1(\delta _1^-) = R_2\), i.e.,

$$ \delta _1^- = \frac{1}{\gamma }\frac{R_2}{\beta }(R_1 + \alpha ). $$

(Note that this can also be derived by taking \(f_1(\delta _1) = R_2\) in (12) with the invariant (4).) This means that

$$ f_1^-(\delta _1^-) = \gamma \frac{\beta ^2}{(R_1 + \alpha )(R_2 + \beta )}, $$

is the minimum supported price for asset 1. As before, a similar derivation yields the case for asset 2. Writing \(k=(R_1+ \alpha )(R_2 + \beta )\), we see that we only need to solve (14) if the price \(\nu _1/\nu _2\) is in the active interval (17),

$$\begin{aligned} \frac{\gamma \beta ^2}{k} < \frac{\nu _1}{\nu _2} < \frac{k}{\gamma \alpha ^2}. \end{aligned}$$
(18)

Otherwise, we know one of the two ‘boundary’ solutions, \(\delta _1^-\) or \(\delta _2^-\), suffices.

Rights and permissions

Reprints and permissions

Copyright information

© 2024 International Financial Cryptography Association

About this paper

Check for updates. Verify currency and authenticity via CrossMark

Cite this paper

Diamandis, T., Resnick, M., Chitra, T., Angeris, G. (2024). An Efficient Algorithm for Optimal Routing Through Constant Function Market Makers. In: Baldimtsi, F., Cachin, C. (eds) Financial Cryptography and Data Security. FC 2023. Lecture Notes in Computer Science, vol 13951. Springer, Cham. https://doi.org/10.1007/978-3-031-47751-5_8

Download citation

  • DOI: https://doi.org/10.1007/978-3-031-47751-5_8

  • Published:

  • Publisher Name: Springer, Cham

  • Print ISBN: 978-3-031-47750-8

  • Online ISBN: 978-3-031-47751-5

  • eBook Packages: Computer ScienceComputer Science (R0)

Publish with us

Policies and ethics