Welcome to the decentralized-exchange module. In the previous module, you learned to map a DeFi protocol’s actors, assets, controls, dependencies, and risk bearers. We now apply that discipline to an automated market maker, or AMM: a smart contract that lets traders swap against pooled assets rather than matched orders.
This lesson focuses on the simplest and most influential AMM design, the constant-product market maker. You will calculate:
- the pool’s spot price;
- a swap’s execution price and price impact;
- slippage between the quote you expected and the amount that actually executes.
The calculations are based on a two-token, full-range constant-product pool, the model associated with Uniswap V2. Later lessons will examine liquidity-provider exposure and concentrated liquidity; for now, stay focused on the trader’s quote and execution.
A pool is the counterparty to the trade
In an order-book exchange, a trader usually trades against other traders’ limit orders. In a constant-product AMM, a trader trades against a pool holding two assets.
Let:
- be the reserve of token ;
- be the reserve of token ;
- be their product.
The pool follows the invariant:
A trader who sells to receive increases the pool’s reserve and removes some reserve. The amount of paid out is constrained by the requirement that the pool remain on its pricing curve.

The essential economic feature is straightforward: the pool always offers a price, but that price worsens as a trade becomes large relative to its reserves. The pool cannot be drained at a finite price because acquiring the final units of an asset would require an increasingly extreme amount of the other asset.
Watch “Uniswap V2 - Pricing” by Smart Contract Programmer for a compact visual explanation of how reserve changes, the constant-product curve, and the swap-output formula fit together.
First watch the pool model. Focus on the distinction between depositing one asset into the pool and receiving the other asset from it. Then watch curve pricing. Pay particular attention to why equal additional amounts of input produce progressively less output, and to the fee-adjusted output formula introduced near the end.
Define the price before calculating it
Suppose is ETH and is USDC. If a pool contains ETH and USDC, then the pool’s initial state is:
The spot price of in units of is the reserve ratio:
For the ETH/USDC example:
So the initial spot price is USDC per ETH.
This is a marginal price: it describes the rate for an infinitesimally small trade. It is not generally the rate a trader receives for a finite trade.
The reciprocal is the spot price of in :
In the example:
So USDC is worth ETH at the pool’s initial marginal price.
The geometric interpretation
The constant-product curve can be written as:
Its slope is negative because adding to the pool requires to leave it. The magnitude of the slope at a point is:
That is why the reserve ratio gives the spot price. The price is the tangent to the curve at the current pool state. A real swap, by contrast, travels between two points on the curve, so it has an average execution price rather than a single marginal price.
Calculate swap output and execution price
Assume first that the pool charges no trading fee. A trader sends units of into the pool and receives units of .
The pool begins with:
After the swap, its reserves are:
The invariant requires:
Solving for output gives the central constant-product swap formula:
The trader’s average execution price, expressed as units of received for each unit of sold, is:
Substituting the output formula gives:
Compare this with the initial spot price:
Because is greater than , the execution price is lower than the initial spot price when selling . You receive fewer units of per unit of than a simple spot-price multiplication would suggest.
Constant Function Market Maker - Uniswap V3 Development Book
Read the relevant opening sections of the Uniswap V3 Development Book. They provide a useful visual and algebraic account of the constant-product constraint, reserve-ratio pricing, and the distinction between spot and execution price.
Read the opening “Constant Function Market Makers” section for the meaning of pool reserves and k. Then read the full “The Trade Function” subsection. Start at the trade constraint, and follow how a deposit of one token requires an offsetting withdrawal of the other. Next, read the full “Pricing” subsection, then the “The Curve” subsection. In the numbered concrete example, read the execution-price comparison. Focus on why the reserve ratio applies only at the starting point, while a finite trade has an average price along the curve.
Worked example: selling ETH for USDC
Return to the pool with ETH and USDC. A trader sells ETH to the pool.
The trader might naively expect:
But USDC is only the initial spot-price estimate. The actual fee-free output is:
The pool’s final reserves are:
Check the invariant:
The average execution price is:
The trader sold ETH into the pool, so ETH became more abundant in that pool and USDC became scarcer. Accordingly, ETH’s post-swap spot price in USDC falls:
There are therefore three distinct prices worth keeping separate:
| Price | Meaning | ETH/USDC example |
|---|---|---|
| Initial spot price | Marginal rate before the trade | USDC per ETH |
| Execution price | Average rate across the trader’s finite swap | USDC per ETH |
| Final spot price | Marginal rate after the trade | USDC per ETH |
The execution price lies between the initial and final spot prices because it is an average across the movement along the curve.
Price impact: the cost caused by your own trade
Price impact is the deterioration in price caused by the trade itself moving the pool along its curve. It is not a network fee, and it is not necessarily evidence that anything unexpected happened.
For a trade that sells for , define curve price impact as the shortfall of the execution price relative to the pre-trade spot price:
Using the constant-product formulas:
In the example:
The trader receives less USDC than the initial spot-price estimate. Equivalently:
This is the curve-induced shortfall for the trader’s ETH trade.
Trade size relative to liquidity matters
A useful result is that the absolute dollar size of a trade is not enough to judge price impact. What matters is its size relative to the input reserve.
In the same pool:
| ETH input | Fraction of ETH reserve | Fee-free price impact |
|---|---|---|
| ETH | about | |
| ETH | about | |
| ETH | ||
| ETH |
A ETH swap is very large for a pool with only ETH, but potentially negligible for a pool holding tens of thousands of ETH. This is why deeper liquidity improves execution quality.
Do not confuse execution-price impact with post-trade spot movement
Another valid but different calculation compares the post-trade marginal price with the initial marginal price:
For the ETH trade:
This figure is the movement in the pool’s marginal ETH price from before to after the trade. It is not the trader’s average price impact, which was .
When reviewing a DEX interface, dashboard, or research note, always ask which definition it uses:
- execution price versus initial spot price; or
- final spot price versus initial spot price.
Both may be described informally as “price impact,” but they measure different things.
Incorporate a trading fee
Most AMMs charge a fee. Let:
For a fee:
If the trader sends into the pool, the amount used in the pricing calculation is:
The fee-adjusted output formula is:
For the same ETH and USDC pool, selling ETH produces:
The fee-inclusive execution price is:
Relative to the initial spot price, the total quoted shortfall is:
That combines two distinct effects:
- the AMM curve worsens the price for a large trade;
- the protocol deducts a trading fee.
Do not simply add and . The fee changes the effective amount moving along the curve, so the exact fee-inclusive calculation should use the swap formula.
A practical detail about and fees
Without fees, the post-swap reserves satisfy:
In many fee-charging AMMs, the fee remains in the pool. The output is calculated using , but the full input amount is retained in the actual reserve. Therefore, the observed reserve product after the trade can increase slightly:
That increase represents fee value retained for liquidity providers, subject to any protocol-level fee arrangement. For trade-quote calculations, use the effective-input formula; do not assume that the raw post-swap reserve product will be exactly unchanged in a fee-charging implementation.
Slippage: the difference between quote and execution
In common trading usage, slippage is the difference between the amount you expected when submitting a transaction and the amount actually received when it executes.
Suppose the AMM quote when you sign the transaction is:
If the transaction later executes and you receive USDC, realized adverse slippage is:
This definition distinguishes slippage from the known, deterministic price impact already included in the quote.
Why actual output can differ from the quote
Between transaction submission and inclusion on-chain, the pool state can change. Common causes include:
- other traders swapping in the same pool;
- arbitrage transactions updating the pool toward an external market price;
- delayed inclusion during congestion;
- transaction ordering or maximal extractable value.
Consider the earlier example without fees so the arithmetic remains transparent. Your quote for selling ETH is:
Before your transaction executes, another trader sells ETH into the same pool. That first trade changes the reserves to approximately:
Your ETH transaction now executes against this less favorable state:
Your realized slippage relative to the original quote is:
Your original quote already reflected your own price impact. The additional is caused by the intervening trade changing the pool state.
Slippage tolerance is a minimum-output condition
A DEX usually lets a trader specify a slippage tolerance. For an exact-input swap, it translates to a minimum acceptable output:
where is the tolerance.
If the fee-inclusive quote is USDC and you set , then:
If the contract cannot provide at least USDC, the transaction reverts rather than executing at a worse rate. The user will generally still pay for the gas consumed by the failed transaction.
A loose tolerance reduces the chance of a failed trade but accepts a wider range of outcomes. A very tight tolerance limits adverse execution but may cause ordinary trades to fail in volatile or congested conditions. It is an execution constraint, not a guarantee of fair market price.
A repeatable calculation workflow
For a constant-product pool, use this process before trusting a displayed quote.
-
Set the direction and units.
Define input token , output token , and ensure that , , and all use consistent units. -
Calculate the initial spot price.
This is the marginal price of in units of .
-
Calculate effective input if there is a fee.
-
Calculate expected output.
-
Calculate the execution price.
-
Report price impact with a stated convention.
For the fee-free curve-only convention:For a fee-charging pool, distinguish curve-only impact from the total quote shortfall.
-
Set or evaluate minimum output.
-
After execution, compare actual output with the quote.
Decimal-normalization warning
On-chain reserve values are usually stored in smallest units, not in human-readable token amounts. ETH has decimals and USDC commonly has . If using raw contract balances, normalize them first:
Then calculate:
Failing to account for decimal differences can produce a price that is wrong by factors of millions or trillions even when the AMM formula itself is correct.
Key takeaways
A constant-product AMM prices trades from pool reserves rather than an order book:
- The initial spot price of in is .
- A finite swap has an average execution price, not the initial spot price.
- For a fee-free exact-input swap, output is:
- Price impact increases with trade size relative to the pool’s input reserve.
- Trading fees reduce output further and must be separated conceptually from curve-induced price impact.
- Slippage is the gap between your quoted output and actual output after the pool state changes before execution.
- A slippage tolerance sets a minimum acceptable output; it does not remove market, timing, or transaction-ordering risk.
Next, you will shift from the trader’s perspective to the liquidity provider’s: how the pool’s asset composition changes as relative prices move, and why fee income does not by itself determine an LP’s return.
Can't find a good explanation? Sign up and we'll make it for you
Sign up