UniswapV2
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.
Install / Use
/learn @tomwangcn/UniswapV2README
🦄 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
👉 These reserves are always synced with the actual contract balances.
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()
📌 Check: Zero liquidity is rejected.
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
Run tests:
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
Related Skills
YC-Killer
2.7kA library of enterprise-grade AI agents designed to democratize artificial intelligence and provide free, open-source alternatives to overvalued Y Combinator startups. If you are excited about democratizing AI access & AI agents, please star ⭐️ this repository and use the link in the readme to join our open source AI research team.
groundhog
398Groundhog's primary purpose is to teach people how Cursor and all these other coding agents work under the hood. If you understand how these coding assistants work from first principles, then you can drive these tools harder (or perhaps make your own!).
sec-edgar-agentkit
10AI agent toolkit for accessing and analyzing SEC EDGAR filing data. Build intelligent agents with LangChain, MCP-use, Gradio, Dify, and smolagents to analyze financial statements, insider trading, and company filings.
Kiln
4.7kBuild, Evaluate, and Optimize AI Systems. Includes evals, RAG, agents, fine-tuning, synthetic data generation, dataset management, MCP, and more.
