# Understanding Aave V2 Code (1/n) - Key Concepts & Repo Overview ## TLDR - **Aave V2 = Decentralized Bank**: Lenders earn interest, borrowers get loans with collateral - **Three Token System**: aTokens (deposits), Variable Debt (flexible rate), Stable Debt (locked rate) - **Architecture**: LendingPool orchestrates everything → Libraries handle logic → Tokens track positions - **Why Study Aave V2**: Core Defi primitive, battle-tested protocol, rich security lessons (flash loans, oracle attacks, MEV) - **Series Overview**: Deposit & Supply Mechanism, Borrow & Health Factor, Liquidations, Flash Loans, Interest Rate Model ## What's Aave V2? Aave V2 is a decentralized lending borrowing protocol that allows users to lend and borrow crypto without intermediaries. Think of it as a decentralized bank where: - Lenders deposit assets and earn interest automatically - Borrowers take loans by providing collateral - Liquidators keep the system healthy by liquidating risky positions ![defi_lendborrow](https://hackmd.io/_uploads/r1BjhejbZe.png)(Source: [Berkeley Defi Lecture 6](https://github.com/berkeley-defi/berkeley-defi.github.io/blob/main/assets/material/Lecture%206%20Slides.pdf)) ## Why Study Aave V2? - Core Defi Primitive - Lending is fundamental, apart from DEX - Swap + Lending = 80% of Defi building blocks - Battle-Tested Codebase - Aave v2 has $10b+ in TVL at its peak and survived multiple market cycles - Code was [audited](https://github.com/aave/protocol-v2/tree/master/audits) extensively by multiple 3rd parties - Rich Learning Material for Security - Flash loans attack - Reentrancy protection - Oracle manipulation that affect liquidation - Interest rate exploits - Liquidation MEV - Simpler to Understand Compared to V3 ## High-Level Flow of Aave V2 ``` ┌─────────────────────────────────────────────────────────────────────┐ │ EXTERNAL USERS │ │ (Depositors, Borrowers, Liquidators) │ └────────────┬─────────────────────────┬──────────────────┬───────────┘ │ │ │ ▼ ▼ ▼ ┌────────────────┐ ┌──────────────────┐ ┌────────────────-┐ │ deposit() │ │ borrow() │ │liquidationCall()│ │ withdraw() │ │ repay() │ │ │ │ │ │ swapBorrowRate │ │ │ └────────┬───────┘ └────────┬─────────┘ └────────┬──────-─┘ │ │ │ └───────────────────────┼──────────────────────┘ ▼ ┌────────────────────────────────┐ │ LendingPool.sol │ │ ⭐ (Main Entry Point) │ │ │ │ - Orchestrates all actions │ │ - Calls validation logic │ │ - Updates reserve state │ │ - Transfers tokens │ │ - Emits events │ └────────────┬───────────────────┘ │ ┌────────────────────────┼────────────────────────┐ │ │ │ ▼ ▼ ▼ ┌──────────────┐ ┌──────────────┐ ┌───────────────┐ │ LIBRARIES │ │ TOKENIZATION │ │ CONFIGURATION │ │ │ │ │ │ │ │• Validation │ │• AToken │ │• Interest Rate│ │• Reserve │ │• Debt Tokens │ │ Strategy │ │• Borrow │ │• Delegation │ │• Provider │ │• Liquidation │ │ │ │ │ │• Generic │ │ │ │ │ │• Deposit │ │ │ │ │ └──────────────┘ └──────────────┘ └───────────────┘ │ │ │ └──────────────────────┼──────────────────────┘ ▼ ┌────────────────────────────────┐ │ STORAGE (LendingPool) │ │ │ │ • _reserves[asset] │ │ • _usersConfig[user] │ │ • _reservesList[] │ │ • _usersLiquidationHistory[] │ └────────────────────────────────┘ ``` ## Core Concepts to Know Before Reading Code ### Three Token Types (unique to Aave) - **aToken** - Interest-bearing deposit receipts - **Variable Debt Token** - Track variable rate borrowing - **Stable Debt Token** - Track stable rate borrowing ### Key Data Structures - **Reserve** - All data about one asset - Liquidity/ Borrow indices - Interest rates strategy - aToken address - Configuration - **User Configuration** - Bitmap tracking which assets user deposited/ borrowed - **Interest Rate Mode** - 3 rates mode - None - Stable: lock in the rate at borrow time - Variable: flutuate with market condition ## Repository Structure Overview ``` contracts/ │ ├── protocol/ ← MAIN PROTOCOL - Core DeFi Logic │ │ │ ├── lendingpool/ ← Lending Pool Core │ │ ├── LendingPool.sol ← ⭐⭐ ENTRY POINT - All user functions │ │ ├── LendingPoolStorage.sol ← State Container │ │ └── LendingPoolConfigurator.sol ← Admin Configuration │ ├── libraries/ ← Business Logic Libraries │ │ │ (Called by LendingPool) │ │ ├── logic/ ← Core Protocol Logic │ │ │ ├── ReserveLogic.sol ← ⭐ Interest Calculations │ │ │ ├── ValidationLogic.sol ← Input Validation │ │ │ ├── GenericLogic.sol ← ⭐ Account Health │ │ │ ├── BorrowLogic.sol ← Borrow Operations │ │ │ ├── DepositLogic.sol ← Deposit Operations │ │ │ └── LiquidationLogic.sol ← Liquidation Logic │ │ ├── configuration/ ← Configuration Management │ │ │ ├── ReserveConfiguration.sol ← ⭐ Bitmap Encoding │ │ │ └── UserConfiguration.sol ← User Bitmap Tracking │ │ ├── types/ ← Data Structure Definitions │ │ │ └── DataTypes.sol ← ⭐ All Structs │ │ ├── math/ ← Math Utilities (Fixed-Point) │ │ │ ├── WadRayMath.sol ← ⭐ Precision Math │ │ │ ├── PercentageMath.sol ← Percentage Calculations │ │ │ └── MathUtils.sol ← Utility Functions │ │ ├── helpers/ ← Helper Utilities │ │ └── aave-upgradeability/ ← Upgradeability (Proxy Pattern) │ ├── tokenization/ ← Token Contracts (ERC20 Based) │ │ ├── AToken.sol ← ⭐ Interest-Bearing Token │ │ ├── VariableDebtToken.sol ← Variable Rate Debt Token │ │ ├── StableDebtToken.sol ← Stable Rate Debt Token │ │ ├── IncentivizedERC20.sol ← Base ERC20 with Incentives │ │ └── base/ ← Base Contracts │ └── configuration/ ← Interest Rate Configuration │ └── DefaultReserveInterestRateStrategy.sol ← ⭐ Interest Rate Model ├── flashloan/ ← Flash Loan Functionality ├── misc/ ← Miscellaneous Helper Contracts │ ├── AaveOracle.sol ← ⭐ Price Oracle │ └── [Other docs] ├── interfaces/ ← External Interfaces (ABIs) │ ├── ILendingPool.sol - Interface for LendingPool │ └── [Other interfaces] ├── mocks/ ← Testing & Mock Contracts ├── dependencies/ ← External Dependencies └── adapters/ ← Protocol Adapters (Optional) ``` ## Aave V2 Series Overview - Part 0: Aave V2 Repo Overview - Part 1: Deposit & Supply Mechanism - Part 2: Borrow & Health Factor - Part 3: Liquidations - Part 4: Flash Loans - Part 5: Interest Rate Model ## Reference - Aave v2 documentation: https://aave.com/docs/resources/legacy-versions/v2 - Aave v2 github repo: https://github.com/aave/protocol-v2 ## Discussion - Twitter: [@chloe_zhuX](https://x.com/Chloe_zhuX) - Telegram: [@Chloe_zhu](https://t.me/chloe_zhu) - GitHub: [@Chloezhu010](https://github.com/Chloezhu010) --- *Last updated: Dec 1st, 2025* *Part of my #LearnInPublic Defi series*