Uploaded by zhangda1987

lecture10-DEXs

advertisement
Decentralized
Exchanges (DEXs)
Ali Yahya
Why try to "decentralize" an exchange?
•
Composability (brief rant)
•
Credibly Neutral
•
Security
•
Global Reach
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.
Key Properties
•
Composable / Programmable
•
Credibly Neutral
•
Non-Custodial
•
Permissionless
First Approach: Order Book Based DEXs
Order Book Based DEXs
The 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
Many examples of DEXs that initially worked this way:
•
0x protocol
•
EtherDelta
•
Kyber
•
Airswap
Order Book Based DEXs
Limitations of the Relayer Model
•
Less programmable/composable
•
Depends on the presence of a centralized party
•
Peer-to-peer —hard to bootstrap liquidity
•
It's expensive with today's blockchains because of gas
Great resource:
Front-Running, Griefing, and the Perils of Virtual Settlement,
by Will Warren
Is there a simpler way to build a DEX?
A Bit of History: Automated Market Makers (AMMs)
•
Pricing shares in prediction markets —
Hanson’s Market Scoring Rules
•
•
Idea first explored in crypto in 2016 by:
•
•
Also used to price online ads
Vitalik Buterin — reddit post
Then generalized by Alan Lu and Martin Koppelman:
•
Blogpost: Building a Decentralized Exchange in Ethereum
High Level Aspiration
Two-Sided Marketplace
ETH
ETH
DAI
Liquidity
Providers
DAI
DAI
ETH
ETH
DAI
Smart
Contract
ETH
DAI
DAI
Traders
ETH
ETH
ETH
DAI
DAI
ETH: 10
DAI: 12
xy = k
Trader
0.012 DAI
4.0 ETH
Liquidity
Providers
8.0 DAI
Smart
Contract
8.0
4.0
10.0
6.0
6.0 ETH
ETH ** 20.0
12.0 DAI
DAI == 120
120
Uniswap V2
Demo: app.uniswap.org
Invariant:
xy = k
Simple Pricing Rule
(x − Δx)(y + Δy) = k
xy = k
Simple Pricing Rule
(x − Δx)(y + ϕΔy) = k
where (1 − ϕ) is the percentage fee
that is paid to liquidity providers,
and where Δx > 0 and Δy > 0.
xy = k
Simple Pricing Rule
(x − Δx)(y + ϕΔy) = k
xy
ϕΔy =
−y
x − Δx
xy − y(x − Δx)
=
x − Δx
xy − xy − yΔx
=
x − Δx
1
yΔx
Δy = ⋅
ϕ x − Δx
xy = k
Simple Pricing Rule
1
yΔx
Δy = ⋅
ϕ x − Δx
This rule specifies the price of buying
Δx in terms of y.
A similar exercise (swapping xs and ys)
produces a rule that specifies the price
of selling Δx in terms of y:
yϕΔx
Δy =
x + ϕΔx
xy = k
Simple Pricing Rule
Example where the contract contains
4.0 ETH and 30.0 DAI and charges
a fee for liquidity providers of 30 bps.
8.0
yϕΔx
Δy =
x + ϕΔx
20.0
30 * 0.997 * Δx
Δy =
4 + 0.997 * Δx
Say a trader wants to sell 8.0 ETH to
the contract. How much DAI should
she get in return?
30 * 0.997 * 8
Δy =
= 19.98
4 + 0.997 * 8
(The fee to liquidity providers is 0.02.)
In the Wild: Uniswap
Selling x for y
yϕΔx
Δy =
x + ϕΔx
Buying x for y
1
yΔx
Δy = ⋅
ϕ x − Δx
UniswapV2Library.sol
Quick Demo: https://app.uniswap.org/
How to Think about an AMM’s Price
Price is the ratio between assets (e.g. DAI) paid and assets (e.g. ETH) received.
If I pay 100 DAI for 4 ETH, then my price per ETH is 25 DAI.
In our notation, this is given by | Δy/Δx | .
Selling x for y
Buying x for y
yϕΔx
Δy =
x + ϕΔx
1
yΔx
Δy = ⋅
ϕ x − Δx
Divide both sides by Δx to get Δy/Δx.
Δy
yϕ
=
Δx x + ϕΔx
Δy
1
y
= ⋅
Δx ϕ x − Δx
xy = k
Marginal Price & Slippage
Selling x for y
Δy
yϕ
=
Δx x + ϕΔx
Buying x for y
Δy
1
y
= ⋅
Δx ϕ x − Δx
Observation #1
Pricing depends on the size of the trade, Δx.
For example with 20.0 ETH * 6.0 DAI = 120,
Buying 10 ETH (i.e. Δx = 10) costs 6.02 DAI*
Or 0.602 DAI per ETH
6.0
2.0
10.0 5.0
Whereas buying 5 ETH costs 2.006 DAI
Or 0.401 DAI per ETH
* assuming ϕ = 0.997
xy = k
Marginal Price & Slippage
Selling x for y
Δy
yϕ
=
Δx x + ϕΔx
Buying x for y
Δy
1
y
= ⋅
Δx ϕ x − Δx
In the limit, as Δx approaches 0:
Δy
y
lim
=ϕ
x
Δx→0 Δx
Δy
1y
lim
=
ϕx
Δx→0 Δx
And, if we set the fee to zero (ϕ = 1), then:
2.0
5.0
y
Mp =
x
where Mp denotes
marginal price
Mp is equal to the magnitude of the slope of
the tangent line.
xy = k
Marginal Price & Slippage
Selling x for y
Δy
yϕ
=
Δx x + ϕΔx
24.0
52.9
Buying x for y
Δy
1
y
= ⋅
Δx ϕ x − Δx
Observation #2
Pricing depends on the size of x and y (i.e. k)
It’s straightforward to see that, as k
increases, the effective price of the AMM is
less sensitive to Δx.
24.0
16.0
Incentives for
Liquidity Providers
Alice deposits 10 ETH and 12 DAI of liquidity,
which implies:
Alice
Mp = 1.2
Liquidity
Provider
where Mp denotes marginal price
10.0 ETH
Alice waits for a month, during which traders
drive $700 worth of volume through the AMM.
12.0 DAI
xy = k
At the end of the month, Alice withdraws her
ETH and DAI. By that time, the price of ETH
has gone up 4x. The marginal price is now:
M′p = 4.8
10.0 ETH * 12.0 DAI = 120
What is Alice’s return?
Assume: (1 − ϕ) = 0.003
First, what does Alice earn from liquidity
provider fees?
Alice
2.1 DAI
Liquidity
Provider
5.0 ETH
24.0 DAI
xy = k
V(1 − ϕ) = 700 * 0.003 = $2.1
where V denotes trading volume
Second, how many ETH and DAI does Alice get
back?
x′ = 5 ETH
y′ = 24 DAI
Mp = 4.8
x′ ETH * y′ DAI = 120
Mp = 1.2
Impermanent Divergence Loss
So, how did Alice do?
Alice
2.1 DAI
Liquidity
Provider
5.0 ETH
24.0 DAI
Measured in DAI, Alice now has:
4.8 DAI
R = 5 ETH *
+ 24 DAI + 2.1 DAI
ETH
R = 50.1 DAI
xy = k
Not bad, but how would she have done if she had
just held onto her 12 ETH and 10 DAI?
4.8 DAI
RB = 12 ETH *
+ 10 DAI
ETH
x′ ETH * y′ DAI = 120
RB = 67.6 DAI
This is called impermanent loss
divergence
Impermanent Divergence Loss
What if volume had been higher?
Alice
21.0 DAI
Liquidity
Provider
5.0 ETH
Say, volume had been $7,000 instead of $700:
V(1 − ϕ) = 7000 * 0.003 = $21
Therefore,
24.0 DAI
xy = k
4.8 DAI
R = 5 ETH *
+ 24 DAI + 21 DAI
ETH
R = 69.0 DAI
x′ ETH * y′ DAI = 120
This time, Alice’s returns are greater than her
baseline return RB of 67.6 DAI. Her profit:
R
PL =
− 1 = 2.1 %
RB
Impermanent Divergence Loss
More generally
Alice
Alice’s return R is given by:
Liquidity
Provider
V(1 − ϕ) DAI
x′ ETH
R = x′M′p + y′ + V(1 − ϕ)
Her baseline return RB is given by:
y′ DAI
xy = k
RB = xM′p + y
Her profit, in percentage terms is given by:
x′M′p + y′ + V(1 − ϕ)
R
PL =
−1=
−1
RB
xM′p + y
x′ ETH * y′ DAI = 120
Let’s ignore the volume term for now, and simplify:
PL =
x′M′p + y′
xM′p + y
−1
assuming V = 0 for now
Impermanent Divergence Loss
Recall
xy = k
and
Mp = y/x
Simplifying
Thus,
x=
k
Mp
and y =
kMp
PL =
x′M′p + y′
Step 1: let’s express everything in terms of Mp and k.
xMp + y
Also,
x′ =
k
M′p
and y′ =
kM′p
PL =
Finally, let:
M′p = rMp
PL =
k
rMp
rMp
+
k
rM
p
Mp
+
2 r
r+1
krMp
kMp
−1=
2 r
r kMp +
kMp
kMp
−1
−1
Step 2: Reintroduce the volume term:
2 r
V(1 − ϕ)
PL =
+
−1
r+1
c
Step 3: Plot this equation
Impermanent Divergence Loss
2 r
Image credit: https://www.tokendaily.co/blog/pnl-analysis-of-uniswap-market-making
V(1 − ϕ)
PL =
+
−1
r+1
c
Quick Demo: https://zumzoom.github.io/analytics/uniswap/roi/
Big Limitation of Uniswap V2
Capital Efficiency
Distribution of Liquidity
One Approach: Curve.Fi
Uniswap V3: Universal AMM
Demo: app.uniswap.org
Concentrate Liquidity
https://uniswap.org/blog/uniswap-v3/
Narrow Activation
https://uniswap.org/blog/uniswap-v3/
Unified Pool
https://uniswap.org/blog/uniswap-v3/
Capital Efficiency: Example
https://uniswap.org/blog/uniswap-v3/
White Paper
https://uniswap.org/whitepaper-v3.pdf
Uniswap’s Metrics To Date
Quick Demo: https://uniswap.info/
Example: Uniswap
Interoperability
DEXs: Concluding Thoughts
Desired Characteristics
•
Simple — buildable as a smart contract
•
Automated liquidity — no dependence on active
market-makers
•
No single points of control — no dependence on
centralized parties
•
Composable/Programmable
Download