A complete Solidity-based DeFi lending protocol inspired by Aave V3, built on EVM-compatible blockchains. This protocol enables users to supply assets as collateral, borrow against their collateral, and earn interest on supplied assets.
- Asset Supplying: Users can supply ERC20 tokens to earn interest
- Borrowing: Users can borrow assets against their supplied collateral
- Variable Interest Rates: Dynamic interest rates based on utilization
- Flash Loans: Uncollateralized loans within a single transaction
- Liquidation: Automatic liquidation of undercollateralized positions
- Interest Accrual: Real-time interest accrual for both suppliers and borrowers
- Multiple Reserves: Support for multiple ERC20 assets (DAI, USDC, WETH, etc.)
- Price Oracles: Integration with price oracles for collateral valuation
- Access Control: Role-based access control for admin functions
- Pausability: Emergency pause functionality
- Reentrancy Protection: Guards against reentrancy attacks
- Gas Optimized: Efficient storage and computation patterns
┌─────────────────────────────────────────────────────────────┐
│ Users │
└────────────────────┬────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Pool Contract │
│ (Main entry point for all user interactions) │
│ - supply() │
│ - withdraw() │
│ - borrow() │
│ - repay() │
│ - flashLoan() │
│ - liquidationCall() │
└──────┬──────────────┬──────────────┬────────────────────────┘
│ │ │
▼ ▼ ▼
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ AToken │ │ VariableDebt│ │ StableDebt │
│ (Supply) │ │ Token │ │ Token │
└─────────────┘ └─────────────┘ └─────────────┘
│ │ │
└──────────────┴──────────────┘
│
┌─────────────┴─────────────┐
│ │
▼ ▼
┌──────────────┐ ┌──────────────────┐
│ Price Oracle │ │ Interest Rate │
│ │ │ Strategy │
└──────────────┘ └──────────────────┘
- Pool: Main contract handling all user interactions
- AToken: Interest-bearing tokens representing supplied assets
- VariableDebtToken: Tokens representing variable-rate debt
- StableDebtToken: Tokens representing stable-rate debt
- DefaultReserveInterestRateStrategy: Calculates interest rates based on utilization
- PriceOracle: Provides asset prices for collateral valuation
evm-defi-lending-protocol/
├── contracts/
│ ├── interfaces/
│ │ ├── IPool.sol
│ │ ├── IAToken.sol
│ │ ├── IDebtToken.sol
│ │ ├── IPriceOracle.sol
│ │ ├── IInterestRateStrategy.sol
│ │ └── IFlashLoanReceiver.sol
│ ├── libraries/
│ │ ├── ReserveConfiguration.sol
│ │ ├── WadRayMath.sol
│ │ └── MathUtils.sol
│ ├── protocol/
│ │ ├── pool/
│ │ │ ├── Pool.sol
│ │ │ └── DefaultReserveInterestRateStrategy.sol
│ │ ├── tokenization/
│ │ │ ├── AToken.sol
│ │ │ ├── VariableDebtToken.sol
│ │ │ └── StableDebtToken.sol
│ │ ├── oracle/
│ │ │ └── PriceOracle.sol
│ │ └── libraries/
│ │ └── types/
│ │ └── DataTypes.sol
│ └── mocks/
│ ├── MockERC20.sol
│ ├── MockDAI.sol
│ ├── MockUSDC.sol
│ ├── MockWETH.sol
│ ├── MockChainlinkAggregator.sol
│ └── MockFlashLoanReceiver.sol
├── scripts/
│ └── deploy.js
├── test/
│ ├── Pool.test.js
│ ├── AToken.test.js
│ └── InterestRateStrategy.test.js
├── hardhat.config.js
├── helper-hardhat-config.js
├── package.json
└── README.md
- Node.js (v16 or higher)
- npm or yarn
- Git
- Clone the repository:
cd lending-protocol- Install dependencies:
npm install
# or
yarn install- Create a
.envfile (optional, for testnet deployment):
cp .env.example .envEdit .env and add your private key and RPC URLs:
PRIVATE_KEY=your_private_key_here
SEPOLIA_RPC_URL=https://sepolia.infura.io/v3/your_key
ETHERSCAN_API_KEY=your_etherscan_api_key
- Compile the contracts:
npx hardhat compileRun the test suite:
npx hardhat testRun specific test files:
npx hardhat test test/Pool.test.js
npx hardhat test test/AToken.test.js
npx hardhat test test/InterestRateStrategy.test.jsRun tests with gas reporting:
REPORT_GAS=true npx hardhat testThe test suite covers:
- ✅ Asset supplying and withdrawal
- ✅ Borrowing and repayment
- ✅ Interest rate calculations
- ✅ Flash loans
- ✅ Liquidation mechanics
- ✅ Edge cases (zero amounts, insufficient balance, etc.)
- Start a local Hardhat node:
npx hardhat node- In another terminal, deploy to localhost:
npx hardhat run scripts/deploy.js --network localhost-
Ensure your
.envfile is configured with:PRIVATE_KEY: Your wallet private keySEPOLIA_RPC_URL: Sepolia RPC endpointETHERSCAN_API_KEY: Etherscan API key (for verification)
-
Deploy to Sepolia:
npx hardhat run scripts/deploy.js --network sepolia- Verify contracts (optional):
npx hardhat verify --network sepolia <CONTRACT_ADDRESS> <CONSTRUCTOR_ARGS>- Telegram: https://t.me/rouncey
- Twitter: https://x.com/rouncey_