👻
Aave Book
  • Introduction
  • TradFi vs DeFi: Lending
  • Market forces x Interest Rate Models
  • On Indexes
    • Why use indexes?
  • Scaling and ATokens
  • Deposit & Borrow Interest
  • Stable borrowing
    • 🚧Under construction
  • Liquidation
    • 🚧TODO: Full example
    • 🚧Under construction: oracles
  • Primer
    • Bitmap & Masks
      • 🚧padding and bytes
    • WadRayLibrary
      • Math Operations
      • 🚧WIP: Scaling different decimal representations
      • 🚧WIP: 2's complement
      • 🚧casting: to uint128
    • PercentageMath
    • Embedded vs Linked Libraries
  • Functions
    • General Execution flow
    • Architecture & Design choices
      • 🚧Upgradability and Proxies
    • Common Functions
      • getReserveFactor, getDecimals
      • .cache
      • .updateState
      • .updateInterestRates
      • SupplyCap, BorrowCap
      • getFlags
        • 🚧more on flags
      • calculateUserAccountData
    • supply
      • validateSupply
      • transfer & mint
      • isFirstSupply
        • isUsingAsCollateralOne, isUsingAsCollateralAny
      • On check-effects-interactions pattern
    • withdraw
      • get user balance & withdraw amount
      • validateWithdraw
      • collateral check
      • burn ATokens
      • Ensure existing loans are collateralized
    • borrow
      • getIsolationModeState
      • .validateBorrow
      • Mint debt token
      • setBorrowing
      • update IsolationMode debt
      • transfer underlying to user
    • repay
      • get current debt
      • validateRepay, paybackAmount
      • burn debt tokens
      • Cleanup + Collect repay
    • liquidate
      • _calculateDebt
      • validateLiquidationCall
      • getConfigurationData
      • calculateAvailableCollateralToLiquidate
      • 🚧_burnDebtTokens
      • liquidate/burn collateral
      • liquidation Fee
      • Wrap-up
    • 🚧swapBorrowRateMode
    • 🚧setUserUseReserveAsCollateral
  • Contracts
    • AToken
      • Simple example: mint & balanceOf
    • StableDebtToken
      • Implementation explained
    • VariableDebtToken
    • DefaultReserveInterestRateStrategy
    • L2
      • 🚧PriceOracleSentinel
  • Audit findings
    • 🚧Under construction
  • Appendix
    • Simple, Compound, APR, APY
  • Aave Features
    • Risk Management
      • Supply & Borrow Caps
      • Isolation Mode
      • Siloed Borrowing
    • Other features
      • Repay with ATokens
      • eMode: High efficiency Mode
      • 🚧Aave Vault
      • 🚧Portal
Powered by GitBook
On this page
  • Overview
  • validateBorrow()
  • getFlags
  • L2 priceOracleSentinel check
  • InterestRateMode check (stable or variable)
  • Get decimals & borrowCap
  • Check borrowCap
  • If in Isolation Mode
  • If in E-Mode
  • calculateUserAccountData
  • Check if new total debt can be supported by collateral
  • If stable borrow -> check if requirements are met
  • If user is already borrowing ANY reserve -> check if in siloed borrowing state

Was this helpful?

Edit on GitHub
  1. Functions
  2. borrow

.validateBorrow

PreviousgetIsolationModeStateNextMint debt token

Last updated 1 year ago

Was this helpful?

Overview

validateBorrow()

  1. getFlags (status check)

  2. L2 priceOracleSentinel check

  3. InterestRateMode check (stable or variable)

  4. Get decimals & borrowCap

  5. Check borrowCap is not exceeded

  6. If in Isolation Mode

    • check if asset is borrowable in Isolation mode and,

    • total exposure is less than the collateral debt ceiling

  7. if in E-Mode

    • check reserve is in the same E-mode as user,

    • get eModePriceSource

  8. CalculateUserAccountData:

    • HF, LTV, userDebt, userCollateral...

    • ensure account health is sound

  9. Check if new total debt can be supported by collateral

    1. Convert new borrow into Base CCY

    2. Calculate total collateral required in Base CCY

    3. check if sufficient collateral

  10. If stable borrow -> check if requirements are met

  11. If user is already borrowing ANY reserve -> check if in siloed borrowing state

getFlags

Asset must be

  • Active

  • NOT PAUSED

  • NOT FROZEN

  • Borrowing enabled

L2 priceOracleSentinel check

require(params.priceOracleSentinel == 
    address(0) ||     
    IPriceOracleSentinel(params.priceOracleSentinel).isBorrowAllowed(),        
    Errors.PRICE_ORACLE_SENTINEL_CHECK_FAILED);

priceOracleSentinel is for L2 markets, when sequencer fails/lags.

  • address(0) -> none set, since L1

  • if address set -> check that borrow is allowed

InterestRateMode check (stable or variable)

//validate interest rate mode
require(
  params.interestRateMode == DataTypes.InterestRateMode.VARIABLE ||
    params.interestRateMode == DataTypes.InterestRateMode.STABLE,
  Errors.INVALID_INTEREST_RATE_MODE_SELECTED
);

params.interestRateMode is an enum; representation of user input via UIUX.

  • either variable or stable must be selected

  • else transaction reverts with error

Get decimals & borrowCap

vars.reserveDecimals = params.reserveCache.reserveConfiguration.getDecimals();
vars.borrowCap = params.reserveCache.reserveConfiguration.getBorrowCap();
unchecked {
  vars.assetUnit = 10 ** vars.reserveDecimals;
}
  • retrieve the asset's decimals and borrow cap, for which the user wishes to borrow.

what assets have borrowCaps, and wen?

Once we have the decimals for the asset, we can define 1 unit of the asset in terms of decimals:

vars.assetUnit = 10 ** vars.reserveDecimals;

This is important because assets can have different definitions of 1 unit, due to inconsistent use of decimals:

  • Most typical ERC20 tokens: 1 unit = 1e18

  • USDC: 1 unit = 1e6

Check borrowCap

  • If (borrowCap != 0) {...check that its not exceeded with this new borrow action...}

  • if there is borrowCap set, vars.borrowCap will be some non-zero value, thereby moving to execute the code within the if block.

    if (vars.borrowCap != 0) {
      vars.totalSupplyVariableDebt = params.reserveCache.currScaledVariableDebt.rayMul(
        params.reserveCache.nextVariableBorrowIndex
      );
    
      vars.totalDebt =
        params.reserveCache.currTotalStableDebt +
        vars.totalSupplyVariableDebt +
        params.amount;
    
      unchecked {
        require(vars.totalDebt <= vars.borrowCap * vars.assetUnit, Errors.BORROW_CAP_EXCEEDED);
      }
    }

  • Check that borrowCap is not exceeded with latest variableBorrowIndex

  • currentDebt * newVariableIndex (index updated in updateState)

Ignores incoming borrow action:

  • only checks that preexisting debt compounded with the most recent interest does not exceed borrow cap.

  • If so, can move forward and validate the incoming borrow action.

If existing debt (variable + stable) accounting for latest interest, exceeds borrow cap -> to hell with your borrow

Remember that totalStableDebt was obtained in .cache via getSupplyData()

  • reserveCache.currTotalStableDebt = _calcTotalSupply(avgRate)

If in Isolation Mode

  • check if asset is borrowable in Isolation mode and,

  • total exposure is less than the collateral debt ceiling

  • Extracts bit 80 - 115 in the bitmap ReserveConfigurationMap

If in E-Mode

calculateUserAccountData

  • Serves to validate account health; healthFactor > 1

Check if new total debt can be supported by collateral

  • Convert new borrow into Base CCY

  • Calculate total collateral required in Base CCY

  • Check if sufficient collateral

What is the base currency on Aave?

  • It depends on the market, V2 ETH Mainnet and V2 Polygon use ETH-based oracles, and all other markets use USD-based oracles

  • Each market has an AaveOracle contract where you can query token prices in the base currency.

If stable borrow -> check if requirements are met

  1. reserve must be enabled for stable rate borrowing

  2. (user should not be using reserve as collateral) || (LTV for reserve should be 0) || borrowAmount < user's Atoken balance

  3. Users can only borrow only a portion of total available liquidity

User can only borrow up to 25% of availableLiquidity, in a single stable borrow transaction.

If user is already borrowing ANY reserve -> check if in siloed borrowing state

If the user already has a borrowing position, check if the user is in siloed borrowing mode.

If user is in siloed borrowing mode, ensure that they are borrowing more of the siloed asset, not some other asset.

For in-depth explanation see: see

To understand getDecimals: see under common functions

To understand getBorrowCap: see under common functions

For in-depth explanation see: see

See , the process is similar.

See

getFlags
getDecimals
SupplyCap, BorrowCap
getFlags
calculateUserAccountData
getReserveFactor
Drawing