Sub topic 10
Liquidity Pools and Price Impact
Prerequisites: You should understand wallets, smart contracts, and ERC-20 tokens. You should also know that a decentralized exchange can trade tokens without a central order book.
How a liquidity pool works
A liquidity pool is a smart contract holding reserves of two tokens. Traders swap one token for the other directly against those reserves. People called liquidity providers deposit both tokens and receive a share of trading fees.
An automated market maker (AMM) is the pricing system used by the pool. A common AMM follows the constant-product rule:
\[ x \times y = k \]
Here, x and y are the two token reserves, and k is a value the pool tries to preserve after each swap, except for fees.
Suppose a pool contains 100 ETH and 1,000,000 MEME. Its initial spot price is:
\[ 1{,}000{,}000 \div 100 = 10{,}000\text{ MEME per ETH} \]
A buyer who adds ETH removes MEME from the pool. The reserves change, so the price changes during the trade.
Calculating output and price impact
For a fee of f, the amount that affects the invariant is:
\[ a_{\text{effective}} = a(1-f) \]
The output amount is:
\[ \text{output} = \frac{y \times a{\text{effective}}} {x + a{\text{effective}}} \]
The following program models a trade in the ETH-MEME pool:
def swap_out(reserve_in, reserve_out, amount_in, fee=0.003):
"""Return output from a constant-product AMM swap."""
amount_after_fee = amount_in * (1 - fee)
return reserve_out * amount_after_fee / (reserve_in + amount_after_fee)
reserve_eth = 100
reserve_meme = 1_000_000
trade_eth = 10
spot_price = reserve_meme / reserve_eth
meme_out = swap_out(reserve_eth, reserve_meme, trade_eth)
average_price = meme_out / trade_eth
price_impact = 1 - average_price / spot_price
print(f"MEME received: {meme_out:,.0f}")
print(f"Price impact: {price_impact:.2%}")The quoted spot price is the reserve ratio before the trade. The average execution price is the tokens received divided by the input amount. Price impact compares these two prices. A large trade relative to the reserves produces greater impact.
| Condition | Likely effect |
|---|---|
| Deep reserves and small trade | Low price impact |
| Thin reserves and large trade | High price impact |
| Higher pool fee | Fewer tokens received |
| Many competing trades | The quoted output may change before execution |
Price impact is caused by the pool's curve. Slippage is the difference between the expected output and the actual output, which can also include price changes from other transactions before yours executes. Traders protect themselves by setting a minimum acceptable output.
Practical analysis
When evaluating a token pool, check the reserve values, trading volume, fee, and whether liquidity can be withdrawn by one wallet. A pool with a high token balance but very little paired asset may still provide poor exit liquidity.
Exercise
A pool has 50 ETH and 500,000 tokens. Is a 20 ETH purchase likely to have more or less price impact than a 1 ETH purchase?
Show answer
More. The 20 ETH trade is a larger fraction of the ETH reserve, so the constant-product curve moves the price further during the swap.