.validateBorrow
Overview

validateBorrow()

getFlags (status check)
L2 priceOracleSentinel check
InterestRateMode check (stable or variable)
Get decimals & borrowCap
Check borrowCap is not exceeded
If in Isolation Mode
check if asset is borrowable in Isolation mode and,
total exposure is less than the collateral debt ceiling
if in E-Mode
check reserve is in the same E-mode as user,
get eModePriceSource
CalculateUserAccountData:
HF, LTV, userDebt, userCollateral...
ensure account health is sound
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
If stable borrow -> check if requirements are met
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.
To understand
getDecimals
: see getDecimals under common functionsTo understand
getBorrowCap
: see SupplyCap, BorrowCap under common functions
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 inupdateState
)
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 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


See getReserveFactor, the process is similar.
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
If stable borrow -> check if requirements are met

reserve must be enabled for stable rate borrowing
(user should not be using reserve as collateral) || (LTV for reserve should be 0) || borrowAmount < user's Atoken balance
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
Was this helpful?