Apr 19, 2021 | Hasu
Every day, thousands of people use a decentralized exchange (DEX) for the first time. However, the idiosyncrasies of a public blockchain routinely catch newcomers off-guard, even those familiar with trading on more traditional venues. As a result, traders bleed money to arbitrageurs and frontrunners, leading to worse-than-necessary execution.
At a high level, we can break down the costs of each trade into several parts:
This article on automated market-makers (AMMs) will serve as an intro to the series and discuss the first and most crucial cost: price impact. You will learn
Most DEXs consist of many liquidity pools that represent different trading pairs, like ETH/WBTC. Instead of matching buyers and sellers in an orderbook, these liquidity pools act as an automated market maker.
A liquidity pool is a smart contract that holds reserves of two or more tokens and allows anyone to deposit and withdraw funds from them, but only according to very specific rules.
One such rule is the constant product formula x * y = k, where x and y are the reserves of two tokens, A and B. In order to withdraw some amount of token A, one must deposit a proportional amount of token B to maintain the constant k before fees.2
From the constant product formula it follows that the price of that token A is simply price_token_A = reserve_token_B / reserve token_A.
Chart 1: Different AMM formulas result in different pricing curves. When a hypothetical Uniswap v2 liquidity pool has 15 Y-tokens, it will only pay 0.1 X-tokens for the marginal Y-token. But when it has only 2.5 Y-token, it will pay 4.0 X-tokens. Other pricing curves are designed to concentrate more liquidity around a certain price (e.g. 1.0 for stablecoins). Source: Curve Whitepaper
To look at a real-world example, at the time of writing there are 2,700 WBTC and 86,000 ETH in Uniswap’s ETH/WBTC pool. This reserve ratio implies that ETH’s market price at the time of writing is 2,700 / 86,000 = 0.0314 WBTC.
Crucially, the AMM does not update this price as other markets move around it. The market price only moves as the reserve ratio of the tokens in the pool changes, which happens when someone trades against it.
To explore an example, what happens if the price on Binance falls to 0.0310 WBTC? That implies Uniswap LPs are currently buying ETH at a premium, creating an arbitrage opportunity. As a result, arbitrageurs buy the “cheap” ETH on Binance and sell it on Uniswap for an immediate profit. They keep doing this until the next unit of Uniswap ETH only pays 0.0310 WBTC—same as on Binance—and they can no longer profit by selling more. In our example above, this point is reached after selling 550 ETH to the pool for 17.2 WBTC (ignoring fees and gas for simplicity).
As a result, even though AMMs don’t update their prices based on incoming real-world information, traders can still expect the price quoted by an AMM to closely track the global market price because of continuous arbitrage.
While we learned how to compute the current market price from the ratio of two token reserves, this market price only shows the price the AMM wants for the marginal token. However, in practice, a trader will often buy or sell many tokens at once, with every token costing more than the previous one.
This difference between the current market price and the expected fill price is called price impact.
Price impact is a function of
Chart 2: Comparing the average fill price (left y-axis) and price impact (right y-axis) for different order sizes (x-axis). Both factors increase with order size. The larger the order relative to the pool, the further above the market price will the trade get filled.
Chart 3: Comparing the average fill price (left y-axis) and price impact (right y-axis) of a 10 WBTC sell order on different pool sizes on Uniswap V2 (x-axis). The pool size is the total value of the pool including the reserves of both assets. The orders represent 0.19%, 1.85%, and 18.52% of the pool respectively. So a good rule of thumb is that the price impact of your order is about twice the size of your order relative to the pool.
As we alluded earlier, price impact can represent a large share of a trade’s overall execution cost. Here are some simple strategies to minimize it:
Chart 4: Trading pairs for UNI, sorted by liquidity within 2% of the market price. Note the difference in spread between Uniswap and Bitfinex. Source: Coingecko
Chart 5: The optimal strategy to buy 10 (
In the second part this series, we will focus entirely on slippage. Almost all AMM trades are subject to frontrunning, causing them to be filled at the maximum slippage the trader is willing to accept. This is a unique “feature” of trading on a public blockchain that cannot be avoided with how decentralized exchanges work today. The cost can only be shifted around, leading us to formulate “The Sandwich Trilemma”.
_Credits: Thanks for valuable discussions and reviews to EvanSS, Georgios Konstantopoulos, Dave White, Dan Robinson, Arjun Balaji, and raul_
Disclaimer: This post is for general information purposes only. It does not constitute investment advice or a recommendation or solicitation to buy or sell any investment and should not be used in the evaluation of the merits of making any investment decision. It should not be relied upon for accounting, legal or tax advice or investment recommendations. This post reflects the current opinions of the authors and is not made on behalf of Paradigm or its affiliates and does not necessarily reflect the opinions of Paradigm, its affiliates or individuals associated with Paradigm. The opinions reflected herein are subject to change without being updated.
Disclaimer: This post is for general information purposes only. It does not constitute investment advice or a recommendation or solicitation to buy or sell any investment and should not be used in the evaluation of the merits of making any investment decision. It should not be relied upon for accounting, legal or tax advice or investment recommendations. This post reflects the current opinions of the authors and is not made on behalf of Paradigm or its affiliates and does not necessarily reflect the opinions of Paradigm, its affiliates or individuals associated with Paradigm. The opinions reflected herein are subject to change without being updated.