Automated Market Making of Trading Pool

Abstract

We introduce a new invariant function associated with generalised mean that underpins the ALEX AMM. ALEX builds DeFi primitives targeting developers looking to build ecosystem on Bitcoin, enabled by Stacks. With a suitable parameterisation, the invariant function supports both risky pairs (i.e. xy=Lx y=L), stable pairs (i.e. x+y=Lx +y=L) and any linear combination in-between (i.e. Curve). We also show that our invariant function maps LL to the liquidity distribution of Uniswap V3.

Introduction

At ALEX, we build DeFi primitives targeting developers looking to build ecosystem on Bitcoin, enabled by Stacks. As such, we focus on trading, lending and borrowing of crypto assets with Bitcoin as the settlement layer and Stacks as the smart contract layer. At the core of this focus is the automated market making ("AMM") protocol, which allows users to exchange one crypto asset with another trustlessly. This paper focuses on technical aspects of AMM.

AMM and Invariant Function

ALEX AMM is built on three beliefs: (i) it is mathematically neat and reflect economic demand and supply and (ii) it is a type of mean, like other AMMs.

We will firstly review some desirable features of AMM that ALEX hopes to exhibit.

Properties of AMM

AMM protocol, which provides liquidity algorithmically, is the core engine of DeFi. In the liquidity pool, two or more assets are deposited and subsequently swapped resulting in both reserve and price movement. The protocol follows an invariant function f(X)=Lf(X)=L, where X=(x1,x2,ā€¦,xd)X=\left(x_1,x_2,\dots,x_d\right) is dd dimension representing dd assets and LL is constant. When d=2d=2, which is the common practise by a range of protocols, AMM f(x1,x2)=Lf(x_1,x_2)=L can be expressed as x2=g(x1)x_2=g(x_1). Although it is not always true, gg tends to be twice differentiable and satisfies the following

  • monotonically decreasing, i.e. dg(x1)dx1<0\frac{dg(x_1)}{dx_1}<0. This is because price is often defined as āˆ’dg(x1)dx1-\frac{dg(x_1)}{dx_1}. A decreasing function ensures price to be positive.

  • convex, i.e. d2g(x1)dx12ā‰„0\frac{d^2g(x_1)}{dx_1^2} \geq 0. This is equivalent to say that āˆ’dg(x1)dx1-\frac{dg(x_1)}{dx_1} is a non-increasing function of x1x_1. It is within the expectation of economic theory of demand and supply, as more reserve of x1x_1 means declining price.

Meanwhile, ff can usually be interpreted as a form of mean, for example, mStable relates to arithmetic mean, where x1+x2=Lx_1+x_2=L (constant sum formula); one of the most popular platforms Uniswap relates to geometric mean, where x1x2=Lx_1 x_2=L (constant product formula); Balancer, which our collateral rebalancing pool employs, applies weighted geometric mean. Its AMM is x1w1x2w2=Lx_1^{w_1} x_2^{w_2}=L where w1w_1 and w2w_2 are fixed weights. ALEX AMM extends these to create a generalised mean.

ALEX AMM

After extensive research, we consider it possible for ALEX AMM to be connected to generalised mean defined as

(1dāˆ‘i=1dxip)1p\left( \frac{1}{d} \sum _{i=1}^{d} x_i^{p} \right)^{\frac{1}{p}}

where 0ā‰¤pā‰¤10 \leq p \leq 1. The expression might remind readers of pp-norm when xiā‰„0x_i \geq 0. It is however not true when p<1p<1 as triangle inequality doesn't hold.

When d=2d=2 and p=1āˆ’tp=1-t (0ā‰¤t<1)(0\leq t<1) is fixed, the core component of generalised mean is assumed constant as below.

x11āˆ’t+x21āˆ’t=Lāˆ’dx2dx1=(x2x1)t\begin{split}x_1^{1-t}+x_2^{1-t}&=L\\ -\frac{dx_2}{dx_1}&=\left(\frac{x_2}{x_1} \right)^{t}\end{split}

This equation is regarded reasonable as AMM, because (i) function gg where x2=g(x1)x_2=g(x_1) is monotonically decreasing and convex; and (ii) The boundary value of t=0t=0 and t=1t=1 corresponds to constant sum and constant product formula respectively. When tt decreases from 1 to 0, price āˆ’dg(x1)x1-\frac{dg(x_1)}{x_1} gradually converges to 1, i.e. the curve converges from constant product to constant sum (see Appendix 1 for the relevant proofs).

Though purely theoretical at this stage, Appendix 2 maps LL to the liquidity distribution of Uniswap V3. This is motivated by an independent research from Paradigm.

Trading Formulae

Market transaction, which involves exchange of one crypto asset and another, satisfies the invariant function. Please note the formulae do not account for the fee re-investment, which results in a slight increase of LL after each transaction, like Uniswap V2.

Out-Given-In

In order to purchase Ī”y\Delta y amount of token Y from the pool, the buyer needs to deposit Ī”x\Delta x amount of token X. Ī”x\Delta x and Ī”y\Delta y satisfy the following

(x+Ī”x)1āˆ’t+(yāˆ’Ī”y)1āˆ’t=x1āˆ’t+y1āˆ’t(x+\Delta x)^{1-t}+(y-\Delta y)^{1-t}=x^{1-t}+y^{1-t}

After each transaction, balance is updated as below: xā†’x+Ī”xx\rightarrow x+\Delta x and yā†’yāˆ’Ī”yy\rightarrow y-\Delta y. Rearranging the formula results in

Ī”y=yāˆ’[x1āˆ’t+y1āˆ’tāˆ’(x+Ī”x)1āˆ’t]11āˆ’t\Delta y=y-\left[x^{1-t}+y^{1-t}-(x+\Delta x)^{1-t}\right]^{\frac{1}{1-t}}

When transaction cost exists, the actual deposit to the pool is less than Ī”x\Delta x. Assuming Ī»Ī”x\lambda\Delta x is the actual amount and (1āˆ’Ī»)Ī”x(1-\lambda)\Delta x is the fee, above can now be expressed as

(x+Ī»Ī”x)1āˆ’t+(yāˆ’Ī”y)1āˆ’t=x1āˆ’t+y1āˆ’tĪ”y=yāˆ’[x1āˆ’t+y1āˆ’tāˆ’(x+Ī»Ī”x)1āˆ’t]11āˆ’t\begin{split} &(x+\lambda\Delta x)^{1-t}+(y-\Delta y)^{1-t}=x^{1-t}+y^{1-t}\\ &\Delta y=y-\left[x^{1-t}+y^{1-t}-(x+\lambda\Delta x)^{1-t}\right]^{\frac{1}{1-t}} \end{split}

To keep LL constant, the updated balance is: xā†’x+Ī»Ī”xx\rightarrow x+\lambda\Delta x and yā†’yāˆ’Ī”yy\rightarrow y-\Delta y.

In-Given-Out

This is the opposite case to above. We are deriving Ī”x\Delta x from Ī”y\Delta y.

Ī”x=1Ī»[x1āˆ’t+y1āˆ’tāˆ’(yāˆ’Ī”y)1āˆ’t]11āˆ’tāˆ’x\Delta x=\frac{1}{\lambda}{\left[x^{1-t}+y^{1-t}-(y-\Delta y)^{1-t}\right]^{\frac{1}{1-t}}-x}

In-Given-Price

Sometimes, trader would like to adjust the price, perhaps due to deviation of AMM price to the market value. Define pā€²p' the AMM price after rebalancing the token X and token Y in the pool

pā€²=(yāˆ’Ī”yx+Ī»Ī”x)tp'=\left(\frac{y-\Delta y}{x+\lambda\Delta x}\right)^{t}

Then, the added amount of Ī”x\Delta x can be calculated from the formula below

(x+Ī»Ī”x)1āˆ’t+(yāˆ’Ī”y)1āˆ’t=x1āˆ’t+y1āˆ’t1+(yx)1āˆ’t=(1+Ī»Ī”xx)1āˆ’t+(yāˆ’Ī”yx)1āˆ’t1+p1āˆ’tt=(1+Ī»Ī”xx)1āˆ’t+pā€²1āˆ’tt(1+Ī»Ī”xx)1āˆ’tĪ”x=xĪ»[(1+p1āˆ’tt1+pā€²1āˆ’tt)11āˆ’tāˆ’1]\begin{split} &(x+\lambda\Delta x)^{1-t}+(y-\Delta y)^{1-t}=x^{1-t}+y^{1-t}\\ &1+\left(\frac{y}{x}\right)^{1-t}=\left(1+\lambda\frac{\Delta x}{x}\right)^{1-t}+(\frac{y-\Delta y}{x})^{1-t}\\ &1+p^{\frac{1-t}{t}}=\left(1+\lambda\frac{\Delta x}{x}\right)^{1-t}+p'^{\frac{1-t}{t}}\left(1+\lambda\frac{\Delta x}{x}\right)^{1-t}\\ &\Delta x=\frac{x}{\lambda}\left[\left(\frac{1+p^{\frac{1-t}{t}}}{1+p'^{\frac{1-t}{t}}}\right)^{\frac{1}{1-t}}-1\right]\\ \end{split}

Appendix 1: Generalised Mean when d=2

ALEX's invariant function is f(x1,x2;p)=x1p+x2p=L.f(x_{1},x_{2};p)=x{_1}^{p}+x_{2}^{p}=L. It can be rearranged as x2=g(x1)=(Lāˆ’x1p)1px{2}=g(x_{1})=(L-x_{1}^{p})^{\frac{1}{p}}. x1x_{1} and x2x_{2} should both be positive meaning the liquidity pool contains both tokens.

Theorem

When 0<p<10<p<1, g(x1)g\left(x_{1}\right) is monotonically decreasing and convex.

Proof

This is equivalent to prove dg(x1)dx1<0\frac{dg(x_{1})}{dx_{1}}<0 and d2g(x1)dx12ā‰„0\frac{d^{2}g(x_{1})}{dx_{1}^{2}}\geq0.

dg(x1)dx1=1p(Lāˆ’x1p)1pāˆ’1(āˆ’px1pāˆ’1)=āˆ’(Lāˆ’x1px1p)1āˆ’pp<0d2g(x1)dx12=āˆ’1āˆ’pp(Lāˆ’x1px1p)1āˆ’2pp[āˆ’px1pāˆ’1x1pāˆ’(Lāˆ’x1p)pxpāˆ’1x12p]=L(1āˆ’p)(x2x1)1āˆ’2px1āˆ’pāˆ’1ā‰„0\begin{split} &\frac{dg(x_{1})}{dx_{1}}=\frac{1}{p}(L-x_{1}^{p})^{\frac{1}{p}-1}\left(-px_{1}^{p-1}\right)=-\left(\frac{L-x_{1}^{p}}{x_{1}^{p}}\right)^{\frac{1-p}{p}}<0\\ &\frac{d^{2}g(x_{1})}{dx_{1}^{2}}=-\frac{1-p}{p}\left(\frac{L-x_{1}^{p}}{x_{1}^{p}}\right)^{\frac{1-2p}{p}}\left[\frac{-px_{1}^{p-1}x_{1}^{p}-(L-x_{1}^{p})px^{p-1}}{x_{1}^{2}p}\right]\\ &=L(1-p)\left(\frac{x_{2}}{x_{1}}\right)^{1-2p}x_{1}^{-p-1}\geq0 \end{split}

The last inequality holds because each component is positive.

When pp= 1, it is straightforward to see that the invariant function is constant sum. To show that the invariant function converges to constant product when pp= 0, we will show and prove an established result in a generalised dd dimensional setting.

Theorem

limā”pā†’0(1dāˆ‘i=1dxip)1p=(āˆi=1dxi)1d\lim_{p\rightarrow0}\left(\frac{1}{d}\sum_{i=1}^{d}x_{i}^{p}\right)^{\frac{1}{p}}=({\prod_{i=1}^{d}x_{i}})^{\frac{1}{d}}

Proof

(1dāˆ‘i=1dxip)1p=exp[log(1dāˆ‘i=1dxip)p]\left(\frac{1}{d}\sum{i=1}^{d}x_{i}^{p}\right)^{\frac{1}{p}}=\text{exp}\left[\frac{\text{log}\left(\frac{1}{d}\sum{i=1}^{d}x_{i}^{p}\right)}{p}\right]. Applying L'Hospital rule to the exponent, which is 0 in both denominator and nominator when pā†’0p\rightarrow0, we have

limā”pā†’0log(1dāˆ‘i=1dxip)p=limā”pā†’0āˆ‘i=1dlog(xi)āˆ‘j=1d(xjxi)p=āˆ‘i=1dlog(xi)d\lim_{p\rightarrow0}\frac{\text{log}\left(\frac{1}{d}\sum_{i=1}^{d}x_{i}^{p}\right)}{p}=\lim_{p\rightarrow0}\sum_{i=1}^{d}\frac{\text{log}(x_{i})}{\sum_{j=1}^{d}\left(\frac{x_{j}}{x_{i}}\right)^{p}}=\frac{\sum_{i=1}^{d}\text{log}(x_{i})}{d}

Therefore

limā”pā†’0(1dāˆ‘i=1dxip)1p=limā”pā†’0expāˆ‘i=1dlog(xi)d=(āˆi=1dxi)1d\lim_{p\rightarrow0}\left(\frac{1}{d}\sum_{i=1}^{d}x_{i}^{p}\right)^{\frac{1}{p}}=\lim_{p\rightarrow0}\text{exp}\frac{\sum_{i=1}^{d}\text{log}(x_{i})}{d}=({\prod_{i=1}^{d}x_{i}})^{\frac{1}{d}}

Corollary

When d = 2,

x1x2=limā”pā†’0[12(x1p+x2p)]2px_{1}x_{2}=\lim_{p\rightarrow0}\left[\frac{1}{2}(x_{1}^{p}+x_{2}^{p})\right]^{\frac{2}{p}}

Proof of the corollary is trivial, as it is a direct application of the theorem. It shows that generalised mean AMM implies constant product AMM when pā†’0p\rightarrow0.

Appendix 2: Liquidity Mapping to Uniswap v3

As Uniswap v3 is able to simulate liquidity curve of any AMM, we are interested in exploring the connection between ALEX's AMM and that of Uniswap's. Interesting questions include: what is the shape of the liquidity distribution? Which point(s) has the highest liquidity? We acknowledge that the section is more of a theoretical study for now.

Uniswap V3 AMM can be expressed as a function of invariant constant LL with respect to price pp, LUniswap=dydpL_{\text{Uniswap}}=\frac{dy}{d\sqrt{p}}. For us, the difference in the invariant function means we can write price as p=ertp=e^{rt} (or, equivalently, r=1tlnā”pr=\frac{1}{t}\ln{p} ) and we have

LUniswap=dydp=2teāˆ’12rtdydrL_{\text{Uniswap}}=\frac{dy}{d\sqrt{p}}=\frac{2}{t}e^{-\frac{1}{2}rt}\frac{dy}{dr}

Based on the previous sections, we can then express yy as

y=[L1+eāˆ’(1āˆ’t)r]11āˆ’ty=\left[\frac{L}{1+e^{-(1-t)r}}\right]^{\frac{1}{1-t}}

Therefore

dydr=L11āˆ’teāˆ’(1āˆ’t)r(1+eāˆ’(1āˆ’t)r)2āˆ’t1āˆ’tLUniswap=2tL11āˆ’t(er(1āˆ’t)2+eāˆ’r(1āˆ’t)2)āˆ’2+t1āˆ’t=2tL11āˆ’t{2coshā”[r(1āˆ’t)2]}āˆ’2+t1āˆ’t\begin{split} &\frac{dy}{dr}=L^{\frac{1}{1-t}}\frac{e^{-(1-t)r}}{(1+e^{-(1-t)r})^{\frac{2-t}{1-t}}}\\ &L_{\text{Uniswap}}=\frac{2}{t}L^{\frac{1}{1-t}}\left(e^{\frac{r(1-t)}{2}}+e^{\frac{-r(1-t)}{2}}\right)^{\frac{-2+t}{1-t}}\\ &=\frac{2}{t}L^{\frac{1}{1-t}}\big\{2\cosh\left[\frac{r(1-t)}{2}\right]\big\}^{\frac{-2+t}{1-t}} \end{split}

Figure 1 plots LUniswapL_{\text{Uniswap}} against rr (which is proportional to pp) regarding various levels of tt. When 0<t<10<t<1, LUniswapL_{\text{Uniswap}} is symmetric around 0% at which the maximum reaches . This is because

  1. coshā”[(r(1āˆ’t)2)]\cosh\left[(\frac{r(1-t)}{2})\right] is symmetric around rr= 0% with minimum at 0% and the minimum value 1;

  2. xzx^z is a decreasing function of xx when xx is positive and power zz is negative. In our case, we have z=āˆ’2+t1āˆ’t<āˆ’1z=\frac{-2+t}{1-t}<-1. Therefore, it is the maximum rather than minimum that LUniswapL_{\text{Uniswap}} achieves at 0.

Furthermore, the higher the tt, the flatter the liquidity distribution is. When tt approaches 1, i.e. AMM converges to the constant product formula, the liquidity distribution is close to a flat line. When tt approaches 0, the distribution concentrates around 0%.

Last updated