## Decentralized Exchanges (DEX) with Automated Market Maker (AMM) @punk3155 --- ### What is a DEX? A decentralized exchange (or DEX) is an online marketplace where transactions occur directly between participants, without the aid of any trusted intermediaries. - Permissionless - Non-custodian ---- ### History of DEX - Onchain Order Book - Etherex - Offchain Order Book - EtherDelta - Relayer Model - Matching is done off-chain by a centralized “Relayer” - The relayer crafts a transaction off-chain that resembles an atomic-swap, then submits it to the blockchain - Trade settlement is done on-chain - Idex, dex.top, 0x, Kyber, Airswap ---- | | EtherEx | EtherDelta | IDEX | |------------|----------|----------------------------|--------------| | Order Book | On-chain | Off-chain | Off-chain | | Matching | User | User | User | | Execution | On-chain | On-chain | Off-chain | --- ### A simpler way to build a DEX ![](https://i.imgur.com/SVxleRv.png) ---- ### Uniswap Math - X * Y = k - https://etherscan.io/address/0xbb2b8038a1640196fbe3e38816f3e67cba72d940#readContract - 0x2260 => WBTC - 0xc02a => WETH - X = 386980791770 = 3869 BTC - Y = 93873571220224792128280 = 93873 ETH - Price = Y/X = 24.26 ETH/WBTC ![](https://i.imgur.com/CFuldve.png) ---- ### State Change - Initial State: X=10, Y=100, k=1000 - Sell x=2 - X’=12, Y’=83.33, k=1000 - dY = 100 - 83.33 = 16.67 - dX = 12 - 10 = 2 - Initial price: Y/X = 100/10 = 10 - End price: Y’/X’ = 83.33/12 = 6.94 - Avg price: dY/dX = 16.67/2 = 8.335 - Slippage = (10-8.335)/10 = 16.65% - Price Impact = (10-6.94)/10 = 30.6% ---- https://www.desmos.com/calculator/7wbvkts2jf ---- ### Math Recap - Spot Price: $p=\frac{r_1}{r_2}$ - Swap Amount: $y=r_2-r_2'$ - Slippage: $\frac{x/y}{p}-1=\frac{x}{r_1}$ - Divergence Loss(Impermernant Loss): - value of token if we hold vs provide liquidity - $V_1=V_2=V/2$ - $V_{hold}=V+V_2*\rho=V_1*(2+\rho)$ - $V_{lp}=2*V_1*\sqrt{1+\rho}$ - $loss=V_{lp}/V_{hold}-1=\frac{\sqrt{1+\rho}}{1+\rho/2}-1$ - https://www.desmos.com/calculator/nz95j801zw ---- ![](https://i.imgur.com/RwLR3jL.png) ---- ### Uniswap Router ![](https://i.imgur.com/snXEcJJ.png) ---- ### Balancer $$C=\prod_{k}r_k^{w^k}$$ Uniswap is a special case of $w_1=w_2=0.5$ - Spot Price: $\frac{r_1*w_2}{r_2*w_1}$ - Divergence Loss: $\frac{(1+\rho)^{w_2}}{1+w_2*\rho}-1$ - https://www.desmos.com/calculator/8k6tn6pzxd --- ### Stablecoin Swap - Most pairs are stable coins. Price volatility is minimal - USDT/USDC/DAI - Focusing on providing less slippage - Why not $C=x+y$? - the whole pool will be drained if price changes ![](https://i.imgur.com/8npN9tw.png =400x) ---- ### Curve Math $$K\sum{x_i}+\prod{x_i}=C$$ $$An^n\sum{x_i}+D=ADn^n+\frac{D^{n+1}}{n^n\prod{x_i}}$$ https://www.desmos.com/calculator/bqnqz365s6 ---- ### Curve War https://mirror.xyz/cryptolunatic.eth/uKjxSopOVcMFyvttJWDcCKkfu-Jp_iaEXxDNql2vf6w ----
{"metaMigratedAt":"2023-06-16T23:02:25.203Z","metaMigratedFrom":"YAML","title":"DEX&AMM","breaks":true,"slideOptions":"{\"theme\":\"league\",\"transition\":\"fade\"}","contributors":"[{\"id\":\"8b25d419-f7ee-4258-90aa-689151cd272c\",\"add\":3522,\"del\":251}]"}
    212 views