The Hitchhiker’s Guide to DeFi (Part II) — DEXs & AMMs

Rahul Rai
Gamma Point Capital
8 min readJan 20, 2021

--

Disclaimer:

  1. The above reflects the views of the investor and should NOT be construed as investment advice, financial advice or legal advice.
  2. This information is purely educational and is NOT meant to be taken as a recommendation to buy or sell any product or service. Please do your own research before making any decisions.
  3. Where relevant, direct quotes & passages have been taken from the sources, whitepapers, and blog posts mentioned.

In our previous piece, we covered lending & borrowing protocols, the building blocks of DeFi. In this post we’ll we’ll dive into Decentralized Exchanges (DEXs). DEXs are peer-to-peer (p2p) electronic markets that are built on the blockchain, and that utilize Automated Market Maker (AMM) protocols to provide continuous liquidity with minimal slippage. We’ll highlight the three main types of DEXs, and highlight the underlying equations that drive their AMM protocols.

We’ll end by deriving an equation to quantify “impermanent loss”, an exotic financial risk that all DEX liquidity providers are exposed to, yet most are either unaware of, or don’t mathematically model out.

DEXs & AMMs

Decentralized Exchanges (DEXs) DEXs are decentralized exchanges for digital assets, allowing investors to seamlessly trade between cryptocurrencies, without the need for a traditional order book-based centralized exchange DEXs typically have three participants:

  • Traders: Use DEXs to swap assets in and out of the pool
  • Liquidity providers (LPs): Deposit their assets in liquidity pools, on which they earn returns by collecting trading fees
  • Arbitrageurs: Maintain the price of assets within that portfolio in accordance with the market price in exchange for a profit.

Automated Market Makers (AMMs) AMMS are smart contracts that hold liquidity reserves (or liquidity pools) that traders can trade against. These reserves are funded by liquidity providers (LPs). AMM protocols use a formulaic approach to determine the price of an asset. Constant Function Market Makers (CFMMs) are specific and the most widely used type of AMMs that were designed by the crypto community. The term “constant function” refers to the fact that any trade must change the reserves in such a way that the product of those reserves remains unchanged.

There are three main types of DEXs/CFMMs:

  1. Uniswap (Constant Product Market Maker)

2. Balancer (Constant Mean Market Maker)

3. Curve (Hybrid Function Market Maker)

Uniswap

Uniswap is one of the first ever open source decentralized exchange protocols. It was launched in November 2018 by Hayden Adams (V2 was launched in May 2020) and it pioneered the implementation of a constant product market. A transaction in this market, swapping Δβ coins of β for Δ⍺ coins of ⍺ must satisfy:

Where Rα and Rβ are reserves of each asset, k is the invariant, and γ is the transaction fee. After each transaction, reserves are update in the following way:

The name “constant product market” comes from the fact that adding any amount of either asset must change the reserves in such a way that the product Rα*Rβ remains equal to the constant k (assuming no fees).This is often simplified in the form of x*y=k, where x and y are the reserves of each asset.

Assuming no-arbitrage conditions, the marginal price of coin ⍺ in terms of coin β , which is defined as the price of an infinitesimally small trade (Δ → 0) can be determined by differentiating the constant product market equation as follows:

Hence marginal price offered by Uniswap (assuming no fees, i.e γ=1) can represented as

A constant product function forms a hyperbola when plotting two assets, which has a desirable property of always having liquidity as prices approach infinity on both sides of the spectrum.

Balancer

Balancer took this one step further by implementing a constant mean market. A constant mean market maker is a generalization of a constant product market maker, allowing for more than two assets and allowing for weights outside of 50/50, and satisfies the following equation:

Where Ri is the reserve of each asset, w is the weights of each asset, and k is the invariant. In other words, in the absence of fees, constant mean markets ensure that the weighted geometric mean of the reserves remains constant. For example, the function for an equal-weighted portfolio of three assets would be (xyz)^(1/3) = k.

Source: Balancer Whitepaper

Curve

Curve Finance, which focuses on stablecoin and wrapped asset liquidity, has seen a significant increase in trading volume.

The following figure shows the daily trading volume of each stablecoins on Curve. The figure shows that DAI, USDC & USDT have the highest trading volume among the stablecoins supported on the platform due to the direct demand from liquidity mining.

Source: TokenInsight

Curve aimed to modify the AMM function to minimize slippage for assets that are relatively stable-priced. The protocol looked for an invariant that’s relatively flat near balance (price changes slowly), however that shifts towards the constant-product invariant as the portfolio becomes more imbalanced. The hence came up with the following Stable Swap invariant:

Source: Curve Whitepaper

Impermanent Loss Calculation

One final, yet crucial, comment on DEXs — there is an exotic financial risk called impermanent loss that is not intuitive, and yet every liquidity provider (LPs) is exposed to it.

Many LPs are unaware of the fact that they are exposed to price risk, that can wipe out the profits they earned through fees by providing liquidity. This price risk is captured by the term “impermanent loss”. For eg, when liquidity providers look at the value of their stake in the liquidity pool, they may find that it’s worth less than what they put in. What’s going on here?

To understand why this can happen, let’s assume that the liquidity providers are using the Uniswap V2 protocol. As shown earlier, assuming no-arbitrage conditions, the price can be represented as:

Where Rα and Rβ are reserves of each asset at time t, and Pt is the price of token ⍺ in terms of token β at time t (i.e the no of tokens of β that you would receive in exchange for one token of ⍺)

Combining this with the constant product market function Rα*Rβ=k , we can work out the size of each liquidity pool at any given price as follows:

We can now compute Vt the value of the LP’s portfolio at time t (in units of coin β)

Hence the LP’s PnL (in units of token β) at any time t=T can be represented as:

The value of a portfolio (HODL) that was bought at time t=0 and held to time t=T is:

Expressing R⍺ and Rβ in terms of Po, this equation can be expressed as:

We’re now ready to calculate the impermanent loss. Impermanent loss is defined as the % difference between portfolio values if we bought and held the underlying coins (HODL) vs provided liquidity into the pool.

When we plot the impermanent loss as a function of the price ratio, we get the following graph that clearly exhibits negative convexity, which is never desirable for LPs!! This means that no matter which direction the price moves in, LPs will always be taking a hit vs HODL because of the impermanent loss.

Source: Pintail

So the actual return for LPs is a balance between the impermanent loss caused by the price differential and the accumulated fees from trades on the exchange. Does this make intuitive sense? Absolutely! When price takers come in looking to trade or swap, the liquidity providers are forced to take the opposite side of the transaction. Hence The phrase “impermanent loss” is hence a misnomer, as the loss is very much permanent, unless the exchange rate returns back to the original price.

If long term price movements are large, they could result in losses much greater than the yield generated from collecting fees. However, if Uniswap manages to gain enough traction it could be profitable for LPs, as long as price movements remain sufficiently rangebound.

Summary

1) Constant Function Market Makers is the brainchildren of the ingenious DeFi community. For LPs providing liquidity to these protocols, the value of their pool at any time t can be represented as:

Hence, we can model out the returns LPs are expected to earn by using the following equations:

2) And finally impermanent loss, which is defined as the % difference in portfolio values for providing liquidity vs buying & holding, can be represented as

--

--

Rahul Rai
Gamma Point Capital

Finance, Tech, Crypto. Formerly FX at Morgan Stanley. Wharton ‘19.