.validateBorrow

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

For in-depth explanation see: see getFlags

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.

  • To understand getDecimals: see getDecimals under common functions

  • To understand getBorrowCap: see SupplyCap, BorrowCap under common functions

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

  • For in-depth explanation see: see getFlags

If in E-Mode

calculateUserAccountData

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.

Last updated