A13: DeFi Builder
HardDeploy Your Own DEX
Points: 60 (+10 bonus)
Group Size: 2-3 students
Materials Needed: Remix IDE (remix.ethereum.org), Price-tracking worksheet, L11 SimpleDEX contract code
Submission: Working DEX on Remix + completed worksheet + 3-minute group presentation
Overview
Decentralised exchanges (DEXes) are the backbone of DeFi, enabling permissionless token trading without intermediaries. In this hands-on assignment you will deploy your own mini DEX using the SimpleDEX contract from Lesson 11. You will create two ERC-20 tokens, provide liquidity, execute swaps, and observe how the constant-product formula x × y = k determines prices in real time.
Learning Objectives
- Deploy and interact with multiple Solidity contracts on Remix IDE
- Understand how an automated market maker (AMM) prices assets using x × y = k
- Observe price impact and slippage through hands-on experimentation
- Analyse how reserve ratios change after successive swaps
- Present technical findings clearly to peers
Prerequisites
- Lesson 10 (Solidity Basics): You should be comfortable deploying contracts on Remix and understand ERC-20 token structure
- Lesson 11 (Building DeFi): You should understand the SimpleDEX contract, the constant-product formula, and the concept of liquidity
- Remix IDE: Have remix.ethereum.org open in your browser
Activity Structure
1 Deploy Two ERC-20 Tokens (15 minutes)
Each group deploys two custom ERC-20 tokens. Use the MyToken contract from L10 or the simplified version below.
- Create a new file
MyToken.solin Remix and paste the code above - Compile with Solidity 0.8.20 or later
- Deploy Token A with constructor arguments: name = your group's chosen name (e.g. "AlphaToken"), symbol (e.g. "ALPHA"), supply =
1000000 - Deploy Token B with a different name/symbol and the same supply
- Copy both contract addresses -- you will need them for the DEX
2 Deploy the SimpleDEX (10 minutes)
Deploy the SimpleDEX contract from Lesson 11. The constructor takes the addresses of your two tokens.
- Create a new file
SimpleDEX.solin Remix - Compile and deploy with the two token addresses as constructor arguments
- Copy the DEX contract address
3 Add Liquidity and Perform Swaps (25 minutes)
This is the core of the assignment. You will provide liquidity, execute swaps, and track how reserves and prices change.
- Approve the DEX: On each token contract, call
approve(DEX_address, 500000)so the DEX can transfer tokens on your behalf - Add initial liquidity: Call
addLiquidity(100000, 100000)on the DEX. Record the initial reserves on your worksheet - Swap 1 -- Small swap: Approve the DEX for Token A again if needed, then call
swap(tokenA_address, 1000). Record the output amount and new reserves - Swap 2 -- Medium swap: Call
swap(tokenA_address, 10000). Record results - Swap 3 -- Large swap: Call
swap(tokenA_address, 50000). Record results - Reverse swap: Now swap Token B back into Token A. Call
swap(tokenB_address, 10000). Record results - For each swap, calculate the effective price (tokens out / tokens in) and the price impact compared to the initial 1:1 ratio
approve() on the input token with the DEX address and the amount you want to swap.
If a transaction reverts with "Allowance exceeded", you need to approve again.
4 Complete the Worksheet (10 minutes)
Fill in the price-tracking worksheet with your recorded data:
- Initial reserves after adding liquidity
- Reserves after each swap
- Output amount for each swap
- Effective price per token for each swap
- Verify that x × y = k holds (approximately) after each swap
- Answer the analysis questions at the bottom of the worksheet
5 Group Presentation (15 minutes total -- 3 min/group)
Present your findings to the class:
- Token Introduction: What tokens did you create? (15 seconds)
- Price Impact Demo: Show how prices changed after your swaps (1 minute)
- Key Observation: What surprised you about the constant-product formula? (1 minute)
- Real-World Connection: How does this relate to real DEXes like Uniswap? (30 seconds)
- Q&A: Answer peer questions (15 seconds)
- Slippage Protection (+5 pts): Modify the swap function to accept a
_minOutparameter and addrequire(amountOut >= _minOut, "Slippage too high") - Simple Escrow (+5 pts): Deploy the Escrow contract from L11 alongside your DEX and demonstrate a token-based escrow trade between group members
Deliverables
| Item | Points | Description |
|---|---|---|
| Working DEX on Remix | 30 | Two ERC-20 tokens deployed, SimpleDEX deployed, liquidity added, at least 3 swaps executed successfully |
| Completed Worksheet | 15 | All swap data recorded, prices calculated, x × y = k verified, analysis questions answered |
| Group Presentation | 15 | Clear explanation of findings, price impact demonstration, real-world connection |
| Total | 60 | |
| Bonus (slippage protection and/or escrow) | +10 | Optional advanced challenges |
Tips for Success
- Keep addresses organised: Write down Token A address, Token B address, and DEX address. You will need them repeatedly.
- Approve before every swap: The most common error is forgetting to call
approve()beforeaddLiquidity()orswap(). - Use simple numbers: Start with round numbers (100000 for liquidity, 1000/10000/50000 for swaps) so the math is easy to verify.
- Check the logs: After each transaction, expand the transaction details in Remix to see event logs with exact amounts.
- Verify x × y = k: After each swap, multiply reserveA × reserveB. It should stay approximately constant (small rounding differences are normal due to integer division).
- Divide the work: One group member can manage Token A, another Token B, and the third can manage the DEX and record data.
Submission Instructions
- Keep your Remix session open so the instructor can verify your deployed contracts
- Submit the completed price-tracking worksheet (one per group) with all member names
- Be prepared to share your screen during your 3-minute presentation
- If you attempted the bonus challenge, include a brief note on your worksheet describing what you added
Related Resources
© Joerg Osterrieder 2025-2026. All rights reserved.