Price Impact in a Constant-Product AMM (Sample)
Starting from x·y = k, derive the output, execution price and price impact of a swap. Also a layout check for formulas, code and tables.

This is a sample post for checking the layout: formulas, code, tables and quotes are all in here. The content is the basic derivation for constant-product market makers like Uniswap v2. Use it as a starting point for your first real long-form post, or delete it.
The constant-product model
A pool holds two tokens, with reserves and . After every trade the product stays constant:
A user deposits of token X and the pool pays out of token Y, such that the product is still after the trade:
Solving for :
Adding a fee
Let the fee rate be ( in Uniswap v2). Only the post-fee amount takes part in pricing:
The fee stays in the pool, so grows slightly after each trade. That growth is where liquidity providers earn their return.
Price impact
The marginal price before the trade is . The average execution price of the trade is . The gap between the two is the price impact:
Impact depends only on the size of the trade relative to the pool’s reserves. The bigger the trade compared with the pool, the larger the slippage.
A numerical example
Take and :
| Input | Output | Price impact |
|---|---|---|
| 1 | 0.9960 | 0.100% |
| 10 | 9.8716 | 0.987% |
| 100 | 90.6611 | 9.066% |
| 500 | 332.6660 | 33.267% |
Figure: price impact for inputs from 0 to 500. The marked points are the four rows in the table above.
Code
The same calculation, ready to run:
def swap(x: float, y: float, dx: float, fee: float = 0.003):
"""Return (output amount, price impact)."""
dx_eff = dx * (1 - fee)
dy = y * dx_eff / (x + dx_eff)
impact = dx_eff / (x + dx_eff)
return dy, impact
dy, impact = swap(1000, 1000, 100)
print(f"out={dy:.4f}, impact={impact:.3%}")
# out=90.6611, impact=9.066%
What’s next
In this model the price is only moved by trades inside the pool, so when it drifts away from the external market, arbitrageurs pull it back. That process is tied directly to MEV and transaction ordering, which makes it a natural topic for the next post.