← Back to Projects
DeFi Protocol Project
Build Decentralized Finance Primitives
Problem Statement:
How do we create financial services without trusted intermediaries?
Learning Objectives
- Understand constant product AMM mechanics
- Implement collateralized lending
- Create flash loan functionality
- Integrate price oracles
Constant Product AMM
x · y = k
- x = Reserve of Token A
- y = Reserve of Token B
- k = Constant (invariant)
Price increases as you buy (slippage). Fees accumulate in k.
Lending Protocol
| Parameter | Typical Value | Purpose |
| Collateral Factor | 75% | Max borrow per collateral |
| Liquidation Threshold | 80% | When liquidation triggers |
| Liquidation Bonus | 5% | Incentive for liquidators |
Flash Loans
- Borrow any amount with zero collateral
- Must repay + fee in same transaction
- If not repaid, entire transaction reverts
- Enables atomic arbitrage without capital
Oracle Integration
// Chainlink Price Feed
(, int256 price,,,) = priceFeed.latestRoundData();
// ETH/USD price with 8 decimals
Risk Analysis
| Risk | Description | Mitigation |
| Smart Contract | Bugs, exploits | Audits, bug bounties |
| Oracle | Price manipulation | TWAPs, multiple sources |
| Liquidation | Cascade failures | Conservative parameters |
| Economic | Depegs, bank runs | Insurance, reserves |
Cryptoeconomics Analysis
| Question | Answer |
| PROBLEM | Financial services without banks |
| INCENTIVES | LPs earn fees, liquidators earn bonus |
| BENEFITS/COSTS | Permissionless access; gas costs, risks |
| FAILURE MODE | Oracle manipulation, flash loan attacks |
| DESIGN CHOICES | Collateral ratios, fee structures |
| ALTERNATIVES | Centralized exchanges, traditional finance |
Implementation Checklist
- Deploy two test tokens
- Create liquidity pool (AMM)
- Implement swap function
- Add lending deposit/withdraw
- Create borrow/repay functions
- Implement liquidation
- Add flash loan capability
- Connect Chainlink price feeds
Related Lessons
- L33: DeFi Introduction
- L34: AMM Mechanics
- L35: Uniswap Deep Dive
- L37: Lending Protocols
- L38: Stablecoins
- L42: Flash Loans
Resources