CORE DEFI PRIMITIVES AND MECHANICS

Understanding AMMs and the Constant Product Formula

10 min read
#DeFi #Liquidity Pools #Yield Farming #AMM #Constant Product
Understanding AMMs and the Constant Product Formula

Introduction

In the world of decentralized finance, liquidity is a key ingredient that powers every transaction, swap, and yield opportunity. Traditional exchanges rely on order books, matching buyers and sellers at quoted prices. Automated Market Makers (AMMs) flip that paradigm on its head by using mathematical formulas to set prices and provide liquidity on a continuous, permissionless basis. The most common AMM design uses a simple but powerful rule: the product of the reserves of two tokens must remain constant. This rule, known as the constant product formula, underlies many popular protocols such as Uniswap, SushiSwap, and PancakeSwap.

This article dives deep into the mechanics of AMMs and the constant product formula. We will explore how liquidity pools work, how trades affect token balances, how pricing is determined, and why the formula is both elegant and resilient. By the end, you should have a solid grasp of the fundamental math, the practical implications for traders and liquidity providers, and the broader role of AMMs in the DeFi ecosystem.


What is an Automated Market Maker?

An Automated Market Maker is a smart contract that automatically determines the price of tokens in a liquidity pool. Instead of matching orders from buyers and sellers, an AMM uses a deterministic rule to calculate prices based on the quantities of each asset held in the pool. This approach eliminates the need for an order book and provides continuous liquidity for any pair of tokens that have a pool.

Core Components of an AMM

  1. Tokens – The two assets that form the trading pair (e.g., ETH and DAI).
  2. Reserves – The current balances of each token held in the pool.
  3. Formula – The mathematical rule that links reserves to price.
  4. Liquidity Providers (LPs) – Users who deposit equal value of both tokens to the pool in exchange for LP tokens.
  5. Trades – Deposits or withdrawals that change the reserves, triggering a new price according to the formula.

In every interaction with an AMM, the smart contract updates reserves and ensures the underlying formula remains satisfied. This guarantees that the pool can always fulfill trades, regardless of the trade size, while imposing a predictable price impact.


The Constant Product Formula Explained

The constant product formula states that the product of the reserves of the two tokens must stay the same after a trade. If the reserves of token A and token B are (x) and (y) respectively, the invariant is:

[ x \times y = k ]

where (k) is a constant that represents the pool’s total value before the trade.

Why a Product?

A product invariant is attractive because it is simple to compute, differentiable, and yields a smooth price curve that is always positive. The key insight is that the price of one token relative to the other can be derived by taking the derivative of the invariant. If the invariant holds, the ratio ( \frac{dx}{dy} ) (the marginal price) is simply the ratio of the reserves:

[ \frac{dy}{dx} = \frac{y}{x} ]

Thus, the price of token A in terms of token B is ( \frac{y}{x} ). When a trader swaps a small amount of A for B, the pool must adjust reserves so that the product remains (k). This adjustment automatically sets the new price.

A Simple Example

Suppose a pool contains 1000 ETH and 2,000,000 DAI, so (x = 1000) and (y = 2,000,000). The constant product is:

[ k = 1000 \times 2{,}000{,}000 = 2{,}000{,}000{,}000 ]

The current price of ETH in DAI is:

[ \frac{y}{x} = \frac{2{,}000{,}000}{1000} = 2000 ]

So 1 ETH equals 2000 DAI.

Now imagine a user wants to swap 10 ETH for DAI. The pool will:

  1. Receive 10 ETH, so new reserve (x' = 1010).
  2. Compute the new reserve (y') that satisfies (x' \times y' = k):

[ 1010 \times y' = 2{,}000{,}000{,}000 \quad\Rightarrow\quad y' = \frac{2{,}000{,}000{,}000}{1010} \approx 1{,}980{,}198 ]

  1. The difference (y - y' \approx 19{,}802) is the amount of DAI returned to the trader.

Notice that the trader receives less than a proportional share of the pool’s DAI because the invariant forces a larger reserve of ETH to balance the product. This price impact is a built‑in mechanism that protects the pool from large slippage and ensures the LPs receive value for providing liquidity.


The Price Curve: How Slippage Arises

The constant product formula produces a hyperbolic price curve. On a graph of (x) (reserve of A) versus (y) (reserve of B), the curve is a rectangular hyperbola (xy = k). The slope of the curve at any point equals the marginal price ( \frac{y}{x} ). As a trader moves along the curve by depositing or withdrawing one token, the slope changes, resulting in a moving price.

Understanding Slippage

Slippage refers to the difference between the expected price of a trade (based on the initial reserves) and the actual price paid (after the trade). In a constant product AMM, slippage grows as the trade size approaches the pool’s liquidity. For very small trades, slippage is negligible, but for large trades relative to the pool, the trader experiences significant price impact.

Mathematically, the slippage percentage can be approximated by:

[ \text{Slippage} \approx \frac{\Delta x}{x} \times 100% ]

where ( \Delta x ) is the amount of token A traded. Because the price increases proportionally to the change in reserves, the formula naturally limits how much a trader can influence the market without moving the price drastically.

Example of Slippage Calculation

Using the earlier pool with (x = 1000) ETH and (y = 2{,}000{,}000) DAI:

  • Small trade: 1 ETH swap.

    • New reserve (x' = 1001).
    • New DAI reserve (y' \approx 1{,}998{,}001).
    • DAI received ≈ 1999.
    • Slippage ≈ ( \frac{1}{1000} \times 100% = 0.1% ).
  • Large trade: 200 ETH swap.

    • New reserve (x' = 1200).
    • New DAI reserve (y' = \frac{2{,}000{,}000{,}000}{1200} \approx 1{,}666{,}667).
    • DAI received ≈ 333{,}333.
    • Slippage ≈ ( \frac{200}{1000} \times 100% = 20% ).

These calculations illustrate how the price curve ensures a predictable and self‑regulating price impact for every trade.


Liquidity Providers and Impermanent Loss

What Do LPs Do?

Liquidity providers supply equal‑value amounts of both tokens to the pool. In return, they receive LP tokens representing their share of the pool. These LP tokens can be redeemed later to withdraw the underlying assets, plus any trading fees earned.

How Do Fees Work?

Every trade incurs a small fee (often 0.30 %). This fee is added to the pool’s reserves, increasing the constant product (k). Over time, the accumulation of fees can offset the effect of impermanent loss for LPs, making liquidity provision potentially profitable.

Impermanent Loss Explained

Impermanent loss occurs when the relative price of the two tokens changes after an LP has added liquidity. Because the pool’s reserves adjust to maintain the product, an LP who withdraws when token prices have diverged will receive less value than simply holding the tokens outside the pool.

Calculating Impermanent Loss

Suppose an LP adds 10 ETH and 20,000 DAI (valued at 2000 DAI/ETH) to a pool. Later, ETH appreciates to 2500 DAI/ETH. If the LP withdraws, the pool will have redistributed the reserves to maintain the constant product. The LP ends up with fewer ETH and more DAI than initially deposited, resulting in a loss relative to holding 10 ETH and 20,000 DAI in a wallet.

The formula for impermanent loss (ignoring fees) is:

[ \text{Loss} = 1 - \frac{2\sqrt{p}}{1 + p} ]

where (p) is the price ratio after the price change (new price / old price). In this case, (p = \frac{2500}{2000} = 1.25), yielding an impermanent loss of approximately 5.6 %.

Mitigating Impermanent Loss

  • Long‑term Liquidity: Hold LP tokens for a duration that allows trading fees to cover or exceed impermanent loss.
  • Stablecoin Pools: Pairing stablecoins (e.g., USDC/USDT) minimizes price drift, virtually eliminating impermanent loss.
  • Dynamic Fees: Some protocols adjust fees based on volatility to reward LPs during turbulent periods.

Advanced Variants of the Constant Product Model

While the basic constant product AMM is powerful, developers have engineered variations to address specific use cases. Below are a few notable enhancements.

Concentrated Liquidity

Protocols like Uniswap V3 allow LPs to specify a price range in which they provide liquidity. This concentrates capital and reduces slippage for traders operating within that range, while increasing impermanent loss risk for LPs if prices move outside the chosen range.

Weighted AMMs

The constant product invariant can be extended to weighted AMMs where the formula becomes:

[ x^{w_x} \times y^{w_y} = k ]

Here, (w_x) and (w_y) are weights that alter the pool’s sensitivity to each asset. These are useful for token pairs with asymmetric value or when one token is more volatile.

Hybrid AMMs

Some protocols combine order book mechanisms with AMMs, allowing traders to benefit from deeper liquidity while still enjoying the continuous nature of AMMs. The invariant still holds for the AMM portion, but the overall pricing can be more efficient.


How to Interact with an AMM: A Step‑by‑Step Guide

Below is a practical walk‑through for a typical user interacting with an AMM like Uniswap.

1. Choosing a Pair

Select a trading pair that exists on the platform (e.g., ETH/DAI). Check the current reserves and the price curve to gauge slippage.

2. Estimating Slippage

Most interfaces provide a slippage tolerance slider (usually 0.5 % to 5 %). Adjust this to your risk tolerance; a lower value protects against price drift but may cause the trade to fail if liquidity is insufficient.

3. Approving Tokens

If the tokens are ERC‑20, you must approve the AMM contract to spend your tokens. This is a one‑time transaction per token per wallet.

4. Executing the Swap

Enter the amount of the token you wish to swap. The interface will display the estimated amount received, including the calculated fee. Confirm the transaction.

5. Confirming on the Network

Pay the gas fee and wait for the transaction to be mined. Once confirmed, you will see the new balances and the updated price reflected in the pool’s reserves.

6. Monitoring LP Tokens

If you have LP tokens, you can view your share of the pool, accumulated fees, and potential impermanent loss using analytics dashboards.


The Broader Impact of AMMs on DeFi

Liquidity Anywhere

AMMs eliminate the need for centralized exchanges or liquidity matching engines, democratizing access to markets. Anyone with a compatible wallet can trade any supported token pair on a platform that has a liquidity pool.

Permissionless Innovation

Developers can launch new token pairs with minimal overhead. Adding a liquidity pool is as simple as deploying a smart contract and depositing initial liquidity.

Market Making and Price Discovery

AMMs provide continuous pricing, which is essential for market making in illiquid or emerging token ecosystems. They also facilitate automated arbitrage opportunities, ensuring prices converge across different platforms.

Risks and Considerations

  • Smart Contract Risk – Bugs or exploits can lead to loss of funds.
  • Oracle Dependence – Some AMMs use external price feeds for initial liquidity provisioning.
  • Regulatory Uncertainty – As AMMs become mainstream, regulatory frameworks may evolve, affecting how they operate.

Future Directions and Open Questions

  1. Dynamic Invariants – Research is ongoing to design invariants that adapt to market conditions, reducing impermanent loss while maintaining price stability.
  2. Layer‑2 Scaling – Integrating AMMs with layer‑2 solutions (Optimistic Rollups, ZK‑Rollups) can drastically lower fees and increase throughput.
  3. Cross‑Chain Liquidity – Bridging AMMs across blockchains could unlock truly global liquidity, but requires robust interoperability solutions.
  4. Governance and Tokenomics – How governance tokens influence fee structures and protocol upgrades remains a rich area of exploration.

Summary

Automated Market Makers have reshaped the DeFi landscape by providing continuous, permissionless liquidity powered by a simple mathematical principle. The constant product formula (x \times y = k) ensures that every trade preserves the invariant, automatically adjusting prices and enforcing a smooth, predictable price curve. Traders benefit from instant execution and lower barriers to entry, while liquidity providers earn fees and expose themselves to impermanent loss.

Understanding the nuances of the invariant, slippage dynamics, LP incentives, and the evolving variants of AMMs equips participants to navigate this space more effectively. As the ecosystem matures, innovations such as concentrated liquidity, weighted pools, and cross‑chain integrations will refine how AMMs function and extend their reach even further.

By mastering these core concepts, users and developers can participate in, contribute to, and shape the future of decentralized finance with confidence.

Sofia Renz
Written by

Sofia Renz

Sofia is a blockchain strategist and educator passionate about Web3 transparency. She explores risk frameworks, incentive design, and sustainable yield systems within DeFi. Her writing simplifies deep crypto concepts for readers at every level.

Discussion (8)

MA
Marco 5 months ago
Nice explanation! The constant product rule is simple yet elegant. Good job.
JI
Jill 5 months ago
Agree, but I think the article missed the slippage part.
AN
Ana 5 months ago
The math is spot on, but the article didn't cover how fee tiers affect the reserve ratio over time. That's crucial for yield farming.
PE
Petr 5 months ago
The constant product formula is too simplistic. In real markets, you need dynamic pricing curves. Also, impermanent loss is a big risk.
ET
Ethan 5 months ago
Petr, you ignore that many protocols now use stablecoin pools with a 1:1 ratio, which mitigates slippage. Plus, AMMs are still viable for liquidity provision.
ET
Ethan 5 months ago
Liquidity isn't just about reserves; it's about the incentives. This article shows how the product rule ensures price discovery even without order books.
GI
Giovanni 5 months ago
True, but we also need to consider gas costs on Ethereum. The author didn't mention how layer 2 solutions affect the calculus.
JI
Jill 5 months ago
Yo, this article is cool, but it’s all math. Real traders also gotta deal with front‑running and sandwich attacks. Not covered.
MA
Marco 5 months ago
Jill, that’s why protocols add anti‑front‑running features. The constant product is just the baseline; add security layers on top.
GI
Giovanni 4 months ago
Uniswap v3’s concentrated liquidity changes the dynamics. The constant product formula still holds, but the shape of the curve changes. The article should mention that.
AN
Ana 4 months ago
Exactly. The article could have discussed how liquidity providers choose price ranges and how that affects impermanent loss.
DM
Dmitri 4 months ago
Can anyone explain how the constant product formula impacts impermanent loss in a simple example?
LU
Luca 4 months ago
Sure, if you add 1 ETH and 100 USDT, the product is 100. If ETH price doubles to 2000, the reserves adjust to maintain the product: X*Y=100. So X=0.05 ETH, Y=2000 USDT. You lose value relative to holding. That’s the math.
LU
Luca 4 months ago
Also note that the slippage for large trades can be dramatic because the price moves along the curve. That’s why traders use order books for small trades.

Join the Discussion

Contents

Luca Also note that the slippage for large trades can be dramatic because the price moves along the curve. That’s why traders... on Understanding AMMs and the Constant Prod... Jun 05, 2025 |
Dmitri Can anyone explain how the constant product formula impacts impermanent loss in a simple example? on Understanding AMMs and the Constant Prod... Jun 02, 2025 |
Giovanni Uniswap v3’s concentrated liquidity changes the dynamics. The constant product formula still holds, but the shape of the... on Understanding AMMs and the Constant Prod... May 28, 2025 |
Jill Yo, this article is cool, but it’s all math. Real traders also gotta deal with front‑running and sandwich attacks. Not c... on Understanding AMMs and the Constant Prod... May 26, 2025 |
Ethan Liquidity isn't just about reserves; it's about the incentives. This article shows how the product rule ensures price di... on Understanding AMMs and the Constant Prod... May 24, 2025 |
Petr The constant product formula is too simplistic. In real markets, you need dynamic pricing curves. Also, impermanent loss... on Understanding AMMs and the Constant Prod... May 23, 2025 |
Ana The math is spot on, but the article didn't cover how fee tiers affect the reserve ratio over time. That's crucial for y... on Understanding AMMs and the Constant Prod... May 21, 2025 |
Marco Nice explanation! The constant product rule is simple yet elegant. Good job. on Understanding AMMs and the Constant Prod... May 20, 2025 |
Luca Also note that the slippage for large trades can be dramatic because the price moves along the curve. That’s why traders... on Understanding AMMs and the Constant Prod... Jun 05, 2025 |
Dmitri Can anyone explain how the constant product formula impacts impermanent loss in a simple example? on Understanding AMMs and the Constant Prod... Jun 02, 2025 |
Giovanni Uniswap v3’s concentrated liquidity changes the dynamics. The constant product formula still holds, but the shape of the... on Understanding AMMs and the Constant Prod... May 28, 2025 |
Jill Yo, this article is cool, but it’s all math. Real traders also gotta deal with front‑running and sandwich attacks. Not c... on Understanding AMMs and the Constant Prod... May 26, 2025 |
Ethan Liquidity isn't just about reserves; it's about the incentives. This article shows how the product rule ensures price di... on Understanding AMMs and the Constant Prod... May 24, 2025 |
Petr The constant product formula is too simplistic. In real markets, you need dynamic pricing curves. Also, impermanent loss... on Understanding AMMs and the Constant Prod... May 23, 2025 |
Ana The math is spot on, but the article didn't cover how fee tiers affect the reserve ratio over time. That's crucial for y... on Understanding AMMs and the Constant Prod... May 21, 2025 |
Marco Nice explanation! The constant product rule is simple yet elegant. Good job. on Understanding AMMs and the Constant Prod... May 20, 2025 |