A Solana-based stablecoin protocol built with Anchor framework that enables users to mint stablecoins by depositing SOL as collateral. This project implements a collateralized debt position (CDP) system with liquidation mechanisms to maintain protocol stability. The protocol uses Pyth Network oracle for real-time price feeds to ensure accurate collateral valuation and liquidation calculations.
Built with Anchor | SOL Collateral CDP | Oracle-Backed Stability
- Deposit & Mint: Deposit SOL collateral and mint stablecoins in a single transaction
- Redeem & Withdraw: Repay stablecoin debt by burning tokens and withdraw SOL collateral with health factor validation
- Pyth Oracle Integration: Real-time price feeds from Pyth Network for accurate collateral valuation
- Liquidation System: Fully automated liquidation with health factor checks, bonus calculations, and debt repayment
- Health Factor Monitoring: Track collateralization ratios to prevent undercollateralization
- Admin Configuration:
- Initialize protocol configuration with custom parameters
- Update minimum health factor dynamically
- Token2022 Support: Native integration with Solana's Token2022 program
- PDA-Based Architecture: Secure program-derived addresses for all protocol accounts
- Overcollateralization: Default 200% collateralization requirement for safety
-
Configuration Account (PDA): Stores protocol-wide parameters including:
- Authority address
- Mint account for stablecoin
- Liquidation threshold (default: 50%)
- Liquidation bonus (default: 10%)
- Minimum health factor (default: 1)
- PDA bumps for security
-
Collateral Account (PDA per user): Tracks individual user positions:
- Depositor address
- SOL account PDA for collateral storage
- Token account for stablecoin balance
- Current lamport balance (SOL deposited)
- Total amount of stablecoins minted
- Initialization status
- PDA bumps for security
-
Mint Account (PDA): Stablecoin mint with:
- 9 decimals precision
- Program-controlled authority
- Freeze authority for security
-
Price Oracle: Integrates with Pyth Network (PriceUpdateV2) to fetch real-time SOL/USD prices for:
- Collateral valuation during deposit
- Health factor calculations
- Liquidation trigger conditions
- USD to SOL conversion for liquidation calculations
- Liquidation Threshold: 50% (200% overcollateralization required)
- Liquidation Bonus: 10% incentive for liquidators
- Min Health Factor: 1 (configurable by admin)
- Decimals: 9 decimals for the stablecoin token
initialize_config: Initialize the protocol configuration and create the stablecoin mintupdate_config: Update the minimum health factor (authority-only)
-
deposit_collateral_and_mint_token: Deposit SOL collateral and mint stablecoins in one transaction- Requires Pyth price update account for collateral valuation
- Automatically creates collateral account if needed
- Creates associated token account if needed
-
redeem_collateral_and_burn_tokens: Repay stablecoin debt and withdraw SOL collateral- Burn stablecoins to reduce
amount_minted(debt) - Withdraw SOL collateral from PDA account
- Health factor validation ensures position remains safe after withdrawal
- Requires Pyth price update account for real-time collateral valuation
- Burn stablecoins to reduce
-
liquidate: Liquidate undercollateralized positions- Only works when health factor < minimum health factor
- Liquidator pays stablecoin debt (burns tokens)
- Liquidator receives SOL collateral + liquidation bonus (10% default)
- Automatically updates collateral account balance and debt
- Requires Pyth price update account for accurate price calculations
- Framework: Anchor 0.30.1
- Blockchain: Solana
- Language: Rust (programs), TypeScript (tests)
- Testing: Anchor test framework with Pyth Solana Receiver SDK integration
- Token Standard: Token2022 (SPL Token Interface)
- Price Oracle: Pyth Network (pyth-solana-receiver-sdk 0.3.1)
- Architecture: PDA-based for enhanced security
ws_stablecoin/
├── programs/
│ └── ws_stablecoin/
│ └── src/
│ ├── lib.rs # Main program entry point
│ ├── constants.rs # Protocol constants and seeds
│ ├── instructions/ # Instruction handlers
│ │ ├── admin/ # Admin instructions
│ │ │ ├── initialize_config.rs
│ │ │ └── update_config.rs
│ │ ├── deposit/ # Deposit instructions
│ │ │ ├── deposit_collateral_and_mint_token.rs
│ │ │ └── util.rs # Utility functions (SOL deposit, token mint)
│ │ ├── withdraw/ # Withdraw instructions
│ │ │ ├── redeem_collatoral_and_burn_tokens.rs
│ │ │ ├── liquidate.rs
│ │ │ └── utils.rs # Utility functions (SOL withdrawal, token burn)
│ │ └── util.rs # Shared utility functions (health factor, price calculations)
│ └── states/ # Account state definitions
│ └── state.rs # Collateral and Configuration structs
├── tests/ # TypeScript integration tests
│ └── ws_stablecoin.ts # Test suite covering all instructions
└── frontend/ # Frontend application
- User deposits SOL: SOL is transferred to a PDA-controlled SOL account
- Price verification: Pyth oracle provides current SOL/USD price
- Collateral calculation: System calculates collateral value based on oracle price
- Stablecoin minting: Stablecoins are minted to user's associated token account
- Position tracking: Collateral account tracks user's SOL balance and minted amount
- User repays debt: Burns stablecoins to reduce
amount_minted(debt) - Health factor validation: System calculates health factor to ensure position remains safe
- Collateral withdrawal: SOL is transferred from PDA account back to user's wallet
- Position update: Collateral account balance (
lamport_balance) and debt (amount_minted) are updated - Safety check: Withdrawal only proceeds if health factor remains >= 1.0
- Health factor check: System verifies health factor < minimum health factor (default: 1.0)
- Price calculation: Uses Pyth oracle to convert stablecoin debt amount to SOL value
- Bonus calculation: Calculates liquidation bonus (10% of SOL value by default)
- Liquidator pays debt: Liquidator burns stablecoins to pay user's debt
- Liquidator receives collateral: SOL is transferred to liquidator (debt amount + bonus)
- Position update: Collateral account balance and debt are automatically updated
- Post-liquidation check: Health factor is recalculated to verify position improvement
- PDA-based accounts: All protocol accounts use Program Derived Addresses for enhanced security
- Overcollateralization: Default 200% collateral requirement prevents undercollateralization
- Oracle price feeds: Real-time price data ensures accurate valuations
- Program-controlled mint: Stablecoin mint authority is controlled by the program PDA
- Health factor validation: All withdrawals are validated to ensure positions remain safe
- Safe debt repayment: Users can only withdraw collateral if health factor stays above threshold
- Automated liquidation: Under-collateralized positions are automatically liquidatable by anyone
- Liquidation bonus: Incentivizes liquidators to maintain protocol health (10% default)
- Error handling: Comprehensive error codes for price validation and health factor checks
- Rust (latest stable)
- Solana CLI
- Anchor Framework
- Node.js and Yarn
- Pyth Solana Receiver SDK (for testing with oracle price feeds)
anchor buildanchor testThe test suite includes comprehensive integration tests covering all protocol instructions:
- Initialize Config: Tests protocol initialization and stablecoin mint creation
- Deposit & Mint: Tests SOL collateral deposit and stablecoin minting (1 SOL deposit, 1 stablecoin mint)
- Redeem & Withdraw: Tests debt repayment and collateral withdrawal (0.5 SOL withdrawal, 0.5 stablecoin burn)
- Update Config: Tests admin configuration updates (sets min health factor to 100 for liquidation test)
- Liquidation: Tests liquidation of undercollateralized positions (0.5 stablecoin debt liquidation)
- Config Reset: Resets min health factor back to 1 after liquidation test
Test Setup:
- Uses Pyth Solana Receiver SDK for oracle price feeds
- Tests run on localnet with Pyth price feed integration
- All transactions use
confirmedcommitment and skip preflight for faster execution
anchor deployThe protocol includes comprehensive error handling with the following custom errors:
PriceNotAvailable: Pyth oracle price feed is not availablePriceNotValid: Price from oracle is invalid (e.g., price <= 0)HealthFactorTooLow: Health factor is below the minimum thresholdAboveMinimumHealthFactor: Cannot liquidate - health factor is above minimum (position is safe)
This project is configured for Solana devnet. Update the cluster and wallet settings in Anchor.toml for different environments.
This project is licensed under the MIT License - see the LICENSE file for details.
MIT License
Copyright (c) 2024 WS Stablecoin
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This is a bootcamp/educational project. Do not use in production without proper security audits and testing.
