Skip to content

rounceydev/lending-protocol

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

6 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

EVM DeFi Lending Protocol

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.

πŸ“‹ Table of Contents

✨ Features

Core Functionality

  • 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

Technical Features

  • 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

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         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         β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Contract Components

  1. Pool: Main contract handling all user interactions
  2. AToken: Interest-bearing tokens representing supplied assets
  3. VariableDebtToken: Tokens representing variable-rate debt
  4. StableDebtToken: Tokens representing stable-rate debt
  5. DefaultReserveInterestRateStrategy: Calculates interest rates based on utilization
  6. PriceOracle: Provides asset prices for collateral valuation

πŸ“ Project Structure

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

πŸš€ Setup

Prerequisites

  • Node.js (v16 or higher)
  • npm or yarn
  • Git

Installation

  1. Clone the repository:
cd lending-protocol
  1. Install dependencies:
npm install
# or
yarn install
  1. Create a .env file (optional, for testnet deployment):
cp .env.example .env

Edit .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
  1. Compile the contracts:
npx hardhat compile

πŸ§ͺ Testing

Run the test suite:

npx hardhat test

Run specific test files:

npx hardhat test test/Pool.test.js
npx hardhat test test/AToken.test.js
npx hardhat test test/InterestRateStrategy.test.js

Run tests with gas reporting:

REPORT_GAS=true npx hardhat test

Test Coverage

The test suite covers:

  • βœ… Asset supplying and withdrawal
  • βœ… Borrowing and repayment
  • βœ… Interest rate calculations
  • βœ… Flash loans
  • βœ… Liquidation mechanics
  • βœ… Edge cases (zero amounts, insufficient balance, etc.)

🚒 Deployment

Local Network

  1. Start a local Hardhat node:
npx hardhat node
  1. In another terminal, deploy to localhost:
npx hardhat run scripts/deploy.js --network localhost

Testnet Deployment (Sepolia)

  1. Ensure your .env file is configured with:

    • PRIVATE_KEY: Your wallet private key
    • SEPOLIA_RPC_URL: Sepolia RPC endpoint
    • ETHERSCAN_API_KEY: Etherscan API key (for verification)
  2. Deploy to Sepolia:

npx hardhat run scripts/deploy.js --network sepolia
  1. Verify contracts (optional):
npx hardhat verify --network sepolia <CONTRACT_ADDRESS> <CONSTRUCTOR_ARGS>

πŸ“§ Contact

Releases

No releases published

Packages

 
 
 

Contributors