A decentralized stablecoin implementation that uses over-collateralization and liquidation mechanisms to maintain price stability. This project demonstrates core concepts of DeFi stablecoin systems including collateral management, price feeds, liquidations, and risk parameters.
- Over-collateralization: Users must provide more collateral value than the stablecoins they mint
- Price Oracle: Uses a Time-Weighted Average Price (TWAP) mechanism for reliable price feeds
- Liquidation System: Automatically liquidates unsafe positions to maintain system solvency
- Risk Parameters: Configurable parameters for collateral ratio, liquidation thresholds, and price feed staleness
- ERC20 Compliant: Both the stablecoin and collateral tokens follow the ERC20 standard
- Upgradeable: Uses the UUPS (Universal Upgradeable Proxy Standard) pattern for contract upgrades
- ERC20-compliant stablecoin token
- Controlled minting and burning by the StableCoinEngine
- Role-based access control for secure operation
- Upgradeable using UUPS proxy pattern
- Manages collateral deposits and stablecoin minting
- Implements TWAP oracle for price feeds
- Handles liquidations of unsafe positions
- Enforces system risk parameters
- Upgradeable using UUPS proxy pattern
- Transparent proxy implementation for UUPS upgrades
- Delegates all calls to the implementation contract
- Maintains separation between storage and logic
- Minimum Collateral Ratio: 150%
- Liquidation Threshold: 120%
- Liquidation Bonus: 10%
- Maximum Price Change: 10% between updates
- Price Update Frequency: Minimum 5 minutes between updates
- Price Staleness: Data considered stale after 1 hour
This project uses the Foundry development framework.
$ forge build
$ forge test
The test suite includes comprehensive tests for:
- Collateral management
- Price feed mechanics
- Liquidation scenarios
- Edge cases and error conditions
- Contract upgrades and proxy patterns
$ forge fmt
- Price feed manipulation protection through TWAP
- Minimum delay between price updates
- Maximum price change limits
- Collateral ratio safety margins
- Liquidation incentives for system stability
- Secure upgrade pattern using UUPS proxies
- Owner-controlled upgrade functionality
- Storage layout protection for upgrades
The project uses the UUPS (Universal Upgradeable Proxy Standard) pattern for contract upgrades:
- Proxy Contracts:
StableCoinProxy
serves as the proxy that delegates calls to the implementation - Implementation Contracts:
StableCoin
andStableCoinEngine
contain the logic - Storage: Utilizes OpenZeppelin's unstructured storage pattern to prevent storage collisions
- Access Control: Only the owner can perform upgrades
- Initialization: Uses
initialize
functions instead of constructors for proper proxy initialization
- Deploy new implementation contract
- Owner calls upgrade function on the proxy
- Proxy updates its implementation pointer
- State is preserved while logic is updated
This project is licensed under MIT.
This project uses Foundry, a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.
- Forge: Ethereum testing framework
- Cast: Swiss army knife for interacting with EVM smart contracts
- Anvil: Local Ethereum node
- Chisel: Solidity REPL
For detailed Foundry documentation, visit https://book.getfoundry.sh/
$ forge build
$ forge test
$ forge fmt
$ forge snapshot
$ anvil
$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key>
$ cast <subcommand>
$ forge --help
$ anvil --help
$ cast --help