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_