Try   HackMD

Mathematical Principle of TWAMM

Definition

Assume that TWAMM requires

N blocks to execute a large virtual transaction, and that the pool selling
X
sells at a rate of
xrate
per block, while the pool selling
Y
sells at a rate of
yrate
per block. Thus, the total amount of
X
sold during the whole period is
xin=Nxrate
and the total amount of
Y
sold is
yin=Nyrate
.

Also, we note the initial reserves

xreserve and
yreserve
for this time period in embeded AMM as
x0=xammStart
and
y0=yammStart
, respectively.

According to the design of TWAMM, large orders are traded with blocks, each block sells

xrate to get
yout
, or sells
yrate
to get
xout
, while AMM updates the values of
xreserve
and
yreserve
, the whole process
N
transactions in total.

It is worth noting that each block transaction of AMM always follows a constant product market making.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Formula

First, after executing the transaction for the

n1 block, we assume that the
xreserve
and
yreserve
values of AMM are
xn1
and
yn1
, respectively.

Next, the transaction for block

n is executed, with
XPool
and
YPool
feeding
xrate
and
yrate
to AMM, respectively. Then,

xn=xn1+xrate

yn=yn1+yrate

Since

xrate and
yrate
are very small and the exchange follow a constant product market maker, we can obtain that,

xout,nyrate=xnyn=xn1+xrateyn1+yrate

yout,nxrate=ynxn=yn1+yratexn1+xrate

Simplifying,

xout,n=yratexnyn=yratexn1+xrateyn1+yrate

yout,n=xrateynxn=xrateyn1+yratexn1+xrate

After getting the values of

xout,n and
yout,n
, we can further obtain the
xreserve
and
yreserve
values
xn
and
yn
after the transaction of
n
blocks.

xn=xnxout,n=xnyratexnyn=yn1xnyn=yn1xn1+xrateyn1+yrate

yn=ynyout,n=ynxrateynxn=xn1ynxn=xn1yn1+yratexn1+xrate

Average Price Pyx=n=1Nxout,nNyrate=xoutyin

Average Price Pxy=n=1Nyout,nNxrate=youtxin

By observation, we find that

xnyn=xn1yn1, which exactly meets the prerequisite requirement of AMM for making a market by following a constant product.

Let

xnyn=xn1yn1=...=x1y1=x0y0=k,
k
is a constant.

Fractional Linear Recursion

First find the general formula for

xn and obtain the value of
xammEnd=xN
, and the same for
yn
.

xn=yn1xn1+xrateyn1+yrate=kxn1xn1+xinNkxn1+yinN=kxn1+xinNyinNxn1+k

Let

a=kxinyin,a=kyinxin,b=xinyink, then

xn=xn1+abNbaNxn1+1

The fractional linear recursive expression for

yn is as follows:

yn=kyn1+yinNxinNyn1+k=yn1+abNbaNyn1+1

Solution

First, if

yin=0 and
xin0
, then

xn=x0+xinNn,xammEnd=xN=x0+xin,xout=x0+xinxammEnd=0

And,

yn=kyn1xinNyn1+k,1yn=1yn1+xinkN

The calculation yields,

yn=11y0+xinkNn,yammEnd=yN=kx0+xin

yout=y0+yinyammEnd=y0x0+xinxin

Average Price Pxy=youtxin=y0x0+xin


When

xin=0 and
yin0
, the discussion is the same as above.


If both

xin0 and
yin0
, for general fractional linear recursion, we can use the Fixed-Point Iteration method to solve the problem.

According to

xn=xn1+abNbaNxn1+1, replace
λ=λ+abNbaNλ+1
and solve for
λ=±a
.
±a
is exactly the two fixed points of the above fractional linear recursive expression.

Further calculations,

xna=a(Nb1)(xn1a)xn1+aNb,xn+a=a(Nb+1)(xn1+a)xn1+aNb

Dividing the two expressions above,

xnaxn+a=NbN+bxn1axn1+a=(12bN+b)xn1axn1+a

According to the geometric progression we get,

xnaxn+a=(12bN+b)nx0ax0+a,xNaxN+a=(12bN+b)Nx0ax0+a

Similarly,

ynayn+a=(12bN+b)ny0ay0+a,yNayN+a=(12bN+b)Ny0ay0+a

Limit Value

The basic principle of TWAMM is to decompose long term large orders into infinitely many infinitely small virtual orders, i.e.

N can be taken to infinity, so the limit value of
xN
can be obtained.

xammEndaxammEnd+a=limN+(xNaxN+a)=limN+(12bN+b)Nx0ax0+a=e2bx0ax0+a

xammEnd=ae2b+ce2bc=kxinyine2xinyink+ce2xinyinkc

Where,

c=x0ax0+a=xammStartyinyammStartxinxammStartyin+yammStartxin

xout=xammStart+xinxammEnd

Average Price Pyx=xoutyin=xammStart+xinxammEndyin

Similarly,

yammEnd=kyinxine2xinyink+ce2xinyinkc

c=yammStartxinxammStartyinxammStartyin+yammStartxin=c

yout=yammStart+yinyammEnd

Average Price Pxy=youtxin=yammStart+yinyammEndxin

An important point is that,

xout,
yout
,
xammEnd
and
yammEnd
must larger than 0:

e2xinyink>|c|=|xammStartyinyammStartxinxammStartyin+yammStartxin|

Finally, after a simple verification

xammEndyammEnd=xammStartyammStart=k, the constant product of AMM is still satisfied.

When

yin0,xin0,through L'Hôpital's Rule, we can obtain:

yammEnd=limyin0(kyinxine2xinyink+ce2xinyinkc)

=limyin0(kyinxine2xinyink+yammStartxinxammStartyinxammStartyin+yammStartxine2xinyinkyammStartxinxammStartyinxammStartyin+yammStartxin)

=11yammStart+xink=yammStartxammStartxammStart+xin

xammEnd=limyin0(kxinyine2xinyinkce2xinyink+c)

=limyin0(kxinyine2xinyinkyammStartxinxammStartyinxammStartyin+yammStartxine2xinyink+yammStartxinxammStartyinxammStartyin+yammStartxin)

=xammStart+xin

Differential Equation Of TWAMM

Assume the trading rate of

XPool and
YPool
is uniform according to time,

xnew=xold+xinTdt

ynew=yold+yinTdt

xnew=yoldxnewynew=yoldxold+xinTdtyold+yinTdt

ynew=xoldynewxnew=xoldyold+yinTdtxold+xinTdt

xnew=yoldxold+xinTdtyold+yinTdt=kxoldxold+xinTdtkxold+yinTdt=kxold+xinTdtyinTdtxold+k

Let

a=kxinyin,a=kyinxin,b=xinyink, then,

xnew=xold+abTdtbaTdtxold+1

xnewaxnew+a=(12bTdt1+bTdt)xoldaxold+a

xnewaxnew+axoldaxold+axoldaxold+a=2bTdt1+bTdt

x+dxax+dx+axax+axax+a=2bTdt1+bTdt

limdt0x+dxax+dx+axax+axax+adt=limdt02bT1+bTdt

d(lnxax+a)dt=2bT

lnxammEndaxammEnd+alnxammStartaxammStart+a=0T2bTdt=2b

xammEndaxammEnd+a=e2bxammStartaxammStart+a

xammEnd=ae2b+ce2bc=kxinyine2xinyink+ce2xinyinkc

Where,

c=x0ax0+a=xammStartyinyammStartxinxammStartyin+yammStartxin


If we use

f(t) and
g(t)
instead of
xinT
and
yinT
, that is , the trading rate of
XPool
and
YPool
is a function of time.

xnew=kxold+f(t)dtg(t)dtxold+k

Let

a=kf(t)g(t),a=kg(t)f(t),b=f(t)g(t)k, then,

xnewxold=abdtbadtxold2badtxold+1

xnewxolddt=abbaxold2badtxold+1

limdt0(xnewxolddt)=limdt0(abbaxold2badtxold+1)

dxdt=abbax2=f(t)g(t)kx2=g(t)k(kf(t)g(t)x2)

Let

h(t)=f(t)g(t)=a2k,

dxdt=g(t)k(kh(t)x2)

If

a is constant, i.e.the trading rate of
XPool
and
YPool
is synchronized.

d(lnxax+a)dt=2f(t)g(t)k

lnxammEnda(T)xammEnd+a(T)lnxammStarta(0)xammStart+a(0)=0T2f(t)g(t)kdt

xammEnd=kf(T)g(T)e20Tf(t)g(t)kdt+ce20Tf(t)g(t)kdtc

Where,

c=xammStarta(0)xammStart+a(0)=xammStartkf(0)g(0)xammStart+kf(0)g(0)=xammStartg(0)yammStartf(0)xammStartg(0)+yammStartf(0)

At this point, we have completed a rigorous argument and explanation of the mathematical principles of TWAMM and obtained exactly the same conclusion as in the article [The Time-Weighted Average Market Maker - TWAMM].

参考