Skip to main content

Simple AMM Swap (Solidity)

A minimal constant-product AMM (x*y=k) swap function — the math behind Uniswap. A minimal constant-product AMM (x*y=k) swap function — the math behind Uniswap.

Explanation

This implements the core constant product formula (x * y = k) that powers Uniswap V2. The math:
  • Adding amountIn to reserveA increases the pool’s A supply.
  • The output is calculated so the product A * B stays constant.
  • Larger trades move the price more (slippage).
What’s missing for production: fees (0.3%), price oracles, flash loan protection, multi-hop routing. This is for understanding the mechanism — use Uniswap’s audited contracts in production.

Code


Canonical knowledge ID: code-example:simple-amm-swap-solidity