How Liquidity Pools work (Yield Farming)
Understand how liquidity pools work on decentralized exchanges like Uniswap, Sushiswap, Pancakeswap, Curve, among others. Let’s start with the basics.
What is liquidity?
When you try to buy bitcoin on a traditional exchange, a buy order is placed. Similarly, those who want to sell bitcoins place a sell order.
A trade will only occur when the price that one person is willing to buy is equal to the price that another person is willing to sell. When there are many people involved, this happens all the time.
When you can easily buy or sell an asset at a price very similar to the last price that was traded on the platform, it means that the asset has liquidity.
Now imagine the opposite. Imagine that an asset has few buy and sell orders, where the highest buy order is $10 and the lowest sell order is $20. Since the price difference is too big, there is no trade. The last trade that took place on this asset was two days ago, at the price of $14. In this case, you don’t know the real price of the asset you only know that there is someone willing to buy for $10 and someone willing to sell for $20. If you want to buy for less than $20 or sell for more than $10, you will not succeed. This means that this asset is not liquid.
Very popular assets usually have high liquidity. It is also common for popular exchanges to have high liquidity. On the other hand, smaller exchanges and little-known cryptocurrencies have always suffered from the problem of lack of liquidity. This is when the concept of a liquidity pool emerged, which became very famous thanks to the Uniswap exchange.
A liquidity pool works like a box divided into two parts, where each part stores a token. This could be, for example, the ETH and USDC tokens. An ETH/USDC pool exists to provide liquidity for buy and sell orders between these two tokens. Let’s use this pool as an example:
Let’s say the price of Ethereum is $1000 at the time the pool was opened. This means that the allocation of tokens in this pool at the time of opening was in the ratio 1000 to 1, since 1000 USDC tokens were needed to purchase 1 ETH token.
Let’s imagine that the pool started with 50,000 USDC tokens and 50 ETH tokens. Whoever provides liquidity to the pool is called a liquidity provider, and is rewarded for it. The fee charged for any trade goes entirely to the liquidity providers.
Let’s understand now how a buying and selling process works.
Alright, consider that a user owns 7.008,77 USDC tokens and decides to buy them all in ETH. Instead of doing this on a centralized exchange like Coinbase, he prefers to use a decentralized Exchange, and has discovered our pool. When he makes this purchase in our pool, he will be sending USDC tokens to the pool and receiving ETH tokens in return. As a result, the pool will be unbalanced.
If the user were to receive ETH at the exact 1000:1 ratio, he would receive about 7 ETH.
Let’s see what the new ratio of tokens in the pool would look like. In other words, 1 ETH is now worth 1.326,05 USDC.
This makes sense, after all there was an ETH purchase. It means that the demand for ETH has increased, so its price should increase.
But there is a problem here. If we perform the operation this way, the liquidity providers in the pool will be at a loss.
Notice: If Bob is a liquidity provider and initially offered 5 ETH and 5,000 USDC,
it means that Bob owns 10% of the pool.
Before the ETH buying transaction, Bob owned in dollars the equivalent of $10,000.00
Notice: Now, considering the current proportion of tokens in the pool and taking 10% of that, at the current price, he has:
If Bob had not provided liquidity, considering the new value of ETH he would have:
Notice that it would have been better not to provide liquidity. This is happening because the entire imbalance between supply and demand is being paid for by the pool.
The user, in this hypothetical scenario, have caused an imbalance but have not been burdened at all.
The concept would not work if it were this way. The actual calculation is done in such a way that the user will pay some of the imbalance and the liquidity provider will pay the other part.
The distribution occurs by the formula:
Where ETHLP stands for liquidity provided of the ETH token. Don’t be alarmed, the concept is simpler than it looks.
As the pool in our example started with 50 ETH tokens and 50,000 USDC tokens we have: k = 50*50000 = 2500000
Now we have another formula, which defines the final ETH price for the pool after any transaction:
In other words, the price of an ETH to the pool will always be given by the current ratio of the amount of USDC tokens and ETH tokens in the pool. But note that this is the final price to the pool it refers to the ratio that an liquidity provider would withdraw from its tokens if it wanted to stop providing liquidity.
With a little algebra, using these two equations to isolate ETHLP and then USDCLP we will have:
That is, since we already know the value of k we can immediately calculate the price of ETH that will be left in the pool from any information from the user that will buy. With the formulas it is quick to see what the final price of ETH in the pool would look like. But how much will the user pay?
What is the price of ETH for that user?
This can be calculated indirectly.
For example, let’s say the user wants to buy 6,1471 ETH. Putting that value into the formula 1 to calculate the ETHPRICE we get a value of $1.300,00. But that is the final price of the pool, not what the user will pay.
The user is going to pay USDC, so we need to calculate what the USDCLP is. Now that we know the ETHPRICE, this is easy from the formula 2. We would have USDCLP = 57.008,77. That is, the user needs to provide 7.008,77 USDC for the pool to have this final balance since before the pool had 50,000 USDC. As we already know how much USDC the user will have to provide the price he is paying for each ETH is.
Notice that this value is higher than the initial $1.000,00 and lower than the final $1.300,00. It means that the user paid a part of the imbalance while the pool paid another part.
If a new user comes to buy ETH, he will pay more than the pool’s current $1.300,00 since this is the value of the current ETHPRICE before the new imbalance. Note: the constant k always remains the same.
In the same way, if this new user is going to sell ETH, he will get less than $1.300,00 for each ETH for the pool owners, who previously gave ETH at one price and are now receiving ETH at another price a return to the initial value of $1.000,00 for each ETH would put them at no loss at exactly the same initial conditions as when they opened the pool.
Observe:
Imagine that another user sells 6,1471 ETH in this pool, which is the same amount that had been purchased previously.
Applying this value to the formula 1 we have ETHPRICE = $1.000,00. Let’s do a summary then to understand the risks for a liquidity provider.
For a liquidity provider, the prices of the tokens at the moment it provides liquidity serves as a reference.
If, at the time of withdrawal from the pool, the prices of the tokens in the pool are different from the reference price he has lost money, as it would be better to be HOLDING both tokens instead of providing liquidity.
If, at the time of withdrawal, the values are the same as when he provided the liquidity he doesn’t lose. This is why this loss is called “Impermanent Loss”, because it is a non-permanent loss, it can change over time.
So, what is the advantage for the liquidity provider?
Every trade executed on Uniswap, for example, has a fee of 0,3%, and this fee goes directly to the liquidity providers. This way, when any liquidity provider withdraws its tokens, it will proportionally take those fees along with it.
In short, for a liquidity provider to make a profit it is necessary that the fees paid are greater than the loss due to imbalance. This does not always happen, so it is important to be aware of the risk.
One way to minimize the risk is to offer liquidity for tokens that do not usually vary from one another.
For example: the pair USDC/DAI
Exchange Curve specializes in this kind of pool.
Many decentralized exchanges offer extra incentives to liquidity providers by giving native governance tokens (e.g. UNI token on Uniswap). This is called yield farming, or liquidity mining.
So that’s what yield farming is: receiving native tokens for providing liquidity to the Exchange.
The name given to the protocol responsible for determining prices and trades between assets is Automated Market Maker.
Each decentralized Exchange has its own AMM. For traders, this strategy mentioned in this article “solves” the problem of liquidity on decentralized exchanges.
After all, if the price on decentralized exchanges is mismatched with the market, this will bring an immediate arbitrage opportunity.
Through the AMM system explained in this article, Uniswap “guarantees” automatically executed orders at market price. The name of this is Swap.
If the pool is big enough so that the size of the order is very small relative to the size of the pool, the fee that the trader will pay ends up being around 0,3% only with no loss for having unbalanced the pool.
We showed in this article an example of a very small pool to facilitate understanding, but obviously the pools are much larger than that, which minimizes the unbalancing effect.
In practice, providing liquidity to smaller pools results in more governance tokens received to encourage small pools to grow.
Exchange Sushiswap is a fork of Uniswap, which came up with the idea of providing SUSHI governance tokens before Uniswap gave UNIs.
Pancakeswap is Binance’s version of AMM exchange.