p^{*})\\}_{i=1}^{d}$ and $\\{\ell_{i}\\}_{i=1}^{d}$ such the domain of $\psi$
is split into $d$ regions with $d+1$ endpoints, defined as
$\bm{\beta}(\bm{\ell};i^{*})=\\{\beta_{i}(\bm{\ell};i^{*})\\}_{i=1}^{d+1}$.
These regions correspond to the exchange rate ticks $\\{p_{i}\\}_{i=1}^{d+1}$
in the pool. If the current exchange rate is $p^{*}$ contained in the current
tick $i^{*}$, we calculate $\beta_{i}(\bm{\ell};i^{*})$ as

$\beta_{i}(\bm{\ell};i^{*}):=\begin{cases}-\sum_{j=i+1}^{i^{*}-1}B_{j},&1\leq
i\leq i^{*}-1,\\\
\sum_{j=i^{*}}^{i}\ell_{j}(\sqrt{p_{j+1}}-\sqrt{p_{j}}),&i^{*}\leq i\leq
d.\end{cases}$ (A.8)

We derive the deposit limits $\ell_{j}(\sqrt{p_{j+1}}-\sqrt{p_{j}})$ from
Equation A.6 and the withdrawal limits $-B_{j}$ from the requirement that we
cannot withdraw more of token B from tick $j$ than what the tick contains.

Now that we have defined the quantities $\beta_{i}(\bm{\ell};i^{*})$, we will
at times suppress the dependence on $\bm{\ell}$ and $p^{*}$ (as we have done
for $A_{i}$ and $B_{i}$) for ease of reading. Again using Equation A.6, we can
now define each of the piecewise function components $\psi^{i}$ as

$\psi^{i}(x,\bm{\ell},p^{*}):=\begin{cases}-\ell_{i}(1/\sqrt{p_{i}}-1/\sqrt{p_{i+1}}),&\
x\leq\beta_{i}<0,\\\
A_{i}+\ell_{i}/\sqrt{p_{i+1}}-\dfrac{(\ell_{i})^{2}}{B_{i}+(x-(\beta_{i+1}\wedge
0))+\ell_{i}\sqrt{p_{i}}},&\ x\in(\beta_{i},\beta_{i+1}],x<0,\\\ 0,&\
\beta_{i+1}<x<0\text{ or }0<x<\beta_{i},\\\
A_{i}+\ell_{i}/\sqrt{p_{i+1}}-\dfrac{(\ell_{i})^{2}}{B_{i}+(x-(\beta_{i}\vee
0))+\ell_{i}\sqrt{p_{i}}},&\ 0<x\in[\beta_{i},\beta_{i+1}),\\\ A_{i},&\
0<\beta_{i+1}\leq x.\end{cases}$ (A.9)

A graph of $\psi$ for a simple pool is given in Figure 22, where the multiple
colors represent each of the piecewise functions $\psi^{i}$ which make up the
full-pool exchange function. We summarize the various pool parameters and
variables in Table 6.

The first-time reader may find the five different cases overwhelming, but the
intuition is relatively straightforward. Token B is exchanged for token A by
“using up” the available liquidity in each tick, working outward from $i^{*}$
in a direction corresponding to the sign of $x$. If there is more liquidity
available in a tick than there is token B left to exchange, Equation A.6
determines the amount of token A which corresponds to that amount of token B.
This tick is the new $i^{*}$. If a swap doesn’t “reach” a tick $i$, its
$\psi^{i}$ returns 0.

After each swap, $\bm{\ell}$ remains fixed but $\bm{A}$ and $\bm{B}$ have
changed to $\bm{A}^{\text{new}}(\xi,\bm{\ell},i^{*})$ and
$\bm{B}^{\text{new}}(\xi,\bm{\ell},i^{*})$. The new pool exchange rate can be
calculated via Equation A.7 where $i^{*}$ now refers to the tick in which the
swap ended, and which now contains $p^{*}_{\text{new}}$. The function domain
$\bm{\beta}$ is also updated to $\bm{\beta}^{\text{new}}$ which corresponds
with $\bm{A}^{\text{new}}$, $\bm{B}^{\text{new}}$, and $p^{*}_{\text{new}}$.
The amount of fees earned by all LPs with active liquidity in tick $i$ when a
swap of size $\xi$ is executed is given by

$\phi(i,\xi;\bm{\ell},p^{*}):=\gamma|B^{\text{new}}_{i}(\xi,\bm{\ell}_{i};p^{*})-B_{i}(\bm{\ell}_{i};p^{*})|.$
(A.10)

This function essentially determines how many tokens of B were added or
removed from each tick in the pool, and calculates the fees per tick
accordingly.

A full example is worked through in Section A.2.

Figure 22: An example of $\psi(x,\bm{\ell},p^{*})$ when $\bm{\ell}=[100,100,100,100,100,100]$ and the exchange rate ticks are $[1,1.21,1.44,1.69,2.25,2.56]$. Pool Characteristics |  Definition
---|---
$\gamma\in(0,1)$ |  Fixed pool transaction fee.
$\\{p_{i}\\}_{i=1}^{d+1}$ |  The exchange rates associated with ticks in the pool, also fixed.
Pool Variables |  Definition
$p^{*}\in\mathbb{R}_{+}$ |  Pool exchange rate which evolves with swaps.
$i^{*}\in[d]$ |  Index corresponding to $p^{*}$.
$\bm{\ell}\in\mathbb{R}^{d}_{+}$ |  Liquidity distribution which determines the CPMM for each tick.
$\\{A_{i}(\ell_{i},p^{*})\\}_{i=1}^{d}\in\mathbb{R}^{d}_{+}$ |  Token A distribution.
$\\{B_{i}(\ell_{i},p^{*})\\}_{i=1}^{d}\in\mathbb{R}^{d}_{+}$ |  Token B distribution.
$\\{\beta_{i}(\bm{\ell};i^{*})\\}_{i=1}^{d+1}\in\mathbb{R}^{d+1}_{+}$ |  Domains of $\psi^{i}$ functions in terms of token B.
Table 6: Model parameters and variables, consolidated.

#### Example: Toy Swap Calculation

Now, we will provide two examples of the calculations for the exchange rate of
a swap transaction using the CPMM algorithm. We will start with a pool
containing a distribution of token A and B as displayed in table Table 7. The
current exchange rate in the pool will be $p^{*}=1.25$ and this corresponds to
tick 2, so the “active” tick is $i^{*}=2$. For simplicity, we will set the
pool transaction fee $\gamma=0$.

Tick | 0 | 1 | $i^{*}=2$ | 3 | 4 | 5
---|---|---|---|---|---|---
Token A | 0 | 0 | 2.154 | 6.25 | 6.25 | 6.25
Token B | 10 | 10 | 6.553 | 0 | 0 | 0
$[\beta_{i},\beta_{i+1})$ | [-26.553, -16.553) | [-16.553, -6.553) | [-6.553, 3.542) | [3.542, 14.917) | [14.917, 28.042) | [28.042, 43.042)
$\ell_{i}$ | 100 | 100 | 100.956 | 113.75 | 131.25 | 150
$[p_{i},p_{i+1})$ | [1, 1.21) | [1.21, 1.44) | [1.44, 1.69) | [1.69, 1.96) | [1.96, 2.25) | [2.25, 2.56)
Table 7: The initial setup of a liquidity pool with $p^{*}=1.5$ B/A and
$i^{*}=2$.

In the first example, we will demonstrate a scenario where the swap does not
“cross ticks” - that is, the composition of the liquidity in the pool changes
and the exchange rate $p^{*}$ changes, but $i^{*}$ does not change. In Figure
23, we provide an intuitive illustration of what is happening in the pool
after a swap where $\beta_{i^{*}}\leq\xi\leq\beta_{i^{*}+1}$.

(a) An example initial liquidity distribution with $i^{*}=2$.

(b) The result after a swap of $-B^{*}<\xi<0$, $i^{*}=2$ still.

Figure 23: We illustrate an initial liquidity distribution (left) and the
result of a swap where $B^{*}<\xi<0$ (right), meaning that token B is
withdrawn and token A is deposited.

For a more concrete example, say that we choose $\xi=-5$ (the swapper wishes
to withdraw 5 tokens of B from the pool). Looking at Table 7, we can see that
$\beta_{2}=-6.553\leq-5\leq\beta_{3}=3.542$, so the tokens in tick 2 are
enough to facilitate this trade. To determine how many tokens of A must the
swapper deposit, we must use Equation A.6, giving us

$(6.553+100.956\sqrt{1.44}-5)(2.154+100.956/\sqrt{1.69}+y)=(100.956)^{2},$

where we solve for $y$ and find that $y=3.252$. The new liquidity distribution
looks like Table 8, where only the Token A, Token B, and $\beta_{i}$ rows have
changed. Tokens A and B have only changed for Tick 2, and $\beta_{i}$ have all
shifted up by $-\xi=5$. If we wish to determine the new exchange rate from the
updated liquidity, we can use Equation A.7 to calculate

$\dfrac{1.553+100.956\sqrt{1.44}}{5.406+100.956/\sqrt{1.69}}=1.477.$

Notice that in Table 8 the $\ell_{i}$ have not been changed by the swap. In
addition, if $\gamma>0$, only LPs who include tick 2 in their liquidity
position will earn fees from this swap.

Tick $i$ | 0 | 1 | $i^{*}=2$ | 3 | 4 | 5
---|---|---|---|---|---|---
Token A | 0 | 0 | 5.406 | 6.25 | 6.25 | 6.25
Token B | 10 | 10 | 1.553 | 0 | 0 | 0
$[\beta_{i},\beta_{i+1})$ | [-21.553, -11.553) | [-11.553, -1.553) | [ -1.553, 8.542) | [8.542, 19.917) | [19.917, 33.042) | [33.0422, 48.042)
$\ell_{i}$ | 100 | 100 | 100.956 | 113.75 | 131.25 | 150
$[p_{i},p_{i+1})$ | [1, 1.21) | [1.21, 1.44) | [1.44, 1.69) | [1.69, 1.96) | [1.96, 2.25) | [2.25, 2.56)
Table 8: The result of a swap $\xi=-5$. Now,
$p^{*}_{\text{new}}=1.477<p^{*}=1.5$ and $i^{*}=2$ still.

In our next example, we provide a calculation for a swap transaction in which
a user wishes to swap more currency than is contained in the current active
tick $i^{*}$, so liquidity is drawn from multiple ticks. To provide some
intuition for the case of a swap which crosses multiple ticks, we have
illustrated a generic scenario in Figure 24.

(a) An example initial liquidity distribution with $i^{*}=2$.

(b) The result after a swap of $\xi<-B^{*}$, now $i^{*}=1$.

Figure 24: We illustrate an initial liquidity distribution (left) and the
result of a swap where $\xi<B^{*}$ (right), meaning that token B is withdrawn
and token A is deposited.

Now, we will provide a more thorough explanation with calculations. We again
start with Table 7, but we choose $\xi=-10$. Notice that
$\beta_{1}=-16.553\leq-10\leq\beta_{2}=-6.553$, so we know that we will change
the token distributions in both tick 1 and tick 2, and after the swap the
active tick will move to $i^{*}=1$. Some calculation is required to determine
how the tokens are distributed between the two ticks. We move outward tick by
tick, starting from the original $i^{*}=2$. Since $\beta_{2}=-6.553$, we can
withdraw a maximum of 6.553 tokens of B from tick 2. Again, we use Equation
A.6 with $i=2$ to determine that $y_{2}=4.317$ tokens of A should be deposited
in tick 2 in exchange for the 6.553 tokens of B. Now that we have drained all
the tokens of B in tick 2, we move left to tick 1 (because $\xi<0$). We still
wish to withdraw an additional $(10-6.553)=3.447$ tokens of B, so we use
Equation A.6 applied to tick 1 to determine that $y_{1}=2.464$. Therefore, the
total amount deposited in the pool is $y_{1}+y_{2}=6.781$ tokens of A. The new
liquidity distribution is shown in Table 9. Again, use Equation A.7 to find
$p^{*}_{\text{new}}=1.358$. Notice that the larger swap magnitude resulted in
a larger change in the pool exchange rate. Additionally, if $\gamma>0$ then
LPs who included at least one of ticks 1 and 2 in their liquidity position
will earn fees from the swap, with those that contributed to both ticks
earning more fees.

Tick $i$ | 0 | $i^{*}=1$ | 2 | 3 | 4 | 5
---|---|---|---|---|---|---
Token A | 0 | 2.464 | 6.472 | 6.25 | 6.25 | 6.25
Token B | 10 | 6.553 | 0 | 0 | 0 | 0
$[\beta_{i},\beta_{i+1})$ | [-16.553, -6.553) | [-6.553, 3.447) | [3.447, 13.542) | [13.542, 24.917) | [24.917, 38.042) | [38.042, 53.042)
$\ell_{i}$ | 100 | 100 | 100.956 | 113.75 | 131.25 | 150
$[p_{i},p_{i+1})$ | [1, 1.21) | [1.21, 1.44) | [1.44, 1.69) | [1.69, 1.96) | [1.96, 2.25) | [2.25, 2.56)
Table 9: The result of a swap $\xi=-10$. Now, $p^{*}_{\text{new}}=1.358$ and
$i^{*}=1$, showing more movement change in the previous example.

##  Appendix B Numerical Methods

###  B.1 The Single LP Optimization

In Numerical Method 1, we solve the single LP optimization problem given by

$V(j^{1},j^{2};\
\theta,\bm{\ell}^{0}):=\mathbb{E}_{\bm{\xi},\delta(\theta)}[\pi(j^{1},j^{2},\bm{\xi};\
\theta,\bm{\ell}^{0})]-\lambda(\theta)\text{Var}_{\bm{\xi},\delta(\theta)}(\pi(j^{1},j^{2},\bm{\xi};\
\theta,\bm{\ell}^{0},)).$

Numerical Method 1 Single LP Optimization

1:Take $T$, $\theta$, $\gamma$, $\bm{\ell}^{0}$, $p^{*}_{0}$, and $m^{*}_{0}$
as fixed and known.

2:Initialize the running maximum as rmax$\leftarrow-1000$ and initialize the
optimal position as $j^{1,*}\leftarrow 0$ and $j^{2,*}\leftarrow 0$.

3:for $(j^{1},j^{2})\in J$ do

4: Initialize the value function for the current choice of $(i,j)$ as
Vcurr$\leftarrow 0$. Define an empty array $V=[]$ to hold the total reward for
all MC simulations.

5: for many Monte Carlo simulations do

6: Initialize the total reward for the current MC simulation as $R\leftarrow
0$.

7: for $t=0,\ldots,T$ do

8: Simulate $\Delta W_{t}\sim N(0,\Delta t)$ and calculate
$m^{*}_{t}=m^{*}_{t-1}e^{(\alpha+\delta)\Delta t+\sigma\Delta W_{t}}$.

9: Simulate $X_{t}\sim Bernoulli(\rho(p^{*}_{t}-m^{*}_{t}))$.

10: if $X_{t}=1$ then

11: Simulate $\xi_{t}$ according to $f_{X|Y}(x\mid Y=p^{*}_{t}-m^{*}_{t})$.

12: $R\leftarrow
R+r(\xi_{t},\bm{\ell}^{1}(j^{1},j^{2},\bm{\ell}^{0}),p^{*}_{t},\theta)$.

13: end if

14: end for

15: $V$.append($R$)

16: end for

17: Calculate Vcurr$=\mathbb{E}[V]-\lambda Var(V)$.

18: if Vcurr$\geq$ rmax then

19: rmax$\leftarrow$ Vcurr, $j^{1,*}\leftarrow j^{1}$ and $j^{2,*}\leftarrow
j^{2}$.

20: end if

21:end for

22:The optimal control is $(j^{1,*},j^{2,*})$ which produces utility rmax.

###  B.2 The N-Player Game

In the $N$-player Bayesian game, all players have the same type-dependent
strategy in equilibrium, because all players have symmetric beliefs before
types are assigned. Therefore, we want to reverse-engineer $\Theta$ so that
each player’s equilibrium best response strategy creates a distribution of
actions over $J$ which resembles $\bm{\ell}^{0}$ in shape. We will therefore
begin by assuming that each player’s actions corresponding to their strategy
are distributed proportionally to $\bm{\ell}^{0}$. For each type
$\theta\in\Theta$, we will determine the best response strategy of a generic
player $n$ to this action distribution. To determine the strategy which
maximizes the player’s expected payoff given the unknown actions of the other
players, we will need to use Monte Carlo trials to repeatedly sample $N-1$
actions from the space. Once $(j^{1}_{n}(\theta_{n}),j^{2}_{n}(\theta_{n}))$
has been calculated for all choices of
$\theta_{n}\in\operatorname*{supp}(\Theta)$ based on the sampled actions, we
use the estimated action distribution to determine the distribution over types
which would most closely replicate $\bm{\ell}^{0}$. The method is described in
more detail in Method 2. Note that the method is not complex, but the
calculations are expensive. Even though the players are symmetric and so
finding one player’s strategy is equivalent to finding all their strategies,
the computational time grows with the number of players. Notably, the expected
payoff of player $n$ must be calculated with respect to the
$(N-1)$-dimensional random variable representing the actions of the other
players. As $N$ increases, this random vector becomes more and more difficult
to handle, even when using Monte Carlo simulations. This is because as $N$
increases, more and more Monte Carlo simulations are required to accurately
sample from the probability space.

Numerical Method 2 $\Theta$ Calibration: Bayesian $N$-Player Game

1:The observed target liquidity distribution $\bm{\ell}^{0}$.

2:An estimation of the type distribution $\Theta$.

3:Lexicographically order the action space $J$ such that
$J=\\{(j^{1},j^{2})_{q}\\}_{q=1}^{|J|}$. For convenience, we will sometimes
refer to $(j^{1},j^{2})_{q}$ as $\mathcal{j}_{q}\in J$. Recall $|J|=d(d+1)/2$.

4:Create a $|J|\times d$ matrix $J_{mat}$ where row $q$ is the unit liquidity
position corresponding with action $\mathcal{j}_{q}\in J$, such that
$\mathcal{j}_{q}=(j^{1},j^{2})_{q}\to[0,\ldots,0,\underbrace{1,\ldots\ldots,1,}_{j^{1}\text{
to }j^{2}-1}0,\ldots,0]\in\mathbb{R}^{d}.$

5:Define $\bm{x}\in\mathbb{R}^{|J|}_{+}$ which represents how much liquidity
each position in $J$ should hold in order to match $\bm{\ell}^{0}$. Calculate
it by applying a linear solver to
$\min_{\bm{x}\in\mathbb{R}^{|J|}_{+}}||\bm{x}^{T}J_{mat}-\bm{\ell}^{0}||_{2}^{2}.$

6:Calculate an empirical distribution $\mu\in\mathbb{R}^{|J|}_{+}$ over the
action space $J$ represented by $\mu=\dfrac{\bm{x}}{||\bm{x}||_{1}}$, where
$\mu_{q}$ represents the estimated frequency of players choosing action
$\mathcal{j}_{q}$.

7:From player $n$’s perspective, use Monte Carlo simulations to generate $S$
samples of a $(N-1)$-dimensional random vector representing the actions of the
other players, given by $\\{\bm{j}^{-n}_{s}\\}_{s=1}^{S}$ where each element
of $\bm{j}^{-n}_{s}$ is independently sampled from $J$ according to $\mu$.

8:Discretize the space of risk-aversion levels as
$\lambda\in\\{\lambda_{1},\ldots,\lambda_{30}\\}$ where $\lambda_{1}=0$ and
$\lambda_{30}=\lambda_{max}$.

9:Discretize $\operatorname*{supp}(\Theta)$ as
$\Delta:=\\{212400,3578600,170603400\\}\times\\{\lambda_{1},\ldots,\lambda_{30}\\}\times\\{-1,0,1\\}$.

10:for all $\theta\in\Delta$ do

11: Calculate the player’s best response
$(j^{1}_{n}(\theta),j^{2}_{n}(\theta))\in J$, where
$(j^{1}_{n}(\theta),j^{2}_{n}(\theta))\ \in\
\operatorname*{arg\,max}_{(j^{1},j^{2})\in J}\
\frac{1}{S}\sum_{s=1}^{S}V_{n}(j^{1},j^{2},\theta,\bm{j}^{-n}_{s}).$

12:end for

13:For tie-breaking purposes, count how many types $\theta_{n}$ result in a
best-response action $\mathcal{j}_{q}\in J$ by defining the counter function
$\\#(\mathcal{j}_{q}):=\sum_{\theta\in\Delta}\mathbf{1}_{\\{(j^{1}_{n}(\theta),j^{2}_{n}(\theta))=\mathcal{j}_{q}\\}}$
.

14:Calculate the discrete probability distribution $\nu$ over $\Delta$ where,
for all $\theta\in\Delta$,
$\nu(\theta):=\sum_{q=1}^{|J|}\dfrac{\mu_{q}}{\\#(\mathcal{j}_{q})}\mathbf{1}_{\\{(j^{1}_{n}(\theta),j^{2}_{n}(\theta))=\mathcal{j}_{q}\\}}$
If there exist $\mathcal{j}_{q}$ where $\mu_{q}>0$ but
$\forall\theta\in\Delta,(j^{1}_{n}(\theta),j^{2}_{n}(\theta))\neq\mathcal{j}_{q}$,
re-scale $\nu$ s.t. $\sum_{\theta\in\Delta}\nu(\theta)=1$.

15:To satisfy 3.1, we use kernel density estimation and the discrete
distribution $\nu$ over $\Delta$ to calculate the density of $\lambda$ over
the domain $[0,\lambda_{max}]$ for each ($k,\delta$). This produces an
atomless estimate of $\Theta$.

For a given type configuration $\bm{\theta}$, we approximate a Nash
equilibrium using the fictitious play approach which was first introduced in
[Brown, 1951]. Fictitious play has been shown to converge to an equilibrium in
a variety of games, and [Rabinovich et al., 2013] proves the convergence of a
version of fictitious play applied to private-value games of incomplete
information with finite actions and continuous types. We define a version of
fictitious play which is tailored to our game. In round $I$ of fictitious
play, each player $n\in\\{1,\ldots,N\\}$ calculates its best response to its
“memory” (the average of the liquidity distributions arising from the actions
of the other players in the previous $I$ rounds), called
$\bm{\ell}^{\text{hist}}_{n}$. All players then move simultaneously and the
best response actions of all players form an action profile
$\bm{j}^{I}(\bm{\theta})$. Player $n$’s “memory” is updated based on
$\bm{j}^{I}(\bm{\theta}^{-n})$. The historical liquidity distribution arising
from all players’ actions, $\bm{\ell}^{\text{hist}}$, is also updated based on
$\bm{j}^{I}(\bm{\theta})$. The process repeats until the difference between
$\bm{\ell}(\bm{j}^{I}(\bm{\theta}))$ and $\bm{\ell}^{\text{hist}}$ falls below
a preset threshold. The fixed point of the iterative method corresponds to a
Nash equilibrium, which we denote as $\bm{j}^{*}$. We describe our approach in
detail in Numerical Method 3.

Numerical Method 3 Fictitious Play: $N$-Player Game

1:An initial action profile $\bm{j}^{0}\in J^{N}$ and a type assignment vector
$\bm{\theta}=(\theta_{1},\ldots,\theta_{N})\in\operatorname*{supp}(\Theta)^{N}$.

2:An estimated mixed-strategy equilibrium given by $\bm{j}^{*}$.

3:Initialize $N$ players with characteristics $\theta_{n}$ based on the
$N$-dimensional type vector $\bm{\theta}$.

4:Initialize an arbitrary action profile for all the players given by
$\bm{j}^{0}(\bm{\theta})=\\{(j^{1}_{n},j^{2}_{n})\\}_{n=1}^{N}$. Note
$\bm{\theta}$ is fixed so actions are deterministic.

5:Initialize the “historical” liquidity distribution as
$\bm{\ell}^{\text{hist}}\leftarrow\bm{\ell}(\bm{j}^{0}(\bm{\theta}))$, which
is updated with each iteration of fictitious play.

6:For each player $n$, initialize the liquidity distribution formed by all
other players as
$\bm{\ell}^{\text{hist}}_{n}\leftarrow\bm{\ell}(\bm{j}^{0}(\bm{\theta}^{-n}))$.
We will refer to $\bm{\ell}^{\text{hist}}_{n}$ as player $n$’s “memory” which
is also updated with each iteration of fictitious play.

7:Initialize an iteration counter $I\leftarrow 1$.

8:error $\leftarrow\infty$.

9:Set a error tolerance threshold thresh which determines how closely we wish
to approximate a Nash equilibrium.

10:while error $\geq$ thresh do

11: for player $n\in\\{1,\ldots,N\\}$ do

12: Determine player $n$’s best response to $\bm{\ell}^{\text{hist}}_{n}$,
given by  $(j^{1,*}_{n},j^{2,*}_{n})\ \in\
\operatorname*{arg\,max}_{(j^{1},j^{2})\in J}\
V_{n}(j^{1},j^{2},\theta_{n},\bm{\ell}^{\text{hist}}_{n}).$

13: end for

14: Define the action profile for iteration $I$ as
$\bm{j}^{I}(\bm{\theta})\leftarrow\\{(j^{1,*}_{n},j^{2,*}_{n})\\}_{n=1}^{N}$.

15: Update error $\leftarrow W_{1}(\bm{\ell}(\bm{j}^{I}(\bm{\theta})),\
\bm{\ell}^{\text{hist}})$ where $W_{1}$ is the Wasserstein-1 distance.

16: Update the overall historical liquidity distribution and each player’s
personalized “memory” where
$\bm{\ell}^{\text{hist}}\leftarrow\frac{1}{I+1}\sum_{i=0}^{I}\bm{\ell}(\bm{j}^{i}(\bm{\theta}))\quad\text{
and
}\quad\bm{\ell}^{\text{hist}}_{n}\leftarrow\frac{1}{I+1}\sum_{i=0}^{I}\bm{\ell}(\bm{j}^{i}(\bm{\theta}^{-n})),\
\forall n\in\\{1,\ldots,N\\}.$

17: Update the counter $I=I+1$.

18:end while

19:The control vector $\bm{j}^{I}(\bm{\theta})\in\mathbb{R}^{d}$ where
$W_{1}(\bm{\ell}(\bm{j}^{I}(\bm{\theta}))<$thresh approximates a Nash
equilibrium, and we therefore assign
$\bm{j}^{*}(\bm{\theta}):=\bm{j}^{I}(\bm{\theta})$.

We note that both Numerical Method 2 and Numerical Method 3 become more
expensive as $N$ increases. In step 9 of Numerical Method 2, the best response
for each type is computed to maximize the player’s expected payoff with
respect to the $(N-1)$-dimensional random vector of the other players’
actions. This expectation is calculated empirically using 1000 Monte Carlo
samples of $N-1$ actions when $N=10$ and as $N$ increases the number of
samples must also increase to thoroughly sample the larger probability space.
Thus, it becomes more and more expensive to sample the type vectors and to
compute the empirical expectation. Furthermore, as the number of players
increases, step 9 of Numerical Method 3 also becomes more expensive, as every
player’s best response is individually calculated for each iteration of the
fictitious play algorithm.

###  B.3 The Mean-Field Game

The calibration of $\Theta$ in the current MFG setting is simplified because
we do not need to work with an $(N-1)$-dimensional random vector representing
the actions of the other players. Instead, the representative player reacts to
the target liquidity distribution $\bm{\ell}^{0}$ directly. The distribution
of types is calculated such that its type-dependent best responses to
$\bm{\ell}^{0}$ form a distribution that mimics $\bm{\ell}^{0}$. We show the
steps below in Numerical Method 4.

Numerical Method 4 $\Theta$ Calibration: MFG

1:An observed target liquidity distribution $\bm{\ell}^{0}$.

2:An estimation for the type distribution $\Theta$.

3:Initialize $\bm{\ell}^{0}$ as the liquidity distribution associated with
Figure 8(a).

4:Discretize the space of risk-aversion levels as
$\lambda\in\\{\lambda_{1},\ldots,\lambda_{30}\\}$ where $\lambda_{1}=0$ and
$\lambda_{30}=\lambda_{max}$.

5:Define the discretization of $\operatorname*{supp}(\Theta)$ as
$\Delta:=\\{2124,35786,1706034\\}\times\\{\lambda_{1},\ldots,\lambda_{30}\\}\times\\{-1,0,1\\}$
and index $\Delta=\\{\theta_{i}\\}_{i=1}^{|\Delta|}$.

6:Define an empty matrix $P\in\mathbb{R}^{|\Delta|\times d}$ which will hold
the optimal liquidity positions associated with each type $\theta_{i}$ in
$\Delta$.

7:for $\theta_{i}\in\Delta$, where $i=1,\ldots,|\Delta|$ do

8: Find the representative player’s best response such that
$(j^{1}(\theta_{i}),j^{2}(\theta_{i}))\ \in\
\operatorname*{arg\,max}_{(j^{1},j^{2})\in J}\
V_{\text{MFG}}((j^{1},j^{2}),\theta_{i},\bm{\ell}^{0}).$

9: Set row $i$ of $P$ equal to the liquidity position associated with action
$(j^{1}(\theta_{i}),j^{2}(\theta_{i}))$ and type $\theta_{i}$ such that
$P_{i}\leftarrow\bm{\ell}_{\text{new}}\left(u(j^{1}(\theta_{i}),j^{2}(\theta_{i}),k(\theta_{i}),m^{*}_{0}),j^{1}(\theta_{i}),j^{2}(\theta_{i})\right)\in\mathbb{R}^{d},$
where $\ell_{\text{new}}$ is given in Equation 2.2.

10:end for

11:Define $\bm{x}\in\mathbb{R}^{|\Delta|}_{+}$ which represents how much
liquidity each position in $P$ should hold in order to match $\bm{\ell}^{0}$.
Calculate this by solving  $\min_{\bm{x}\in\mathbb{R}^{|\Delta|}_{+}}\
||\bm{x}^{T}P-\bm{\ell}^{0}||^{2}_{2}.$

12:Calculate a discrete distribution $\nu$ over $\Delta$ where
$\nu(\theta_{i}):=\dfrac{\bm{x}_{i}}{||\bm{x}||_{1}}.$

13:To satisfy 3.1, we use kernel density estimation and the discrete
distribution $\nu$ over $\Delta$ to calculate the density of $\lambda$ over
the domain $[0,\lambda_{max}]$ for each combination of $k$ and $\delta$. This
produces an atomless estimate of $\Theta$.

To approximate the mean-field equilibrium once $\Theta$ was calibrated, we
used the method described in Numerical Method 5.

Numerical Method 5 Fictitious Play: MFG

1: An initial liquidity distribution $\bm{\ell}^{0}$ and the type distribution
$\Theta$.

2:An equilibrium liquidity distribution $\bm{\ell}^{*}$.

3:Initialize the “historical” liquidity distribution
$\bm{\ell}^{\text{hist}}:=\bm{\ell}^{0}$, the liquidity distribution
associated with Figure 8(a).

4:Initialize an iteration counter $I\leftarrow 0$.

5:error $\leftarrow\infty$.

6:Set an error tolerance threshold thresh which determines how closely we wish
to approximate a MFE.

7:while error $\geq$ thresh do

8: Calculate the representative player’s best response to
$\bm{\ell}^{\text{hist}}$, such that for all
$\theta\in\operatorname*{supp}(\Theta)$, $\bm{j}^{*}$ satisfies
$\bm{j}^{*}(\theta)\in\operatorname*{arg\,max}_{(j^{1},j^{2})\in
J}V_{\text{MFG}}((j^{1},j^{2}),\theta,\bm{\ell}^{\text{hist}}).$

9: Calculate the resultant liquidity distribution according to
$\ell_{\text{MFG}}$ given in Equation 3.6, such that
$\bm{\ell}^{I}=\bm{\ell}_{\text{MFG}}(\bm{j}^{*}).$

10: Update error $\leftarrow W_{1}(\bm{\ell}^{\text{hist}},\bm{\ell}^{I})$
where $W_{1}$ is the Wasserstein-1 distance.

11: Update $\bm{\ell}^{\text{hist}}\leftarrow\sum_{i=0}^{I}\bm{\ell}^{i}$.

12: Update the counter $I$.

13:end while

14:Denote the final $\bm{\ell}^{I}$ where
$W_{1}(\bm{\ell}^{\text{hist}},\bm{\ell}^{I})<$ thresh as $\bm{\ell}^{*}$
where its associated action strategy is $\bm{j}^{*}(\theta)$. These
approximate a MFE.

##  Appendix C Bot threshold calculation

We wish to determine the Bot’s threshold value $\bar{\xi}(\bm{\ell})$ for
which

$\frac{L}{\ell^{0}_{i^{*}}+L}\Big{(}\xi-m^{*}\psi(\xi,\bm{\ell}^{1},p^{*})+\gamma|\xi|\Big{)}-G>0.$

We first replace $\psi(\xi,\bm{\ell}^{1},p^{*})$ with its functional form in
terms of $\xi$ and $\bm{\ell}^{1}$. We make the simplifying assumption that
the original liquidity $\bm{\ell}^{*}_{i^{*}}$ plus the Bot’s contribution $L$
will be sufficient to accommodate a swap $\xi$ of any size, meaning that we
will not consider the case where the swap “spills over” into neighboring ticks
(for a detailed explanation of what this means, refer to Section A.2).
Therefore, we will drop the subscript $i^{*}$ from $A_{i^{*}}$, $B_{i^{*}}$,
and $\ell^{0}_{i^{*}}$, and shorten $\ell^{1}_{i^{*}}$ to $\bm{\ell}^{*}+L$ to
improve the readability of our calculations and emphasize the dependence on
$L$. We now must solve

$\xi-m^{*}\left(\left(\frac{\ell^{0}+L}{\ell^{0}}\right)A+(\ell^{0}+L)/\sqrt{p_{i^{*}+1}}-\dfrac{(\ell^{0}+L)^{2}}{\left(\frac{\ell^{0}+L}{\ell^{0}}\right)B+(\ell^{0}+L)\sqrt{p_{i^{*}}}+\xi}\right)+\gamma|\xi|-\dfrac{G(\ell^{0}+L)}{L}>0,$

where we note that the amounts of token A and B in tick $i^{*}$ increase
proportionally with respect to the new liquidity $(\ell^{0}+L)$. We can
rearrange and simplify with the help of Equation A.6 and Equation A.7, and
finally we will obtain the quadratic inequality

$\displaystyle C\xi^{2}+D\xi+E>0,$

where

$\displaystyle C=(1+\gamma\operatorname{sgn}(\xi)),$ $\displaystyle
D=\left(\frac{\ell^{0}+L}{\ell^{0}}\right)((1+\gamma\operatorname{sgn}(\xi))p^{*}-m^{*})(A+\ell^{0}/\sqrt{p_{i^{*}+1}})-\dfrac{G(\ell^{0}+L)}{L},$
$\displaystyle
E=-(B+\ell^{0}\sqrt{p_{i^{*}}})\dfrac{G(\ell^{0}+L)^{2}}{\ell^{0}L}.$

We now solve for $\xi$ to find

$\begin{split}&\bar{\xi}^{+}(\bm{\ell})=\dfrac{-D+\sqrt{D^{2}-4(1+\gamma)E}}{2+2\gamma}\vee
0,\\\
&\bar{\xi}^{-}(\bm{\ell})=\dfrac{-D-\sqrt{D^{2}-4(1-\gamma)E}}{2-2\gamma}\wedge
0.\end{split}$ (C.1)

Finally, we briefly justify that we have chosen the correct roots in each
case. First, we assert that $(1+\gamma\operatorname{sgn}(\xi))>0$ for all
$\xi$ by the definition of $\gamma\in(0,1)$ so the parabola opens upward for
all values of $\xi$, $\bm{\ell}$, $L$, and $G$. This implies that
$\bar{\xi}^{+}(\bm{\ell})<\infty$ and $\bar{\xi}^{-}(\bm{\ell})>-\infty$.

Given the shape of the parabola, $\bar{\xi}^{+}(\bm{\ell})$ will correspond to
the rightmost real root for the parabola with $\xi>0$ (if it exists) and
$\bar{\xi}^{-}(\bm{\ell})$ will correspond to the leftmost real root for the
parabola corresponding to $\xi<0$ (if it exists). In the case where the
solution for $\bar{\xi}^{+}(\bm{\ell})$ has two negative roots or the solution
for $\bar{\xi}^{-}(\bm{\ell})$ has two positive roots, the threshold is set at
zero instead. If the parabola has no real roots, then an attack of size $L$ is
always profitable. However, this will not happen since $E\leq 0$, given that
all quantities with $E$ must be positive.

##  Appendix D Parameter Calibration

###  D.1 Bernoulli Arrival of Swaps

To determine how the probability of a swap arriving at block $t$ with pool
exchange rate $p^{*}_{t}$ and market exchange rate $m^{*}_{t}$, we first
plotted the relationship between $|p^{*}-m^{*}|$ and the frequency of at least
one swap arriving and calculated a line of best fit. From this, we were able
to determine that the relationship between $|p^{*}-m^{*}|$ and the likelihood
of a block containing at least one swap was approximately linear with slope
0.0026 and intercept 0.3505. However, $|p^{*}-m^{*}|$ is unbounded above and
$\rho(x)$ must be contained in $[0,1]$. Therefore, we chose a sigmoid function
to avoid a discontinuity in $\rho$ and scaled it such that $\rho(0)=0.3505$
and $\frac{\partial\rho}{\partial x}(0)=0.0026$. The resulting function is

$\rho(x)=\dfrac{1}{1+e^{-0.01145|x|+0.6169}}.$

Notice that as $|x|\to\infty$, $\rho(x)\to 1$, as we desired. The scatter plot
of frequencies, the line of best fit, and the fitted sigmoid function are
displayed in Figure 25.

Figure 25: Comparison of scatter plot, line of best fit, and sigmoid function
to determine the probability of a swap arriving given the arbitrage level in
the pool.

###  D.2 Capital Endowment

To determine how to discretize the space of possible capital endowments, we
examined the distribution of LP contributions to the pool over the period from
September 1 to September 30, 2023. We looked at the Uniswap ETH/USDC pool with
a 0.5% fee, as usual. By looking at the density of the logarithm (base 10) of
the value in USDC of liquidity contributions over the month, we observed three
distinct regions. These liquidity additions could be roughly sorted into
“small” contributions with value less than $10^{3.78}$, “big” contributions
with value more than $10^{6.17}$, and “medium” contributions which fell
between these two extremes. Corresponding to those three regions, the highest
densities were assigned to contributions with value $10^{3.33}$, $10^{4.55}$,
and $10^{6.23}$. Therefore, we chose $k\in\\{2124,35786,1706034\\}$. In
addition, by analyzing the data we found that 47.25% of transactions were
“small”, 48.94% of transactions were “medium”, and 3.81% of transactions were
“big”. We did not enforce this ratio during the calibration, but we note that
an accurate $\Theta$ would reflect the relative frequencies of differently
sized transactions, and this is indeed the case in Table 2.

Figure 26: The cluster analysis performed on LP contributions to the ETH/USDC
Uniswap pool with 0.5% fee, between September 1 and September 30, 2023. The
points represent the small, medium, and large capital endowments that
contribute to $\operatorname*{supp}(\Theta)$.