To protect LPs from front-run when oracle price is delayed and to simulate market makersβ behavior when a large order is traded, Symmetry implements an "Adjusted AMM mechanism".
Liquidity is uniformly distributed between β Ξ» -\lambda β Ξ» and. Outside this range, LP provides extra linear liquidity, which has the same liquidity distribution between and Ξ» \lambda Ξ» .
Parameters:
oracle price before the trade
mid price before the trade
takerβs buy price before the trade
oracle price after the trade
mid price after the trade
takerβs buy price after the trade
Calculation
AMM formula is as below:
Before trade is executed, p r e m = Ξ» β
s p r β
L P prem = \frac { \lambda \cdot s} {pr \cdot LP} p re m = p r β
L P Ξ» β
s β ,
After trade is executed, p r e m β² = Ξ» β
s β² p r β
L P prem' =\frac {\lambda \cdot s'} {pr \cdot LP} p re m β² = p r β
L P Ξ» β
s β² β ,
where s β² = s + T s' = s + T s β² = s + T , T T T is trade quantity.
Therefore,
p m i d = p o r a c l e ( 1 + p r e m ) = p o r a c l e ( 1 + Ξ» β
s p r β
L P ) p_{mid} = p_{oracle}(1+prem)
= p_{oracle}
\left(1 + \frac { \lambda \cdot s}
{pr \cdot LP}\right) p mi d β = p or a c l e β ( 1 + p re m ) = p or a c l e β ( 1 + p r β
L P Ξ» β
s β ) p m i d β² = p o r a c l e β² ( 1 + p r e m β² ) = p o r a c l e β² ( 1 + Ξ» β
( s + T ) p r β
L P ) p'_{mid} = p'_{oracle}(1+prem') = p'_{oracle}
\left(1 + \frac { \lambda \cdot (s+T)}
{pr \cdot LP}\right) p mi d β² β = p or a c l e β² β ( 1 + p re m β² ) = p or a c l e β² β ( 1 + p r β
L P Ξ» β
( s + T ) β ) Besides p o r a c l e p_{oracle} p or a c l e β and p m i d p_{mid} p mi d β , smart contract also keeps track of the taker's buy price p b u y p_{buy} p b u y β and sell price p s e l l p_{sell} p se ll β .
γUserβs Buy Price & Sell Priceγ
After the trade, when p m i d p_{mid} p mi d β is between p b u y p_{buy} p b u y β and p s e l l p_{sell} p se ll β ., both prices gradually move towards p m i d β² p'_{mid} p mi d β² β at a fixed speed, reaching p m i d β² p'_{mid} p mi d β² β after 60 seconds.
When p m i d β² p'_{mid} p mi d β² β exceeds either of these two prices, the crossed price moves immediately to p m i d β² p'_{mid} p mi d β² β . And the other one will gradually move towards p m i d β² p'_{mid} p mi d β² β at a fixed speed, reaching p m i d β² p'_{mid} p mi d β² β after 60 seconds.
Formally speaking, after t t t seconds following a trade, these two prices change as follows.
p b u y β² = { m a x ( t β
p m i d β² + ( 60 β t ) β
p b u y 60 , p m i d β² ) t < 60 p m i d β² t β₯ 60 p s e l l β² = { m i n ( t β
p m i d β² + ( 60 β t ) β
p s e l l 60 , p m i d β² ) t < 60 p m i d β² t β₯ 60 \begin{align*}
p'_{buy} &=
\begin{dcases}
max\left(\frac{t \cdot p'_{mid} + (60 - t) \cdot p_{buy}}{60}, p'_{mid} \right) && t < 60
\\
p'_{mid} && t \ge 60\\
\end{dcases} \\
p'_{sell} &=
\begin{dcases}
min \left(\frac{t \cdot p'_{mid} + (60 - t) \cdot p_{sell}}{60},p'_{mid} \right) && t < 60
\\
p'_{mid} && t \ge 60\\
\end{dcases}
\end{align*} p b u y β² β p se ll β² β β = β© β¨ β§ β ma x ( 60 t β
p mi d β² β + ( 60 β t ) β
p b u y β β , p mi d β² β ) p mi d β² β β β t < 60 t β₯ 60 β = β© β¨ β§ β min ( 60 t β
p mi d β² β + ( 60 β t ) β
p se ll β β , p mi d β² β ) p mi d β² β β β t < 60 t β₯ 60 β β γExecution Priceγ
A trade is executed as if it is divided into infinite tiny pieces, which are executed one by one. For example, assume that a buy trade of volume T T T changes the mid price from p m i d p_{mid} p mi d β to p m i d β² p'_{mid} p mi d β² β and the userβs buy price is p b u y p_{buy} p b u y β before the trade. Its turnover is
p e x e c β
T = β« 0 T max β‘ ( v β
p m i d + ( T β v ) β
p m i d β² T , p b u y ) d v p_{exec} \cdot T =
\int_0^T
\max\left(
\frac{v \cdot p_{mid} + (T - v) \cdot p'_{mid}}{T},
p_{buy}
\right)
\mathrm{d}v p e x ec β β
T = β« 0 T β max ( T v β
p mi d β + ( T β v ) β
p mi d β² β β , p b u y β ) d v Therefore, the execution price for userβs buy order is
p e x e c = { p b u y p m i d β² β€ p b u y ( p b u y β p m i d ) β
p b u y + ( p m i d β² β p b u y ) β
p m i d β² + p b u y 2 p m i d β² β p m i d p m i d β² > p b u y p_{exec} =
\begin{dcases}
p_{buy} & p'_{mid} \le p_{buy} \\
\frac{(p_{buy} - p_{mid}) \cdot p_{buy} + (p'_{mid} - p_{buy}) \cdot \frac{p'_{mid} + p_{buy}}{2}}{p'_{mid} - p_{mid}} & p'_{mid} > p_{buy}
\end{dcases} p e x ec β = β© β¨ β§ β p b u y β p mi d β² β β p mi d β ( p b u y β β p mi d β ) β
p b u y β + ( p mi d β² β β p b u y β ) β
2 p mi d β² β + p b u y β β β β p mi d β² β β€ p b u y β p mi d β² β > p b u y β β Same as above, the execution notional for userβs sell order is
p e x e c β
T = β« 0 T min β‘ ( v β
p m i d + ( T β v ) β
p m i d β² T , p s e l l ) d v p_{exec} \cdot T =
\int_0^T
\min\left(
\frac{v \cdot p_{mid} + (T - v) \cdot p'_{mid}}{T},
p_{sell}
\right)
\mathrm{d}v p e x ec β β
T = β« 0 T β min ( T v β
p mi d β + ( T β v ) β
p mi d β² β β , p se ll β ) d v Example
Initial State
L P LP L P = 100,000,000 (assume it does not change in this example)
Ξ± \alpha Ξ± = 1
Ξ» \lambda Ξ» = 0.05
p r pr p r = 0.5
p o r a c l e p_{oracle} p or a c l e β = 20000
s = 0
p m i d p_{mid} p mi d β = p b u y p_{buy} p b u y β = p s e l l p_{sell} p se ll β = 20000
Trade #1 at time t = 0s
State before the trade
s = 0 s = 0 s = 0
p m i d = p b u y = p s e l l = 20000 p_{mid} = p_{buy} = p_{sell} = 20000 p mi d β = p b u y β = p se ll β = 20000
User sells $40,000,000
s β² = β 40 , 000 , 000 s' = -40,000,000 s β² = β 40 , 000 , 000
p m i d β² = 20000 Γ ( 1 + 1 Γ 0.05 Γ β 40 , 000 , 000 0.5 Γ 100 , 000 , 000 ) = 19200 p'_{mid} = 20000 \times \left( 1 + \frac{1 \times 0.05 \times -40,000,000}{0.5 \times 100,000,000} \right) = 19200 p mi d β² β = 20000 Γ ( 1 + 0.5 Γ 100 , 000 , 000 1 Γ 0.05 Γβ 40 , 000 , 000 β ) = 19200
p β² s e l l = min β‘ ( p s e l l , p m i d β² ) = 19200 p'{sell} = \min(p_{sell}, p'_{mid}) = 19200 p β² se ll = min ( p se ll β , p mi d β² β ) = 19200
The trade volume is evenly distributed between p s e l l p_{sell} p se ll β and p s e l l β² p'_{sell} p se ll β² β .
p e x e c = p s e l l + p s e l l β² 2 = 19600 p_{exec} = \frac{p_{sell} + p'_{sell}}{2} = 19600 p e x ec β = 2 p se ll β + p se ll β² β β = 19600
p b u y β² = p b u y = 20000 p'_{buy} = p_{buy} = 20000 p b u y β² β = p b u y β = 20000
Summary
Before this trade
After this trade
Trade #2 at time t = 15s
State before the trade
s = β 40 , 000 , 000 s = -40,000,000 s = β 40 , 000 , 000
p m i d = p s e l l = 19200 p_{mid} = p_{sell} = 19200 p mi d β = p se ll β = 19200
p b u y = p m i d Γ 15 + 20000 Γ ( 60 β 15 ) 60 = 19800 p_{buy} = \frac{p_{mid} \times 15 + 20000 \times (60 - 15)}{60} = 19800 p b u y β = 60 p mi d β Γ 15 + 20000 Γ ( 60 β 15 ) β = 19800
User sells $20,000,000
s β² = β 60 , 000 , 000 s' = -60,000,000 s β² = β 60 , 000 , 000
p m i d β² = 18800 p'_{mid} = 18800 p mi d β² β = 18800
p s e l l β² = min β‘ ( p s e l l , p m i d β² ) = 18800 p'_{sell} = \min(p_{sell}, p'_{mid}) = 18800 p se ll β² β = min ( p se ll β , p mi d β² β ) = 18800
The trade volume is evenly distributed between p s e l l p_{sell} p se ll β and p s e l l β² p'_{sell} p se ll β² β .
p e x e c = p s e l l + p s e l l β² 2 = 19000 p_{exec} = \frac{p_{sell} + p'_{sell}}{2} = 19000 p e x ec β = 2 p se ll β + p se ll β² β β = 19000
p b u y β² = p b u y = 19800 p'_{buy} = p_{buy} = 19800 p b u y β² β = p b u y β = 19800
Summary
After the last trade
Before this trade
After this trade
Trade #3 at time t = 39s (24 seconds later)
State before the trade
s = β 60 , 000 , 000 s = -60,000,000 s = β 60 , 000 , 000
p m i d = p s e l l = 18800 p_{mid} = p_{sell} = 18800 p mi d β = p se ll β = 18800
p b u y = p m i d Γ 24 + 19800 Γ ( 60 β 24 ) 60 = 19400 p_{buy} = \frac{p_{mid} \times 24 + 19800 \times (60 - 24)}{60} = 19400 p b u y β = 60 p mi d β Γ 24 + 19800 Γ ( 60 β 24 ) β = 19400
User buys $10,000,000
s β² = β 50 , 000 , 000 s'=-50,000,000 s β² = β 50 , 000 , 000
p m i d β² = 19000 p'_{mid} = 19000 p mi d β² β = 19000
p b u y β² = max β‘ ( p b u y , p m i d β² ) = 19400 p'_{buy} = \max(p_{buy}, p'_{mid}) = 19400 p b u y β² β = max ( p b u y β , p mi d β² β ) = 19400
The trade is completely executed at p b u y p_{buy} p b u y β , which does not move in this trade
p e x e c = p b u y = 19400 p_{exec} = p_{buy} = 19400 p e x ec β = p b u y β = 19400
p s e l l β² = p s e l l = 18800 p'_{sell} = p_{sell} = 18800 p se ll β² β = p se ll β = 18800
Summary
After the last trade
Before this trade
After this trade
Trade #4 at time t = 54s (15 seconds later)
State before the trade
s = β 50 , 000 , 000 s = -50,000,000 s = β 50 , 000 , 000
p m i d = 19000 p_{mid} = 19000 p mi d β = 19000
p b u y = p m i d Γ 15 + 19400 Γ ( 60 β 15 ) 60 = 19300 p_{buy} = \frac{p_{mid} \times 15 + 19400 \times (60 - 15)}{60} = 19300 p b u y β = 60 p mi d β Γ 15 + 19400 Γ ( 60 β 15 ) β = 19300
p s e l l = p m i d Γ 15 + 18800 Γ ( 60 β 15 ) 60 = 18850 p_{sell} = \frac{p_{mid} \times 15 + 18800 \times (60 - 15)}{60} = 18850 p se ll β = 60 p mi d β Γ 15 + 18800 Γ ( 60 β 15 ) β = 18850
User buys $50,000,000
s β² = 0 s' = 0 s β² = 0
p m i d β² = 20000 p'_{mid} = 20000 p mi d β² β = 20000
p b u y β² = max β‘ ( p b u y , p m i d β² ) = 20000 p'_{buy} = \max(p_{buy}, p'_{mid}) = 20000 p b u y β² β = max ( p b u y β , p mi d β² β ) = 20000
A fraction of the trade volume is executed at p b u y p_{buy} p b u y β and the rest is evenly distributed between p b u y p_{buy} p b u y β and p b u y β² p'_{buy} p b u y β² β .
p e x e c = ( ( p b u y β p m i d ) Γ p b u y + ( p b u y β² β p b u y ) Γ p b u y + p b u y β² 2 ) Γ· ( p b u y β² β p m i d ) = 19545 p_{exec} = \left((p_{buy} - p_{mid}) \times p_{buy} + (p'_{buy} - p_{buy}) \times \frac{p_{buy} + p'_{buy}}{2} \right) \div (p'_{buy} - p_{mid}) = 19545 p e x ec β = ( ( p b u y β β p mi d β ) Γ p b u y β + ( p b u y β² β β p b u y β ) Γ 2 p b u y β + p b u y β² β β ) Γ· ( p b u y β² β β p mi d β ) = 19545
p s e l l β² = p s e l l = 18850 p'_{sell} = p_{sell} = 18850 p se ll β² β = p se ll β = 18850
Summary
After the last trade
Before this trade
After this trade