Definition# Assume that TWAMM requires N N N blocks to execute a large virtual transaction, and that the pool selling X X X sells at a rate of x r a t e x_{rate} x r a t e β per block, while the pool selling Y Y Y sells at a rate of y r a t e y_{rate} y r a t e β per block. Thus, the total amount of X X X sold during the whole period is x i n = N x r a t e x_{in}=Nx_{rate} x in β = N x r a t e β and the total amount of Y Y Y sold is y i n = N y r a t e y_{in}=Ny_{rate} y in β = N y r a t e β .
Also, we note the initial reserves x r e s e r v e x_{reserve} x reser v e β and y r e s e r v e y_{reserve} y reser v e β for this time period in embedded AMM as x 0 = x a m m S t a r t x_{0}=x_{ammStart} x 0 β = x amm St a r t β and y 0 = y a m m S t a r t y_{0}=y_{ammStart} y 0 β = y amm St a r t β , respectively.
According to the design of TWAMM , large orders are traded with blocks, each block sells x r a t e x_{rate} x r a t e β to get y o u t y_{out} y o u t β , or sells y r a t e y_{rate} y r a t e β to get x o u t x_{out} x o u t β , while AMM updates the values of x r e s e r v e x_{reserve} x reser v e β and y r e s e r v e y_{reserve} y reser v e β , the whole process N N N 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 β 1 n-1 n β 1 block, we assume that the x r e s e r v e x_{reserve} x reser v e β and y r e s e r v e y_{reserve} y reser v e β values of AMM are x n β 1 x_{n-1} x n β 1 β and y n β 1 y_{n-1} y n β 1 β , respectively.
Next, the transaction for block n n n is executed, with X β P o o l X-Pool X β P oo l and Y β P o o l Y-Pool Y β P oo l feeding x r a t e x_{rate} x r a t e β and y r a t e y_{rate} y r a t e β to AMM , respectively. Then,
x βΎ n = x n β 1 + x r a t e \overline{x}_{n}=x_{n-1}+x_{rate} x n β = x n β 1 β + x r a t e β
y βΎ n = y n β 1 + y r a t e \overline{y}_{n}=y_{n-1}+y_{rate} y β n β = y n β 1 β + y r a t e β
Since x r a t e x_{rate} x r a t e β and y r a t e y_{rate} y r a t e β are very small and the exchange follow a constant product market maker, we can obtain that,
x o u t , n y r a t e = x βΎ n y βΎ n = x n β 1 + x r a t e y n β 1 + y r a t e \dfrac{x_{out,n}}{y_{rate}}=\dfrac{\overline{x}_{n}}{\overline{y}_{n}}=\dfrac{x_{n-1}+x_{rate}}{y_{n-1} +y_{rate}} y r a t e β x o u t , n β β = y β n β x n β β = y n β 1 β + y r a t e β x n β 1 β + x r a t e β β
y o u t , n x r a t e = y βΎ n x βΎ n = y n β 1 + y r a t e x n β 1 + x r a t e \dfrac{y_{out,n}}{x_{rate}}=\dfrac{\overline{y}_{n}}{\overline{x}_{n}}=\dfrac{y_{n-1}+y_{rate}}{x_{n-1}+ x_{rate}} x r a t e β y o u t , n β β = x n β y β n β β = x n β 1 β + x r a t e β y n β 1 β + y r a t e β β
Simplifying,
x o u t , n = y r a t e β
x βΎ n y βΎ n = y r a t e β
x n β 1 + x r a t e y n β 1 + y r a t e x_{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}} x o u t , n β = y r a t e β β
y β n β x n β β = y r a t e β β
y n β 1 β + y r a t e β x n β 1 β + x r a t e β β
y o u t , n = x r a t e β
y βΎ n x βΎ n = x r a t e β
y n β 1 + y r a t e x n β 1 + x r a t e y_{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}} y o u t , n β = x r a t e β β
x n β y β n β β = x r a t e β β
x n β 1 β + x r a t e β y n β 1 β + y r a t e β β
After getting the values of x o u t , n x_{out,n} x o u t , n β and y o u t , n y_{out,n} y o u t , n β , we can further obtain the x r e s e r v e x_{reserve} x reser v e β and y r e s e r v e y_{reserve} y reser v e β values x n x_{n} x n β and y n y_{n} y n β after the transaction of n n n blocks.
x n = x βΎ n β x o u t , n = x βΎ n β y r a t e β
x βΎ n y βΎ n = y n β 1 β
x βΎ n y βΎ n = y n β 1 β
x n β 1 + x r a t e y n β 1 + y r a t e x_{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}} x n β = x n β β x o u t , n β = x n β β y r a t e β β
y β n β x n β β = y n β 1 β β
y β n β x n β β = y n β 1 β β
y n β 1 β + y r a t e β x n β 1 β + x r a t e β β
y n = y βΎ n β y o u t , n = y βΎ n β x r a t e β
y βΎ n x βΎ n = x n β 1 β
y βΎ n x βΎ n = x n β 1 β
y n β 1 + y r a t e x n β 1 + x r a t e y_{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}} y n β = y β n β β y o u t , n β = y β n β β x r a t e β β
x n β y β n β β = x n β 1 β β
x n β y β n β β = x n β 1 β β
x n β 1 β + x r a t e β y n β 1 β + y r a t e β β
A v e r a g e Β P r i c e Β P y x = β n = 1 N x o u t , n N y r a t e = x o u t y i n Average\ Price\ P^{x}_{y}=\sum_{n=1}^{N}\dfrac{x_{out,n}}{Ny_{rate}}=\dfrac{x_{out}}{y_{in}} A v er a g e Β P r i ce Β P y x β = β n = 1 N β N y r a t e β x o u t , n β β = y in β x o u t β β
A v e r a g e Β P r i c e Β P x y = β n = 1 N y o u t , n N x r a t e = y o u t x i n Average\ Price\ P^{y}_{x}=\sum_{n=1}^{N}\dfrac{y_{out,n}}{Nx_{rate}}=\dfrac{y_{out}}{x_{in}} A v er a g e Β P r i ce Β P x y β = β n = 1 N β N x r a t e β y o u t , n β β = x in β y o u t β β
By observation, we find that x n β
y n = x n β 1 β
y n β 1 x_{n}\cdot y_{n}= x_{n-1}\cdot y_{n-1} x n β β
y n β = x n β 1 β β
y n β 1 β , which exactly meets the prerequisite requirement of AMM for making a market by following a constant product.
Let x n y n = x n β 1 y n β 1 = . . . = x 1 y 1 = x 0 y 0 = k x_{n}y_{n}=x_{n-1}y_{n-1}=... = x_{1}y_{1}=x_{0}y_{0}=k x n β y n β = x n β 1 β y n β 1 β = ... = x 1 β y 1 β = x 0 β y 0 β = k , k k k is a constant.
Fractional Linear Recursion# First find the general formula for x n x_{n} x n β and obtain the value of x a m m E n d = x N x_{ammEnd}=x_{N} x amm E n d β = x N β , and the same for y n y_{n} y n β .
x n = y n β 1 β
x n β 1 + x r a t e y n β 1 + y r a t e = k x n β 1 β
x n β 1 + x i n N k x n β 1 + y i n N = k β
x n β 1 + x i n N y i n N β
x n β 1 + k x_{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} x n β = y n β 1 β β
y n β 1 β + y r a t e β x n β 1 β + x r a t e β β = x n β 1 β k β β
x n β 1 β k β + N y in β β x n β 1 β + N x in β β β = k β
N y in β β β
x n β 1 β + k x n β 1 β + N x in β β β
Let a = k x i n y i n , a βΎ = k y i n x i n , b = x i n y i n k a=\sqrt{\dfrac{kx_{in}}{y_{in}}},\overline{a}=\sqrt{\dfrac{ky_{in}}{x_{in}}},b=\sqrt{\dfrac{x_{in}y_{in}}{k}} a = y in β k x in β β β , a = x in β k y in β β β , b = k x in β y in β β β , then
x n = x n β 1 + a b N b a N β
x n β 1 + 1 x_{n}=\dfrac{x_{n-1}+\dfrac{ab}{N}}{\dfrac{b}{aN}\cdot x_{n-1}+1} x n β = a N b β β
x n β 1 β + 1 x n β 1 β + N ab β β
The fractional linear recursive expression for y n y_{n} y n β is as follows:
y n = k β
y n β 1 + y i n N x i n N β
y n β 1 + k = y n β 1 + a βΎ b N b a βΎ N β
y n β 1 + 1 y_{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} y n β = k β
N x in β β β
y n β 1 β + k y n β 1 β + N y in β β β = a N b β β
y n β 1 β + 1 y n β 1 β + N a b β β
Solution# First, if y i n = 0 y_{in}=0 y in β = 0 and x i n β 0 x_{in}\neq0 x in β ξ = 0 , then
x n = x 0 + x i n N β
n , x a m m E n d = x N = x 0 + x i n , x o u t = x 0 + x i n β x a m m E n d = 0 x_{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 x n β = x 0 β + N x in β β β
n , x amm E n d β = x N β = x 0 β + x in β , x o u t β = x 0 β + x in β β x amm E n d β = 0
And,
y n = k β
y n β 1 x i n N β
y n β 1 + k , 1 y n = 1 y n β 1 + x i n k N y_{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} y n β = k β
N x in β β β
y n β 1 β + k y n β 1 β β , y n β 1 β = y n β 1 β 1 β + k N x in β β
The calculation yields,
y n = 1 1 y 0 + x i n k N β
n , y a m m E n d = y N = k x 0 + x i n y_{n}=\dfrac{1}{\dfrac{1}{y_{0}}+\dfrac{x_{in}}{kN}\cdot n},y_{ammEnd}=y_{N}=\dfrac{k}{x_{0}+x_{in}} y n β = y 0 β 1 β + k N x in β β β
n 1 β , y amm E n d β = y N β = x 0 β + x in β k β
y o u t = y 0 + y i n β y a m m E n d = y 0 x 0 + x i n β
x i n y_{out}=y_{0}+y_{in}-y_{ammEnd}=\dfrac{y_{0}}{x_{0}+x_{in}}\cdot x_{in} y o u t β = y 0 β + y in β β y amm E n d β = x 0 β + x in β y 0 β β β
x in β
A v e r a g e Β P r i c e Β P x y = y o u t x i n = y 0 x 0 + x i n Average\ Price\ P^{y}_{x}=\dfrac{y_{out}}{x_{in}}=\dfrac{y_{0}}{x_{0}+x_{in}} A v er a g e Β P r i ce Β P x y β = x in β y o u t β β = x 0 β + x in β y 0 β β
When x i n = 0 x_{in}=0 x in β = 0 and y i n β 0 y_{in}\neq0 y in β ξ = 0 , the discussion is the same as above.
If both x i n β 0 x_{in}\neq0 x in β ξ = 0 and y i n β 0 y_{in}\neq0 y in β ξ = 0 , for general fractional linear recursion, we can use the Fixed-Point Iteration method to solve the problem.
According to x n = x n β 1 + a b N b a N β
x n β 1 + 1 x_{n}=\dfrac{x_{n-1}+\dfrac{ab}{N}}{ \dfrac{b}{aN}\cdot x_{n-1}+1} x n β = a N b β β
x n β 1 β + 1 x n β 1 β + N ab β β , replace Ξ» = Ξ» + a b N b a N β
Ξ» + 1 \lambda=\dfrac{\lambda+\dfrac{ab}{N}}{\dfrac{b}{aN}\cdot\lambda+1} Ξ» = a N b β β
Ξ» + 1 Ξ» + N ab β β and solve for Ξ» = Β± a \lambda=\pm{a} Ξ» = Β± a . Β± a \pm{a} Β± a is exactly the two fixed points of the above fractional linear recursive expression.
Further calculations,
x n β a = a ( N b β 1 ) ( x n β 1 β a ) x n β 1 + a N b , x n + a = a ( N b + 1 ) ( x n β 1 + a ) x n β 1 + a N b x_{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}} x n β β a = x n β 1 β + b a N β a ( b N β β 1 ) ( x n β 1 β β a ) β , x n β + a = x n β 1 β + b a N β a ( b N β + 1 ) ( x n β 1 β + a ) β
Dividing the two expressions above,
x n β a x n + a = N β b N + b β
x n β 1 β a x n β 1 + a = ( 1 β 2 b N + b ) β
x n β 1 β a x n β 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} x n β + a x n β β a β = N + b N β b β β
x n β 1 β + a x n β 1 β β a β = ( 1 β N + b 2 b β ) β
x n β 1 β + a x n β 1 β β a β
According to the geometric progression we get,
x n β a x n + a = ( 1 β 2 b N + b ) n β
x 0 β a x 0 + a , x N β a x N + a = ( 1 β 2 b N + b ) N β
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},\dfrac{x_{N}-a}{x_{N}+a}=(1-\dfrac{2b}{N+b})^{ N}\cdot\dfrac{x_{0}-a}{x_{0}+a} x n β + a x n β β a β = ( 1 β N + b 2 b β ) n β
x 0 β + a x 0 β β a β , x N β + a x N β β a β = ( 1 β N + b 2 b β ) N β
x 0 β + a x 0 β β a β
Similarly,
y n β a βΎ y n + a βΎ = ( 1 β 2 b N + b ) n β
y 0 β a βΎ y 0 + a βΎ , y N β a βΎ y N + a βΎ = ( 1 β 2 b N + b ) N β
y 0 β a βΎ y 0 + 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}} y n β + a y n β β a β = ( 1 β N + b 2 b β ) n β
y 0 β + a y 0 β β a β , y N β + a y N β β a β = ( 1 β N + b 2 b β ) N β
y 0 β + a y 0 β β 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 N N can be taken to infinity, so the limit value of x N x_{N} x N β can be obtained.
x a m m E n d β a x a m m E n d + a = lim β‘ N β + β ( x N β a x N + a ) = lim β‘ N β + β ( 1 β 2 b N + b ) N β
x 0 β a x 0 + a = e β 2 b β
x 0 β a x 0 + 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} x amm E n d β + a x amm E n d β β a β = lim N β + β β ( x N β + a x N β β a β ) = lim N β + β β ( 1 β N + b 2 b β ) N β
x 0 β + a x 0 β β a β = e β 2 b β
x 0 β + a x 0 β β a β
x a m m E n d = a β
e 2 b + c e 2 b β c = k x i n y i n β
e 2 x i n y i n k + c e 2 x i n y i n k β c x_{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} x amm E n d β = a β
e 2 b β c e 2 b + c β = y in β k x in β β β β
e 2 k x in β y in β β β β c e 2 k x in β y in β β β + c β
WhereοΌ
c = x 0 β a x 0 + a = x a m m S t a r t β
y i n β y a m m S t a r t β
x i n x a m m S t a r t β
y i n + y a m m S t a r t β
x i n c=\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}}} c = x 0 β + a x 0 β β a β = x amm St a r t β β
y in β β + y amm St a r t β β
x in β β x amm St a r t β β
y in β β β y amm St a r t β β
x in β β β
x o u t = x a m m S t a r t + x i n β x a m m E n d x_{out}=x_{ammStart}+x_{in}-x_{ammEnd} x o u t β = x amm St a r t β + x in β β x amm E n d β
A v e r a g e Β P r i c e Β P y x = x o u t y i n = x a m m S t a r t + x i n β x a m m E n d y i n Average\ Price\ P^{x}_{y}=\dfrac{x_{out}}{y_{in}}=\dfrac{x_{ammStart}+x_{in}-x_{ammEnd}}{y_{in}} A v er a g e Β P r i ce Β P y x β = y in β x o u t β β = y in β x amm St a r t β + x in β β x amm E n d β β
Similarly,
y a m m E n d = k y i n x i n β
e 2 x i n y i n k + c βΎ e 2 x i n y i n k β 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}} y amm E n d β = x in β k y in β β β β
e 2 k x in β y in β β β β c e 2 k x in β y in β β β + c β
c βΎ = y a m m S t a r t β
x i n β x a m m S t a r t β
y i n x a m m S t a r t β
y i n + y a m m S t a r t β
x i n = β 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 c = x amm St a r t β β
y in β β + y amm St a r t β β
x in β β y amm St a r t β β
x in β β β x amm St a r t β β
y in β β β = β c
y o u t = y a m m S t a r t + y i n β y a m m E n d y_{out}=y_{ammStart}+y_{in}-y_{ammEnd} y o u t β = y amm St a r t β + y in β β y amm E n d β
A v e r a g e Β P r i c e Β P x y = y o u t x i n = y a m m S t a r t + y i n β y a m m E n d x i n Average\ Price\ P^{y}_{x}=\dfrac{y_{out}}{x_{in}}=\dfrac{y_{ammStart}+y_{in}-y_{ammEnd}}{x_{in}} A v er a g e Β P r i ce Β P x y β = x in β y o u t β β = x in β y amm St a r t β + y in β β y amm E n d β β
An important point is thatοΌx o u t x_{out} x o u t β , y o u t y_{out} y o u t β , x a m m E n d x_{ammEnd} x amm E n d β and y a m m E n d y_{ammEnd} y amm E n d β must larger than 0:
e 2 x i n y i n k > β£ c β£ = β£ x a m m S t a r t β
y i n β y a m m S t a r t β
x i n x a m m S t a r t β
y i n + y a m m S t a r t β
x i n β£ 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 e 2 k x in β y in β β β > β£ c β£ = β£ β£ β x amm St a r t β β
y in β β + y amm St a r t β β
x in β β x amm St a r t β β
y in β β β y amm St a r t β β
x in β β β β£ β£ β
Finally, after a simple verification x a m m E n d β
y a m m E n d = x a m m S t a r t β
y a m m S t a r t = k x_{ammEnd}\cdot y_{ammEnd}=x_{ammStart}\cdot y_{ammStart}=k x amm E n d β β
y amm E n d β = x amm St a r t β β
y amm St a r t β = k , the constant product of AMM is still satisfied.
When y i n β 0 , x i n β 0 y_{in}\rightarrow0, x_{in}\neq0 y in β β 0 , x in β ξ = 0 οΌthrough L'HΓ΄pital's Rule , we can obtain:
y a m m E n d = lim β‘ y i n β 0 ( k y i n x i n β
e 2 x i n y i n k + c βΎ e 2 x i n y i n k β 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) y amm E n d β = lim y in β β 0 β β β β x in β k y in β β β β
e 2 k x in β y in β β β β c e 2 k x in β y in β β β + c β β β β
= lim β‘ y i n β 0 ( k y i n x i n β
e 2 x i n y i n k + y a m m S t a r t β
x i n β x a m m S t a r t β
y i n x a m m S t a r t β
y i n + y a m m S t a r t β
x i n e 2 x i n y i n k β y a m m S t a r t β
x i n β x a m m S t a r t β
y i n x a m m S t a r t β
y i n + y a m m S t a r t β
x i n ) =\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) = lim y in β β 0 β β β β x in β k y in β β β β
e 2 k x in β y in β β β β x amm St a r t β β
y in β β + y amm St a r t β β
x in β β y amm St a r t β β
x in β β β x amm St a r t β β
y in β β β e 2 k x in β y in β β β + x amm St a r t β β
y in β β + y amm St a r t β β
x in β β y amm St a r t β β
x in β β β x amm St a r t β β
y in β β β β β β β
= 1 1 y a m m S t a r t + x i n k = y a m m S t a r t β
x a m m S t a r t x a m m S t a r t + x i n =\dfrac{1}{\dfrac{1}{y_{ammStart}}+\dfrac{x_{in}}{k}}=\dfrac{y_{ammStart}\cdot x_{ammStart}}{x_{ammStart}+x_{in}} = y amm St a r t β 1 β + k x in β β 1 β = x amm St a r t β + x in β y amm St a r t β β
x amm St a r t β β
x a m m E n d = lim β‘ y i n β 0 ( k x i n y i n β
e 2 x i n y i n k β c βΎ e 2 x i n y i n k + 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) x amm E n d β = lim y in β β 0 β β β β y in β k x in β β β β
e 2 k x in β y in β β β + c e 2 k x in β y in β β β β c β β β β
= lim β‘ y i n β 0 ( k x i n y i n β
e 2 x i n y i n k β y a m m S t a r t β
x i n β x a m m S t a r t β
y i n x a m m S t a r t β
y i n + y a m m S t a r t β
x i n e 2 x i n y i n k + y a m m S t a r t β
x i n β x a m m S t a r t β
y i n x a m m S t a r t β
y i n + y a m m S t a r t β
x i n ) =\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) = lim y in β β 0 β β β β y in β k x in β β β β
e 2 k x in β y in β β β + x amm St a r t β β
y in β β + y amm St a r t β β
x in β β y amm St a r t β β
x in β β β x amm St a r t β β
y in β β β e 2 k x in β y in β β β β x amm St a r t β β
y in β β + y amm St a r t β β
x in β β y amm St a r t β β
x in β β β x amm St a r t β β
y in β β β β β β β
= x a m m S t a r t + x i n ={x_{ammStart}+x_{in}} = x amm St a r t β + x in β
Differential Equation Of TWAMM# Assume the trading rate of X β P o o l X-Pool X β P oo l and Y β P o o l Y-Pool Y β P oo l is uniform according to timeοΌ
x βΎ n e w = x o l d + x i n T d t \overline{x}_{new}=x_{old}+\dfrac{x_{in}}{T}dt x n e w β = x o l d β + T x in β β d t
y βΎ n e w = y o l d + y i n T d t \overline{y}_{new}=y_{old}+\dfrac{y_{in}}{T}dt y β n e w β = y o l d β + T y in β β d t
x n e w = y o l d β
x βΎ n e w y βΎ n e w = y o l d β
x o l d + x i n T d t y o l d + y i n T d t x_{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} x n e w β = y o l d β β
y β n e w β x n e w β β = y o l d β β
y o l d β + T y in β β d t x o l d β + T x in β β d t β
y n e w = x o l d β
y βΎ n e w x βΎ n e w = x o l d β
y o l d + y i n T d t x o l d + x i n T d t y_{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} y n e w β = x o l d β β
x n e w β y β n e w β β = x o l d β β
x o l d β + T x in β β d t y o l d β + T y in β β d t β
x n e w = y o l d β
x o l d + x i n T d t y o l d + y i n T d t = k x o l d β
x o l d + x i n T d t k x o l d + y i n T d t = k β
x o l d + x i n T d t y i n T d t β
x o l d + k x_{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} x n e w β = y o l d β β
y o l d β + T y in β β d t x o l d β + T x in β β d t β = x o l d β k β β
x o l d β k β + T y in β β d t x o l d β + T x in β β d t β = k β
T y in β β d t β
x o l d β + k x o l d β + T x in β β d t β
Let a = k x i n y i n , a βΎ = k y i n x i n , b = x i n y i n k a=\sqrt{\dfrac{kx_{in}}{y_{in}}},\overline{a}=\sqrt{\dfrac{ky_{in}}{x_{in}}},b=\sqrt{\dfrac{x_{in}y_{in}}{k}} a = y in β k x in β β β , a = x in β k y in β β β , b = k x in β y in β β β , then,
x n e w = x o l d + a b T d t b a T d t β
x o l d + 1 x_{new}=\dfrac{x_{old}+\dfrac{ab}{T}dt}{\dfrac{b}{aT}dt\cdot x_{old}+1} x n e w β = a T b β d t β
x o l d β + 1 x o l d β + T ab β d t β
x n e w β a x n e w + a = ( 1 β 2 b T d t 1 + b T d t ) β
x o l d β a x o l d + 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} x n e w β + a x n e w β β a β = β β β 1 β 1 + T b β d t T 2 b β d t β β β β β
x o l d β + a x o l d β β a β
x n e w β a x n e w + a β x o l d β a x o l d + a x o l d β a x o l d + a = β 2 b T d t 1 + b T d t \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 o l d β + a x o l d β β a β x n e w β + a x n e w β β a β β x o l d β + a x o l d β β a β β = β 1 + T b β d t T 2 b β d t β
x + d x β a x + d x + a β x β a x + a x β a x + a = β 2 b T d t 1 + b T d t \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} x β + a x β β a β x + d x + a x + d x β a β β x β + a x β β a β β = β 1 + T b β d t T 2 b β d t β
lim β‘ d t β 0 x + d x β a x + d x + a β x β a x + a x β a x + a β
d t = lim β‘ d t β 0 β 2 b T 1 + b T d t \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} lim d t β 0 β x β + a x β β a β β
d t x + d x + a x + d x β a β β x β + a x β β a β β = lim d t β 0 β 1 + T b β d t β T 2 b β β
d ( l n x β a x + a ) d t = β 2 b T \dfrac{d\left(ln\dfrac{x-a}{x+a}\right)}{dt}=-\dfrac{2b}{T} d t d ( l n x + a x β a β ) β = β T 2 b β
l n x a m m E n d β a x a m m E n d + a β l n x a m m S t a r t β a x a m m S t a r t + a = β« 0 T β 2 b T d t = β 2 b ln\dfrac{x_{ammEnd}-a}{x_{ammEnd}+a}-ln\dfrac{x_{ammStart}-a}{x_{ammStart}+a}=\int ^{T}_{0}-\dfrac{2b}{T}dt=-2b l n x amm E n d β + a x amm E n d β β a β β l n x amm St a r t β + a x amm St a r t β β a β = β« 0 T β β T 2 b β d t = β 2 b
x a m m E n d β a x a m m E n d + a = e β 2 b β
x a m m S t a r t β a x a m m S t a r t + a \dfrac{x_{ammEnd}-a}{x_{ammEnd}+a}=e^{-2b}\cdot\dfrac{x_{ammStart}-a}{x_{ammStart}+a} x amm E n d β + a x amm E n d β β a β = e β 2 b β
x amm St a r t β + a x amm St a r t β β a β
x a m m E n d = a β
e 2 b + c e 2 b β c = k x i n y i n β
e 2 x i n y i n k + c e 2 x i n y i n k β c x_{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} x amm E n d β = a β
e 2 b β c e 2 b + c β = y in β k x in β β β β
e 2 k x in β y in β β β β c e 2 k x in β y in β β β + c β
WhereοΌ
c = x 0 β a x 0 + a = x a m m S t a r t β
y i n β y a m m S t a r t β
x i n x a m m S t a r t β
y i n + y a m m S t a r t β
x i n c=\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}}} c = x 0 β + a x 0 β β a β = x amm St a r t β β
y in β β + y amm St a r t β β
x in β β x amm St a r t β β
y in β β β y amm St a r t β β
x in β β β
If we use f ( t ) f(t) f ( t ) and g ( t ) g(t) g ( t ) instead of x i n T \dfrac{x_{in}}{T} T x in β β and y i n T \dfrac{y_{in}}{T} T y in β β , that is , the trading rate of X β P o o l X-Pool X β P oo l and Y β P o o l Y-Pool Y β P oo l is a function of time.
x n e w = k β
x o l d + f ( t ) d t g ( t ) d t β
x o l d + k x_{new}=k\cdot\dfrac{x_{old}+f(t)dt}{g(t)dt\cdot x_{old}+k} x n e w β = k β
g ( t ) d t β
x o l d β + k x o l d β + f ( t ) d t β
Let a = k f ( t ) g ( t ) , a βΎ = k g ( t ) f ( t ) , b = f ( t ) g ( t ) k a=\sqrt{\dfrac{kf(t)}{g(t)}},\overline{a}=\sqrt{\dfrac{kg(t)}{f(t)}},b=\sqrt{\dfrac{f(t)g(t)}{k}} a = g ( t ) k f ( t ) β β , a = f ( t ) k g ( t ) β β , b = k f ( t ) g ( t ) β β , then,
x n e w β x o l d = a b β
d t β b a d t β
x o l d 2 b a d t β
x o l d + 1 x_{new}-x_{old}=\dfrac{ab\cdot dt-\dfrac{b}{a}dt\cdot x_{old}^{2}}{\dfrac{b}{a}dt\cdot x_{old}+1} x n e w β β x o l d β = a b β d t β
x o l d β + 1 ab β
d t β a b β d t β
x o l d 2 β β
x n e w β x o l d d t = a b β b a β
x o l d 2 b a d t β
x o l d + 1 \dfrac{x_{new}-x_{old}}{dt}=\dfrac{ab-\dfrac{b}{a}\cdot x_{old}^{2}}{\dfrac{b}{a}dt\cdot x_{old}+1} d t x n e w β β x o l d β β = a b β d t β
x o l d β + 1 ab β a b β β
x o l d 2 β β
lim β‘ d t β 0 ( x n e w β x o l d d t ) = lim β‘ d t β 0 ( a b β b a β
x o l d 2 b a d t β
x o l d + 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) lim d t β 0 β ( d t x n e w β β x o l d β β ) = lim d t β 0 β β β β a b β d t β
x o l d β + 1 ab β a b β β
x o l d 2 β β β β β
d x d t = a b β b a β
x 2 = f ( t ) β g ( t ) k x 2 = g ( t ) k β
( k f ( t ) g ( t ) β x 2 ) \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) d t d x β = ab β a b β β
x 2 = f ( t ) β k g ( t ) β x 2 = k g ( t ) β β
( g ( t ) k f ( t ) β β x 2 )
Let h ( t ) = f ( t ) g ( t ) = a 2 k h(t)=\dfrac{f(t)}{g(t)}=\dfrac{a^2}{k} h ( t ) = g ( t ) f ( t ) β = k a 2 β ,
d x d t = g ( t ) k β
( k β
h ( t ) β x 2 ) \dfrac{dx}{dt}=\dfrac{g(t)}{k}\cdot\left(k\cdot h(t)-x^2\right) d t d x β = k g ( t ) β β
( k β
h ( t ) β x 2 )
If a a a is constant, i.e.the trading rate of X β P o o l X-Pool X β P oo l and Y β P o o l Y-Pool Y β P oo l is synchronized.
d ( l n x β a x + a ) d t = β 2 f ( t ) g ( t ) k \dfrac{d(ln\dfrac{x-a}{x+a})}{dt}=-2\sqrt{\dfrac{f(t)g(t)}{k}} d t d ( l n x + a x β a β ) β = β 2 k f ( t ) g ( t ) β β
l n x a m m E n d β a ( T ) x a m m E n d + a ( T ) β l n x a m m S t a r t β a ( 0 ) x a m m S t a r t + a ( 0 ) = β« 0 T β 2 f ( t ) g ( t ) k d t ln\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 l n x amm E n d β + a ( T ) x amm E n d β β a ( T ) β β l n x amm St a r t β + a ( 0 ) x amm St a r t β β a ( 0 ) β = β« 0 T β β 2 k f ( t ) g ( t ) β β d t
x a m m E n d = k f ( T ) g ( T ) β
e 2 β« 0 T f ( t ) g ( t ) k d t + c e 2 β« 0 T f ( t ) g ( t ) k d t β c x_{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} x amm E n d β = g ( T ) k f ( T ) β β β
e 2 β« 0 T β k f ( t ) g ( t ) β β d t β c e 2 β« 0 T β k f ( t ) g ( t ) β β d t + c β
WhereοΌ
c = x a m m S t a r t β a ( 0 ) x a m m S t a r t + a ( 0 ) = x a m m S t a r t β k f ( 0 ) g ( 0 ) x a m m S t a r t + k f ( 0 ) g ( 0 ) = x a m m S t a r t β
g ( 0 ) β y a m m S t a r t β
f ( 0 ) x a m m S t a r t β
g ( 0 ) + y a m m S t a r t β
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)}} c = x amm St a r t β + a ( 0 ) x amm St a r t β β a ( 0 ) β = x amm St a r t β + g ( 0 ) k f ( 0 ) β β x amm St a r t β β g ( 0 ) k f ( 0 ) β β β = x amm St a r t β β
g ( 0 ) β + y amm St a r t β β
f ( 0 ) β x amm St a r t β β
g ( 0 ) β β y amm St a r t β β
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 ] .
εθ#