A collateralized lending platform implementing industry-standard DeFi primitives with dynamic interest rates and automated liquidations.
This project implements a functional lending protocol similar to Aave or Compound, featuring over-collateralized borrowing, dynamic interest rate models, and automated liquidation mechanisms. Built as a portfolio piece to demonstrate practical knowledge of DeFi architecture patterns and Solidity best practices.
After studying existing DeFi protocols, I wanted to understand the mechanics by building one from scratch. This project challenged me to think through:
- Precise financial calculations in Solidity without floating-point arithmetic
- Managing complex state across multiple users and token markets
- Implementing time-based interest accrual efficiently
- Designing secure liquidation mechanics that protect both lenders and borrowers
- Integrating external price feeds with proper error handling
The result is approximately 800 lines of production-quality Solidity code.
- Collateralized Lending: Deposit assets and borrow against them (75% LTV)
- Dynamic Interest Rates: Kinked model responding to utilization (2-52% APY)
- Automated Liquidations: Protect protocol solvency with 5% liquidator incentive
- Multi-Token Support: Add any ERC20 token as a market
- Real-Time Pricing: Chainlink oracle integration with staleness checks
- Gas Optimizations: Custom errors, constants, unchecked math where safe
- Security Patterns: ReentrancyGuard, SafeERC20, access control
- Continuous Compounding: Efficient index-based interest accrual
- Health Monitoring: Real-time liquidation risk calculation
LendingPool.sol (500+ LOC)
- Core lending and borrowing logic
- Interest rate calculations using kinked model
- Liquidation engine
- Account health tracking
PriceOracle.sol (100 LOC)
- Chainlink price feed integration
- Fallback pricing mechanism
- Staleness validation (1-hour threshold)
- 18-decimal normalization
IPriceOracle.sol
- Standard oracle interface
if utilization <= 80%:
rate = 2% + (utilization × 10%)
else:
rate = 2% + (80% × 10%) + ((utilization - 80%) × 50%)
This creates two slopes:
- 0-80% utilization: Gentle increase (2-10% APY)
- 80-100% utilization: Steep jump (10-52% APY)
The kink protects lender liquidity during high demand.
When collateral value falls below 80% of debt value:
- Anyone can liquidate up to 50% of the debt
- Liquidator repays borrowed tokens
- Receives equivalent collateral + 5% bonus
- Borrower retains remaining position
The 5% bonus incentivizes quick liquidations while being conservative enough to avoid unnecessary liquidations of marginally underwater positions.
Interest compounds continuously using:
newIndex = oldIndex × (1 + rate × timeElapsed / secondsPerYear)
Each user's debt scales by the market's borrow index, so interest accrues automatically without per-user storage updates. This pattern scales to thousands of users efficiently.
- Node.js v16+
- npm or yarn
- Testnet ETH (for deployment)
git clone https://github.com/yaminidesai/defi-lending-protocol
cd defi-lending-protocol
npm installnpx hardhat compilenpx hardhat test# Terminal 1: Start local node
npx hardhat node
# Terminal 2: Deploy contracts
npx hardhat run scripts/deploy.js --network localhost// Approve and deposit USDC
await usdc.approve(lendingPool.address, depositAmount);
await lendingPool.deposit(usdc.address, depositAmount);
// Withdraw anytime (if not used as collateral)
await lendingPool.withdraw(usdc.address, withdrawAmount);// Deposit ETH as collateral
await weth.approve(lendingPool.address, collateralAmount);
await lendingPool.deposit(weth.address, collateralAmount);
// Borrow USDC (up to 75% of collateral value)
await lendingPool.borrow(usdc.address, borrowAmount);
// Repay debt
await usdc.approve(lendingPool.address, repayAmount);
await lendingPool.repay(usdc.address, repayAmount);// Monitor for unhealthy positions
const health = await lendingPool.getAccountHealth(borrower);
if (health < 8000) { // Below 80%
await lendingPool.liquidate(
borrower,
borrowToken,
collateralToken,
repayAmount
);
}Unlike traditional finance, smart contracts cannot enforce legal recourse. The 75% collateral factor provides a buffer against volatility. Even with a 25% price drop, the protocol remains solvent.
Custom errors save approximately 50 gas per revert compared to require strings. For a protocol handling millions in TVL, this adds up significantly.
The oracle address is immutable to prevent admin rug-pulls. In production, this would be combined with governance-controlled oracle updates through a separate registry.
Single Oracle Source - Production systems should use multiple oracles with median pricing to prevent manipulation.
No Flash Loan Protection - While ReentrancyGuard helps, dedicated same-block borrow limits would be better.
Fixed Parameters - Collateral factors and liquidation thresholds are constants. These could be made governance-adjustable per asset.
No Pause Mechanism - Cannot emergency-stop in case of critical bug. This is intentional for true decentralization, but governance should have emergency powers.
Estimated costs on Ethereum mainnet (at 25 gwei):
| Operation | Gas | Cost (25 gwei) |
|---|---|---|
| Deposit | ~80k | ~$0.50 |
| Withdraw | ~70k | ~$0.44 |
| Borrow | ~120k | ~$0.75 |
| Repay | ~100k | ~$0.63 |
| Liquidate | ~150k | ~$0.94 |
contracts/
├── LendingPool.sol # Core protocol logic
├── PriceOracle.sol # Chainlink integration
└── interfaces/
└── IPriceOracle.sol # Oracle interface
test/
└── LendingPool.test.js # Test suite
scripts/
└── deploy.js # Deployment script
hardhat.config.js # Hardhat configuration
✅ ReentrancyGuard on all state-changing functions
✅ SafeERC20 for all token transfers
✅ Custom errors for gas efficiency
✅ Access control via Ownable
✅ Integer overflow protection (Solidity 0.8+)
✅ Price staleness checks
✅ Liquidation amount limits
Building this protocol taught me:
- How to handle precision in fixed-point math
- The importance of gas optimization in financial protocols
- Why oracle security is critical (price manipulation attacks)
- How interest rate curves affect market behavior
- The complexity of handling edge cases in liquidations
Most importantly: DeFi hacks rarely come from Solidity bugs—they come from economic exploits. Code security is table stakes; economic security is the hard part.
- Solidity 0.8.20 - Smart contract language
- Hardhat - Development environment
- OpenZeppelin - Security-audited contract libraries
- Chainlink - Decentralized price oracles
- ethers.js - Ethereum interaction library
- Aave V2 Documentation - Interest rate model
- Compound Whitepaper - Liquidation mechanics
- OpenZeppelin Contracts - Security patterns
- Patrick Collins Tutorials - Solidity fundamentals
- Flash loan functionality
- Variable collateral factors per asset
- Governance token and voting
- Protocol revenue distribution
- Cross-chain bridge integration
- Liquidity mining rewards
MIT License - Feel free to use this code for learning or building your own projects.
- GitHub: @yaminidesai
- Email: ydesai2401@gmail.com
** If this project helped you understand DeFi development, consider starring the repo! ⭐