Symmetry
  • ๐Ÿš€Welcome to Symmetry
  • ๐Ÿ’กGetting Started
  • Guides and FAQs
    • ๐Ÿ“œDashboard
    • ๐Ÿ“ˆTrade
      • ๐Ÿง‘โ€๐ŸซRisk Management Measurements
        • Funding
        • Position Financial Cost Mechanism
        • "Adjusted" AMM
      • ๐Ÿ’ฃCase Studies - Liquidation
      • ๐Ÿ‘ฉโ€๐ŸซUSD Settlement & Interest Rate Model
    • ๐ŸณLiquidity
      • Mint and redeem of LP tokens
    • ๐ŸŽฐCoupons
  • Developers
    • Deployed Contracts
  • Links
    • Symmetry Website
    • Github
    • Twitter
    • Discord
    • Telegram
    • Medium
Powered by GitBook
On this page
  • Calculation
  • Example
  • Initial State
  • Trade #1 at time t = 0s
  • Trade #2 at time t = 15s
  • Trade #3 at time t = 39s (24 seconds later)
  • Trade #4 at time t = 54s (15 seconds later)
  1. Guides and FAQs
  2. Trade
  3. Risk Management Measurements

"Adjusted" AMM

PreviousPosition Financial Cost MechanismNextCase Studies - Liquidation

Last updated 1 year ago

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:

Symbol
Meaning

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, prem=ฮปโ‹…sprโ‹…LPprem = \frac { \lambda \cdot s} {pr \cdot LP}prem=prโ‹…LPฮปโ‹…sโ€‹,

After trade is executed, premโ€ฒ=ฮปโ‹…sโ€ฒprโ‹…LPprem' =\frac {\lambda \cdot s'} {pr \cdot LP}premโ€ฒ=prโ‹…LPฮปโ‹…sโ€ฒโ€‹,

where sโ€ฒ=s+Ts' = s + Tsโ€ฒ=s+T, TTT is trade quantity.

Therefore,

pmid=poracle(1+prem)=poracle(1+ฮปโ‹…sprโ‹…LP)p_{mid} = p_{oracle}(1+prem) = p_{oracle} \left(1 + \frac { \lambda \cdot s} {pr \cdot LP}\right)pmidโ€‹=poracleโ€‹(1+prem)=poracleโ€‹(1+prโ‹…LPฮปโ‹…sโ€‹)

ใ€Userโ€™s Buy Price & Sell Priceใ€‘

ใ€Execution Priceใ€‘

Therefore, the execution price for userโ€™s buy order is

Same as above, the execution notional for userโ€™s sell order is


Example

Initial State

s = 0

Trade #1 at time t = 0s

State before the trade

User sells $40,000,000

Summary

Before this trade
After this trade

Buy Price

0%

0%

Mid Price

0%

-4%

Sell Price

0%

-4%

Trade #2 at time t = 15s

State before the trade

User sells $20,000,000

Summary

After the last trade
Before this trade
After this trade

Buy Price

0%

-1%

-1%

Mid

-4%

-4%

-6%

Sell Price

-4%

-4%

-6%

Trade #3 at time t = 39s (24 seconds later)

State before the trade

User buys $10,000,000

Summary

After the last trade
Before this trade
After this trade

Buy Price

-1%

-3%

-3%

Mid

-6%

-6%

-5%

Sell Price

-6%

-6%

-6%

Trade #4 at time t = 54s (15 seconds later)

State before the trade

User buys $50,000,000

Summary

After the last trade
Before this trade
After this trade

Buy Price

-3%

-3.5%

0%

Mid

-5%

-5%

0%

Sell Price

-6%

-5.75%

-5.75%

takerโ€™s sell price before the trade.

takerโ€™s sell price after the trade.

pmidโ€ฒ=poracleโ€ฒ(1+premโ€ฒ)=poracleโ€ฒ(1+ฮปโ‹…(s+T)prโ‹…LP)p'_{mid} = p'_{oracle}(1+prem') = p'_{oracle} \left(1 + \frac { \lambda \cdot (s+T)} {pr \cdot LP}\right)pmidโ€ฒโ€‹=poracleโ€ฒโ€‹(1+premโ€ฒ)=poracleโ€ฒโ€‹(1+prโ‹…LPฮปโ‹…(s+T)โ€‹)

Besides poraclep_{oracle}poracleโ€‹ and pmidp_{mid}pmidโ€‹, smart contract also keeps track of the taker's buy price pbuyp_{buy}pbuyโ€‹ and sell price psellp_{sell}psellโ€‹.

After the trade, when pmidp_{mid}pmidโ€‹ is between pbuyp_{buy}pbuyโ€‹ and psellp_{sell}psellโ€‹., both prices gradually move towards pmidโ€ฒp'_{mid}pmidโ€ฒโ€‹at a fixed speed, reaching pmidโ€ฒp'_{mid}pmidโ€ฒโ€‹ after 60 seconds.

When pmidโ€ฒp'_{mid}pmidโ€ฒโ€‹ exceeds either of these two prices, the crossed price moves immediately to pmidโ€ฒp'_{mid}pmidโ€ฒโ€‹. And the other one will gradually move towards pmidโ€ฒp'_{mid}pmidโ€ฒโ€‹ at a fixed speed, reaching pmidโ€ฒp'_{mid}pmidโ€ฒโ€‹ after 60 seconds.

Formally speaking, after ttt seconds following a trade, these two prices change as follows.

pbuyโ€ฒ={max(tโ‹…pmidโ€ฒ+(60โˆ’t)โ‹…pbuy60,pmidโ€ฒ)t<60pmidโ€ฒtโ‰ฅ60psellโ€ฒ={min(tโ‹…pmidโ€ฒ+(60โˆ’t)โ‹…psell60,pmidโ€ฒ)t<60pmidโ€ฒ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*}pbuyโ€ฒโ€‹psellโ€ฒโ€‹โ€‹=โŽฉโŽจโŽงโ€‹max(60tโ‹…pmidโ€ฒโ€‹+(60โˆ’t)โ‹…pbuyโ€‹โ€‹,pmidโ€ฒโ€‹)pmidโ€ฒโ€‹โ€‹โ€‹t<60tโ‰ฅ60โ€‹=โŽฉโŽจโŽงโ€‹min(60tโ‹…pmidโ€ฒโ€‹+(60โˆ’t)โ‹…psellโ€‹โ€‹,pmidโ€ฒโ€‹)pmidโ€ฒโ€‹โ€‹โ€‹t<60tโ‰ฅ60โ€‹โ€‹

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 TTT changes the mid price from pmidp_{mid}pmidโ€‹ to pmidโ€ฒp'_{mid}pmidโ€ฒโ€‹ and the userโ€™s buy price is pbuyp_{buy}pbuyโ€‹ before the trade. Its turnover is

pexecโ‹…T=โˆซ0Tmaxโก(vโ‹…pmid+(Tโˆ’v)โ‹…pmidโ€ฒT,pbuy)dvp_{exec} \cdot T = \int_0^T \max\left( \frac{v \cdot p_{mid} + (T - v) \cdot p'_{mid}}{T}, p_{buy} \right) \mathrm{d}vpexecโ€‹โ‹…T=โˆซ0Tโ€‹max(Tvโ‹…pmidโ€‹+(Tโˆ’v)โ‹…pmidโ€ฒโ€‹โ€‹,pbuyโ€‹)dv
pexec={pbuypmidโ€ฒโ‰คpbuy(pbuyโˆ’pmid)โ‹…pbuy+(pmidโ€ฒโˆ’pbuy)โ‹…pmidโ€ฒ+pbuy2pmidโ€ฒโˆ’pmidpmidโ€ฒ>pbuyp_{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}pexecโ€‹=โŽฉโŽจโŽงโ€‹pbuyโ€‹pmidโ€ฒโ€‹โˆ’pmidโ€‹(pbuyโ€‹โˆ’pmidโ€‹)โ‹…pbuyโ€‹+(pmidโ€ฒโ€‹โˆ’pbuyโ€‹)โ‹…2pmidโ€ฒโ€‹+pbuyโ€‹โ€‹โ€‹โ€‹pmidโ€ฒโ€‹โ‰คpbuyโ€‹pmidโ€ฒโ€‹>pbuyโ€‹โ€‹
pexecโ‹…T=โˆซ0Tminโก(vโ‹…pmid+(Tโˆ’v)โ‹…pmidโ€ฒT,psell)dvp_{exec} \cdot T = \int_0^T \min\left( \frac{v \cdot p_{mid} + (T - v) \cdot p'_{mid}}{T}, p_{sell} \right) \mathrm{d}vpexecโ€‹โ‹…T=โˆซ0Tโ€‹min(Tvโ‹…pmidโ€‹+(Tโˆ’v)โ‹…pmidโ€ฒโ€‹โ€‹,psellโ€‹)dv

LPLPLP = 100,000,000 (assume it does not change in this example)

ฮฑ\alpha ฮฑ= 1

ฮป\lambdaฮป = 0.05

prprpr = 0.5

poraclep_{oracle}poracleโ€‹ = 20000

pmidp_{mid}pmidโ€‹ = pbuyp_{buy}pbuyโ€‹ = psellp_{sell}psellโ€‹ = 20000

s=0s = 0s=0

pmid=pbuy=psell=20000p_{mid} = p_{buy} = p_{sell} = 20000pmidโ€‹=pbuyโ€‹=psellโ€‹=20000

sโ€ฒ=โˆ’40,000,000s' = -40,000,000sโ€ฒ=โˆ’40,000,000

pmidโ€ฒ=20000ร—(1+1ร—0.05ร—โˆ’40,000,0000.5ร—100,000,000)=19200p'_{mid} = 20000 \times \left( 1 + \frac{1 \times 0.05 \times -40,000,000}{0.5 \times 100,000,000} \right) = 19200pmidโ€ฒโ€‹=20000ร—(1+0.5ร—100,000,0001ร—0.05ร—โˆ’40,000,000โ€‹)=19200

pโ€ฒsell=minโก(psell,pmidโ€ฒ)=19200p'{sell} = \min(p_{sell}, p'_{mid}) = 19200pโ€ฒsell=min(psellโ€‹,pmidโ€ฒโ€‹)=19200

The trade volume is evenly distributed between psellp_{sell}psellโ€‹ and psellโ€ฒp'_{sell}psellโ€ฒโ€‹.

pexec=psell+psellโ€ฒ2=19600p_{exec} = \frac{p_{sell} + p'_{sell}}{2} = 19600pexecโ€‹=2psellโ€‹+psellโ€ฒโ€‹โ€‹=19600

pbuyโ€ฒ=pbuy=20000p'_{buy} = p_{buy} = 20000pbuyโ€ฒโ€‹=pbuyโ€‹=20000

s=โˆ’40,000,000s = -40,000,000s=โˆ’40,000,000

pmid=psell=19200p_{mid} = p_{sell} = 19200pmidโ€‹=psellโ€‹=19200

pbuy=pmidร—15+20000ร—(60โˆ’15)60=19800p_{buy} = \frac{p_{mid} \times 15 + 20000 \times (60 - 15)}{60} = 19800pbuyโ€‹=60pmidโ€‹ร—15+20000ร—(60โˆ’15)โ€‹=19800

sโ€ฒ=โˆ’60,000,000s' = -60,000,000sโ€ฒ=โˆ’60,000,000

pmidโ€ฒ=18800p'_{mid} = 18800pmidโ€ฒโ€‹=18800

psellโ€ฒ=minโก(psell,pmidโ€ฒ)=18800p'_{sell} = \min(p_{sell}, p'_{mid}) = 18800psellโ€ฒโ€‹=min(psellโ€‹,pmidโ€ฒโ€‹)=18800

The trade volume is evenly distributed between psellp_{sell}psellโ€‹ and psellโ€ฒp'_{sell}psellโ€ฒโ€‹.

pexec=psell+psellโ€ฒ2=19000p_{exec} = \frac{p_{sell} + p'_{sell}}{2} = 19000pexecโ€‹=2psellโ€‹+psellโ€ฒโ€‹โ€‹=19000

pbuyโ€ฒ=pbuy=19800p'_{buy} = p_{buy} = 19800pbuyโ€ฒโ€‹=pbuyโ€‹=19800

s=โˆ’60,000,000s = -60,000,000s=โˆ’60,000,000

pmid=psell=18800p_{mid} = p_{sell} = 18800pmidโ€‹=psellโ€‹=18800

pbuy=pmidร—24+19800ร—(60โˆ’24)60=19400p_{buy} = \frac{p_{mid} \times 24 + 19800 \times (60 - 24)}{60} = 19400pbuyโ€‹=60pmidโ€‹ร—24+19800ร—(60โˆ’24)โ€‹=19400

sโ€ฒ=โˆ’50,000,000s'=-50,000,000sโ€ฒ=โˆ’50,000,000

pmidโ€ฒ=19000p'_{mid} = 19000pmidโ€ฒโ€‹=19000

pbuyโ€ฒ=maxโก(pbuy,pmidโ€ฒ)=19400p'_{buy} = \max(p_{buy}, p'_{mid}) = 19400pbuyโ€ฒโ€‹=max(pbuyโ€‹,pmidโ€ฒโ€‹)=19400

The trade is completely executed at pbuyp_{buy}pbuyโ€‹, which does not move in this trade

pexec=pbuy=19400p_{exec} = p_{buy} = 19400pexecโ€‹=pbuyโ€‹=19400

psellโ€ฒ=psell=18800p'_{sell} = p_{sell} = 18800psellโ€ฒโ€‹=psellโ€‹=18800

s=โˆ’50,000,000s = -50,000,000s=โˆ’50,000,000

pmid=19000p_{mid} = 19000pmidโ€‹=19000

pbuy=pmidร—15+19400ร—(60โˆ’15)60=19300p_{buy} = \frac{p_{mid} \times 15 + 19400 \times (60 - 15)}{60} = 19300pbuyโ€‹=60pmidโ€‹ร—15+19400ร—(60โˆ’15)โ€‹=19300

psell=pmidร—15+18800ร—(60โˆ’15)60=18850p_{sell} = \frac{p_{mid} \times 15 + 18800 \times (60 - 15)}{60} = 18850psellโ€‹=60pmidโ€‹ร—15+18800ร—(60โˆ’15)โ€‹=18850

sโ€ฒ=0s' = 0sโ€ฒ=0

pmidโ€ฒ=20000p'_{mid} = 20000pmidโ€ฒโ€‹=20000

pbuyโ€ฒ=maxโก(pbuy,pmidโ€ฒ)=20000p'_{buy} = \max(p_{buy}, p'_{mid}) = 20000pbuyโ€ฒโ€‹=max(pbuyโ€‹,pmidโ€ฒโ€‹)=20000

A fraction of the trade volume is executed at pbuyp_{buy}pbuyโ€‹ and the rest is evenly distributed between pbuyp_{buy}pbuyโ€‹ and pbuyโ€ฒp'_{buy}pbuyโ€ฒโ€‹.

pexec=((pbuyโˆ’pmid)ร—pbuy+(pbuyโ€ฒโˆ’pbuy)ร—pbuy+pbuyโ€ฒ2)รท(pbuyโ€ฒโˆ’pmid)=19545p_{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}) = 19545pexecโ€‹=((pbuyโ€‹โˆ’pmidโ€‹)ร—pbuyโ€‹+(pbuyโ€ฒโ€‹โˆ’pbuyโ€‹)ร—2pbuyโ€‹+pbuyโ€ฒโ€‹โ€‹)รท(pbuyโ€ฒโ€‹โˆ’pmidโ€‹)=19545

psellโ€ฒ=psell=18850p'_{sell} = p_{sell} = 18850psellโ€ฒโ€‹=psellโ€‹=18850

๐Ÿ“ˆ
๐Ÿง‘โ€๐Ÿซ
poraclep_{oracle}poracleโ€‹
pmidp_{mid}pmidโ€‹
pbuyp_{buy}pbuyโ€‹
psellp_{sell}psellโ€‹
psellโ‰คpbuyp_{sell}\leq p_{buy}psellโ€‹โ‰คpbuyโ€‹
poracleโ€ฒp'_{oracle}poracleโ€ฒโ€‹
pmidโ€ฒp'_{mid}pmidโ€ฒโ€‹
pbuyโ€ฒp'_{buy}pbuyโ€ฒโ€‹
psellโ€ฒp'_{sell}psellโ€ฒโ€‹
psellโ€ฒโ‰คpbuyโ€ฒp'_{sell}\leq p'_{buy}psellโ€ฒโ€‹โ‰คpbuyโ€ฒโ€‹