Skip to main content

Mathematical Principle of TWAMM

Definition#

Assume that TWAMM requires NN blocks to execute a large virtual transaction, and that the pool selling XX sells at a rate of xratex_{rate} per block, while the pool selling YY sells at a rate of yratey_{rate} per block. Thus, the total amount of XX sold during the whole period is xin=Nxratex_{in}=Nx_{rate} and the total amount of YY sold is yin=Nyratey_{in}=Ny_{rate}.

Also, we note the initial reserves xreservex_{reserve} and yreservey_{reserve} for this time period in embedded AMM as x0=xammStartx_{0}=x_{ammStart} and y0=yammStarty_{0}=y_{ammStart}, respectively.

According to the design of TWAMM, large orders are traded with blocks, each block sells xratex_{rate} to get youty_{out}, or sells yratey_{rate} to get xoutx_{out}, while AMM updates the values of xreservex_{reserve} and yreservey_{reserve}, the whole process NN transactions in total.

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

Formula#

First, after executing the transaction for the nβˆ’1n-1 block, we assume that the xreservex_{reserve} and yreservey_{reserve} values of AMM are xnβˆ’1x_{n-1} and ynβˆ’1y_{n-1}, respectively.

Next, the transaction for block nn is executed, with Xβˆ’PoolX-Pool and Yβˆ’PoolY-Pool feeding xratex_{rate} and yratey_{rate} to AMM, respectively. Then,

xβ€Ύn=xnβˆ’1+xrate\overline{x}_{n}=x_{n-1}+x_{rate}

yβ€Ύn=ynβˆ’1+yrate\overline{y}_{n}=y_{n-1}+y_{rate}

Since xratex_{rate} and yratey_{rate} are very small and the exchange follow a constant product market maker, we can obtain that,

xout,nyrate=xβ€Ύnyβ€Ύn=xnβˆ’1+xrateynβˆ’1+yrate\dfrac{x_{out,n}}{y_{rate}}=\dfrac{\overline{x}_{n}}{\overline{y}_{n}}=\dfrac{x_{n-1}+x_{rate}}{y_{n-1} +y_{rate}}

yout,nxrate=yβ€Ύnxβ€Ύn=ynβˆ’1+yratexnβˆ’1+xrate\dfrac{y_{out,n}}{x_{rate}}=\dfrac{\overline{y}_{n}}{\overline{x}_{n}}=\dfrac{y_{n-1}+y_{rate}}{x_{n-1}+ x_{rate}}

Simplifying,

xout,n=yrateβ‹…xβ€Ύnyβ€Ύn=yrateβ‹…xnβˆ’1+xrateynβˆ’1+yratex_{out,n}=y_{rate}\cdot\dfrac{\overline{x}_{n}}{\overline{y}_{n}}=y_{rate}\cdot\dfrac{x_{n-1}+x_{rate}}{y_{n-1}+y_{rate}}

yout,n=xrateβ‹…yβ€Ύnxβ€Ύn=xrateβ‹…ynβˆ’1+yratexnβˆ’1+xratey_{out,n}=x_{rate}\cdot\dfrac{\overline{y}_{n}}{\overline{x}_{n}}=x_{rate}\cdot\dfrac{y_{n-1} +y_{rate}}{x_{n-1}+x_{rate}}

After getting the values of xout,nx_{out,n} and yout,ny_{out,n}, we can further obtain the xreservex_{reserve} and yreservey_{reserve} values xnx_{n} and yny_{n} after the transaction of nn blocks.

xn=xβ€Ύnβˆ’xout,n=xβ€Ύnβˆ’yrateβ‹…xβ€Ύnyβ€Ύn=ynβˆ’1β‹…xβ€Ύnyβ€Ύn=ynβˆ’1β‹…xnβˆ’1+xrateynβˆ’1+yratex_{n}=\overline{x}_{n}-x_{out,n}=\overline{x}_{n}- y_{rate}\cdot\dfrac{\overline{x}_{n}}{\overline{y}_{n}}=y_{n-1}\cdot\dfrac{\overline{x }_{n}}{\overline{y}_{n}}= y_{n-1}\cdot\dfrac{x_{n-1}+x_{rate}}{y_{n-1}+y_{rate}}

yn=yβ€Ύnβˆ’yout,n=yβ€Ύnβˆ’xrateβ‹…yβ€Ύnxβ€Ύn=xnβˆ’1β‹…yβ€Ύnxβ€Ύn=xnβˆ’1β‹…ynβˆ’1+yratexnβˆ’1+xratey_{n}=\overline{y}_{n}-y_{out,n}=\overline{y}_{n}- x_{rate}\cdot\dfrac{\overline{y}_{n}}{\overline{x}_{n}}=x_{n-1}\cdot\dfrac{\overline{y }_{n}}{\overline{x}_{n}}=x_{n-1}\cdot\dfrac{y_{n-1}+y_{rate}}{x_{n-1}+x_{rate}}

AverageΒ PriceΒ Pyx=βˆ‘n=1Nxout,nNyrate=xoutyinAverage\ Price\ P^{x}_{y}=\sum_{n=1}^{N}\dfrac{x_{out,n}}{Ny_{rate}}=\dfrac{x_{out}}{y_{in}}

AverageΒ PriceΒ Pxy=βˆ‘n=1Nyout,nNxrate=youtxinAverage\ Price\ P^{y}_{x}=\sum_{n=1}^{N}\dfrac{y_{out,n}}{Nx_{rate}}=\dfrac{y_{out}}{x_{in}}

By observation, we find that xnβ‹…yn=xnβˆ’1β‹…ynβˆ’1x_{n}\cdot y_{n}= x_{n-1}\cdot y_{n-1}, which exactly meets the prerequisite requirement of AMM for making a market by following a constant product.

Let xnyn=xnβˆ’1ynβˆ’1=...=x1y1=x0y0=kx_{n}y_{n}=x_{n-1}y_{n-1}=... = x_{1}y_{1}=x_{0}y_{0}=k, kk is a constant.

Fractional Linear Recursion#

First find the general formula for xnx_{n} and obtain the value of xammEnd=xNx_{ammEnd}=x_{N}, and the same for yny_{n}.

xn=ynβˆ’1β‹…xnβˆ’1+xrateynβˆ’1+yrate=kxnβˆ’1β‹…xnβˆ’1+xinNkxnβˆ’1+yinN=kβ‹…xnβˆ’1+xinNyinNβ‹…xnβˆ’1+kx_{n}=y_{n-1}\cdot\dfrac{x_{n-1}+x_{rate}}{y_{n-1} +y_{rate}}=\dfrac{k}{x_{n-1}}\cdot\dfrac{x_{n-1} + \dfrac{x_{in}}{N}}{\dfrac{k}{x_{n-1}}+\dfrac{y_{in}}{N}}=k\cdot\dfrac{x_{n-1}+\dfrac{x_{in}}{N}}{\dfrac{y_{in}}{N}\cdot x_{n-1}+k}

Let a=kxinyin,aβ€Ύ=kyinxin,b=xinyinka=\sqrt{\dfrac{kx_{in}}{y_{in}}},\overline{a}=\sqrt{\dfrac{ky_{in}}{x_{in}}},b=\sqrt{\dfrac{x_{in}y_{in}}{k}}, then

xn=xnβˆ’1+abNbaNβ‹…xnβˆ’1+1x_{n}=\dfrac{x_{n-1}+\dfrac{ab}{N}}{\dfrac{b}{aN}\cdot x_{n-1}+1}

The fractional linear recursive expression for yny_{n} is as follows:

yn=kβ‹…ynβˆ’1+yinNxinNβ‹…ynβˆ’1+k=ynβˆ’1+aβ€ΎbNbaβ€ΎNβ‹…ynβˆ’1+1y_{n}=k\cdot\dfrac{y_{n-1}+\dfrac{y_{in}}{N}}{\dfrac{x_{in}}{N}\cdot y_{n-1}+k}=\dfrac{y_{n-1}+ \dfrac{\overline{a}b}{N}}{\dfrac{b}{\overline{a}N}\cdot y_{n-1}+1}

Solution#

First, if yin=0y_{in}=0 and xin≠0x_{in}\neq0 , then

xn=x0+xinNβ‹…n,xammEnd=xN=x0+xin,xout=x0+xinβˆ’xammEnd=0x_{n}=x_{0}+\dfrac{x_{in}}{N}\cdot n,x_{ammEnd}=x_{N}=x_{0}+x_{in},x_{out}=x_{0}+x_{in}-x_{ammEnd}=0

And,

yn=kβ‹…ynβˆ’1xinNβ‹…ynβˆ’1+k,1yn=1ynβˆ’1+xinkNy_{n}=k\cdot\dfrac{y_{n-1}}{\dfrac{x_{in}}{N}\cdot y_{n-1}+ k},\dfrac{1}{y_{n}}=\dfrac{1}{y_{n-1}}+\dfrac{x_{in}}{kN}

The calculation yields,

yn=11y0+xinkNβ‹…n,yammEnd=yN=kx0+xiny_{n}=\dfrac{1}{\dfrac{1}{y_{0}}+\dfrac{x_{in}}{kN}\cdot n},y_{ammEnd}=y_{N}=\dfrac{k}{x_{0}+x_{in}}

yout=y0+yinβˆ’yammEnd=y0x0+xinβ‹…xiny_{out}=y_{0}+y_{in}-y_{ammEnd}=\dfrac{y_{0}}{x_{0}+x_{in}}\cdot x_{in}

AverageΒ PriceΒ Pxy=youtxin=y0x0+xinAverage\ Price\ P^{y}_{x}=\dfrac{y_{out}}{x_{in}}=\dfrac{y_{0}}{x_{0}+x_{in}}


When xin=0x_{in}=0 and yin≠0y_{in}\neq0 , the discussion is the same as above.


If both xin≠0x_{in}\neq0 and yin≠0y_{in}\neq0, for general fractional linear recursion, we can use the Fixed-Point Iteration method to solve the problem.

According to xn=xnβˆ’1+abNbaNβ‹…xnβˆ’1+1x_{n}=\dfrac{x_{n-1}+\dfrac{ab}{N}}{ \dfrac{b}{aN}\cdot x_{n-1}+1}, replace Ξ»=Ξ»+abNbaNβ‹…Ξ»+1\lambda=\dfrac{\lambda+\dfrac{ab}{N}}{\dfrac{b}{aN}\cdot\lambda+1} and solve for Ξ»=Β±a\lambda=\pm{a}. Β±a\pm{a} is exactly the two fixed points of the above fractional linear recursive expression.

Further calculations,

xnβˆ’a=a(Nbβˆ’1)(xnβˆ’1βˆ’a)xnβˆ’1+aNb,xn+a=a(Nb+1)(xnβˆ’1+a)xnβˆ’1+aNbx_{n}-a=\dfrac{a(\dfrac{N}{b}-1)(x_{n-1}-a)}{x_{n-1}+\dfrac{aN}{b}},x_{n}+a=\dfrac{a(\dfrac{N}{b}+1)(x_{n-1}+a)}{x_{n-1}+\dfrac{aN}{b}}

Dividing the two expressions above,

xnβˆ’axn+a=Nβˆ’bN+bβ‹…xnβˆ’1βˆ’axnβˆ’1+a=(1βˆ’2bN+b)β‹…xnβˆ’1βˆ’axnβˆ’1+a\dfrac{x_{n}-a}{x_{n}+a}=\dfrac{N-b}{N+b}\cdot\dfrac{x_{n-1}-a}{x_{n-1}+a}=(1-\dfrac{2b}{N+b})\cdot\dfrac{x_{n-1}-a}{x_{n-1}+a}

According to the geometric progression we get,

xnβˆ’axn+a=(1βˆ’2bN+b)nβ‹…x0βˆ’ax0+a,xNβˆ’axN+a=(1βˆ’2bN+b)Nβ‹…x0βˆ’ax0+a\dfrac{x_{n}-a}{x_{n}+a}=(1-\dfrac{2b}{N+b})^{n}\cdot\dfrac{x_{0}-a}{x_{0}+a},\dfrac{x_{N}-a}{x_{N}+a}=(1-\dfrac{2b}{N+b})^{ N}\cdot\dfrac{x_{0}-a}{x_{0}+a}

Similarly,

ynβˆ’aβ€Ύyn+aβ€Ύ=(1βˆ’2bN+b)nβ‹…y0βˆ’aβ€Ύy0+aβ€Ύ,yNβˆ’aβ€ΎyN+aβ€Ύ=(1βˆ’2bN+b)Nβ‹…y0βˆ’aβ€Ύy0+aβ€Ύ\dfrac{y_{n}-\overline{a}}{y_{n}+\overline{a}}=(1-\dfrac{2b}{N+b})^{n}\cdot\dfrac{y_{0}-\overline{a}}{y_{0}+\overline{a}},\dfrac{y_{N}-\overline{a}}{y_{N}+\overline{a}}=(1-\dfrac{2b}{N+b})^{N}\cdot\dfrac{y_{0}-\overline{a}}{y_{0}+\overline{a}}

Limit Value#

The basic principle of TWAMM is to decompose long-term large orders into infinitely many infinitely small virtual orders, i.e. NN can be taken to infinity, so the limit value of xNx_{N} can be obtained.

xammEndβˆ’axammEnd+a=lim⁑Nβ†’+∞(xNβˆ’axN+a)=lim⁑Nβ†’+∞(1βˆ’2bN+b)Nβ‹…x0βˆ’ax0+a=eβˆ’2bβ‹…x0βˆ’ax0+a\dfrac{x_{ammEnd}-a}{x_{ammEnd}+a}=\lim_{N\rightarrow+\infty}(\dfrac{x_{N}-a}{x_{N}+a})=\lim_{N\rightarrow+\infty}(1-\dfrac{2b}{N+b})^{N}\cdot\dfrac{x_{0}-a}{x_{0}+a}=e^{-2b}\cdot\dfrac{x_{0}-a}{x_{0}+a}

xammEnd=aβ‹…e2b+ce2bβˆ’c=kxinyinβ‹…e2xinyink+ce2xinyinkβˆ’cx_{ammEnd}=a\cdot\dfrac{e^{2b}+c}{e^{2b}-c}=\sqrt{\dfrac{kx_{in}}{y_{in}}}\cdot\dfrac{e^{2\sqrt{\dfrac{x_{in}y_{in}}{k}}}+c}{e^{2\sqrt{\dfrac{x_{in}y_{in}}{k}}}-c}

Where,

c=x0βˆ’ax0+a=xammStartβ‹…yinβˆ’yammStartβ‹…xinxammStartβ‹…yin+yammStartβ‹…xinc=\dfrac{x_{0}-a}{x_{0}+a}=\dfrac{\sqrt{x_{ammStart}\cdot y_{in}}-\sqrt{y_{ammStart}\cdot x_{in}}}{\sqrt{x_{ammStart}\cdot y_{in}}+\sqrt{y_{ ammStart}\cdot x_{in}}}

xout=xammStart+xinβˆ’xammEndx_{out}=x_{ammStart}+x_{in}-x_{ammEnd}

AverageΒ PriceΒ Pyx=xoutyin=xammStart+xinβˆ’xammEndyinAverage\ Price\ P^{x}_{y}=\dfrac{x_{out}}{y_{in}}=\dfrac{x_{ammStart}+x_{in}-x_{ammEnd}}{y_{in}}

Similarly,

yammEnd=kyinxinβ‹…e2xinyink+cβ€Ύe2xinyinkβˆ’cβ€Ύy_{ammEnd}=\sqrt{\dfrac{ky_{in}}{x_{in}}}\cdot\dfrac{e^{2\sqrt{\dfrac{x_{in}y_{in}}{k}}}+\overline{c}}{e^{2\sqrt{\dfrac{x_{in}y_{in}}{k}}}-\overline{c}}

cβ€Ύ=yammStartβ‹…xinβˆ’xammStartβ‹…yinxammStartβ‹…yin+yammStartβ‹…xin=βˆ’c\overline{c}=\dfrac{\sqrt{y_{ammStart}\cdot x_{in}}-\sqrt{x_{ammStart}\cdot y_{in}}}{\sqrt{x_{ammStart}\cdot y_{in}}+\sqrt{y_{ammStar t}\cdot x_{in}}}=-c

yout=yammStart+yinβˆ’yammEndy_{out}=y_{ammStart}+y_{in}-y_{ammEnd}

AverageΒ PriceΒ Pxy=youtxin=yammStart+yinβˆ’yammEndxinAverage\ Price\ P^{y}_{x}=\dfrac{y_{out}}{x_{in}}=\dfrac{y_{ammStart}+y_{in}-y_{ammEnd}}{x_{in}}

An important point is that,xoutx_{out}, youty_{out}, xammEndx_{ammEnd} and yammEndy_{ammEnd} must larger than 0:

e2xinyink>∣c∣=∣xammStartβ‹…yinβˆ’yammStartβ‹…xinxammStartβ‹…yin+yammStartβ‹…xin∣e^{2\sqrt{\dfrac{x_{in}y_{in}}{k}}}>\left\lvert c\right\rvert=\left\lvert \dfrac{\sqrt{x_{ammStart}\cdot y_{in}}-\sqrt{y_{ammStart}\cdot x_{in}}}{\sqrt{x_{ammStart}\cdot y_{in}}+\sqrt{y_{ ammStart}\cdot x_{in}}}\right\rvert

Finally, after a simple verification xammEndβ‹…yammEnd=xammStartβ‹…yammStart=kx_{ammEnd}\cdot y_{ammEnd}=x_{ammStart}\cdot y_{ammStart}=k, the constant product of AMM is still satisfied.

When yin→0,xin≠0y_{in}\rightarrow0, x_{in}\neq0,through L'Hôpital's Rule, we can obtain:

yammEnd=lim⁑yinβ†’0(kyinxinβ‹…e2xinyink+cβ€Ύe2xinyinkβˆ’cβ€Ύ)y_{ammEnd}=\lim_{y_{in}\rightarrow0}\left(\sqrt{\dfrac{ky_{in}}{x_{in}}}\cdot\dfrac{e^{2\sqrt{\dfrac{x_{in}y_{in}}{k}}}+\overline{c}}{e^{2\sqrt{\dfrac{x_{in}y_{in}}{k}}}-\overline{c}}\right)

=lim⁑yinβ†’0(kyinxinβ‹…e2xinyink+yammStartβ‹…xinβˆ’xammStartβ‹…yinxammStartβ‹…yin+yammStartβ‹…xine2xinyinkβˆ’yammStartβ‹…xinβˆ’xammStartβ‹…yinxammStartβ‹…yin+yammStartβ‹…xin)=\lim_{y_{in}\rightarrow0}\left(\sqrt{\dfrac{ky_{in}}{x_{in}}}\cdot\dfrac{e^{2\sqrt{\dfrac{x_{in}y_{in}}{k}}}+\dfrac{\sqrt{y_{ammStart}\cdot x_{in}}-\sqrt{x_{ammStart}\cdot y_{in}}}{\sqrt{x_{ammStart}\cdot y_{in}}+\sqrt{y_{ammStar t}\cdot x_{in}}}}{e^{2\sqrt{\dfrac{x_{in}y_{in}}{k}}}-\dfrac{\sqrt{y_{ammStart}\cdot x_{in}}-\sqrt{x_{ammStart}\cdot y_{in}}}{\sqrt{x_{ammStart}\cdot y_{in}}+\sqrt{y_{ammStar t}\cdot x_{in}}}}\right)

=11yammStart+xink=yammStartβ‹…xammStartxammStart+xin=\dfrac{1}{\dfrac{1}{y_{ammStart}}+\dfrac{x_{in}}{k}}=\dfrac{y_{ammStart}\cdot x_{ammStart}}{x_{ammStart}+x_{in}}

xammEnd=lim⁑yinβ†’0(kxinyinβ‹…e2xinyinkβˆ’cβ€Ύe2xinyink+cβ€Ύ)x_{ammEnd}=\lim_{y_{in}\rightarrow0}\left(\sqrt{\dfrac{kx_{in}}{y_{in}}}\cdot\dfrac{e^{2\sqrt{\dfrac{x_{in}y_{in}}{k}}}-\overline{c}}{e^{2\sqrt{\dfrac{x_{in}y_{in}}{k}}}+\overline{c}}\right)

=lim⁑yinβ†’0(kxinyinβ‹…e2xinyinkβˆ’yammStartβ‹…xinβˆ’xammStartβ‹…yinxammStartβ‹…yin+yammStartβ‹…xine2xinyink+yammStartβ‹…xinβˆ’xammStartβ‹…yinxammStartβ‹…yin+yammStartβ‹…xin)=\lim_{y_{in}\rightarrow0}\left(\sqrt{\dfrac{kx_{in}}{y_{in}}}\cdot\dfrac{e^{2\sqrt{\dfrac{x_{in}y_{in}}{k}}}-\dfrac{\sqrt{y_{ammStart}\cdot x_{in}}-\sqrt{x_{ammStart}\cdot y_{in}}}{\sqrt{x_{ammStart}\cdot y_{in}}+\sqrt{y_{ammStar t}\cdot x_{in}}}}{e^{2\sqrt{\dfrac{x_{in}y_{in}}{k}}}+\dfrac{\sqrt{y_{ammStart}\cdot x_{in}}-\sqrt{x_{ammStart}\cdot y_{in}}}{\sqrt{x_{ammStart}\cdot y_{in}}+\sqrt{y_{ammStar t}\cdot x_{in}}}}\right)

=xammStart+xin={x_{ammStart}+x_{in}}

Differential Equation Of TWAMM#

Assume the trading rate of Xβˆ’PoolX-Pool and Yβˆ’PoolY-Pool is uniform according to time,

xβ€Ύnew=xold+xinTdt\overline{x}_{new}=x_{old}+\dfrac{x_{in}}{T}dt

yβ€Ύnew=yold+yinTdt\overline{y}_{new}=y_{old}+\dfrac{y_{in}}{T}dt

xnew=yoldβ‹…xβ€Ύnewyβ€Ύnew=yoldβ‹…xold+xinTdtyold+yinTdtx_{new}=y_{old}\cdot\dfrac{\overline{x}_{new}}{\overline{y}_{new}}= y_{old}\cdot\dfrac{x_{old}+\dfrac{x_{in}}{T}dt}{y_{old}+\dfrac{y_{in}}{T}dt}

ynew=xoldβ‹…yβ€Ύnewxβ€Ύnew=xoldβ‹…yold+yinTdtxold+xinTdty_{new}=x_{old}\cdot\dfrac{\overline{y}_{new}}{\overline{x}_{new}}= x_{old}\cdot\dfrac{y_{old}+\dfrac{y_{in}}{T}dt}{x_{old}+\dfrac{x_{in}}{T}dt}

xnew=yoldβ‹…xold+xinTdtyold+yinTdt=kxoldβ‹…xold+xinTdtkxold+yinTdt=kβ‹…xold+xinTdtyinTdtβ‹…xold+kx_{new}=y_{old}\cdot\dfrac{x_{old}+\dfrac{x_{in}}{T}dt}{y_{old}+\dfrac{y_{in}}{T}dt}=\dfrac{k}{x_{old}}\cdot\dfrac{x_{old} + \dfrac{x_{in}}{T}dt}{\dfrac{k}{x_{old}}+\dfrac{y_{in}}{T}dt}=k\cdot\dfrac{x_{old}+\dfrac{x_{in}}{T}dt}{\dfrac{y_{in}}{T}dt\cdot x_{old}+k}

Let a=kxinyin,aβ€Ύ=kyinxin,b=xinyinka=\sqrt{\dfrac{kx_{in}}{y_{in}}},\overline{a}=\sqrt{\dfrac{ky_{in}}{x_{in}}},b=\sqrt{\dfrac{x_{in}y_{in}}{k}}, then,

xnew=xold+abTdtbaTdtβ‹…xold+1x_{new}=\dfrac{x_{old}+\dfrac{ab}{T}dt}{\dfrac{b}{aT}dt\cdot x_{old}+1}

xnewβˆ’axnew+a=(1βˆ’2bTdt1+bTdt)β‹…xoldβˆ’axold+a\dfrac{x_{new}-a}{x_{new}+a}=\left(1-\dfrac{\dfrac{2b}{T}dt}{1+\dfrac{b}{T}dt}\right)\cdot\dfrac{x_{old}-a}{x_{old}+a}

xnewβˆ’axnew+aβˆ’xoldβˆ’axold+axoldβˆ’axold+a=βˆ’2bTdt1+bTdt\dfrac{\dfrac{x_{new}-a}{x_{new}+a}-\dfrac{x_{old}-a}{x_{old}+a}}{\dfrac{x_{old}-a}{x_{old}+a}}=-\dfrac{\dfrac{2b}{T}dt}{1+\dfrac{b}{T}dt}

x+dxβˆ’ax+dx+aβˆ’xβˆ’ax+axβˆ’ax+a=βˆ’2bTdt1+bTdt\dfrac{\dfrac{x+dx-a}{x+dx+a}-\dfrac{x_{}-a}{x_{}+a}}{\dfrac{x_{}-a}{x_{}+a}}=-\dfrac{\dfrac{2b}{T}dt}{1+\dfrac{b}{T}dt}

lim⁑dtβ†’0x+dxβˆ’ax+dx+aβˆ’xβˆ’ax+axβˆ’ax+aβ‹…dt=lim⁑dtβ†’0βˆ’2bT1+bTdt\lim_{dt\rightarrow0}\dfrac{\dfrac{x+dx-a}{x+dx+a}-\dfrac{x_{}-a}{x_{}+a}}{\dfrac{x_{}-a}{x_{}+a}\cdot dt}=\lim_{dt\rightarrow0}\dfrac{-\dfrac{2b}{T}}{1+\dfrac{b}{T}dt}

d(lnxβˆ’ax+a)dt=βˆ’2bT\dfrac{d\left(ln\dfrac{x-a}{x+a}\right)}{dt}=-\dfrac{2b}{T}

lnxammEndβˆ’axammEnd+aβˆ’lnxammStartβˆ’axammStart+a=∫0Tβˆ’2bTdt=βˆ’2bln\dfrac{x_{ammEnd}-a}{x_{ammEnd}+a}-ln\dfrac{x_{ammStart}-a}{x_{ammStart}+a}=\int ^{T}_{0}-\dfrac{2b}{T}dt=-2b

xammEndβˆ’axammEnd+a=eβˆ’2bβ‹…xammStartβˆ’axammStart+a\dfrac{x_{ammEnd}-a}{x_{ammEnd}+a}=e^{-2b}\cdot\dfrac{x_{ammStart}-a}{x_{ammStart}+a}

xammEnd=aβ‹…e2b+ce2bβˆ’c=kxinyinβ‹…e2xinyink+ce2xinyinkβˆ’cx_{ammEnd}=a\cdot\dfrac{e^{2b}+c}{e^{2b}-c}=\sqrt{\dfrac{kx_{in}}{y_{in}}}\cdot\dfrac{e^{2\sqrt{\dfrac{x_{in}y_{in}}{k}}}+c}{e^{2\sqrt{\dfrac{x_{in}y_{in}}{k}}}-c}

Where,

c=x0βˆ’ax0+a=xammStartβ‹…yinβˆ’yammStartβ‹…xinxammStartβ‹…yin+yammStartβ‹…xinc=\dfrac{x_{0}-a}{x_{0}+a}=\dfrac{\sqrt{x_{ammStart}\cdot y_{in}}-\sqrt{y_{ammStart}\cdot x_{in}}}{\sqrt{x_{ammStart}\cdot y_{in}}+\sqrt{y_{ ammStart}\cdot x_{in}}}


If we use f(t)f(t) and g(t)g(t) instead of xinT\dfrac{x_{in}}{T} and yinT\dfrac{y_{in}}{T}, that is , the trading rate of Xβˆ’PoolX-Pool and Yβˆ’PoolY-Pool is a function of time.

xnew=kβ‹…xold+f(t)dtg(t)dtβ‹…xold+kx_{new}=k\cdot\dfrac{x_{old}+f(t)dt}{g(t)dt\cdot x_{old}+k}

Let a=kf(t)g(t),aβ€Ύ=kg(t)f(t),b=f(t)g(t)ka=\sqrt{\dfrac{kf(t)}{g(t)}},\overline{a}=\sqrt{\dfrac{kg(t)}{f(t)}},b=\sqrt{\dfrac{f(t)g(t)}{k}}, then,

xnewβˆ’xold=abβ‹…dtβˆ’badtβ‹…xold2badtβ‹…xold+1x_{new}-x_{old}=\dfrac{ab\cdot dt-\dfrac{b}{a}dt\cdot x_{old}^{2}}{\dfrac{b}{a}dt\cdot x_{old}+1}

xnewβˆ’xolddt=abβˆ’baβ‹…xold2badtβ‹…xold+1\dfrac{x_{new}-x_{old}}{dt}=\dfrac{ab-\dfrac{b}{a}\cdot x_{old}^{2}}{\dfrac{b}{a}dt\cdot x_{old}+1}

lim⁑dtβ†’0(xnewβˆ’xolddt)=lim⁑dtβ†’0(abβˆ’baβ‹…xold2badtβ‹…xold+1)\lim_{dt\rightarrow0}\left(\dfrac{x_{new}-x_{old}}{dt}\right)=\lim_{dt\rightarrow0}\left(\dfrac{ab-\dfrac{b}{a}\cdot x_{old}^{2}}{\dfrac{b}{a}dt\cdot x_{old}+1}\right)

dxdt=abβˆ’baβ‹…x2=f(t)βˆ’g(t)kx2=g(t)kβ‹…(kf(t)g(t)βˆ’x2)\dfrac{dx}{dt}=ab-\dfrac{b}{a}\cdot x^{2}=f(t)-\dfrac{g(t)}{k}x^2=\dfrac{g(t)}{k}\cdot \left(\dfrac{kf(t)}{g(t)}-x^2\right)

Let h(t)=f(t)g(t)=a2kh(t)=\dfrac{f(t)}{g(t)}=\dfrac{a^2}{k},

dxdt=g(t)kβ‹…(kβ‹…h(t)βˆ’x2)\dfrac{dx}{dt}=\dfrac{g(t)}{k}\cdot\left(k\cdot h(t)-x^2\right)

If aa is constant, i.e.the trading rate of Xβˆ’PoolX-Pool and Yβˆ’PoolY-Pool is synchronized.

d(lnxβˆ’ax+a)dt=βˆ’2f(t)g(t)k\dfrac{d(ln\dfrac{x-a}{x+a})}{dt}=-2\sqrt{\dfrac{f(t)g(t)}{k}}

lnxammEndβˆ’a(T)xammEnd+a(T)βˆ’lnxammStartβˆ’a(0)xammStart+a(0)=∫0Tβˆ’2f(t)g(t)kdtln\dfrac{x_{ammEnd}-a(T)}{x_{ammEnd}+a(T)}-ln\dfrac{x_{ammStart}-a(0)}{x_{ammStart}+a(0)}=\int^{T}_{0}-2\sqrt{\dfrac{f(t)g(t)}{k}}dt

xammEnd=kf(T)g(T)β‹…e2∫0Tf(t)g(t)kdt+ce2∫0Tf(t)g(t)kdtβˆ’cx_{ammEnd}=\sqrt{\dfrac{kf(T)}{g(T)}}\cdot\dfrac{e^{2\int^{T}_{0}\sqrt{\dfrac{f(t)g(t)}{k}}dt}+c}{e^{2\int^{T}_{0}\sqrt{\dfrac{f(t)g(t)}{k}}dt}-c}

Where,

c=xammStartβˆ’a(0)xammStart+a(0)=xammStartβˆ’kf(0)g(0)xammStart+kf(0)g(0)=xammStartβ‹…g(0)βˆ’yammStartβ‹…f(0)xammStartβ‹…g(0)+yammStartβ‹…f(0)c=\dfrac{x_{ammStart}-a(0)}{x_{ammStart}+a(0)}=\dfrac{x_{ammStart}-\sqrt{\dfrac{kf(0)}{g(0)}}}{x_{ammStart}+\sqrt{\dfrac{kf(0)}{g(0)}}}=\dfrac{\sqrt{x_{ammStart}\cdot g(0)}-\sqrt{y_{ammStart}\cdot f(0)}}{\sqrt{x_{ammStart}\cdot g(0)}+\sqrt{y_{ ammStart}\cdot f(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].

参考#