# Constant Product Market Makers

By [Danki](https://paragraph.com/@danki) · 2023-05-05

---

I took a vacation.

Well, not really. I was reading codebases everyday and practically lost track of the days. And the further I went, the harder it was to explain what the hekk iz Danki up to. I was already deep into Curve’s Stableswap Equation when I realized that almost a week has already passed.

So here I am frens, doing my best attempt to catch you up.

Now the story. It all started with Constant Product Market Makers…

The Most Basic Type of Swap
---------------------------

If you’re swapping your USDT for ETH on say, Uniswap v1, then USDT doesn’t magically transform into ETH tokens through some wizard program. No na no… instead da ETH is already stored somewhere, as well as the USDT you just deposited. And that ‘somewhere’ is called a liquidity pool.

In the USDT-ETH example, a liquidity pool works like this:

1.  Somebody initially supplies two tokens in the pool, in equal value
    
2.  As users like you start swapping their tokens in the pool, the ratio of the two tokens change
    
3.  But the total liquidity remains the same, just the ratio is different
    
4.  The price of one token is computed by the ratio of its supply with the other token.
    

…And this liquidity is expressed in the famous Constant Product MM equation:

    x * y = k

Where x is the amount of token1, y is the amount of token2, and k is the liquidity of the pool.

So the price of each token changes as people swap because the ratio in the liquidity pool changes.

It looks like dis…
------------------

Say, somebody initially supplied 10,000 USDT and and 5 ETH in the pool:

![](https://storage.googleapis.com/papyrus_images/f0575824b377efefa53a931bf38a4b49ce3f4b934d23799b759d784c919c1a06.png)

At this point, if we use x _y = k, then_ k = 10,000 \* 5 (which is 50,000). And the ratio will be 2,000 USDT per 1 ETH

Now, enter you, and you wanna swap your 2000 USDT for ETH. Let’s ignore fees for now. How will this affect the pool and at what price will you get your ETH?

Well, you are practically adding 2000 USDT in the pool which should correspond to a change in the amount of ETH. ETH will decrease in order for _k_ to stay constant.

![](https://storage.googleapis.com/papyrus_images/25efc24bd5ff4ce7969e75b2b8fc8b92f1141e34c49107d95fd9fc37567455e4.png)

Then, the market maker will solve for the amount of ETH it will give you:

k = 50,000 = (10,000 + 2,000) \* (5 + dx)

**dx = -0.833333 ETH**

ETH supply in the pool will decreased by 0.833333, which is the amount they’ll give you for the 2000 USDT you deposited

_“But why 0.8333333, I thought the price is at 2000 USDT per 1 ETH!”_

Were you cheated? Not really, because the ratio of USDT per ETH was going up as you were adding more USDT to the pool, bringing the price of ETH higher than what was initially shown… And dat, frends, is called the **price impact** of your trade.

And because price is computed by the two token’s ratio in the pool, the lower the k value or liquidity, the bigger the price impact of each swap.

In traditional finance, you can also recognize the same phenomenon where the low-volume assets are more volatile. In their case, instead of an algorithm, the market makers are the hedge funds (except the hedgies got alpha but that’s for another story lol).

Anyway, the amount of tokens in the pool is not only affected by swaps. Liquidity in the pool can also be removed or added.

Then what happens if somebody provides more liquidity?
------------------------------------------------------

When somebody provides liquidity to a protocol, they get minted what is called an LP Token. The **LP token** represents the percentage of liquidity that the user owns in the pool. This keeps track of how much of the pool they can withdraw and the share of rewards they get from the collected fees.

Ok, let’s see this in action.

Say, your good frend Marites supplied 5 more ETH and 10,000 USDT to the pool at the current rate of 2000 USDT per 1 ETH.

The pool had 5 ETH and 10,000 USDT before and had minted 10 LP tokens to its providers, and now that Marites added to it, it has 10 ETH and 20,000 USDT. Rich frend Marites now owns 50% of the pool, and she gets minted 10 LP tokens for that. Now she owns 10 out of the 20 LP tokens.

If Marites wanted to withdraw her share of the pool, she will burn the 10 LP tokens.

But will Marites get the exact same 5 ETH and 10,000 USDT she deposited?

99% of the time, she will not. Instead she will get her two tokens in whatever ratio the pool currently has after users have swapped tokens in it. This could result to impermanent loss.

I wanna talk about impermanent loss, but das a long topic that I reserve for investors, traders, and degens in DeFi Philippines. If you wanna know more about the benefits and risks of providing liquidity to a Constant Product AMM, wait for my post in there!

You will rarely find pure CPMMs nowadays
----------------------------------------

It might sound amazing on paper, but the reality iz Constant Product MMs are very inefficient in managing liquidity.

Most of the liquidity in the pool will fall on the extreme ends of the curve where they aren’t utilized. Meanwhile, price impact is still huge in the middle range where most of the trades happen. So the newer dexes and protocols use a different model to remedy this inefficiency… and that’s what I actually wanna talk about. haha

But at least now you understand how the most basic form of AMM works, and we can move on to the real exciting part. We’re gonna talk modern market maker models in the next posts! 🐎✨

* * *

---

*Originally published on [Danki](https://paragraph.com/@danki/constant-product-market-makers)*
