Liquidity pools are integral to decentralized finance (DeFi) applications like decentralized exchanges. At their core, liquidity pools are smart contracts that hold reserves of two assets and facilitate trading between them through automated market-making algorithms. By closely examining the key mathematical formulas and models underpinning liquidity pools, one can better understand how these revolutionary protocols function under the hood.
How Liquidity Pools Power Decentralized Exchanges
A liquidity pool contains reserves of two tokens, such as ETH and USDC. Liquidity providers deposit an equal value of both tokens into the pool to fund it. The assets are locked into a smart contract that holds them in balance. This pooled liquidity is the backbone for enabling decentralized exchanges to swap one asset for another without traditional order books.
When a trader wants to exchange one token for another, they simply trade against the liquidity pool contract instead of needing to find another person to trade with directly. Their desired token is automatically exchanged at the current rate by interacting with the pool’s smart contract. In return, a small trading fee is paid to the liquidity providers.
The Constant Product Formula
The first key mathematical model underpinning most liquidity pools is the constant product formula pioneered by Bancor. This formula maintains the following relationship between token balances in a pool:
x * y = k
x = Quantity of Token X in the pool
y = Quantity of Token Y in the pool
k = Constant product value when the pool is in equilibrium
Moreover, this formula maintains a constant product value between the quantity of token X and token Y in the pool at all times. When the pool is balanced and no trades have occurred, k remains unchanged.
For example, a pool initially contains 100 ETH and 10,000 USDC. The constant product would be
100 * 10,000 = 1,000,000.
If someone swaps 10 USDC for ETH, the pool balances become
The new product is
100.1 * 9,990 = 1,000,099.
The constant product formula keeps the product of token balances almost unchanged after trades occur. For example, if the original product is 1,000,000, it might become 1,000,099 after some trades. This near-constant product minimizes slippage, as exchange rates stay close to their original relative values. If the initial rate is 1 ETH = 10 USDC, many small trades will keep the rate near 1 ETH = 10 USDC. The constant product mechanism is key to maintaining stable exchange rates during trading activity in the pool.
Determining Exchange Rates
The exchange rate between the two assets in a pool is determined by taking the ratio between their quantities. When a trade occurs, the fee is added to the received quantity to account for the spread.
For example, based on the starting pool balances, the ETH/USDC exchange rate is
100 ETH / 10,000 USDC = 0.01 ETH per 1 USDC.
When a trade occurs against a liquidity pool, a small trading fee is charged to the trader. Typically, this fee is around 1% of the trade value. The fee gets added to the quantity of tokens the trader receives from the pool.
For example, if a trader swaps 10 USDC for ETH, the 1% fee would be 0.1 ETH. This 0.1 ETH gets added to the ETH side of the swap. So, the trader would receive something like 10.1 ETH in exchange for their 10 USDC. This means the effective ETH/USDC exchange rate is slightly higher for the trader than the actual pool rate.
This premium that traders pay on the exchange rate goes to liquidity providers as compensation. It incentivizes liquidity providers to deposit funds in the pool by giving them a cut of the trading fees. So, the slight increase in exchange rates traders pay provides instant liquidity and capability for asset traders.
Fluctuating Exchange Rates
A key feature of liquidity pools is that asset exchange rates fluctuate based on trading activity. When a trader swaps token Y for token X, the relative balance of tokens in the pool shifts; this automatically causes the exchange rate to update based on the new quantities per the formula.
For example, if 50 ETH is traded for USDC, the new pool balances would be
The new exchange rate is
50 ETH / 10,500 USDC = 0.0048 ETH per 1 USDC.
The ETH/USDC rate decreased due to the changing pool balances. This dynamic exchange rate allows liquidity pools to effectively facilitate trades by absorbing the volume.
While automated exchange rates are useful for traders, fluctuating rates introduce impermanent loss risk for liquidity providers. Impermanent loss occurs when the pool’s total value drops because the tokens’ prices diverge after depositing.
For example, ETH doubles in USD value while USDC stays static at $1. Now, the pool contains
50 ETH x $200 = $10,000
10,500 USDC x $1 = $10,500.
But if the provider had just held the initial assets separately, they would have:
100 ETH x $200 = $20,000
10,000 USDC x $1 = $10,000.
By providing liquidity, the total value is $10,000 + $10,500 = $20,500 instead of $30,000. This $9,500 difference is an impermanent loss. The provider can withdraw the assets at the new rates but realizes a loss versus just holding them.
The constant product formula allows liquidity pools to enable decentralized exchanges by algorithmically determining exchange rates based on asset balances. This automated market-making is a breakthrough innovation. However, the token balance fluctuations facilitating trading also create an impermanent loss risk when prices diverge.
While impermanent loss is challenging, the constant product model marks major progress for on-chain trading. Further mathematical enhancements to the model could optimize parameters like fees to reduce risks. Deep analysis provides critical insights for designing next-generation decentralized protocols.