Lecture 11: Decentralized Lending: Architecture and Security
Instructor: Yu Feng, UCSB
CS190: Blockchain Programming and Applications
Lecture 11 — Exploring how DeFi replaces traditional banking with smart contracts, examining over-collateralization, algorithmic interest rates, automated liquidation, and the critical oracle problem through the lens of protocols like Aave.
The Trustless Banking Problem
Traditional Finance (TradFi)
  • Centralized intermediaries arbitrate trust
  • Credit history and KYC verification
  • Legal contracts for recourse
  • Permissioned system
Decentralized Finance (DeFi)
  • Self-executing smart contracts
  • Open, permissionless access
  • Cryptographic certainty replaces institutional trust
  • Only requires wallet and internet

The Core Challenge: Smart contracts cannot access off-chain data or initiate legal action. DeFi must safely lend to anonymous users using only on-chain, verifiable assets—leading directly to over-collateralization.
The Digital Pawn Shop with Over-Collateralization
DeFi lending protocols use over-collateralization where borrowers pledge digital assets worth significantly more than the loan. This secures loans without traditional credit checks, as the loan is fully backed by verifiable on-chain assets.
Secured Lending
Loans are backed by more collateral than their value, ensuring security for lenders.
Anonymous & Instant
Enables global, permissionless access to liquidity without personal information or delays.
Liquidation Risk
Borrowers face automatic liquidation if collateral value drops below a set threshold due to market volatility.
Capital Inefficiency
Requires locking up more capital than received, making it less capital-efficient than traditional loans.
Mechanics of Over-Collateralization
Over-collateralization requires borrowers to pledge collateral worth more than the loan amount, creating a safety buffer against crypto asset price volatility and protecting protocols from bad debt.
Loan-to-Value Ratio
Defines maximum borrowing against collateral. Example: 80% LTV on ETH allows $800 loan against $1,000 collateral.
Volatility Protection
Lower LTV ratios for volatile assets. Set by protocol governance to manage risk exposure.
Primary Use Cases
Leveraged trading, accessing liquidity without selling assets, avoiding taxable events.

Capital Requirement: This model is impractical for users without significant capital. It primarily serves crypto-native participants executing on-chain strategies.
Algorithmic Interest Rates
In DeFi, interest rates aren't set by central authorities—they're determined algorithmically based on real-time market conditions. The core driver is the Utilization Rate (U).
Utilization Rate Formula
U = \frac{\text{Total Borrowed}}{\text{Total Available Liquidity}}
Acts as a real-time supply and demand signal:
  • Low U: Interest rates fall to encourage borrowing
  • High U: Interest rates rise to incentivize repayment and attract liquidity
80%
Typical Optimal Rate
Target utilization for most protocols
The Kinked Interest Rate Model
Protocols like Aave use a "kinked" model to balance capital efficiency with liquidity risk. The curve has two distinct slopes with a sharp transition at the optimal utilization rate.
Below Optimal (Gentle Slope)
Interest rates rise slowly to encourage borrowing and maximize capital efficiency.
Above Optimal (Steep Slope)
Interest rates surge sharply as a circuit breaker, preventing pool drainage and attracting new liquidity with high returns.
Automated Liquidation: The Protocol's Immune System
The Health Factor
The Health Factor (HF) is the primary safety metric used by protocols to quantify a borrower's position and determine liquidation risk. It's a real-time, on-chain value continuously monitored.
Health Factor Formula
HF = \frac{\text{Total Collateral Value} \times \text{Weighted Avg Liquidation Threshold}}{\text{Total Borrow Value}}
  • HF > 1: Safe position, well above liquidation threshold
  • HF ≤ 1: Under-collateralized, eligible for liquidation
Factors Affecting HF
  • Collateral asset value decreases
  • Borrowed asset value increases
  • Taking on additional debt
Risk Management: Borrowers can improve HF by supplying more collateral or repaying debt.
Health Factor Example
Consider a user on Aave who supplies $10,000 in ETH (liquidation threshold: 80%) and borrows $6,000 in GHO stablecoin.
1
Initial Position (Safe)
HF = \frac{\$10,000 \times 0.80}{\$6,000} \approx 1.333
Health Factor of 1.333 indicates a safe, well-collateralized position.
2
After ETH Price Drop (At Risk)
HF = \frac{\$7,000 \times 0.80}{\$6,000} \approx 0.933
ETH value drops to $7,000. Health Factor falls below 1—position is now under-collateralized and eligible for liquidation.
The Liquidation Process
Liquidation is an automated process executed by external profit-seeking actors called liquidators, who run bots to detect and capitalize on vulnerable positions.
01
Trigger Event
A borrower's Health Factor falls below 1, signaling under-collateralization.
02
Detection
Automated liquidator bots continuously monitor the blockchain and identify the vulnerable position.
03
Execution
A liquidator calls the protocol's liquidate() function, repaying the borrower's debt on their behalf.
04
Profit Incentive
The liquidator receives a portion of the borrower's collateral at a discount (the liquidation bonus), typically 5-10%.
Liquidation Example: Following the Money
Returning to our HF = 0.933 example ($7,000 ETH collateral, $6,000 GHO debt), with a 5% liquidation bonus:
The Liquidator
Spends: $6,000 GHO
Receives: $6,300 ETH
Profit: $300
The Protocol (LPs)
Recovers: $6,000 GHO debt repaid
Status: Remains solvent, no bad debt
The Borrower
Debt: Cleared to $0
Remaining: $700 ETH collateral
Lost: $6,300 to liquidation

Systemic Risk: During market crashes, simultaneous liquidations can create a "death spiral"—forced selling depresses prices further, triggering cascading liquidations.
The Oracle Dilemma
Bridging Blockchain to Reality
Blockchains are isolated systems—they cannot natively access external data like asset prices. This is the "Oracle Problem".
A blockchain oracle is a service that feeds external, real-world data to smart contracts. For lending protocols, oracles are mission-critical for valuing collateral and triggering liquidations.
Robust Oracle Design
  • Data Aggregation: Multiple independent sources
  • Time-Weighted Pricing: Resistant to short-term manipulation
  • Decentralized Networks: No single point of failure
Flash Loans: Zero-Collateral Borrowing
A flash loan allows users to borrow massive amounts with zero collateral, under one critical condition: the loan must be borrowed and repaid within the same atomic transaction.
Atomic Transactions
Blockchain transactions are all-or-nothing: every step succeeds, or the entire transaction reverts as if it never happened.
The Flash Loan Mechanism
The flash loan is one step in a larger transaction. At the end, the lending contract checks: "Did I get my money back?" If no, everything reverts—making it risk-free for lenders.
Legitimate Uses
Arbitrage opportunities, efficient liquidations, collateral swaps—powerful "money lego" for developers and traders.
Flash Loan Oracle Attack Vector
Flash loans combined with oracle vulnerabilities create a dangerous attack surface. Protocols relying on single-source or low-liquidity oracles become prime targets.
Figure: Flow of a flash loan oracle attack. All steps (1-5) execute within a single atomic transaction. If repayment fails, the entire transaction reverts.
Anatomy of an Oracle Manipulation Attack
Borrow Massive Capital
Attacker takes a flash loan of 20M USDC (or similar stablecoin) from a liquid protocol.
Manipulate Price Oracle
Uses funds to execute huge swap on low-liquidity DEX, artificially inflating price of Token X from $1 to $50.
Exploit Target Protocol
Deposits inflated Token X as collateral into lending protocol using manipulated DEX as oracle. Borrows maximum ETH against falsely valued collateral.
Repay & Profit
Repays original flash loan. Keeps stolen ETH. Price of Token X crashes back to $1. Protocol left with worthless collateral and massive bad debt.
Concrete Attack Example
All steps execute within a single atomic transaction:
1
Initial State
Protocol: $5M ETH. Small DEX as oracle. TokenX=$1. LTV: 75%.
2
Flash Loan
Attacker borrows 2M USDC (flash loan).
3
Price Manipulation
2M USDC buys TokenX on DEX. Price jumps $1 → $50.
4
Deposit Collateral
Deposits 100k TokenX, valued at $5M (100k x $50).
5
Steal Assets
Borrows $3.75M ETH (75% LTV) against inflated collateral.
6
Repay & Exit
Repays flash loan. TokenX crashes to $1. Protocol insolvent.

The Aftermath: Attacker profits $3.75M ETH. Protocol holds 100,000 TokenX worth only $100,000, leaving $3.65M bad debt.
Oracle Security: Mitigation Strategies
Secure protocols must defend against price manipulation attacks through robust oracle design:
Decentralized Oracle Networks (DONs)
Services like Chainlink aggregate price data from multiple independent sources, making manipulation exponentially more expensive and difficult.
Time-Weighted Average Price (TWAP)
Calculate average price over time window (e.g., 30 minutes). Highly resistant to short-term price spikes and single-transaction manipulation attempts.
Multi-Source Validation
Require price agreement across multiple independent oracles before accepting value. Detect and reject outlier data points automatically.
Case Study: Aave Protocol
Aave is a leading decentralized, non-custodial liquidity protocol and a cornerstone of the DeFi ecosystem. Known for its feature-rich, flexible design catering to advanced users and developers seeking maximum capital efficiency.
Flexible Interest Rates
Users choose between variable and stable interest rates, enabling sophisticated risk management strategies unavailable in traditional finance.
Flash Loan Pioneer
Aave pioneered uncollateralized loans within single transactions—now a fundamental "money lego" for arbitrage, swaps, and liquidation strategies.
Broad Asset Support
Supports diverse range of assets including volatile and niche tokens, providing greater flexibility for collateral and borrowing compared to conservative platforms.
References
  1. Aave Protocol. 2025. Aave Documentation. https://docs.aave.com/faq/ Accessed: 2025-10-01.
  1. Robert Leshner and Geoffrey Hayes. 2019. Compound: The Money Market Protocol. unknown link Accessed: 2025-10-01.
  1. Sergey Nazarov, Steve Ellis, Ari Juels, et al. 2017. Chainlink: A Decentralized Oracle Network. https://chain.link/whitepaper Accessed: 2025-10-01.
  1. Uniswap Labs. 2020. Uniswap v2: Oracle. https://docs.uniswap.org/contracts/v2/concepts/advanced-topics/oracles Accessed: 2025-10-01.