This project aims to break down and reimplement the core logic of Uniswap V2, providing a simplified version of an Automated Market Maker (AMM) for educational and experimental purposes. The main contract demonstrates how liquidity pools, pricing formulas, and token swaps work in a decentralized exchange.
Last updated Jul 3, 2026
84
Stars
0
Forks
0
Issues
+2
Stars/day
Attention Score
38
Language breakdown
Solidity 100.0%
▸ Files
click to expand
README
🦄 Uniswap V2 Mini Exchange (Simplified)
This project is a minimal AMM (Automated Market Maker), inspired by Uniswap V2. It supports adding liquidity, quoting prices, and swapping ETH ↔ Token.
Deploy address https://sepolia.etherscan.io/address/0x4e6037b6613dba5aba761e3b14c7954f114947b7
Structure
src/UniswapV2.sol: Main contract implementing the AMM logiclib/openzeppelin-contracts/: ERC20 standard contract dependencytest/: Test scripts and casesscript/: Deployment scripts
Main Contract Flow
Core invariant:
x * y = k
x= ETH reservey= Token reservek= Constant (liquidity invariant)
🔑 Core Components
1. Storage
IERC20 public immutable token;
uint256 public reserveEth;
uint256 public reserveToken;
token: ERC20 token contractreserveEth: ETH liquidity storedreserveToken: Token liquidity stored
2. Add Liquidity
function addLiquidity(uint256 tokenAmount) external payable
- User sends ETH + Tokens
- Both are deposited into the pool
- Internal reserves are updated via
_sync()
3. Quote Function (Pricing Formula)
function quote(uint256 amountIn, bool ethToToken)
Formula (with 0.3% fee):
amountOut = (amountIn 997 reserveOut)
/ (reserveIn 1000 + amountIn 997)
- Preserves
x * y = k - Larger trades = higher slippage
4. Swap ETH → Token
function swapEthForToken() external payable
Steps:
- User sends ETH (
msg.value) - Contract calculates
tokenOut = quote(ethIn, true) - Transfer Tokens to user
- Sync reserves
5. Swap Token → ETH
function swapTokenForEth(uint256 tokenIn) external
Steps:
- User sends Tokens via
transferFrom - Contract calculates ETH out using
quote - Contract sends ETH (
.call{value: ethOut}) - Sync reserves
📊 AMM Curve
The constant product formula produces a hyperbolic curve:

- Adding ETH decreases available Token (and vice versa)
- Prevents draining the pool completely
- Defines price slippage
🧪 Unit Tests
Covered cases:
- ✅ Add liquidity success & revert on zero input
- ✅ Quote matches Uniswap V2 formula
- ✅ Swap ETH→Token & Token→ETH
- ✅ Reverts on insufficient liquidity
forge test -vv
🚀 Deployment
Deploy on Sepolia testnet:
forge script script/Deploy.s.sol:DeployExchange \
--rpc-url $SEPOLIA_RPC \
--broadcast
📌 Takeaways
- Simple AMM = no order book, only liquidity pool
- Core invariant = x * y = k
- Prices adjust automatically → slippage is inevitable
- Liquidity providers earn fees from swaps
References
🔗 More in this category