0% found this document useful (0 votes)
31 views8 pages

2402.06731v1

Uploaded by

ypnxpn6zxd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views8 pages

2402.06731v1

Uploaded by

ypnxpn6zxd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Closed-form solutions for generic N -token AMM arbitrage

Matthew Willetts and Christian Harrington


QuantAMM.fi

February 2024
arXiv:2402.06731v1 [q-fin.TR] 9 Feb 2024

Abstract

Convex optimisation has provided a mechanism to determine arbitrage trades on automated


market markets (AMMs) since almost their inception. Here we outline generic closed-form solutions
for N -token geometric mean market maker pool arbitrage, that in simulation (with synthetic
and historic data) provide better arbitrage opportunities than convex optimisers and is able to
capitalise on those opportunities sooner. Furthermore, the intrinsic parallelism of the proposed
approach (unlike convex optimisation) offers the ability to scale on GPUs, opening up a new
approach to AMM modelling by offering an alternative to numerical-solver-based methods. The
lower computational cost of running this new mechanism can also enable on-chain arbitrage bots
for multi-asset pools.

1 Introduction

Automated market makers (AMMs), of which most decentralized exchanges (DEXs) are examples,
are some of the most important Decentralized Finance (DeFi) infrastructure this is. In these systems
Liquidity providers deposit assets (LPs) that is used to enable traders to exchange tokens against the
deposited capital. These systems provide close-to-market prices in their trades as there is an economic
incentive for them to do so: quoted prices depend inversely on the pool’s reserves, meaning that if
the quoted price for an asset is too high a trader can gain by extracting that asset out of the pool
(exchanging something else in) and vice versa if the quoted price is too low the trader can gain from
trading that asset out of the pool. These trades are arbitrage trades, are carried out by the pool’s
arbitrageurs, and have the effect of bringing the pool’s quoted prices closer to the current market
prices.

We are interested in modelling the arbitrage opportunities that arise on multi-token (i.e. N > 2)
geometric mean market makers, in particular finding the optimum trade for the arbitrageur to perform
to maximise their return at current market prices. This takes us beyond swaps, as in general the
arbitrage opportunity requires the trading of multiple different assets into or out of the pool, or both.

This problem can be attacked numerically using linear programming/convex optimisation [1]. Here we
take a different approach, deriving closed-form analytical expressions for the optimal arbitrage trade.
Fundamentally, these closed-form expressions for the optimal multi-asset arbitrage trade enable new
and fine-grained mathematical analysis of the properties of the trade. These closed form expressions
are particularly useful in the context of Temporal-function Market Making (TFMM) [2], where pools’
weights change with time, as they enable modelling of the arbitrage trade (and resulting changes in
pool reserves) through which one can take gradients for the purpose of optimisation.

Unlike a linear program, these closed form expressions require an a priori known amount of arithmetic
operations to calculate, and for reasonable numbers of assets (N ⪅ 7) are substantially quicker to
perform than convex optimisation (see Figure 2).

1
2 Optimal Multi-Asset Trades in the presence of fees

The pool contains N tokens, so a trader has a vector ∆ of token amounts being traded to the pool
for another set of tokens Λ, where entries in each are ≥ 0. Where ∆i > 0, Λi = 0: tokens cannot be
traded for themselves.1 With fees (1 − γ), for a trade to be accepted it must be the case that
N
Y wi
(Ri + γ∆i − Λi ) ≥ k. (1)
i=1
PN QN
where R = {Ri }i=1,...,N , w = {wi }i=1,...,N , ∀i, 0 < wi < 1, i=1 wi = 1 and k = i=1 Riwi . While
QN w
any trade that increases i=1 (Ri + γ∆i − Λi ) i above k is accepted, a trader does better if their
2
trade maintain k. In constructing the optimal arbitrage trade, first we have to know which tokens to
trade in and which to withdraw. We will return to that question in Section 3.2.

It is notationally simpler if we combine the inwards and outwards trade legs into one variable Φ :=
∆ − Λ. If token i is not being traded, Φi = 0. We introduce a trade signature s of length N ,
si ∈ {−1, 0, 1} which encoders whether a token is being extracted from the pool, not being traded,
or is being added to the pool. We can index over the active tokens in the pool, those with si ̸= 0.
We defineP the set A = {i ∈ [N ]|si ̸= 0}, and we define an active-token normalised set of weights
w̆i := wi / j∈A wj . We also introduce an auxiliary variable d = Is=1 for application of fees.3

We can rewrite Eq (1) as


w̆i
Y γ di Φi
1+ = 1, (2)
Ri
i∈A

a reduced form of the trading function (Appendix B). The we can form the multi-asset objective as
 
Y dj
w̆j
X γ Φj
L=− (mp,i Φi ) − λ  1+ − 1 . (3)
Rj
i∈A j∈A

Taking partial derivatives w.r.t. Φi and setting to 0


w̆i w̆j
γ dj Φj
 
γ di Φi
γ di
Q
w̆i 1 + Ri j̸=i 1+ Rj
∂L j∈A
= −mp,i − λ   =0 (4)
∂Φi 1+ γ di Φi
Ri
w̆j
QN  γ dj Φj
w̆i γ di j=1 1+ Rj
⇒ −mp,i − λ   =0 (5)
γ di Φi
1+ Ri
 
γ di Φi
mp,i Ri 1 + Ri
⇒λ=− (6)
w̆i γ di
This gives us |A| non-degenerate equations each solving for λ. Putting any two into equality,
γ dj Φ
 di
  
mp,i Ri 1 + γ RiΦi mp,j Rj 1 + Rj j
= (7)
w̆i γ di w̆j γ dj
dj dj
γ di Φi
   
γ Φj w̆j γ mp,i Ri
⇒ 1+ = 1+ (8)
Rj w̆i γ di mp,j Rj Ri
 N  N
1 Equivalently, ∃N ∈ N, N ≥ 2 : ∀Λ ∈ R+ 0 , ∆ ∈ R+ 0 , ∆ · Λ = 0.
2 The inequality trading constraint is of use, however, as it enables convex optimisation (which demanding equality

does not), with the obtained solution lying on the level set for which equality is maintained [1].
3 This turns our problem into a mixed-integer convex program if we have our trading-function invariance imposed via

an inequality, c.f. the above footnote.

2
We can now sub in Eq (8) to Eq (2) for all j ̸= i and rearrange (Appendix A) to obtain
 
1−w̆ w̆
w̆i γ di
  i 
Y mp,j  j

∀i ∈ A, Φi = γ −di 
 
k̆ mp,i d
− Ri 
, (9)
w̆j γ j
j̸=i
j∈A

where k̆ = i∈A Riw̆i This gives the optimal arbitrage trade for a multi-token/basket trade for geo-
Q
metric mean market makers.

Comment on derivation When first looking at these problems of optimal trades, it can seem that
closed-form solutions are not possible as we have to impose inequality constraints on the trade (e.g.
that Φi > 0 where si = 1 in Eq (2)) meaning we have (Karush–Kuhn–Tucker) KKT conditions.

Our derivations works because a) we externally chose the trade signature, b) the signature allows for
tokens to be untouched in the trade and c) the solutions to the full problem satisfy complementary
slackness. Together these mean that we do not have to follow a KKT formulation, instead we get
closed-form expressions via Lagrange multipliers.

Finally, note that in the case that fees are not present, the modelling task is much simpler [2]. Various
approaches and results can then be layered on top of the zero-fee-case model (e.g. the bound in [3]).
Here we are interested in full treatment of the effect of fees.

2.1 Finding the optimal trade signature

To use our results, it seems we need a priori the arbitrage trade’s signature s. There is a simple fix:
run through all possible variants of s calculating Φ(s), and for each Φ(s) calculate its return to the
PN
arbitrageur − i=1 (mp,i Φi ) and check that it fulfils the trade invariant Eq (2). The best such trade is
then the optimal. If no checked trade fulfils Eq (2) while having a return > 0, the best trade is Φ = 0
and we are in the no-arb region.
PN
This computationally-intensive parts of this approach (calculating Φ(s), − i=1 (mp,i Φi ) and checking
Eq (2)) are embarrassingly-parallel over different settings of s.

What are the sets of s that one has to check for a given
pool size N ? We denote set of valid trade signatures S(N ).
The upper limit for |S(N )|, the number of signatures a pool
can have, is 3N (each entry si can be one of {−1, 0, 1}), but
not all combinations are valid. A signature s has to have at
least one entry of 1 and at least one −1 (at least one token
has to be traded for at least one other token). For pools
with N < 6 or so, these restrictions mean the number of
valid signatures is < 80% of the naive 3N calculation. We
plot |S(N )| as a fraction of 3N in Figure 1. As N increases,
this ratio tends to one. For reference, |S(N = 3)| = 12 and
|S(N = 4)| = 50.

Figure 1: Number of valid trade signa-


Another possibility is create a computationally-less-
tures as a fraction of all possible uncon-
intensive heuristic we can use to calculate the trade sig-
strained signatures.
nature/trade direction–though for low N -token pool run-
ning through all variants is still considerably faster than convex optimisation even prior to any other
speedups. An example of such a heuristic is provided in Appendix A.1.

3
3 Simulation results

Fundamentally, arbitrage trades are how AMM pools’ reserves update. In modelling our results in
silico and benchmarking against existing (numerical) approaches we are interested in both the run
time of our approach and in how high-fidelity it is in finding arb opportunities. Both these areas
matter whether one is interested in using our approach for simulating AMM pool rebalancing (for
example in a backtest) or for using it to find and act on arbitrage opportunities in real time.

Figure 2: Logarithmic plot of the time taken to calculate an optimal trade as a function of number of
pool assets N . All experiments were run in python, using CVXPY for convex optimisation. We used
a MacBook Air M2 for all CPU results and a workstation with a GPU with compute capability 8.9.

3.1 Run time

For large values of N , standard convex solvers scale better than the closed form solutions, given the
naive brute force methodology for finding the signature of the optimal trade requires roughly 3N
computations (though new heuristics may remove the advantage). As demonstrated in Figure 2, the
scalability of the closed form solution can be drastically helped because unlike current convex solutions,
it can be parallelised (for both CPUs and GPUs). While this does only delay the point at which convex
solvers become faster, even with a single GPU it delays the point at an N -token number that is rarely,
if at all, seen in contemporary AMMs.

3.2 Effectiveness

We create simulated, independent, potential arbitrage opportunities. For each we start with a pool
initially at equilibrium (pool prices match market prices) and then randomly sample new market prices
that deviate slightly, which have a chance of pushing the pool out of the no-arb region.
w
mp,i ∼ Uniform(0, 1), Rinitial = V0 , ui ∼ Uniform(0, 1), m∗p = mp + au,
mp

where V0 is the initial value of the pool in the numéraire of mp , each trial’s w is chosen to be close
to uniform weights wi = N1 (as convex optimisation is less stable when w is close to one-hot) and
γ = 0.05.

We perform 120,000 independent random trials, and for each record the result of convex optimisation
and our approach. Figure 3 compares the N -token closed to convex results. This demonstrate the
superiority of the N -token approach across pools to find arbitrage opportunities.

4
Figure 3: Comparing arbitrage returns from our approach as a function of the number of pool assets
against numerical convex optimisation.

3.3 Duelling arbitrageurs

If you run multiple separate simulations of an N -token pool over time, in each updating the pool
according to the arbitrage trades produced by a particular arbitrage algorithm, the algorithm that is
less good at constructing arbitrage trades will often show greater profit. Why is this unexpected result
found? In a less-than-ideal system, the arbitrage opportunity is only detected and acted on further
away from the no-arb region of the pool.

As there is no competition between arbitrageurs (we are comparing separate runs), the less-good al-
gorithm wins by ‘farming’ the pool, letting the arbitrage opportunity build up and up before eventually
being reaped.

(a) Asset prices for the basket over time (plotted (b) Cumulative arbitrageur profits as a fraction of
indexed to initial prices). initial pool value.

Figure 4: Comparing arbitrage returns from competition between a simulated arbitrageur our approach
and one using numerical solutions from convex optimisation. After every new market price comes in
the convex optimiser arbitrageur gets priority (for free) in trading with the pool yet performs worse.

So in comparing multiple arbitrage algorithms over time we have to have them compete. Here we
compare our closed-form approach to convex optimisation (performed using CVXPY) on a historical
backtest of a CFMM pool with N = 3 run on a basket of ETH, BTC and DAI with uniform weights
wi = 13 from June 2021 to July 2022. γ = 0.003 and initial pool value was 1m USD. We plot the
results in Figure 4.

5
To give the convex-solver-arbitrageur an edge over the closed form algorithm, the convex-solver-arb is
provided the top transaction in the block every time, for free. The closed form algorithm reaps the
arbitrage opportunity a considerable number of times before convex-optimisation arbitrageur can.

4 Conclusions

We have described the closed-form expressions for the optimal arbitrage trade for N -token pools. In
a range of experiments we have demonstrated the speed and arbitrage performance advantages of this
new closed-form approach over current approaches.

Coupling this with the ability to run the approach at even higher speeds on GPUs (due to its paral-
lelisability), and to use take gradients through the process (enabling more advanced machine learning
techniques for AMM strategy training [2]) means that this approach may find use both for practi-
tioners interested in performing arbitrage trades as well as those interested primarily in simulating &
modelling AMM pools.

There are on-chain implications too that are significant, given how this technique could be used not
only for on-chain arbitrage bots that have the direct ability to structure the arbitrage trade (via access
to the current state of the pool) in the same transaction that the trade is performed, but also for
DEX order routers and DEX aggregator routers. TSTORE advances, intelligent caching, and also
signature detection heuristics being developed could each significantly reduce the computational cost
of obtaining such a solution on-chain.

References
[1] Guillermo Angeris, Akshay Agrawal, Alex Evans, Tarun Chitra, and Stephen Boyd. Constant
function market makers: Multi-asset trades via convex optimization. 2021.

[2] QuantAMM team. Temporal-function market making, 2023.


[3] Guillermo Angeris, Alex Evans, and Tarun Chitra. When does the tail wag the dog? curvature
and market making. 2020.
[4] QuantAMM team. Temporal-function market making litepaper, 2023.

6
Technical Appendix

A Derivation of multi-asset optimal arbitrage trade

We sub in Eq (8) to Eq (2) for all j ̸= i, i, j ∈ A:


w̆i Y  w̆j
γ di Φi w̆j γ dj mp,i Ri γ di Φi
 
1= 1+ 1 + (A.10)
Ri w̆i γ di mp,j Rj Ri
j̸=i
j∈A
w̆ w̆j
γ di Φi Y mp,i Ri j Y w̆j γ dj
   
⇒1= 1+ (A.11)
Ri w̆i γ di mp,j Rj
j̸=i j̸=i
j∈A j∈A
1−w̆i Y  w̆j
γ di Φi w̆j γ dj
 
mp,i Ri
⇒1= 1+ (A.12)
Ri w̆i γ di mp,j Rj
j̸=i
j∈A
1−w̆i Y  w̆j
γ di Φi w̆i γ di
  
mp,j Rj
⇒ 1+ = (A.13)
Ri mp,i Ri w̆j γ dj
j̸=i
j∈A
1−w̆i Y  w̆j
γ di Φi w̆i γ di
  
mp,j Rj
⇒ 1+ = (A.14)
Ri mp,i Ri w̆j γ dj
j̸=i
j∈A
1−w̆i w̆j
γ di Φi w̆i γ di
   
k̆ Y mp,j
⇒ 1+ = (A.15)
Ri mp,i Ri Riw̆i j̸=i w̆j γ dj
j∈A
di
 1−w̆i Y 
di
w̆j
γ Φi k̆ w̆i γ mp,j
⇒ = −1 (A.16)
Ri Ri mp,i w̆j γ dj
j̸=i
j∈A
 
1− w̆ w̆j
w̆i γ di
  i Y  
mp,j
⇒ ∀i ∈ A, Φi = γ −di 
 
k̆ − Ri 
, (A.17)
mp,i w̆j γ dj
j̸=i
j∈A

where k̆ := i∈A Riw̆i . There is also a slightly more symmetric form, which however is slightly less
Q
appropriate for implementation in a resource-constrained setting:
 
 w̆i γ di 1−w̆i Y mp,j Rj w̆j
   
∀i ∈ A, Φi = γ −di Ri 

 mp,i Ri d
− 1 (A.18)
w̆j γ j 
j̸=i
j∈A

Amplified Liquidity This approach also naturally extends to multi-token pools that make use of
amplified liquidity [4]. There a pool’s virtual reserves are R̆i =Q νRi where ν is a (potentially
Q varying
w w
from block to block) scalar and the trading invariant is that i (νRi + γ∆i − Λ) i = ν i (Ri ) i .
Applying the mapping Ri → νRi to Eq (A.17), we get the optimal arbitrage trade for these pools:
 
1−w̆ w̆j
w̆i γ di
  i Y  
mp,j
∀i ∈ A, Φi = γ −di 
 
ν̆ k̆ mp,i d
− νRi
, (A.19)
w̆j γ j 
j̸=i
j∈A
P
w̆i
where ν̆ := ν i∈A , with the analogue of Eq (A.18) similarly following.

7
A.1 Heuristic for Trade Signature

A heuristic that works reasonably well for finding a workable trade signature s in the close vicinity
of the no-arb-region boundary is to look for, in effect, swap-level arb opportunities between each pair
within the pool and use those to form s. Do as follows:

Find the zeros Construct the ratio between the (zero-fees) quoted prices of the pool (in the same
w
numéraire as the market prices mp ) and the market prices: ℓ := V Rm p
(where all operations are done
PN
elementwise) and V = i=1 mp,i Ri . Then construct the ‘price quotient matrix’ Γ, where each entry
PN PN
is Γi,j = ℓi /ℓj . For any tokens i for which j=1 IΓi,j >γ −1 = j=1 IΓi,j <γ = 0, si = 0.

Active trade direction For tokens i with si ̸= 0, simply have si = 1 if ℓi > 1 and si = −1 if ℓi < 1.

B Reduced form of Trading Function for G3Ms

Starting with Eq (1) and making the substitutions from {∆, Λ} to {Φ, d}:
N
Y w i
Ri + γ di Φi ≥ k. (B.20)
i=1

QN wi
The most profitable trade will be when equality is reached. Divide by i=1 (Ri ) = k to obtain
N wi
Y Ri + γ di Φi
w =1 (B.21)
i=1
(Ri ) i
N  wi
Y γ di Φi
1+ = 1. (B.22)
i=1
Ri
 
γ di Φi
As Φi = 0 for i ∈/ A, 1+ Ri = 1 for i ∈
/ A, so in the product we can consider only the set of
indices A, giving us wi
Y γ d i Φi
⇒ 1+ = 1. (B.23)
Ri
i∈A
P th
We can now take the j∈A wj root, so we obtain
wi
Y γ di Φi
P
j∈A wj
⇒ 1+ =1 (B.24)
Ri
i∈A
w̆i
Y γ d i Φi
⇒ 1+ =1 (B.25)
Ri
i∈A

as required.

You might also like