Aave V3 Protocol DocumentationΒΆ

Comprehensive control flow diagrams and amount transformations for debugging Aave V3 Market operations.

Quick NavigationΒΆ

Core Operation FlowsΒΆ

Operation

Entry Point

Key Transformation

Supply

Pool.supply(asset, amount, onBehalfOf, referralCode)

amount β†’ scaledBalance

Withdraw

Pool.withdraw(asset, amount, to)

scaledBalance β†’ amount

Borrow

Pool.borrow(asset, amount, interestRateMode, referralCode, onBehalfOf)

amount β†’ scaledDebt

Repay

Pool.repay(asset, amount, interestRateMode, onBehalfOf)

scaledDebt β†’ amount

Liquidation

Pool.liquidationCall(collateralAsset, debtAsset, user, debtToCover, receiveAToken)

debtValue β†’ collateralAmount

Amount Transformations ReferenceΒΆ


For Automated AgentsΒΆ

Debugging WorkflowΒΆ

  1. Identify the operation being investigated (supply, borrow, liquidation, etc.)

  2. Load the flow file for that operation to see the execution path

  3. Follow the transformations - refer to the transformations/index.md for detailed math

  4. Check validation - each flow shows all validation checks and error conditions

  5. Verify state changes - see exactly what storage is modified

Common Debugging PatternsΒΆ

**Investigating balance discrepancy:**
1. Check [Collateral Transformations](./transformations/index.md#collateral-token-transformations)
2. Verify rounding mode (v1-3 vs v4+)
3. Check index at specific block height
4. Look for treasury accruals

**Investigating liquidation:**
1. Check [Liquidation Flow](./flows/liquidation.md)
2. Verify health factor calculation in [E-Mode](./transformations/index.md#e-mode-calculations)
3. Check collateral conversion math
4. Verify close factor logic

**Investigating failed transaction:**
1. Find the operation flow
2. Check "Error Conditions" section
3. Match error to validation check
4. See validation logic in Solidity

Solidity Source MappingΒΆ

Each flow file includes:

  • File paths to actual contract source

  • Function names with line-level references

  • Complete code snippets showing actual implementation

  • Transformation markers linking to detailed math

Example:

// File: contracts/protocol/libraries/logic/SupplyLogic.sol
// Function: executeSupply()

uint256 scaledAmount = amount.rayDiv(index);  // [TRANSFORMATION]
// Refer to transformations/index.md for:
// - Exact formula
// - Solidity implementation
// - Version differences
// - Edge cases

Document StructureΒΆ

docs/aave/
β”œβ”€β”€ README.md                          # This file - navigation hub
β”œβ”€β”€ AGENTS.md                          # Instructions for automated agents
β”œβ”€β”€ flows/                             # Individual operation flows
β”‚   β”œβ”€β”€ supply.md                      # Supply execution flow
β”‚   β”œβ”€β”€ supply_with_permit.md           # Supply with permit
β”‚   β”œβ”€β”€ withdraw.md                    # Withdraw execution flow
β”‚   β”œβ”€β”€ borrow.md                      # Borrow execution flow
β”‚   β”œβ”€β”€ repay.md                       # Repay execution flow
β”‚   β”œβ”€β”€ repay_with_atokens.md          # Repay with aTokens
β”‚   β”œβ”€β”€ repay_with_permit.md           # Repay with permit
β”‚   β”œβ”€β”€ liquidation.md                 # Liquidation execution flow
β”‚   β”œβ”€β”€ collateral_management.md       # Collateral enable/disable
β”‚   β”œβ”€β”€ emode_management.md            # E-Mode category management
β”‚   β”œβ”€β”€ flash_loan.md                  # Flash loan (with callback)
β”‚   β”œβ”€β”€ flash_loan_simple.md           # Simple flash loan
β”‚   β”œβ”€β”€ gho_borrowing.md               # GHO borrowing
β”‚   β”œβ”€β”€ gho_discount.md                # GHO discount mechanism
β”‚   β”œβ”€β”€ rewards_claiming.md            # Rewards claiming
β”‚   β”œβ”€β”€ position_manager.md            # Position manager operations
β”‚   β”œβ”€β”€ eliminate_deficit.md           # Umbrella deficit elimination
β”‚   β”œβ”€β”€ stk_aave_staking.md           # stkAAVE staking
β”‚   β”œβ”€β”€ stk_aave_unstaking.md          # stkAAVE unstaking
β”‚   └── stk_aave_slashing.md           # stkAAVE slashing
└── transformations/                   # Amount transformation reference
    └── index.md                       # All math operations

Key ConceptsΒΆ

Scaled Balances vs. Unscaled AmountsΒΆ

Collateral (aTokens):

  • Storage: Scaled balance (_scaledBalance[user])

  • Precision: RAY (27 decimals)

  • Conversion: unscaled = scaled.rayMul(liquidityIndex)

  • Interest: Automatic via index growth

Variable Debt:

  • Storage: Scaled balance (_scaledBalance[user])

  • Precision: RAY (27 decimals)

  • Conversion: unscaled = scaled.rayMul(borrowIndex)

  • Interest: Automatic via index growth

Stable Debt:

  • Storage: Principal + timestamp

  • Precision: WAD (18 decimals)

  • Conversion: Calculate interest on-demand

  • Interest: Compounded at borrow/repay time

Index UpdatesΒΆ

// Liquidity Index (for collateral)
liquidityIndex[t] = liquidityIndex[t-1].rayMul(1 + rate * timeDelta / YEAR)

// Borrow Index (for variable debt)
borrowIndex[t] = borrowIndex[t-1].rayMul(compoundInterest(rate, timeDelta))

See Interest Accrual for full details.

Rounding ModesΒΆ

Operation

v1-3

v4+

Reason

aToken Mint

rayDiv (half-up)

rayDivFloor

Prevent rounding up debt

aToken Burn

rayDiv (half-up)

rayDivCeil

Ensure full repayment

vToken Mint

rayDiv (half-up)

rayDivCeil

Round up debt issued

vToken Burn

rayDiv (half-up)

rayDivFloor

Round down debt reduction

See Version Differences.


Aave V3 Contract Addresses (Mainnet)ΒΆ

Pool Proxy:           0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2
PoolAddressesProvider: 0x2f39d218133AFaB8F2B819B1066c7E434Ad94E9e
Oracle:               0x54586bE62E3c3580375aE3723C145253060Ca0C2

ValidationΒΆ

All Mermaid diagrams in this documentation are validated using Maid:

# Validate all diagrams
npx maid .

# Auto-fix issues
npx maid --fix .

ContributingΒΆ

When adding new flows or transformations:

  1. Include complete Solidity code snippets

  2. Add links between flows and transformations

  3. Document all error conditions

  4. Show version differences where applicable

  5. Run npx maid --fix before committing