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
amountIntoreserveAincreases the pool’s A supply. - The output is calculated so the product
A * Bstays constant. - Larger trades move the price more (slippage).
Code
Canonical knowledge ID:
code-example:simple-amm-swap-solidity