Understanding DEX smart contract architecture helps you evaluate security, gas costs, and capabilities. This guide covers the Factory-Router pattern used by Uniswap and most AMMs.
Core Components
Factory Contract
- Creates new pool contracts
- Maintains registry of all pools
- Ensures unique token pairs
- Immutable deployment parameters
Pool Contracts
- Hold token reserves
- Execute swaps and LP operations
- Track LP share accounting
- Emit events for indexing
Router Contract
- User-facing interface
- Multi-hop routing
- Slippage protection
- Deadline enforcement
Key Takeaways
- Factory-Router-Pool is the standard pattern
- Separation of concerns improves security
- Understanding architecture helps evaluate protocols
- V4’s singleton changes this paradigm significantly