Professional Documents
Culture Documents
Updated Lecture 5 Slides
Updated Lecture 5 Slides
2
Financial Exchanges 101
Exchange
1 1
Trade Matching
Alice
1 1 Bob
2 Matched Orders
Trade Settlement
3
Trade Matching Models
Exchange
Trade Matching
Non-Custodial
Trade Settlement
4
Order Book
5
Two Order Book Models
Server On-Chain
Censorship resistance
Fast matching
Robust
No fees for canceled orders
Slow matching
No censorship resistance
Blockchain fees for orders
Exchange front running
Miner/trader front running
6
EtherDelta
7
LOB DEX: Lessons Learned
§ Advantages:
§ No KYC/AML
§ No fees paid to the exchange
§ No impermanent loss (explained later in AMM)
§ Disadvantages:
§ Fees for deposit, withdraw, trade creation/cancel
§ Slow execution
§ Not fully decentralized (mediating server)
8
Settlement Layer
Exchange
Trade Matching
Non-Custodial
Trade Settlement
9
Why do we need DEX?
Alice
DEX
Bob
1. Price discovery
2. Trade matching
Smart Contract
3. Trade clearing
Blockchain
11
DEX trading volume
Daily Volume:
- DEXes: 3.5B
- Binance: 49B
Source:
https://defiprime.com/dex-volume
https://defi-learning.org
Liquidity Pool
Idea: Let a smart contract do the market making.
Asset Y Provide X
+fee for Alice
for Y
Asset X Asset Y
constant
quantity quantity
Properties:
§ Instant liquidity, irrespective of the trade size
§ Purchase of asset X increases price of X
and decreases the price of Y
§ Ratio of asset X and Y sets the price
§ Known as Constant Product (CP) AMM 15
AMM Example
16
AMM Example
17
Expected Slippage
The expected increase or decrease in price based
on the trading volume and available liquidity.
18
Unexpected Slippage à Worse Execution Price
19
Unexpected Slippage à Better Execution Price
20
Slippage Protection
Configures a slippage protection threshold to prevent unacceptable slippage
21
Slippage Protection
A transaction fails when crossing the slippage limit.
22
Slippage Protection
A transaction fails when crossing the slippage limit.
23
Pros and Cons of an AMM
https://defi-learning.org
Exchange Transaction Propagation
26
Exchange Transaction Propagation
27
Exchange Transaction Propagation
28
Exchange Transaction Propagation
29
Exchange Transaction Propagation
https://defi-learning.org
Pegged/Stablecoin Swap
33
Pegged/Stablecoins
34
Pegged/Stablecoin Swap
Curve Uniswap
initially,
x=5
y=5
36
Slippage Comparison
Stableswap (aka Curve Finance)
37
What happens if a coin de-pegs?
38
What happens if a coin gets blacklisted?
39
AMM Whitepaper
§ Curve:
§ https://curve.fi/files/stableswap-paper.pdf
§ https://curve.fi/files/crypto-pools-paper.pdf
§ Uniswap:
§ https://uniswap.org/whitepaper.pdf
§ https://uniswap.org/whitepaper-v3.pdf 40
AMM Arbitrage
https://defi-learning.org
Arbitrage
BTC/USD
BTC/USD
BTC/USD 42
Arbitrage
§ Multiple Markets with
§ the same assets X and Y
§ different prices for X and Y
43
Arbitrage on two markets
44
Arbitrage (with Flash Loan)
45
AMM Impermanent Loss
https://defi-learning.org
Impermanent Loss Example
2. Price increase of ETH
1 ETH == 400 DAI
§ Possible Solutions
§ Challenging
§ Change of the bonding curve 48
Impermanent Loss Calculator
49
AMM Liquidity Mining
https://defi-learning.org
Liquidity Mining == Incentive
§ 2 Types of rewards in DeFi Pools
§ Trading fees (e.g. 0.03% in Curve)
§ Liquidity Mining rewards
§ Liquidity Mining
§ An incentive to provide liquidity to a pool
§ Proportional rewards in terms of liquidity
§ Can be added/removed anytime
§ Retrospective airdrops possible à address history is valuable
51
Liquidity Mining
Curve Alpha Homora v2
52
DEX Aggregator
https://defi-learning.org
DEX Aggregator
§ Users may ask
§ Where do I get the best price for a trade?
§ Where is the deepest liquidity?
§ Routing
§ Explains which route taken
§ No arbitrage performed
55
SwapSwap
§ Aggregates 2 DEX
§ Uniswap and Sushiswap
§ No UI change for the user
https://defi-learning.org
How to detect arbitrage/profitable opportunities?
§ Bellman Ford Algorithm
§ Negative cycle detection
§ Works among multiple markets
§ Used in traditional finance and DeFi
58
DeFiPoser-ARB and DeFiPoser-SMT [S&P’21]
§ DeFiPoser-ARB
§ builds a directed DeFi market graph
§ identifies negative cycles
§ Bellman Ford-Moore algorithm
§ DeFiPoser-SMT
§ state transition model
§ prunes search space
§ theorem prover
59
DeFiPoser-ARB
𝐴 𝐵
= 𝑝! = 𝑝"
𝐵 𝐶
𝐶 𝐵
= 𝑝# = 𝑝$
𝐴 𝐴
60
DeFiPoser-ARB
Profitable condition
𝑝! . 𝑝$ > 1
1×𝐴
𝐴 𝐵
= 𝑝! = 𝑝"
𝐵 𝐶
𝑝! ×𝐵 𝑝! . 𝑝$ ×𝐴
𝐶 𝐵
= 𝑝# = 𝑝$
𝐴 𝐴
61
DeFiPoser-ARB
Profitable condition
𝑝! . 𝑝" . 𝑝# > 1
1×𝐴 𝑝! ×𝐵
𝐴 𝐵
= 𝑝! = 𝑝"
𝐵 𝐶
𝑝! . 𝑝" . 𝑝# ×𝐴
𝑝! . 𝑝" ×𝐶
𝐶 𝐵
= 𝑝# = 𝑝$
𝐴 𝐴
62
DeFiPoser-ARB
Profitable condition
𝑝! . 𝑝" . 𝑝# > 1
− log 𝑝!
𝐴 𝐵
⟺
− log 𝑝" (− log 𝑝! ) + (− log 𝑝" ) + (− log 𝑝# ) < 0
− log 𝑝#
𝐶 𝐷
63
DeFiPoser-ARB
6 𝑝% > 1
%
− log 𝑝!
⟺
𝐴 𝐵
7 (− log 𝑝% ) < 0
%
− log 𝑝"
− log 𝑝#
BellmanFord-Moore algorithm
𝐶 𝐷
𝑂(|𝑁 " | ; |𝐸|)
64
DeFiPoser-SMT
Optimization
65
DeFiPoser Evaluation
transaction fee in ETH 4000
Cumulative revenue/
DeFiPoser-SMT: best strategy per block found
DeFiPoser-SMT: validated revenue by concrete execution
2000 DeFiPoser-SMT: transaction fee
DeFiPoser-ARB: validated revenue by concrete execution
DeFiPoser-ARB: transaction fee
0 Jan-02-2020 Feb-02-2020 Mar-03-2020 Apr-03-2020 May-04-2020
9200000 9400000 9600000 9800000 10000000
Block number
DeFiPoser-ARB DeFiPoser-SMT
Path selection Combines multiple sub-paths Selects the highest revenue path
Maximum Revenue 81.31 ETH (32,524 USD) 22.40 ETH (8,960 USD)
Total Revenue (over 150 days) 4,103.22 ETH (1,641,288 USD) 1,552.32 ETH (620,928 USD)