β οΈ EDUCATIONAL PURPOSE ONLY
This codebase is a learning exercise for building DeFi solutions with algorithmic stablecoins. It has NOT been audited and should NEVER be used in production environments.
DSC (Decentralized Stable Coin) is an educational implementation of an overcollateralized algorithmic stablecoin protocol. Think of it as a simplified version of MakerDAO's DAI, designed to teach the fundamentals of:
- π¦ Collateralized Debt Positions (CDPs)
- π Oracle-based price feeds
- βοΈ Liquidation mechanisms
- π DeFi security patterns
- π§ͺ Property-based testing
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β DSC Token ββββββ DSC Engine ββββββ€ Price Oracles β
β (ERC20) β β (Core Logic) β β (Chainlink) β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β
βΌ
βββββββββββββββββββ
β Collateral β
β (WETH, WBTC, β
β WSOL) β
βββββββββββββββββββ
-
Relative Stability: Anchored/Pegged to USD $1.00
- Using Chainlink price feeds
- Exchange function for WETH & WBTC β $$$
-
Stability Mechanism: Algorithmic (Decentralized)
- Overcollateralized minting
- Automated liquidation system
-
Collateral Type: Exogenous (Crypto)
- Ethereum (WETH)
- Bitcoin (WBTC)
- Solana (WSOL)
- π― Overcollateralized Stability - Maintain >150% collateral ratio
- π Multi-Collateral Support - WETH, WBTC, WSOL
- π Real-time Price Feeds - Chainlink oracle integration
- β‘ Instant Liquidations - Automated liquidation system
- π‘οΈ Security First - Comprehensive testing and analysis tools
- π§ Modular Design - Clean, upgradeable architecture
- Smart Contracts: Solidity ^0.8.30
- Development Framework: Foundry
- Oracle Provider: Chainlink
- Security Tools: Slither, Aderyn, Echidna, Solhint
- Testing: Unit, Integration, Invariant, Property-based
- Networks: Anvil (local), Sepolia (testnet)
Ensure you have the following installed:
# Clone the repository
git clone https://github.com/vidalpaul/dsc-defi.git
cd dsc-defi
# Install Foundry dependencies
forge install
# Install security tools (optional)
./scripts/install-security-tools.sh
# Build the project
forge build
We've implemented comprehensive testing with 95%+ coverage:
# Run all tests
make test
# Run specific test suites
make test-unit # Unit tests
make test-invariant # Invariant tests
# Generate coverage report
make coverage
# Run security analysis
make security
Component | Lines | Statements | Branches | Functions |
---|---|---|---|---|
DSC Token | 100% | 100% | 100% | 100% |
DSC Engine | 95%+ | 95%+ | 90%+ | 95%+ |
DSC Library | 100% | 100% | 100% | 100% |
Scripts | 100% | 100% | 95%+ | 100% |
Before deploying to Sepolia, you'll need testnet ETH and tokens:
Get free Sepolia ETH from these faucets:
The protocol uses these testnet tokens (automatically deployed):
- WETH:
0xdd13E55209Fd76AfE204dBda4007C227904f0a81
- WBTC:
0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063
- WSOL:
0x2644980C2480EB8F31263d24189e2AA5e7f8f1D3
Create a .env
file in the project root:
# Copy the example environment file
cp .env.example .env
# Edit with your details
PRIVATE_KEY=your_private_key_here
SEPOLIA_RPC_URL=https://sepolia.infura.io/v3/YOUR_INFURA_KEY
ETHERSCAN_API_KEY=your_etherscan_api_key
# Deploy the complete protocol
make deploy-sepolia
# Or use forge directly
forge script script/DSC_Protocol_Deploy.s.sol:DSC_Protocol_DeployScript \
--rpc-url $SEPOLIA_RPC_URL \
--private-key $PRIVATE_KEY \
--broadcast \
--verify
Once deployed, you can interact with the protocol:
# Example: Deposit collateral and mint DSC
cast send $DSC_ENGINE_ADDRESS \
"depositCollateralAndMintDSC(address,uint256,uint256)" \
$WETH_ADDRESS \
1000000000000000000 \
500000000000000000 \
--rpc-url $SEPOLIA_RPC_URL \
--private-key $PRIVATE_KEY
// Deposit 1 WETH as collateral
dscEngine.depositCollateral(wethAddress, 1 ether);
// Mint 1000 DSC tokens (ensure health factor > 1)
dscEngine.mintDSC(1000 ether);
// Monitor your position health
uint256 healthFactor = dscEngine.getHealthFactor(userAddress);
// If healthFactor < 1e18, position can be liquidated!
// Burn DSC first, then redeem collateral
dscEngine.burnDSC(500 ether);
dscEngine.redeemCollateral(wethAddress, 0.5 ether);
This educational protocol includes comprehensive security measures:
- Slither: Static analysis for common vulnerabilities
- Aderyn: Advanced DeFi-specific security scanning
- Echidna: Property-based testing with 8 critical invariants
- Solhint: Code quality and style enforcement
- β Protocol must remain overcollateralized
- β User health factors β₯ 1.0 (except during liquidation)
- β DSC total supply β€ total collateral value
- β Accurate collateral accounting
- β Price feed manipulation resistance
# Complete security audit
make security
# Individual tools
make slither # Static analysis
make aderyn # DeFi security scan
make echidna # Property testing
make solhint # Code quality
This project demonstrates key DeFi concepts:
- Access Control: OpenZeppelin's Ownable pattern
- Reentrancy Protection: ReentrancyGuard implementation
- Oracle Integration: Chainlink price feeds
- Safe Math: Built-in overflow protection (Solidity ^0.8.0)
- Collateralization: Over-collateralized lending
- Liquidations: Automated liquidation incentives
- Price Oracles: External price data integration
- Stablecoin Mechanisms: Algorithmic price stability
- Unit Testing: Individual function testing
- Integration Testing: Multi-contract interactions
- Invariant Testing: Protocol-level invariants
- Property-Based Testing: Fuzzing with random inputs
dsc-defi/
βββ src/
β βββ DSC.sol # ERC20 stablecoin implementation
β βββ DSCEngine.sol # Core protocol logic
β βββ DSCLib.sol # Shared utility functions
βββ script/
β βββ Config_Helper.s.sol # Network configuration
β βββ DSC_Protocol_Deploy.s.sol # Deployment script
βββ test/
β βββ unit/ # Unit tests
β βββ echidna/ # Property-based tests
β βββ mocks/ # Mock contracts
βββ security-reports/ # Security analysis output
βββ Makefile # Build and test commands
βββ README.md # This file
By studying this codebase, you'll learn:
-
DeFi Protocol Architecture ποΈ
- How stablecoins maintain their peg
- Collateralized debt position mechanics
- Liquidation system design
-
Smart Contract Security π‘οΈ
- Common vulnerabilities and mitigations
- Comprehensive testing strategies
- Security analysis tools usage
-
Oracle Integration π
- Chainlink price feed implementation
- Oracle manipulation attack prevention
- Price data validation
-
Advanced Solidity β‘
- Library pattern usage
- Complex state management
- Gas optimization techniques
This is an educational project! Contributions are welcome:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
- Follow the existing code style
- Add comprehensive tests for new features
- Run security analysis before submitting
- Update documentation as needed
This project is licensed under the MIT License - see the LICENSE file for details.
THIS CODE IS FOR EDUCATIONAL PURPOSES ONLY
- π« Not Production Ready: This code has not undergone professional security audits
- π Learning Tool: Designed to teach DeFi concepts and smart contract development
- π‘ Experimental: May contain bugs, vulnerabilities, or design flaws
- π Use at Your Own Risk: Authors are not responsible for any losses
This project was built as a learning exercise inspired by:
- MakerDAO - The original DeFi stablecoin protocol
- Chainlink - Decentralized oracle networks
- OpenZeppelin - Secure smart contract libraries
- Foundry - Fast, portable, and modular toolkit
If you're using this for learning and need help:
- π Check the documentation
- π Open an issue
- π¬ Start a discussion
Happy Learning! π
"In DeFi we trust, but always verify" β¨
Made with β€οΈ for the DeFi community