A13: DeFi Builder
Instructor Guide
Assignment Overview: This hands-on activity has students deploy a complete mini DeFi stack on
Remix IDE: two ERC-20 tokens and an automated market maker (AMM). By executing swaps and tracking prices,
students build intuition for constant-product pricing, price impact, and slippage -- core concepts that
underpin billions of dollars in daily DeFi trading volume.
Learning Objectives & Course Alignment
Core Learning Goals:
- Smart Contract Deployment: Deploy and connect multiple interacting Solidity contracts
- AMM Mechanics: Understand constant-product pricing through hands-on experimentation
- Price Impact: Observe and quantify how trade size affects execution price
- DeFi Composability: See how tokens and DEX contracts work together as building blocks
- Technical Communication: Present quantitative findings to peers
Course Integration
This assignment works best after students have completed:
- Lesson 6 (DeFi): Conceptual understanding of AMMs, liquidity pools, yield farming
- Lesson 10 (Solidity Basics): ERC-20 token structure, Remix deployment, approve/transferFrom
- Lesson 11 (Building DeFi): SimpleDEX contract, constant-product formula, escrow contracts
Pre-Class Preparation
Time Required
| Activity | Instructor Time | Class Time |
|---|---|---|
| Pre-class preparation | 45-60 minutes | - |
| Introduction & setup | - | 10 minutes |
| Token + DEX deployment | - | 25 minutes |
| Swaps + worksheet | - | 25 minutes |
| Presentations (5 groups) | - | 15 minutes |
| Total Class Time | - | 75 minutes |
Pre-Class Checklist
CRITICAL -- Do This Before Class:
- Test the full flow yourself on Remix: Deploy 2 tokens, deploy DEX, add liquidity, execute all 4 swaps. Confirm your results match the answer key. This takes ~20 minutes and ensures you can debug any student issues live.
- Verify Remix is accessible: Some institutional networks block remix.ethereum.org. Test from a campus computer.
- Prepare the contract code: Have both contract files (MyToken.sol and SimpleDEX.sol) ready to share. Options:
- Project the code on screen for students to type
- Share a Remix workspace link (File > Share Gist)
- Post the code on the course LMS before class
- Print worksheets: One per group, or have students access the digital version
- Instructions sheet (1 per group, or projected on screen)
- Price-tracking worksheet (1 per group)
- Access to Remix IDE in browser
- Answer key (confidential)
- Grading rubric
- Timer for presentations
- Your own completed Remix session as reference
Group Formation Recommendations
- Size: 2-3 students per group (3 is ideal for dividing work: Token A lead, Token B lead, DEX lead)
- Skill Mix: Pair students who completed A12 (Token Builder) successfully with those who struggled
- Pre-assign: Form groups before class to save time. This assignment is tight at 75 minutes
- Computer Setup: Each group needs at least one computer with Remix open
Minute-by-Minute Teaching Plan
Minutes 0-10: Introduction & Setup
Opening Hook (2 minutes):
"Uniswap has processed over $2 trillion in trades using the same constant-product formula you'll implement today. No order book, no matching engine -- just x times y equals k. In the next 75 minutes you'll build your own DEX, trade your own tokens, and see exactly how this formula determines prices."
Activity Explanation (3 minutes):- Walk through the 5-step activity structure
- Emphasise: "The goal is not just to deploy code but to understand the math. Your worksheet is where you demonstrate that understanding."
- Show the recommended swap amounts: 1,000 / 10,000 / 50,000 / 10,000 reverse
- Mention the bonus challenge for fast finishers
- Open Remix on the projector
- Show where to create new files
- Demonstrate the approve + addLiquidity flow once (this is the step that trips up most students)
- Show how to read transaction logs for output amounts
- Hand out worksheets
- Share contract code (screen, link, or LMS)
- Start the timer
Minutes 10-35: Token + DEX Deployment
Instructor Role During Deployment:
- Circulate immediately: The first 10 minutes of deployment are when most groups get stuck
- Watch for the #1 error: Forgetting to approve() before addLiquidity(). If you see a group stalled, ask "Did you approve the DEX to spend your tokens?"
- Address order: Check groups deploy tokens BEFORE the DEX (they need token addresses for the DEX constructor)
- Milestone check at minute 25: All groups should have the DEX deployed with liquidity added by now. Groups still deploying tokens may need individual help
Most Common Deployment Errors & Fixes:
| Error | Fix |
|---|---|
| "Allowance exceeded" on addLiquidity | Student forgot to call approve() on BOTH tokens before addLiquidity. Must approve the DEX address for at least the liquidity amount on each token. |
| "Allowance exceeded" on swap | Student forgot to approve the DEX for the INPUT token before swapping. approve(DEX_address, amount) on the token being sold. |
| "Invalid token" on swap | Student passed an incorrect address for _tokenIn. Must be exactly one of the two token addresses used in the DEX constructor. |
| DEX deployed but reserves are 0 | Student deployed the DEX but never called addLiquidity(). Or approve failed silently. Check token balanceOf(DEX_address). |
| "Insufficient output" on swap | Usually happens when trying to swap more than the reserve can support. Check reserve amounts. |
| Contract does not compile | Check Solidity compiler version (must be 0.8.20+). Check for copy-paste errors in contract code. |
Minutes 35-60: Swaps + Worksheet
Instructor Role During Swaps:
- Guide worksheet completion: Remind students to record BOTH the output amount AND the new reserve values after each swap
- Check calculations: Visit groups and spot-check their k calculations. Common error: using initial reserves for all swaps instead of post-swap reserves
- Prompt analysis: Ask each group "What happened to the effective price between swap 1 and swap 3?" This primes them for the analysis questions
- Direct fast groups to bonus: Groups finishing early should attempt slippage protection or escrow
- Select presenters by minute 50: Choose 5 groups for presentations, aiming for diversity (different observations, bonus attempts)
Minutes 60-75: Presentations & Discussion
Presentation Management:
- Select 4-5 groups (adjust based on class size and time)
- Strict 3-minute timer: Give 30-second warning, hard stop at 3 minutes
- Screen sharing: Have each group show their Remix session and worksheet
- Encourage comparison: After 2-3 presentations, ask "Did anyone get different results?"
- "Why would anyone use a DEX instead of a traditional exchange?" (permissionless, 24/7, no KYC, no custodian)
- "If you had $1M to trade, would you use our SimpleDEX? Why not?" (slippage would be catastrophic with shallow liquidity)
- "How does Uniswap solve the slippage problem?" (deep liquidity from many providers, concentrated liquidity in V3)
- "What role does arbitrage play?" (keeps DEX prices aligned with centralised exchanges)
Discussion Facilitation Tips
Socratic Questions for Deeper Understanding
For Price Impact:- "If you doubled the initial liquidity to 200,000 each, would swap 1 give a better or worse price?" (better -- more liquidity = less impact)
- "Is there a swap size where you get zero tokens out?" (approaching swap of entire reserve)
- "What would the price curve look like if we plotted swap size vs effective price?" (hyperbolic decline)
- "After swap 3, Token A is cheap in the pool. If there were another DEX with a 1:1 price, what would a trader do?" (buy A on our DEX, sell on the other)
- "How does arbitrage keep DEX prices honest?" (arbitrageurs correct mispricings until no profit remains)
- "Why does Uniswap charge 0.3% per swap?" (pays liquidity providers, makes k increase over time)
- "What is impermanent loss and how does it relate to what you saw today?" (LPs lose value when prices move away from deposit ratio)
- "Could you build a lending protocol using similar mechanics?" (yes -- Aave/Compound use utilisation-based interest rates)
Common Misconceptions to Address
| Misconception | Reality |
|---|---|
| "The DEX sets the price" | The DEX does not set a price -- the formula and current reserves DETERMINE the price for any given trade size. No human or oracle is involved. |
| "k should be exactly constant" | k varies slightly due to integer division rounding. In production DEXes, fees make k increase over time. This is a feature, not a bug. |
| "Bigger swaps are always bad" | Bigger swaps have more price impact relative to the pool, but if you need to trade that amount, you need the liquidity. The solution is deeper pools, not smaller trades. |
| "This only works with equal reserves" | You can start with unequal reserves (e.g. 100,000 A and 50,000 B). The initial implied price would be 0.5 B per A. The formula works the same way. |
| "approve is a one-time thing" | approve sets a spending allowance that decreases with each transferFrom. Students may need to re-approve before each swap unless they approved a large amount upfront. |
Assessment & Grading Tips
Quick Grading Strategy
During Class (take notes on):
- Which groups got the DEX working independently vs. needed heavy help
- Presentation quality and technical accuracy
- Individual engagement level within each group
- Bonus attempts
Post-Class Worksheet Grading (30 minutes total):
- Quick validation: Check swap 1 output. If it is 990, their data is likely correct. If wildly off, they may have used different initial liquidity or calculated incorrectly
- k consistency: Multiply their reported reserves. Should be approximately 10B throughout
- Analysis depth: Look for mentions of "price impact", "slippage", "arbitrage", or "liquidity depth"
- Time estimate: 3-5 minutes per group worksheet
Adaptations for Different Class Sizes
| Class Size | Modifications |
|---|---|
| Small (10-15) | Groups of 2. All groups present (2 min each). More time for discussion. Can extend to 90 min and include bonus as required work. |
| Medium (20-30) | Groups of 3 (7-10 groups). Select 5 to present. Standard 75-minute format. |
| Large (40+) | Groups of 3-4. Consider 90-minute session. Select 6-7 to present (2 min each). Alternatively, have groups submit recorded screen-share presentations instead of live. |
Extension Activities
For Advanced Students
- Liquidity Provider Tokens: Modify the DEX to mint LP tokens proportional to liquidity contribution. Track how LP share changes with deposits/withdrawals.
- Multi-Hop Swaps: Deploy a second DEX pair (Token B / Token C) and implement a router that swaps A to C via B.
- Fee Mechanism: Add a 0.3% fee to swaps. Observe how k grows over time and calculate LP returns.
- Flash Loan Attack Simulation: Demonstrate how a flash loan could manipulate the DEX price. Connect to L07 security concepts.
For Struggling Students
- Pre-deploy tokens: Have tokens already deployed. Share the addresses so students only need to deploy the DEX.
- Guided Remix walkthrough: Project the screen and do the first deployment together as a class.
- Simplified worksheet: Pre-fill the initial state row and the formula. Students only need to fill in results.
- Pair with strong group: For the first 10 minutes, have struggling students observe a successful group.
Troubleshooting Common Issues
| Problem | Solution |
|---|---|
| Remix IDE loads slowly / crashes | Clear browser cache. Try a different browser (Chrome recommended). Use incognito mode if extensions interfere. As last resort, have a backup Remix workspace to share. |
| Student deploys wrong compiler version | In Remix compiler tab, select 0.8.20 or later. Enable "Auto compile" to catch errors early. |
| Groups finish at very different speeds | Fast groups: direct to bonus. Slow groups: instructor helps with deployment, reduces required swaps to 2 (small + large). |
| Presentations run over time | Reduce to 4 groups. Cut Q&A. Use strict timer with audible warning. |
| Students get different numbers than answer key | Likely used different initial liquidity amounts. As long as their k is approximately constant across their swaps, the data is valid. Grade their internal consistency. |
| Network issues with Remix | All work uses Remix JavaScript VM (local), not a real blockchain. No network connection needed after initial page load. If Remix is completely blocked, download Remix desktop app. |
Follow-Up Opportunities
Next Class Session:
- Debrief with Uniswap V2/V3 comparison -- how does concentrated liquidity improve on x*y=k?
- Show real Uniswap analytics (info.uniswap.org) -- volume, TVL, top pools
- Discuss impermanent loss with concrete numbers from their experiment
- Introduce more complex DeFi protocols: lending (Aave), stablecoins (MakerDAO)
- Build a full DeFi stack: token + DEX + lending + governance
- Implement Uniswap V2 core contracts from scratch
- Research paper: "Comparing AMM models: constant product vs constant sum vs concentrated liquidity"
- Participate in DeFi hackathons (ETHGlobal, Devfolio)
Learning Outcome Assessment
How to Measure Success
Students successfully met objectives if they can:- Deploy and connect multiple Solidity contracts on Remix without assistance
- Explain why larger swaps receive worse effective prices
- Calculate the expected output of a swap given reserve values
- Verify that k stays approximately constant and explain why it is not exact
- Connect their experiment to real-world DEX mechanics
- Students can predict swap output before executing (mental model of the formula)
- Students recognise arbitrage opportunities from reserve imbalances
- Students question what happens with fees, multiple LPs, or larger liquidity
- Students connect price impact to the concept of "liquidity depth"
Important Reminder: This is a 75-minute assignment with significant hands-on deployment time.
Be prepared to help groups through the approve/transferFrom pattern, which is the #1 time sink. If multiple
groups are stuck on the same issue, pause the class for a 2-minute mini-lecture rather than helping one at a
time. The deployment steps are procedural -- save your energy for the analytical discussions during the
worksheet and presentation phases.
© Joerg Osterrieder 2025-2026. All rights reserved.