DeFi Dive — Decentralized liquidity
From liquidity pools to Uniswap V4: exploring the mechanics and evolution of liquidity pools
Centralized exchanges are having a rough time lately. It all started with FTX going down, and now both Binance and Coinbase, two crypto giants, are getting sued by the SEC
Our readers who are new to DeFi might not be aware that there's an alternative to centralized exchanges: decentralized exchanges, or DEXes for short.
With a CEX (centralized exchange), you have to rely on a financial intermediary to handle your funds when trading, which means you're not in direct control of them. However, with a DEX, you hold complete control over your funds, as they are securely stored in your own wallet. The real difference between a CEX and a DEX though lies in the underlying technology. A CEX functions similarly to traditional stock or commodity exchanges, where a single (centralized) entity provides an order book for market participants to place orders. In contrast, a DEX operates through smart contracts—self-executing pieces of code that run on a blockchain.
So, where is this liquidity stored then, if not in an order book? Well, it's stored in what we call liquidity pools.
In today's DeFi Dive, we'll explore how liquidity pools work, the risks involved, and the recent advancements brought by Uniswap, the leading decentralized exchange. Let’s jump right into them!
In today’s DeFi Dive:
Why is it difficult to have an order book in DeFi?
How do liquidity pools work?
The risks of providing liquidity
Uniswap: state-of-the-art
(if you already know what LP-ing is, you can skip to this part)
Why does DeFi need liquidity pools?
In the world of electronic asset trading, the go-to solution is the order book. It's a centralized system that collects orders from market participants and uses a matching engine to pair buyers and sellers. The model is efficient, fast, and generally quite effective. So, why doesn't DeFi adopt order books?
Well, because of liquidity—the ability to trade an asset at any time at a good price.
There are two primary challenges that stop DeFi from adopting order books.
Firstly, decentralized trading occurs on-chain, which means each interaction with the order book requires an on-chain transaction and incurs gas fees. This makes the role of market makers (the liquidity providers) very costly, resulting in limited liquidity and bad pricing. On Ethereum, you would have to spend some dollars in fees for a simple transaction, making it impractical to use order books. While Layer 2 scaling solutions can mitigate this issue, that leaves room for another DeFi Dive. Secondly, in the DeFi landscape, new-born coins may have low liquidity. This implies that for a particular coin, it may be challenging to instantly find a buyer or seller who matches your order.
Liquidity pools are the solution to these problems. You don’t need much liquidity to start trading assets, and even more importantly, you don’t require a direct counterparty to fulfill your order.
What’s a liquidity pool?
Well, it's quite a simple concept. Imagine a digital pile where funds or assets are pooled together. This pool is created through a smart contract that collects funds (liquidity) and enables functions like trading or lending. A liquidity pool designed for trading usually contains two assets (the trading pair) that users can seamlessly swap between.
The best part? Anyone can be a liquidity provider, making market-making accessible to all. This system is referred to as Automated Market Making (AMM) and it eliminates the need for institutional market makers and order books. Instead, individual users provide liquidity by depositing assets into the pool, earning trading fees from users who trade their assets.
In the traditional order book model, your order is matched with another party, creating a peer-to-peer transaction. With the AMM model instead, we embrace a peer-to-contract approach. Here, you trade against the liquidity present in the liquidity pool — you deposit one asset in the pool and receive the desired one in return.
The price, in this case, is not set by the counterparty, but by the ratio between the two assets present in the liquidity pool.
Wait, but how does it work?
The underlying theory of an AMM has long been studied in academic game theory and mechanism design. The primary model we're talking about here is known as Constant Product Pools. These pools enable the trading of low-liquidity assets at any price, even during significant market fluctuations. It comes down to maintaining a balance between the quantities of the two assets involved, as in this equation:
During trades, the values of x and y in the pool will indeed change, but the product of the two will always remain constant. When traders engage in a transaction, they deposit the asset they wish to sell and receive back a corresponding amount of the asset they would like to buy. This changes the quantities of the assets in the pool, but the formula above ensures that the product stays the same. So, if a trader intends to sell a quantity ∆x of asset X, he will receive a quantity ∆y of asset Y in return, such that:
What about the price?
As you may already know, the price is determined by supply and demand. Demand is defined by the amount you want to buy, and supply is the pool reserves. Assets are priced in relation to each other. For example, in an ETH/USDC pool, ETH is priced in terms of USDC, and vice versa. The pricing of assets in a pool is determined by the supply of that particular asset, considering the other asset.
However, the actual price of a trade is calculated differently. With standard liquidity pools, the market liquidity is equally distributed along the price curve, making the market depth very thin. The price hence heavily depends on the size of your trade. When you want to buy a large amount of the asset relative to the pool reserves, the price will be higher compared to buying a smaller amount, incurring slippage (when your trade is executed at a very different price than what you intended).
This can be easily visualized in the graph below:
Managing a large trade position in DeFi can be very hard, especially because of this. If you're interested in delving deeper into the mathematical aspects of Constant Product Pools, feel free to explore further here.
So you're ready to become a liquidity provider, huh? Well, let us walk you through the process and shed some light on the associated risks.
Liquidity providing and the risks involved
When you decide to provide liquidity to a liquidity pool, you essentially deposit an equal value of the two assets in a 50/50 ratio into a smart contract. In return, you receive LP tokens, which serve as proof of your share in the liquidity pool. These LP tokens enable you to claim your assets in the future, along with any trading fees (plus incentives) earned by the pool. It's important to note that if you lose your LP tokens, you lose the ability to withdraw your assets from the pool.
LP tokens are not just receipts. You can transfer them to another wallet, transferring ownership of the provided assets. You can deposit them in yield farming protocols that automatically reinvest your profits, allowing for compound interest. And, you can use them as collateral for crypto loans.
Impermanent loss
When providing liquidity, it's important to understand the concept of impermanent loss (IL). IL is a significant risk that can affect your capital and potential profits.
Yes, there is a possibility of losing money while providing liquidity.
Impermanent loss occurs when the price of your assets changes compared to when you initially deposited them into the pool. The larger the change in price, the greater the exposure to impermanent loss.
It's similar to the risk faced by market makers in traditional markets when they provide liquidity to order books by placing buy and sell orders simultaneously. If the price moves significantly in one direction and they are not properly hedged, they can incur substantial losses.
EXAMPLE
Supertramp deposits 1 ETH and 1,000 USDC into the ETH/USDC liquidity pool. The assets have an equivalent value, meaning the price of ETH is 1,000 USDC, and the total value of Supertramp's deposit is 2,000 USD.
Additionally, the pool already contains 9 ETH and 9000 USDC, which means that after his deposit, Supertramp holds a 10% share of the pool. The constant for the pool is calculated as 10 ETH multiplied by 10,000 USDC, resulting in k = 100,000.
Now, let's say the price of ETH skyrockets to 4,000 USDC. As this happens, people buying ETH, along with arbitrageurs, will add USDC to the pool and remove ETH so it reflects the current price.
With 1 ETH now worth 4000 USDC, the pool will consist of 5 ETH and 20,000 USDC, given that the constant remains the same at 100,000 (5x20,000).
Suppose Supertramp decides to withdraw his funds, a 10% share of the pool. He would withdraw 0.5 ETH and 2,000 USDC (0.1*5 + 0.1*20,000), totaling 4000 USD in value.
He made a 100% profit!
However, if Supertramp had simply held his 1 ETH and 1,000 USDC the value of those assets would be 5,000 USD! Supertramp incurred a 1,000 USD loss since he earned less than he could have by simply holding the assets. This is known as impermanent loss and can be seen as a risk associated with opportunity cost. The loss is considered impermanent because it only occurs upon withdrawal, and because if the two assets return to their initial ratio, the loss would vanish.
So why do liquidity providers still provide liquidity despite the potential losses? Well, the interest earned from trading fees (plus potential incentives) can help compensate for those losses. If a given pool has a high trading volume, it can still be profitable to provide liquidity, even if the pool is exposed to impermanent loss.
Here's a tip: To minimize the risk of significant impermanent loss, consider providing liquidity to trading pairs of crypto assets that are strongly correlated (e.g. similar projects). Try out Impermanent Loss calculators, to make more informed liquidity-providing decisions.
Lastly, it's crucial to be aware of external risks as well: smart contract and market manipulation risks. You should always prioritize audited protocols and implement risk management strategies!
Uniswap state-of-the-art
Uniswap is the big boss when it comes to decentralized exchanges. With its 3rd version, it was the first to introduce a concept called concentrated liquidity.
In previous versions of liquidity pools, liquidity was evenly distributed along the price curve, but most of it remained unused. However, with Uniswap V3, liquidity providers now have the ability to choose a specific price range to provide liquidity to. By concentrating their capital on price intervals near the current price, liquidity providers can offer deeper liquidity around the mid-price, resulting also in increased earned interest from fees. This innovation has brought market depth to DeFi, enabling smoother execution of larger orders.
As the price of an asset moves, it may exit the price bounds that liquidity providers have set in a position. When this happens, the liquidity provided by those positions becomes inactive and stops earning fees. This mechanism helps mitigate the impact of impermanent loss on the position.
While there are many more details about Uniswap V3 that could be shared, we don't want to overwhelm you. If you're interested, check out this video:
Everyone is talking about Uniswap V4
A few days ago, the Uniswap team released the draft code of Uniswap V4, along with its white paper. In a nutshell, the standout feature of V4 is the introduction of Hooks. These Hooks are separate contracts that can be integrated into a liquidity pool, bringing in entirely new functionalities that were not available before. Think of them as customizable code plugins that allow pool deployers to customize their pools.
Some examples of these new functionalities include:
Dynamic fees based on volume clusters, volatility, or other inputs (e.g. lower fees for higher trading volume).
Supporting on-chain limit orders directly within Uniswap.
Introducing the concept of time-weighted average market maker (TWAMM), which enables large traders to execute large orders gradually over time, minimizing market impact.
Auto-compounding trading fees back into the LP positions.
Distributing internally captured MEV (Miner Extractable Value) profits back to LPs.
Moreover, Uniswap V4 will allow multiple liquidity pools to coexist within a single contract through a mechanism called flash accounting. This breakthrough significantly reduces the costs associated with trading and maintaining a liquidity pool since there is no need to move the traded asset from one contract to another. In fact, it is estimated that gas costs can be reduced by up to 99%.
While the DeFi landscape may not currently be at its highest peaks, blue chip protocols are stead-fastly announcing innovations that promise to elevate the experience of all participants, transforming and strengthening the future of decentralized finance!
If you found this Dive valuable, why not share it with a friend?
Forward this email—it's the best gift you can give us!
🐳 Feeling generous? Fund this newsletter and the degens behind it:
0x6480474717045771Ecf6561331458dcbA6229C75
(EVM)
1AXaP7Qe9cGzW6oYYs5xLfEgGHb3UppHjn
(BTC)
Powered by Unibo Fintech Society.
Disclosure: Authors may own crypto assets named in this newsletter. Stay on-chain is meant for informational purposes only. It is not meant to serve as investment advice. Please consult your investment, tax, or legal advisor before making investment decisions.