What are Automated Market Makers or AMMs? | Deep Dive

AMMs - What are they? How do DEXs work? What are DEXs and much more.

What are Automated Market Makers or AMMs? | Deep Dive

Introduction

Ohaiyo, anon-kun! Looking for a blog to get started with DeFi? Well, you're at the right place. Throughout our blogs, Midwit Capital aims to educate the users on the basics as well as go more in-depth into crypto. This time, we're going through AMMs. What are they? How do DEXs work? What are DEXs and much more.

DEXs? AMMs? What are you talking about?

So, sit back, grab a cup of coffee because it's gonna be a long read!

MMs? TF are those

To understand how exchanges work, let's take a step back and learn what Market Makers or MMs for short are. MMs are entities that help create a market for a particular asset/token by offering bids and asks across various prices.

Their primary profit comes from the fee rebates that they get for making these markets. Do note that this only covers the basics of MMs and what they are. A seperate blog in itself is needed to dive into different things related to them.

Subscribe to our blogs and if we reach 50 Subscribers then we'll launch a blog on Market Makers or MMs 😏

Most centralized exchanges such as Binance, Bitfinex, Coinbase etc. are orderbook based exchanges which is basically an MMs playground. No, they don't hunt your stops or anything like that but rather provide liquidity at various prices for which they are paid.

Stop believing some random ICT bullshit
Orderbook of ETH-USD across various exchanges. Source: Okotoki

However, DEXs or Decentralised exchanges majorly work on a different model are known as Automated Market Maker or AMM based exchanges. Unlike traditional exchanges, users trade against a central liquidity pool on a DEX thus eliminating the need of any middlemen/exchange or counterparty.

In short, CLOBs have transparent Orderbooks made by several MMs and users placing limit orders and then we have AMMs in which users/entities pool in money into one *sort of a* fund and other users buy/sell against that pool.

With this brief about DEXs, let's take a look at the two primary things involving any AMM based DEX; Liquidity Pool and Slippage.

Liquidity Pool

Liquidity Pools are the base of any AMM. As the name suggests, they are pools of liquidity created and deposited into by people called liquidity providers. Liquidity pools are majorly dual-asset pools in nature implying that you'll need to deposit 2 assets in that pool in a fixed ratio (generally a 50/50 ratio). Having a large liquidity pool is a necessity to help reduce slippage.

Cat ate a 20% slippage on his trade

Slippage

In technical terms, Slippage is the effect on price due to a market order big in size relative to the total liquidity available. Due to this, the user swapping tokens would have lesser number of tokens as his order is getting executed at a worse price than he'd anticipated.

Basically, don't market buy or sell but utilize your DEX's DCA feature, anons!
DCA feature on Jupiter Exchange

To see how badly it might affect a user, let's take an example of SOL-USDC swap which is the most liquid pool on Solana. In the example below, you can see that if I swap 20,000 SOL for USDC, I'll have a 4% price impact thus impacting the minimum received to about $1.685 Million from the expected output of $1.767 Million which is the correct valuation of total SOL.

This implies that my maximum loss could be about $82,000 USDC! (Expected output - minimum received) while my average loss would be about $73,000 ($1.767-$1.694 Million)

Price impact of a large trade against SOL-USDC pool on Solana. Source: Jupiter

The above example implies 2 primary pointers:

  • Why a large liquidity pool is necessary
  • and why traders should utilize DCA/TWAP feature to avoid any major price impacts
Unless you're like me and trading with 1 SATS then it's fine

As mentioned above, the aim of liquidity pools is to provide users with best possible swaps and to incentivize liquidity provision, a fraction of the fees generated by the AMM is shared between the different entities. This process is known as Farming or Yield Farming.

Should we cover some yield farming avenues on Ethereum, Solana etc?

This raises the question: Are there risks for LPs when depositing their funds? The answer is yes. That demon is called as Impermanent Loss.

Impermanent Loss

The most prominent risk to any liquidity provider is known as Impermanent Loss or IL. Impermanent Loss refers to the divergence in value of you just holding the tokens versus putting them in a Liquidity pool. I know, I know the definition can get confusing so how about we illustrate this with an example

Let's assume we hold two tokens called X and Y and there is a liquidity pool on Uniswap for this X-Y pair. We also assume the following for making the example easier:

  • Current prices of X and Y are 1 and 5 USDT respectively.
  • You have 10000 X tokens which is equivalent to 10000*1 = 10000 USDT. Now, to put all of this in a 50/50 token pool which basically means the two assets have equal weightage, you'll require 10000/5 = 2000 Y tokens respectively. It also means you're putting 20000 USDT worth of tokens (10000 X and 2000 Y tokens)
  • Assume that there are 100,000 X and 20,000 Y tokens implying you'll be 10% of the liquidity pool.

Now let's assume the price of token X goes from 1 to 2.5 USDT. Since, AMMs don't adjust their prices themselves, it's the arbitrageurs that do so by buying token X here to sell somewhere else or vice-versa.

Now, after these trades, let's assume the pool has 40,000 X tokens and 20,000 Y tokens to keep the 50/50 ratio thanks to arbitrageurs.

If you now try withdrawing your 10% share of the pool, you'll receive 4000 X tokens and 2000 Y tokens with a total value of 4000*2.5 + 2000*5 = 5000 USDT + 10,000 = 15,000 USDT.

Now, had you kept the same tokens with yourself, you would have a total of 10,000*2.5 + 2000*5 = 25,000 + 10,000 = 35,000 USDT making the difference in value, an astounding 20,000 USDT.

This indicates that holding onto your tokens would have been more profitable than supplying liquidity!

Shoutout to BigDSenpai for the pastas UwU

To sort of TL;DR the whole Impermanent Loss messup happens because AMMs buy the token X if its price increases while sells X when its price decreases which basically means it buys high and sells low *like most of you out there* thus causing IL.

Ways to minimize IL

There are only a few ways to minimize or reduce your IL. First, is to use those pools where one token is a stablecoin like USDC, USDT etc. Imagine in the above case that the price of Y token had also changed, this would have resulted in even bigger losses thus having an exposure to stablecoin on one side of the pool helps in reducing your impermanent losses.

Another way, which is a more degen way is to use those farms that have like 100000% or some huge number APYs. Now, hear me out. The goal is to simply be early to these farms as fast as you can and milk the rewards such that they compensate for any potential IL.

Note: There aren't any IL calculators that can help you know when your yield has offset your IL. So, be careful whenever yield farming.

Types of AMM models

Now that we've covered the basics of an AMM including it's disadvantage in the form of IL, let's dive into the different types of AMM models.

During the initial years of AMMs, we saw Constant Function Market Makers or CFMMs for short come up. These models ran on the principle that any trade proposed is accepted if and only if the value of function before and after the trade takes place is constant. This condition is what gave these CFMMs their names. One such model which was developed by Uniswap is called x*y = k model.

We'll be covering this in more depth later so don't worry!

Now, CFMMs are also known as the first generation AMM models and can be sub divided into three categories. Let's take a look at each of them 👇

Constant Product Market Makers or CPMMs

Popularized by Uniswap and Bancor, CPMMs are your x*y = k model. In simple words, it implies that the product of the token reserves in the pool remain constant before and after any trade. So, if the number of X tokens increase or x in the equation increases, we should see a decrease in the number of Y tokens or y.

The original equation for a CPMM model is:

Original CPMM model equation

where R represents the reserves of each asset and γ is the transaction fee.

If *Big IF* you've studied high school level maths, you'll realize that an equation of this form when graphed would result in a hyperbola. This also implies that at any price point in time, there will always be liquidity, even when the price approaches zero which is a problem.

Graph of a CPMM model equation

Think of it this way, if you're providing liquidity to a BTC-USDT pool, you would prefer keeping most of your liquidity in the current range of price of BTC-USDT rather than equally dividing it from 0 to infinity right? This problem is solved by a newer model called CLMMs which we'll cover at a later point.

Constant Sum Market Makers or CSMMs

This. Is. Shit lmeow. As the name suggests, an AMM based on this model follows that the sum of reserves of two assets remain constant over time which boils down to a simple x+y = k equation.

The original equation of CSMM is:

Original CSMM model equation

where R represents the reserves of the i-th token in the pool.

Now, while I do call it shit, it is very useful for any stablecoin pools i.e those pools where both assets are stablecoins. Another benefit of this is the fact that this could help in creating an n-token pool.

However, if you plot it's graph, you'll see that it's a linear graph with a x and y intercept. What this basically implies is that liquidity for a token isn't infinite i.e can go to zero in extreme cases. Traders can drain one of the assets in the pool thus completely destroying it in the process. Thus, this is an unfit strategy and is rarely used by AMMs. As said, This. Is. Shit.

Graph of a CSMM model equation
Don't forget to hydrate yourself. Take a sip of water before continuing!

Constant Mean Market Makers or CMMMs

The final model in the traditional CFMM models is CMMMs. It is a generalized form of CPMMs which can't have more than two tokens while also not requiring a 50/50 weight distributionn in the pool.

The mean i.e the geometric mean in this model is constant. Again, asking for a lot but if you know geometric mean, for 3 tokens, it goes something like (x*y*z)^(⅓) = k.

The original equation is of the form:

Original CMMM model equation

where R is the token reserve for the i-th token in the pool and w is weight of each asset in the pool.

If there were two tokens and w was 0.5 or 50%, this would resemble your CPMM model.

Since, it's a curve for more than two tokens, this could be harder to visualize however, for three tokens, the graph would look something like:

Graph of a CMMM model equation

It was first introduced by the chads over at Balancer. This breakthrough so to speak allowed for more than two token liquidity pools allowing us to swap between any two assets in that pool.

Second-gen AMM models

Now, why do we call the CFMMs as first-gen AMM models? Because to tackle the two major issues of Slippage and Impermanent Loss, newer AMM models started popping up. While they have definitely helped in reducing these issues, the issue of IL is inherent in any AMM model and thus can not be brought to an end.

On that note, let's dive into the newer models and see how they're solving these problems.

Concentrated Liquidity Market Makers or CLMMs

Introduced by Uniswap in their v3 model, CLMMs are a unique model in the sense that it allows the user to set a price range in which they would like to provide liquidity.

How does it work? Well, it breaks the price range set by users into ticks and then equally distributes the liquidity across all ticks.

What is a tick? Think of it as the smallest denomination of the price movement for the asset. For example, if you open your wallet or even go to Birdeye, you'll see that for most assets, the price would be given till two decimal places, something like $156.88 for SOL.

SOL-USD on Birdeye

This implies that the smallest movement for SOL could be $0.01 in either direction implying that 0.01 is the tick size for SOL.

So, if I am utilizing a CLMM for providing liquidity from $154 to $160 on SOL-USDC, it would equally divide my liquidity into each tick from $154, $154.01, $154.02..... to $159.98, $159.99, $160.

Hopefully you get the gist of how CLMMs work and if not, visit Uniswap and learn about the CLMMs in much more detail

Due to this, it not only solves the issue of slippage as users would be able to provide more liquidity around the current price range but also given the active rebalancing nature of this, it also helps users in reducing impermanent losses.

If you notice, this is eerily similar to typical MMs across orderbooks that you see on centralized exchanges 👀

The only downside is that since the user has to actively rebalance the price range it provides liquidity at, if you're doing it on ETH, you'll be spending a lot on gas fees. Thus, this is better if you're doing it on L2s like arbitrum, optimism or even other L1s like Solana, Binance Smart Chain etc.

Hybrid CFMMs

Hybrid CFMMs were introduced by Curve during their v2 model which basically as the name suggests combines two different CFMMs to get a new model which could be used not only for stablecoin swaps like Curve v1 but also for more volatile pairs like ETH, BTC etc.

What you end up with from this fancy formula is a mathematical curve that projects a hyperbola. It gives you an almost steady line in the middle for prices within a certain range similar to CSMMs, but goes all exponential when prices go beyond that range similar to CPMMs.

Curve AMM Model vs Uniswap AMM Model Source: Curve Whitepaper

If you notice Curve's model vs Uniswap's model, you'll notice it is not only better in the middle portion where you have better liquidity provision and similar to your CSMMs thus leading to reduced slippage or price impact for large trades but also going towards the extremities, the earlier reduced exposure helps prevent any major Impermanent losses to the LP.

If you notice, it's kind of behaving like a CLMM with most of it's liquidity focused in the current price range

Delta Neutral Market Makers or DNMMs

Introduced by Lifinity, *who btw have been crushing the Solana DEX volumes generating 3-4 fig APYs across most pools*, these were an overall improvement to their previous Proactive Market Maker or PMM.

For those of you that don't know, PMM is a very basic but interesting modificatioin of the CPMM model. It basically changes the value of the constant k in the x*y = k equation thus the equation used is x*y = c*k where c can be changed by the model depending on a few parameters to accomodate price fluctuations and thus reduce IL in a way.

However, Lifinity went a step further and improved upon the current PMMs by targeting the value of the initial deposited amount of the base asset rather than focusing on the 50/50 ratio as is the case in CPMMs and thus in PMMs. Remember how we mentioned that IL is caused due to buying high and selling low by the AMMs, this is due to the 50/50 ratio that it has to maintain. However, in DNMMs, what Lifinity does is it targets the amount of deposited base asset rather than the equal weights.

With their v2, the amount of SOL stays fixed until a rebalancing event kicks in. So, when the price goes up, SOL ends up making more than half of the pool, and when it goes down, the opposite happens. Then, when the price changes by a set amount and a rebalancing event occurs, they tweak the SOL amount so it's back to 50% of the pool's value at that moment.

Lifinity's v1 vs v2 AMM model. Source: Lifinity Blog

So, instead of constantly rebalancing to a 50/50 pool with every price change, DNMM delays the rebalancing to only happen when it changes by a set amount. It also implies that fees which is generally in native tokens will be regularly changed to stable assets like USDC/USDT.

The frequency of rebalancing, determined by the size of the price change triggering it, impacts a pool’s Market Maker Profits or MMP and needs optimization for each pool. The goal is to capitalize on volatility by buying low and selling high, but not too frequently to avoid increased IL risk as more frequent rebalancing approaches the v1 model.

Other 2nd Gen models

There have been other newer AMM models as well such as PMMs as seen in the case of Lifinity v1, dAMMs or Dynamic AMMs, vAMMs or Virtual AMMs and more which we can't cover given the length of blog.

However, these second generation models all are trying to ultimately reduce slippage as well as Impermanent loss which by far they've been able to do so and we can't wait to see what further refinements do we see in the AMM models as we progress further in time.

Ending Thoughts

and with a lengthy read, we've come to the end of the blog. We think that while AMMs are an innovative solution, they're still in their nascent stage as is the whole space with very few actual innovations and it would be pretty interesting to see how newer protocols come up with better and improved AMM models with the goal of not only reducing slippage and IL but also providing a smooth user experience.

Here's a reward for reaching the end 😄

Till then, Happy learning and don't midcurve things, anon!

-Midwit