A production-ready DeFi lending and borrowing protocol built on the Oasis Sapphire Network with privacy features and confidential computing capabilities.
- Multi-Asset Support: Deposit and borrow ETH, USDC, DAI, and other ERC20 tokens
- Interest-Bearing Tokens: Receive aTokens that automatically accrue interest
- Dynamic Interest Rates: Rates adjust based on utilization and market conditions
- Collateral Management: Multi-collateral support with configurable LTV ratios
- Liquidation Engine: Automated liquidation of unhealthy positions
- Flash Loans: Execute flash loans for arbitrage and other DeFi strategies
- Confidential Balances: User balances are encrypted and private
- Hidden Collateral Ratios: Collateral ratios are not publicly visible
- MEV Protection: Transactions are protected from front-running
- Private Health Factors: Health factors are calculated confidentially
- Confidential Liquidations: Liquidation processes maintain privacy
- Upgradeable Contracts: UUPS proxy pattern for future upgrades
- Access Control: Role-based permissions for admin functions
- Pause Mechanism: Emergency pause functionality
- Reentrancy Protection: Secure against reentrancy attacks
- Oracle Integration: Chainlink price feeds with fallback mechanisms
defi-sapphire-lending/
βββ contracts/
β βββ core/
β β βββ LendingPool.sol # Main lending pool contract
β βββ interfaces/
β β βββ ILendingPool.sol # Lending pool interface
β β βββ IPriceOracle.sol # Price oracle interface
β β βββ IInterestRateModel.sol # Interest rate model interface
β βββ libraries/
β β βββ DataTypes.sol # Data structures
β β βββ Math.sol # Mathematical operations
β βββ oracles/
β β βββ ChainlinkPriceOracle.sol # Price oracle implementation
β βββ tokens/
β βββ aToken.sol # Interest-bearing token
βββ scripts/
β βββ deploy.js # Deployment script
βββ test/ # Test files
βββ hardhat.config.ts # Hardhat configuration
βββ package.json # Dependencies
- Node.js (v16 or higher)
- npm or yarn
- Hardhat
- Sapphire testnet ETH
-
Clone the repository
git clone <repository-url> cd defi-sapphire-lending
-
Install dependencies
npm install
-
Set up environment variables
cp .env.example .env
Edit
.envwith your configuration:PRIVATE_KEY=your_private_key_here SAPPHIRE_API_KEY=your_sapphire_api_key_here
-
Compile contracts
npm run compile
npm run deploy:localnpm run deploy:sapphire-testnetnpm run deploy:sapphire-mainnet// Deposit USDC
await lendingPool.deposit(
usdcAddress,
ethers.parseUnits("1000", 6), // 1000 USDC
userAddress,
0 // referral code
);// Borrow DAI against USDC collateral
await lendingPool.borrow(
daiAddress,
ethers.parseEther("500"), // 500 DAI
2, // variable rate mode
0, // referral code
userAddress
);// Repay borrowed DAI
await lendingPool.repay(
daiAddress,
ethers.parseEther("100"), // 100 DAI
2, // variable rate mode
userAddress
);// Withdraw USDC
await lendingPool.withdraw(
usdcAddress,
ethers.parseUnits("500", 6), // 500 USDC
userAddress
);// Execute flash loan
await lendingPool.flashLoan(
receiverAddress,
[usdcAddress],
[ethers.parseUnits("10000", 6)],
[0], // no debt mode
userAddress,
"0x", // params
0 // referral code
);// Liquidate unhealthy position
await lendingPool.liquidationCall(
collateralAsset,
debtAsset,
userAddress,
debtToCover,
false // receive aToken
);Each reserve can be configured with the following parameters:
- Collateral Factor: Maximum percentage of collateral value that can be borrowed (e.g., 80%)
- Liquidation Threshold: Health factor threshold for liquidation (e.g., 85%)
- Borrow Factor: Maximum percentage of available liquidity that can be borrowed
- Interest Rate Parameters: Base rate, kink rate, and multiplier for dynamic rates
- Heartbeat: Maximum time between price updates
- Deviation Threshold: Maximum allowed price deviation
- Staleness Threshold: Maximum age of price data
- Price Confidence: Confidence interval for price accuracy
npm testnpm run coveragenpm run gas-report- External audit pending
- Internal review completed
- Bug bounty program planned
- Reentrancy protection on all external calls
- Access control with role-based permissions
- Emergency pause functionality
- Oracle manipulation protection
- Flash loan attack prevention
- Use latest OpenZeppelin contracts
- Follow Solidity security guidelines
- Implement proper access controls
- Use safe math operations
- Validate all inputs
- Total Value Locked (TVL)
- Utilization rates
- Interest rates
- Liquidation events
- Flash loan volume
- Reserve health factors
- Oracle price staleness
- Protocol solvency
- Gas usage optimization
// Pause all operations
await lendingPool.pause();// Emergency withdraw (admin only)
await lendingPool.emergencyWithdraw(asset, amount, to);// Switch to fallback oracle
await priceOracle.setFallbackOracle(fallbackOracleAddress);- Deploy new implementation
- Verify implementation
- Upgrade proxy
- Verify upgrade
// Upgrade LendingPool
await upgrades.upgradeProxy(lendingPoolAddress, newLendingPoolImpl);- The Graph for indexing
- Dune Analytics for dashboards
- Custom analytics dashboard
- User deposit/withdrawal patterns
- Borrowing trends
- Liquidation analysis
- Interest rate evolution
- Fork the repository
- Create feature branch
- Make changes
- Add tests
- Submit pull request
- Follow Solidity style guide
- Add comprehensive tests
- Update documentation
- Include gas optimization
This project is licensed under the MIT License - see the LICENSE file for details.
- OpenZeppelin for secure contract libraries
- Chainlink for price oracle infrastructure
- Oasis Foundation for Sapphire Network
- Hardhat team for development tools
- Initial release
- Core lending functionality
- Privacy features
- Sapphire Network integration
- Governance token
- Yield farming
- Cross-chain bridges
- Advanced analytics