Sub topic 10
Liquidity pools and price impact
This chapter explains how a liquidity pool sets a token's price and why a large trade moves that price. It assumes you know what a wallet and a smart contract are, and that you can read short Python or Solidity snippets. You don't need to know any DeFi (decentralized finance) protocols. The math is multiplication and division only.
Why tokens need pools
An ERC-20 contract only tracks balances. It has no market and no price. For someone to buy your token, a counterparty has to be willing to sell at some price. Traditional exchanges solve this with an order book, where buyers and sellers post offers. On-chain order books are expensive because every posted, edited, or canceled order costs gas, the fee paid for each transaction.
An automated market maker (AMM) replaces the order book with a smart contract holding two token balances. Anyone can trade against those balances, and a formula sets the price. A liquidity pool is that pair of balances. A liquidity provider (LP) is someone who deposits both tokens into the pool and earns a share of trading fees.
Most memecoins and many small utility or governance tokens trade only through pools like this. The pool's size therefore decides how stable the price is.
The constant-product rule
The most common AMM design, used by Uniswap v2 and many forks, keeps the product of the two reserves constant:
x * y = kHere x is the reserve of token A, y is the reserve of token B, and k is a constant between trades. When a trader adds some of token A, the pool must give back enough of token B that the product stays the same. Because the product is fixed, the more of B a trader takes, the more expensive the next unit becomes.
Three terms matter for the rest of the chapter:
- Reserve: the amount of a token currently held by the pool.
- Spot price: the ratio of the reserves, such as
reserve_MEME / reserve_ETH. It is the price for an infinitesimally small trade. - Price impact: how much worse your average execution price is than the spot price, because your own trade shifts the reserves.
Computing a swap
With a fee, the formula for the output amount is:
amount_out = reserve_out * amount_in * (1 - fee) / (reserve_in + amount_in * (1 - fee))The fee is taken from the input before the formula runs. In Uniswap v2 the fee is 0.3 percent. The collected fee stays in the pool, so k grows slightly with each trade. That growth is the LPs' income.
The code below implements the formula and measures price impact for one trade.
def swap_out(reserve_in, reserve_out, amount_in, fee=0.003):
"""Output amount for a constant-product (x * y = k) pool."""
amount_in_after_fee = amount_in * (1 - fee)
return reserve_out * amount_in_after_fee / (reserve_in + amount_in_after_fee)
# A pool holding 100 ETH and 1,000,000 MEME
spot_price = 1_000_000 / 100 # 10,000 MEME per ETH
out = swap_out(100, 1_000_000, 10) # buy with 10 ETH
effective_price = out / 10
price_impact = 1 - effective_price / spot_price
print(round(out), round(price_impact, 4))This prints 90661 0.0934. The trader paid 10 ETH and received about 90,661 MEME, an average of about 9,066 MEME per ETH. The spot price before the trade was 10,000. The trader got roughly 9.3 percent fewer tokens than the quoted price implied.
What the trade did to the pool
After the swap, the pool holds 110 ETH and about 909,339 MEME. The new spot price is 909,339 / 110, or about 8,267 MEME per ETH. One 10 ETH purchase moved the price of MEME up by about 21 percent, measured in ETH (from 0.0001 to about 0.000121 ETH per MEME). In MEME-per-ETH terms the price dropped by about 17 percent.
Note the difference between the two figures. Price impact (9.3 percent) is what the trader lost on this trade. The change in spot price (about 17 percent) is where the market stands afterward, and the next trader pays that new price.
How impact grows with trade size
Price impact depends on the trade size relative to the reserve, not on absolute size. For a pool with fee f, the ratio of effective price to spot price is:
effective / spot = (1 - f) * reserve_in / (reserve_in + amount_in * (1 - f))The next block applies this to the 100 ETH pool above.
def swap_out(reserve_in, reserve_out, amount_in, fee=0.003):
amount_in_after_fee = amount_in * (1 - fee)
return reserve_out * amount_in_after_fee / (reserve_in + amount_in_after_fee)
reserve_eth, reserve_meme = 100, 1_000_000
spot = reserve_meme / reserve_eth
for amount in (0.1, 1, 10, 50, 100):
out = swap_out(reserve_eth, reserve_meme, amount)
impact = 1 - (out / amount) / spot
print(f"{amount:>6} ETH -> {out:>10,.0f} MEME, impact {impact:.2%}")The results, rounded, look like this:
| Trade size (ETH) | Share of ETH reserve | Approximate price impact |
|---|---|---|
| 0.1 | 0.1% | 0.40% |
| 1 | 1% | 1.28% |
| 10 | 10% | 9.34% |
| 50 | 50% | 33.47% |
| 100 | 100% | 50.08% |
For small trades, impact is roughly the fee plus the trade's share of the reserve. For large trades the curve bends: a trade equal to the whole reserve gets about half the quoted price. No trade can drain the pool, because the output approaches the reserve but never reaches it.
Why thin liquidity matters for memecoins
A memecoin often launches with a small pool, for example a few ETH paired with most of the supply. Three consequences follow from the formula.
- Prices swing sharply. A purchase of a few percent of the quote-side reserve moves the price by a similar percentage or more. Attention-driven buying in a shallow pool pushes the price up fast, and selling pushes it down just as fast.
- Quoted market capitalization overstates what holders can sell. Market cap is price times supply, but you can't sell the supply at that price. If a holder owning 20 percent of supply sold into a pool holding a few ETH, the pool would pay out only a fraction of the "value" on paper.
- Whoever controls the LP position controls exit liquidity. The next chapters cover rug pulls, where the deployer withdraws the pool's ETH. Because the pool is the only market, a withdrawal leaves holders with tokens that nobody will buy.
Depth, not price, is the number to check first. Compare the quote-side reserve with the size of trade you would want to make, then compute impact for that trade.
Impermanent loss, briefly
LPs deposit both tokens and take the risk that the price ratio changes. If MEME rises sharply against ETH, arbitrageurs buy MEME from the pool until the pool price matches the outside market. The LP then holds fewer MEME and more ETH than they deposited. Compared with simply holding the two tokens, the LP is worse off. This difference is called impermanent loss. It is "impermanent" only if the price ratio returns to its starting point. Trading fees offset it, and in a thin, volatile memecoin pool fees often don't.
Edge cases to know
- Fee-on-transfer tokens. Some tokens take a tax inside
transfer. The pool receives less than the trader sent, so the standard formula overstates the output and the swap may revert. Check whether a token charges a transfer tax before you estimate trades. - Rounding. Solidity uses integer math. Real pool contracts round in the pool's favor, so actual outputs can differ from floating-point Python by a few units of the smallest denomination.
- Different curves. Stablecoin-focused pools use curves that stay flat near a 1:1 ratio, so impact is much lower for the same trade size. The constant-product table above does not apply to them.
- Concentrated liquidity. Uniswap v3 lets LPs place liquidity in a price range. Depth near the current price can be far higher than a v2 pool with the same capital, but it falls to zero outside the ranges that LPs chose.
Exercise
A pool holds 50 ETH and 500,000 TOKEN, with a 0.3 percent fee. A trader buys with 5 ETH. Using swap_out, roughly how many TOKEN does the trader receive, and is the price impact closer to 1 percent, 10 percent, or 30 percent?
Show answer
The spot price is 10,000 TOKEN per ETH. After the fee, 4.985 ETH enters the formula, so output = 500,000 * 4.985 / (50 + 4.985) = about 45,332 TOKEN. The effective price is about 9,066 TOKEN per ETH, so impact is about 9.3 percent, closest to 10 percent. The trade is 10 percent of the ETH reserve, the same ratio as the 10 ETH trade into the 100 ETH pool, so the result is the same percentage. Impact depends on trade size relative to the reserve, not on the absolute amount.
Exercise
Explain why a memecoin with a quoted market capitalization of 50 million dollars may still be impossible to sell in size, and name the single pool figure you would check first.
Show answer
Market cap is the last trade price multiplied by total supply, but selling moves the price along the constant-product curve. If the pool's quote-side reserve (for example, ETH) is small, even a modest sale pushes the price down sharply, so holders cannot realize anything close to the quoted value. Check the quote-side reserve, then compute price impact for the sale size you have in mind.