A from-scratch implementation of Uniswap V2 core mechanics, built for learning and demonstration purposes.
This project re-implements the core Uniswap V2 contracts without relying on the original codebase, demonstrating deep understanding of AMM design, EVM storage optimization, and DeFi security patterns.
| Contract | Description |
|---|---|
MiniswapERC20 |
LP token base contract (ERC20 from scratch) |
MiniswapFactory |
Pair registry and creation |
MiniswapPair |
Core AMM — mint / burn / swap / TWAP oracle |
MiniswapRouter |
User-facing router with multi-hop routing |
- Constant product formula
x * y = k - Storage slot packing —
reserve0 / reserve1 / blockTimestampLastpacked into a single 32-byte slot (saves 2/3 SLOAD cost) - TWAP price oracle — UQ112x112 fixed-point cumulative price
- Reentrancy guard — uint256 lock pattern
- Flash loans — optimistic transfer pattern in
swap() - 0.3% swap fee — verified via adjusted k check
- Safe transfer — low-level call to handle non-standard ERC20s
forge test -vvRan 6 tests
[PASS] test_CreatePair
[PASS] test_AddLiquidity
[PASS] test_Swap (amountIn: 1000 TKA → amountOut: 987 TKB)
[PASS] test_RemoveLiquidity
[PASS] test_ReentrancyProtection
[PASS] testFuzz_GetAmountOut (256 runs)
# Install Foundry
curl -L https://foundry.paradigm.xyz | bash && foundryup
# Clone and test
git clone https://github.com/tusugit/miniswap.git
cd miniswap
forge test -vvUser
│
▼
MiniswapRouter ← single entry point, calculates amounts, routes swaps
│
├── MiniswapFactory ← deploys and tracks Pair contracts
│
└── MiniswapPair ← holds liquidity, executes swaps, mints LP tokens
│
└── MiniswapERC20 ← LP token (inherited)