The Microstructure of
Crypto-Asset Trading

Andreas Park

 



 

Traditional Markets

What is Market Microstructure?

  • How do trading institutions affect market outcomes, e.g.
    • trading costs
    • information efficiency
    • allocative efficiency
       
  • A lot of tricky issues
    • no one model can do it all
    • complex theoretical problems (e.g., infinite regress)
    • large datasets

Broker

Exchange

Internalizer

Wholeseller

Darkpool

Venue

Settlement

Traditional Institutions

Investors

Trading Arrangements

central limit order book

complexity

price impact of trades

anonymity

price discovery

centralized auction

bilateral
negotiation

Request
for Quote

open
outcry

Broker

Exchange

Internalizer

Wholeseller

Darkpool

Venue

Settlement

Investor

Decentralized Trading

Decentralized trading using automated market makers (AMM)

Liquidity providers

Liquidity demander

Liquidity Pool

AMM pricing is mechanical:

  • determined by the amounts of deposits 
  • most common:
    • constant product
    • #USDC \(\times\) #ETH = const

No effect on the marginal price

AMM Theory: The Price Function

Basic Requirements

  • Two assets:
    • cash and an asset 
    • deposits in pool of \(c\) cash and \(a\) asset
  • AMMs should be self-contained: pricing rule based on
    • deposits \(a,c\) and
    • liquidity demanded \(q\)
  • implied value of the asset at deposit: \[p=\frac{c}{a}.\]
  • marginal price: an infinitesimal unit should cost \[p^m=p\]

What does an AMM need?

  • trades create linear changes:
    • a trade of \(q\in(-\infty,a),\) will cost \(\Delta c(q)\in(-c,\infty)\)
    • liquidity changes
      • \(a\) \(\to\) \(a-q\)
      • \(c\) \(\to\) \(c+\Delta c(q)\)
    • \(\to\) marginal price after trade of \(q\) is

      \[p^m(0)=\frac{c}{a}~~\to~~p^m(q)=\frac{c+\Delta c(q)}{a-q}.\] 

quantity

price

\(q\)

\(p^m(q)\)

\(\Delta c(q)(Q)=p(q)^m\times q\)

Traditional pricing: auctions/open outcry/RFQ: uniform price

idea: cost of \(q=\) price\(\times\) quantity

Some Pricing Rules from Traditional Markets: Uniform Price

  • assume cost is price \(\times\) quantity: \[\Delta c(q)=q\cdot p(q)\]
  • plus assume average price \(=\) marginal price after trade

    \[p(q)=\frac{c+q\cdot p(q)}{a-q}~~\Leftrightarrow~~p(q)=\frac{c}{a-2q}.\]
     
  • formalized and analyzed by Canidio and Fritsch (2023) and implemented in COW-Swap
  • theoretical/implementation problem: splitting an order into smaller units is profitable!

\(q=2\)

\(\Delta c(q)= q\times p^m(q)=2\times 15.5\)

Main pricing rule in stock exchanges: limit order book

quantity

price

\(q\)

\(p^m(q)\)

\(\Delta c(q)=\int_0^qp^m(s)~ds\)

Some Pricing Rules from Traditional Markets: Limit Order Book

  • For AMM: assume continuity \(\to\) price of "final" marginal unit coincides with price of  "first" marginal unit of next trade \[\Delta c(q)=\int_0^q \rho(s) ds~\to~\Delta c'(q)=\rho(q)=p(q).\]
  • Also "first" unit costs nothing \(\Delta c(0)=0\)
  • This brings differential equation
    \[p(q)=\frac{c+\int_0^q \rho(s) ds}{a-q}~~\Leftrightarrow~~\Delta c'(q)=\frac{c+\Delta c(q)}{a-q},\]
  • with solution \[\Delta c(q)=q\cdot\frac{c}{a-q}.\]
  • Or, we can write \[\Delta c(q)=\int\limits_0^q p(s)ds=\int\limits_0^q\frac{ac}{(a-q)^2}ds=\frac{cq}{a-q}.\]
  • Idea of pricing: liquidity before trade \(=\) after trade
    \[L(a,c)=L(a-q,c+\Delta c)\]

Most Common Pricing Rule in DeFi: Constant Product

Most Common Pricing Rule in DeFi: Constant Product

  • With \[a\cdot c= (a-q) (c+\Delta c(q))\]
  • we compute cost \[\Delta c(q)=q\cdot  \frac{c}{a-q},\]
  • average price \[p(q)=\frac{c}{a-q}\]
  • and marginal price \[p^m(q)=\frac{c+\frac{cq}{a-q}}{a-q}=\frac{ac}{(a-q)^2}.\]

Insight: AMM pricing function is the same as a limit order book when we require

  • that prices are continuous and
  • that prices are determined by liquidity deposits only
  • price \(\times\) quantity: \[p(q)=\frac{c}{a-2q}\]
  • limit order book: \[p(q)=\frac{c}{a-q}.\]
  • constant product: \[p(q)=\frac{c}{a-q}.\]  

Some insights on pricing functions

  • Price \(\times\) quantity
    • nice feature:
      • regret-free price: if price move is due to information, you have no positional loss
    • poor feature:
      • you can always save by order-splitting
      • limit price for splitting = limit order book price
  • LOB/CP
    • nice feature:
      • order splitting doesn't pay
    • poor feature:
      • prices not regret-free: if price move is due to information, you always lose

The Pricing Function

  • Most common form of AMM liquidity rule is Constant Product Pricing
    \[L(a,c)=a\cdot c~\Rightarrow~a\cdot c= (a-q)\cdot (c+\Delta c).\]
  • Total cost of trading \(q\) \[\Delta c=\frac{cq}{a-q}.\]
  • Price per unit \[p(q)=\frac{c}{a-q}.\]
  • Average spread paid\[\frac{p(q)}{p(0)}-1=\frac{q}{a-q}.\]
  • liquidity provider makes asset and cash deposit
  • more deposits flatten price curve
    • may attract more volume
    • but larger "positional" dollar loss when prices move
  • larger liquidity deposits \(a\) \(\Rightarrow\) 
    • lower costs (price impact) for liquidity demanders

Sidebar: we can quantify how much a PASSIVE LP loses when the price moves by \(R\)

for orientation:

  • If the stock price drops by 10% the incremental loss for liquidity providers is 13 basis points on their deposit
    • \(\to\) total loss=-10.13%
  • If the stock price rises by 10%, the liquidity provider gains 12 basis points less on the deposit
    • \(\to\) total gain =9.88%

\[\frac{\text{adverse selection loss when the return is \(R\)}}{\text{initial deposit}}=\sqrt{R}-\frac{1}{2}(R+1)\]

see Barbon & Ranaldo (2022)

Liquidity Demander's Decision & (optimal) AMM Fees

  • Two opposing forces in equilibrium for fee \(F\nearrow\) 
    1.  more liquidity provision
      \(\to\) lower price impact
    2. more fees to pay

Result:

competitive liq provision\(\to\) there exists an optimal (min trading costs) fee \(>0\)

  • \(\to\) derive closed form solution for competitive liquidity provision
  • depends on return distribution, balanced volume, quantity demanded

Similar to Lehar&Parlour (2023) and Hasbrouck, Riviera, Saleh (2023)

  • For trading of a fixed quantity:

    \[\text{LD cost}=\text{AMM price impact} +\text{AMM fee} .\]

\[F^\pi=\frac{1}{E[|\sqrt{R}-1|/2]+V}\left(-2q\ E[\text{position loss}]+ \sqrt{-2qV\ E[\text{position loss}]}\right).\]

assume: liquidity providers add liquidity until they break even in expectation

Empirical Work

Lehar and Parlour (2021)

Barbon & Ranaldo (2023)

Other notables:

  • Lehar, Parlour, Zoican (2022):
    • study and document fragmentation of liquidity across different fee levels
    • most intense provision-withdrawal activity for low fees \(\to\) UniSwap v3 starts mimicking tradfi
  • Capponi & Jia (2022) study impact of curvature of pricing function
    • Theory: curvature of optimal pricing curve \(\nearrow\) volatility
    • Empirics: cost volatility is 60% lower for stable-coin pairs
    • corr(deposit inflows ,volatility)<0
      corr(deposit inflows ,volume)>0

Malinova & Park (2023): AMM applied to equities would reduce trading costs by 30%

UniSwap v3

UniSwap v3 has "concentrated liquidity provision"

  • liquidity provision choice in v2: 
    • submit \(a,c\) at the marginal \(p_0=\frac{c}{a}\)
    • \(\to\) for given \(p_0\), there is ONE degree of freedom
    • provide liquidity
      • for all prices \(p\in(0,\infty)\)
      • all asset amounts \(q\in(0,a)\)
      • all cash amounts \(\Delta c\in (0,c)\)
  • liquidity provision choice in v3: 
    • submit \(u,\Delta c (d)\) 
    • choose price range \([p_d,p_u]\)
    • provide liquidity
      • for all prices \(p\in[p_d,p_u]\)
      • all asset amounts \(q\in(-\infty,u)\)
      • all cash amounts \(\Delta c\in (-\Delta c(d),\infty)\)

Just a little more institutional details

  • users submit liquidity positions \((u,d,p_u,p_d)\) and receive an ERC-721 (=NFT) token as a receipt 

  • the code segments liquidity into discrete exponential intervals \([p_k,p_{k+1}]\) with \[p_k=(1+\delta)^k. ~~~(\text{numerically: }p(k)=1.0001^k)\]

  • it aggregates liquidity over these intervals

  • the pricing curve for each interval is determined by the constant product rule

  • intervals may be "empty"

How the price is determined

  • In v2, for given \(p_0=\frac{c}{a}\), users have ONE degree of freedom.
  • for instance, when you pick prices \(p_u,p_d\) and the quantity \(u\) that you are willing to supply, then the amount of cash that you have to contribute is pre-determined.
  • You can pick \(p_d=p_0\) so that \(\Delta c(d)=0\)
  • In v3, for given \(p_0\), users have THREE degree of freedom.

How the price is determined

  • some more facts
    • if the marginal price \(p_u\) is reached, you will sell \(u\) units of the asset
    • the price is still using constant product
    • but we use the term "virtual" liquidity
    • its the v2 liquidity that would pertain if you'd supply liquidity on the entire interval of prices

\(p_d\)

\(p_u\)

\(p_0\)

we know this curve has functional form \[p^m(q)=\frac{\tilde{a}c}{(\tilde{a}-c)^2}\]

where \(\tilde{a}\) is the virtual liquidity

quick disclaimer: what follows is not how UniSwap is explained on its website etc. But the resulting maths are the same

More on UniSwap v3

marginal "limit order book" price

\[\gamma(s)=\frac{ac}{(a-s)^2}\]

\(p_u=15\)

\(p_d=7\)

\(u=2\)

\(p_0=10\) (that's exogenous, not a choice)

Finding virtual liquidity factor \(\tilde{a}\)

marginal "limit order book" price

\[\gamma(s)=\frac{ac}{(a-s)^2}\]

\(p_u=15\)

\(p_d=7\)

\(u=2\)

\(p_0=10\) (that's exogenous, not a choice)

= find the right curve

= find the right "\(\tilde{a}\)"

Finding the fourth parameter \(\Delta c(d)\)

marginal "limit order book" price

\[\gamma(s)=\frac{ac}{(a-s)^2}\]

\(p_u=15\)

\(p_d=7\)

\(u=2\)

\(d=?\)

required cash deposit \(\Delta c(d)=\) the amount that I pay for \(d\)

Solutions

  •  virtual liquidity factor: \[\tilde{a}=\frac{u}{1-\sqrt{p_0/p_u}}=u\cdot \frac{\sqrt{p_u}}{\sqrt{p_u}-\sqrt{p_0}}.\]
  • required quantity to reach \(p_d\) \[\rho(-\tilde{d})=\frac{\tilde{a}\tilde{c}}{(\tilde{a}+\tilde{d})^2}=p_d.~~\Leftrightarrow~~d^*=-\tilde{a}(1-\sqrt{p_0/p_d})\]
  • required cash deposit \[\Delta c(\tilde{d})=-\tilde{a}(1 - \sqrt{p_0/p_d})\sqrt{p_0p_d}=-u\sqrt{p_0p_d}\frac{1 - \sqrt{p_0/p_d}}{1 - \sqrt{p_0/p_u}}\]
  • marginal pricing function \[\tilde{\gamma}(q)=\frac{\tilde{a}^2p_0}{(\tilde{a}-q)^2}=\frac{u^2p_0p_u}{((u-q)\sqrt{p_u}+q\sqrt{p_0})^2}.\]

Numerical example

  • Suppose \(p_0=\$10\).
  • LP is willing to sell up  to \(u=100\)
  • v2 liquidity:
    • Required cash deposit: \(c=100\times 10=1000\)
    • liquidity \(c\cdot a=100\times 1,000\)
  • For v3: suppose willing to supply liquidity in \([\$9,\$11]\)
  • v3 liquidity:
    • virtual liquidity factor: \(\tilde{a}\approx 2,150\)
    • required cash \(c^*=\$1,100\)
  • consider a buy order of \(q=10\)
  • cost per unit 
    • v2: \(\frac{1000}{100-10}=11.11\)
    • v3: \(\frac{21,500}{2,150-10}=10.05\)

Want to read more?

  • A Primer on UniSwap v3 Math: As Easy as 1,2, v3 https://blog.uniswap.org/uniswap-v3-math-primer
  • Elts 2012: Liquidity Math in UniSwap v3
  • Hasbrouck, Riviera, Saleh (2023):
    • UniSwap Liquidity Provision is like a covered call,
    • whereas a standard market (Copeland & Galai JF 1983) is a short strangle (call at the ask, put at the bid)

Concerns around Automated Market Makers

a

b

c

d

e

f

g

Problem 1: Public Mempools allow sandwich (MEV) attacks

related paper: “Maximal Extractable Value and Allocative Inefficiencies in Public Blockchains”. A. Capponi, R. Jia, and Y. Wang (2023)

Problem 2: Just-In-Time Liquidity (single trade cream skimming)

X-Router

Liquidity pool

OTC

Problem 2: Just-In-Time Liquidity (at the MEV level)

X-Router

Liquidity pool

searcher/builder

 balanced orders

add as much liquidity as possible

withdraw liquidity

 unbalanced orders

  • the issue: 
    • order flow segmentation increases adverse selection
    • JIT: not enough fees from balanced order flow

related paper: Capponi, Jia, and Zhu (2024) "The Paradox of Just-in-Time Liquidity in Decentralized Exchanges: More Providers Can Lead to Less Liquidity"

From Vitalik Buterin's post on the topic:

https://ethresear.ch/t/improving-front-running-resistance-of-x-y-k-market-makers/1281

  • February 2022: UniSwap (V2), ETH-USDC
    • 38,100 ETH
    • 118M USDC
    • \(p=\$3,097\)
  • Buy: 100 ETH
    • pay approximately $3,105 per ETH. accoridng to bonding curve
  • 100 ETH MEV sandwich attack
    • Initial trade cost: $3,105 per ETH
    • reversing yields $3,121 per ETH
    • total profit of $1,639 \(=\) value extracted from original trader.
  • 1,000 ETH MEV attack
    • profit/loss \(=\) $17K
  • 10,000 ETH MEV attack
    • profit/loss \(=\) $261K.

Theorem (Park 2023): 

  • Constant product pricing and linear limit order book pricing allows unlimited MEV losses
  • Uniform pricing has limited MEV losses but profitable splitting

Hypothetical example

The Bigger Picture: MEV Extraction

  • MEV extraction is a reality and almost inevitable
  • imposes negative externalities as e=bots bid up gas prices
  • "Flashbots" protocol "democratizes" exploits
  • protocols became better at preventing sandwich attacks with better functionality

read more at: "Battle of the Bots: Flash Loans, Miner Extractable Value and Efficient Settlement", Lehar & Parlour, 2023

The Bigger Picture and Last Words

Last Words

  • here: blockchain \(\approx\) value management infrastructure
  • can combine asset custody, orders, trading, clearing, and settlement
  • allows different IO of markets and market services
  • AMMs are a genuinely new market institution 
  • evolving space, new ideas come up  all the time:
    • UniSwap v4 is on its way 
    • may be embedded in many applications and processes
    • lots to learn

@financeUTM

andreas.park@rotman.utoronto.ca

slides.com/ap248

sites.google.com/site/parkandreas/

youtube.com/user/andreaspark2812/

Crypto-Trading for MFRM 2024-25

By Andreas Park

Crypto-Trading for MFRM 2024-25

  • 1,067