isUsingAsCollateralOne, isUsingAsCollateralAny
isUsingAsCollateralAny
The function isUsingAsCollateralAny in Aave is used to determine if a user has supplied any reserve as collateral.
takes
UserConfigurationMapas input, which containsdata; bitmap of the user's collaterals and borrows.returns
FALSEif 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 and0s: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 indatathat corresponds to a0bit inCOLLATERAL_MASKwill be zeroed out, while the bits corresponding to1s in theCOLLATERAL_MASKwill 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_MASKwas explained above

collateralData: isolated collateral bits:1010...1010collateralData != 0 && (collateralData & (collateral - 1) == 0)
On collateralData & (collateral - 1)
collateralData & (collateral - 1) General form:
n & (n - 1)This trick is useful for figuring out if
nis either 0 or an exact power of two.
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:
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:
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.

Last updated
Was this helpful?