👻
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
  • isUsingAsCollateralAny
  • isUsingCollateralOne
  • On collateralData & (collateral - 1)

Was this helpful?

Edit on GitHub
  1. Functions
  2. supply
  3. isFirstSupply

isUsingAsCollateralOne, isUsingAsCollateralAny

PreviousisFirstSupplyNextOn check-effects-interactions pattern

Last updated 1 year ago

Was this helpful?

isUsingAsCollateralAny

The function isUsingAsCollateralAny in Aave is used to determine if a user has supplied any reserve as collateral.

  • takes UserConfigurationMap as input, which contains data; bitmap of the user's collaterals and borrows.

  • returns FALSE if user has NOT supplied ANY asset so far

  • COLLATERAL_MASK, is used to isolate the collateral bits within data.

  • In binary representation, it consists of alternating 1s and 0s: 1010...1010.

  • It creates a bit pattern that selectively zeroes out the non-collateral bits in the data field.

  • When performing a bitwise AND operation with data, any bit in data that corresponds to a 0 bit in COLLATERAL_MASK will be zeroed out, while the bits corresponding to 1s in the COLLATERAL_MASK will be preserved.

To check if the user is using any reserve as collateral, the function performs a bitwise AND operation between data and COLLATERAL_MASK.

If the result of this operation is not zero, it means that at least one bit in the collateral position is set, indicating that the user has supplied a reserve as collateral.

isUsingCollateralOne

  • usage of COLLATERAL_MASK was explained above

  • collateralData: isolated collateral bits: 1010...1010

  • collateralData != 0 && (collateralData & (collateral - 1) == 0)

On collateralData & (collateral - 1)

  • General form: n & (n - 1)

  • This trick is useful for figuring out if n is either 0 or an exact power of two.

// returns 0/false if n is a power of 2 (only works for n > 0)
function isPowerOfTwo() returns(bool) {
    return (n > 0) && ((n & (n - 1)) == 0);
}

It works because a binary power of two is of the form 1000...000 and subtracting one will give you 111...111. Then, when you AND those together, you get zero:

  1000 0000 0000 0000  (n)
&  111 1111 1111 1111  (n - 1)
  ==== ==== ==== ====
= 0000 0000 0000 0000

Any non-power-of-two input value (other than zero) will not give you zero when you perform that operation. For example, let's try all the 4-bit combinations:

    <------ binary ----->
 n      n    n-1   n&(n-1)
--   ----   ----   -------
 0   0000   0111    0000 *
 1   0001   0000    0000 *
 2   0010   0001    0000 *
 3   0011   0010    0010
 4   0100   0011    0000 *
 5   0101   0100    0100
 6   0110   0101    0100
 7   0111   0110    0110
 8   1000   0111    0000 *
 9   1001   1000    1000
10   1010   1001    1000
11   1011   1010    1010
12   1100   1011    1000
13   1101   1100    1100
14   1110   1101    1100
15   1111   1110    1110

You can see that only 0 and the powers of two (1, 2, 4 and 8) result in a 0000/false bit pattern, all others are non-zero or true.

Drawing