Mastery Track
Liquidity Pools & the Power of Automated Market Makers
In its simplest form, A liquidity pool is a collection of tokens & digital assets created by multiple liquidity providers pooling in their assets
Introduction
Liquidity is a fundamental part of both the crypto and financial markets. It is the manner in which assets are converted to cash quickly and efficiently. When an asset is illiquid, it takes a long time to convert it to cash.
Liquidity pools are at the heart of all complex DeFi protocols, & help them function. They also enable crypto investors to earn yield on their digital assets. There is estimated to be over $30 billion of value locked in liquidity pools.
Today we’ll explore the world of liquidity pools in detail & understand its underlying algorithm, the Automated Market Maker (AMM), which is an important component that enables the trading of digital assets in an automated way.
The Order Book model
Traditionally, trading is done using the Order Book Model & a good example of this is the stock markets.
So what is the Order Book Model exactly?
The Order Book Model works like this. All the buyers & sellers write down their orders, i.e. how many stocks they want to buy/sell & at what price. Buyers want the price to be as low as possible & sellers want the price to be as high as possible. And whoever is in the minority, gets what they wish. If there are more buyers than sellers, the price spikes & if there are more sellers & than buyers, the price drops.
Whenever a buyer & a seller meet at the same price, the buyer walks away with shares, the seller walks away with the cash & a transaction is recorded.
But there is a flaw to this model. If you are a buyer/seller, you have to wait around for a seller/buyer who is willing to trade at your price. If you set a price too high/ too low, you might have to wait around for weeks or months hoping that the price matches what you are looking for.
While order books are foundational to finance and work great for certain use cases, they suffer from a few important limitations that are especially magnified when applied to a decentralized setting. Order books require intermediary infrastructure to host the orderbook and match orders. This creates points of control and adds additional layers of complexity.
This system is very inefficient for an ecosystem where anyone can create their own token, and those tokens usually have low liquidity.
So how can we make this system more efficient? We can use something called liquidity pools.
A Liquidity pool runs on an algorithm that enables us to buy/sell an asset, no matter how high or low th the price is, doesn’t matter what time of the day is it or worry about another seller/buyer wanting to trade with us.
But there is a flaw to this model. If you are a buyer/seller, you have to wait around for a seller/buyer who is willing to trade at your price. If you set a price too high/ too low, you might have to wait around for weeks or months hoping that the price matches what you are looking for.
While order books are foundational to finance and work great for certain use cases, they suffer from a few important limitations that are especially magnified when applied to a decentralized setting. Order books require intermediary infrastructure to host the orderbook and match orders. This creates points of control and adds additional layers of complexity.
This system is very inefficient for an ecosystem where anyone can create their own token, and those tokens usually have low liquidity.
So how can we make this system more efficient? We can use something called liquidity pools.
Liquidity pool runs on an algorithm that enables us to buy/sell an asset, no matter how high or low the price is, doesn’t matter what time of the day is it or worry about another seller/buyer wanting to trade with us.
What is a liquidity pool?
In its simplest form, A liquidity pool is a collection of a tokens & digital assets created by multiple liquidity providers pooling in their assets. It’s also a venue that facilitates the trading of that pair.
In reality, a liquidity pool is just a smart contract that can hold crypto deposits, do complex calculations on them & give the tokens back to the users accordingly.
How does a Liquidity Pool work?
The person depositing tokens into the pool is known as a Liquidity Provider. Whenever liquidity(tokens) is deposited into a pool, unique tokens known as Liquidity Provider Tokens (LPT) are minted (created) & sent to the liquidity provider’s address. These tokens represent the depositor’s contribution to the pool.
To retrieve the deposited crypto tokens, the liquidity providers must exchange their LPTs for their portion of the liquidity pool, plus the fee allocation (earned through the trading fee charged)
At a very basic level, the liquidity pool holds 2 tokens & creates a new market for that Token-Pair. The pool maintains the ratio of the assets at exactly 50:50.
Some examples of top token-pairs are
USDC/ETH
DAI/USDC
ETH/USDT
DAI/ETH
Creation of a Liquidity Pool
When a pool is created, the balance of each token is 0. You need to deposit equal value (i.e. ratio of 50:50) of both the tokens, known as Token Pairs. The first person to put an initial deposit of each token is the first Liquidity Provider & the one who sets the initial price of the pool.
Lets say you want to create a trading pool containing the pair DAI/ETH & have $1000. You would need to deposit $500 worth of DAI & $500 worth of ETH. Let’s look into this in detail.
AUTOMATED MARKET MAKERS
In a liquidity pool whenever a token swap happens, it results in a price adjustment. This price is set by a pricing algorithm that continually adjusts based on the pool’s trading activity. Most DEXs use a deterministic pricing algorithm, also known as Automated Market Maker (AMM). Most AMMs use the Constant Product Market Maker model.
Basic liquidity pools use a constant product market maker algorithm. It makes sure that the product of quantities of the 2 supplied tokens always remains the same.
Let’s say we have a liquidity pool with 2 tokens pair Kitty coins (KTY)/Puppy coins (PPY).
And the first liquidity provider gives 5000 worth of KTY tokens & 5000 worth of PPY tokens.
A user brings $500 worth of PPY tokens & wants to swap them for KTY tokens. So, how many KTY tokens should we give him? (Hint: use the above formula X * Y = k)
Let’s find out how many KTY tokens should we give the user
X = 5000 KTY tokens (in the pool initially)
Y = 5000 PPY tokens (in the pool initially)
k= 25,000,000 (i.e. 5000 x 5000 total quantity of tokens)
When the user brings his 500 PPY tokens to the pool, we have
X= 5000 KTY tokens
Y= 5500 PPY tokens
k= 25,000,000 (it’s a constant)
The balance of our equation is off. So, to maintain our total liquidity of 25,000,000 we will have to give the extra KTY tokens to the user.
25,000,000 / 5500
= 4545
To maintain our equation we need only 4545 KTY tokens in our pool. We give the difference to the user.
So, for 500 PPY tokens, our user gets back 455 KTY tokens back (5000 - 4545)
How come we gave 500 PPY tokens, but only got back 455 KTY tokens?
As the demand for KTY tokens increased, they got more expensive.
Let’s find out the price of each token.
Let’s say each token was worth $1 initially. So we had $5000 worth of each token in our pool initially, i.e. $5000 worth of KTY tokens & $5000 worth of PPY tokens
Now, let’s calculate the current value of each token after our user swapped his 500 PPY tokens for 455 KTY tokens. How do we get the current price?
(Hint : we use the same formula again)
k= $5000 (keeping their initial value as the constant)
PPY tokens = $ 0.90 (5000/5500)
KTY tokens = $ 1.10 (5000/4545)
As the demand for KTY increased, so did its value.
As the supply of PPY tokens increased in our pool, its value dropped.
As we discussed earlier/ One more thing, our algorithm always wants the value of the tokens it’s holding to be 50:50,
i.e. current price of a token X current quantity of the token = Initial Liquidity of the token
While swapping & trading, the price of each KTY token will change & so would its quantity in the pool, but the total value would always be $5000 worth of KTY tokens & $5000 worth of PPY tokens.
More The Merrier
When the pool is small, a single trader would be able to influence the prices drastically. As this pool grows, the influence a single large trade has on the price change decreases making it more stable. Therefore, The bigger the pool is, the better.
You can think of liquidity providers as investors of the pool.
When there are 2 liquidity providers, they each own 50% of the pool & the total rewards (liquidity provider fees) would be distributed among them in half.
If there were total of 5 liquidity providers in a pool of $5000 worth of assets where you all contributed $1000 each, you would own 20% of the pool & would get 20% of the total rewards distributed.
As more & more people join the pool, the ratios change accordingly & the calculations turn more complex.
TRADING
Whenever a trade occurs, a fee is charged to the transaction sender which is distributed pro rata among all the liquidity providers in the pool upon the completion of the trade, as a reward for providing liquidity.
Conclusion
Like with any other investment, liquidity pools also have risks associated with them — smart contract vulnerabilities & impermanent loss being the major ones, but we’ll keep them for next time.
Most DeFi projects use liquidity pools in their protocol, but each has their own unique flair of creativity & complexity. We’ll explore these projects & much more in future articles.
Thanks for reading! If you enjoyed the blog, please click the 👏 button and share it to help others! Feel free to leave a comment 💬 below. Have feedback? Let’s connect on Twitter.