Project · Intermediate · about 22 hours
A constant-product AMM in Solidity
Implement a minimal constant-product pool with liquidity shares, swaps with fees and slippage limits, and test it against a Python reference model.
Skills:DeFi, Smart contracts and Solidity, Automated testing
Part of: Blockchain Developer
Data and licences
- Uniswap v2 core contracts ↗GPL-3.0
Brief
Automated market makers are the core of DeFi. Implementing one and checking it against an independent model teaches the maths and the rounding far better than reading about them. Study the Uniswap v2 design, then write your own simplified version. Use a local Anvil chain and, when you deploy publicly, a testnet such as Sepolia with free test ether from a faucet. Never use real funds or a wallet that holds any, and never paste a private key into code or a website.
What to build
Implement a pool for two ERC-20 tokens that mints liquidity shares on deposit and burns them on withdrawal
Implement swaps with the constant-product formula and a 0.30% fee, with amountOutMin and deadline parameters
Write a Python model of the same maths, starting from the DeFi shelf example
Write differential fuzz tests: random sequences of deposits, swaps and withdrawals must match the model within one unit of rounding
Document rounding decisions, why spot price must not be used as an oracle, and other design risks
Deliverables
Repository with
src/,test/,model/andDESIGN.md
How it is graded
Reviewers run the differential tests with more fuzz runs and check that k never decreases across swaps.
Stretch
Add a time-weighted average price accumulator and explain why it is harder to move than the spot price.
Deliverables
- A pool contract with add and remove liquidity, swaps in both directions and a 0.30% fee
- Slippage and deadline parameters on swaps
- A Python reference model and differential fuzz tests comparing contract and model
- A write-up of rounding choices and design risks such as using spot price as an oracle
How a reviewer grades it
| Criterion | Weight | What good looks like |
|---|---|---|
| Maths is correct and rounding favours the pool | 30% | |
| Slippage limits, deadlines and safe external calls are in place | 20% | |
| Differential fuzz tests against the reference model pass | 30% | |
| Design risks are documented honestly | 20% |