2020-08-05 10:40:24 +00:00
|
|
|
// SPDX-License-Identifier: agpl-3.0
|
|
|
|
pragma solidity ^0.6.8;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @title UserConfiguration library
|
|
|
|
* @author Aave
|
|
|
|
* @notice Implements the bitmap logic to handle the user configuration
|
|
|
|
*/
|
|
|
|
library UserConfiguration {
|
2020-10-14 07:53:48 +00:00
|
|
|
uint256 internal constant BORROWING_MASK = 0x5555555555555555555555555555555555555555555555555555555555555555;
|
2020-08-05 10:40:24 +00:00
|
|
|
|
|
|
|
struct Map {
|
|
|
|
uint256 data;
|
|
|
|
}
|
|
|
|
|
2020-08-06 07:57:12 +00:00
|
|
|
/**
|
2020-08-21 12:52:10 +00:00
|
|
|
* @dev sets if the user is borrowing the reserve identified by reserveIndex
|
|
|
|
* @param self the configuration object
|
|
|
|
* @param reserveIndex the index of the reserve in the bitmap
|
|
|
|
* @param borrowing true if the user is borrowing the reserve, false otherwise
|
2020-08-19 15:56:51 +00:00
|
|
|
**/
|
2020-08-05 22:46:22 +00:00
|
|
|
function setBorrowing(
|
2020-08-21 12:52:10 +00:00
|
|
|
UserConfiguration.Map storage self,
|
|
|
|
uint256 reserveIndex,
|
|
|
|
bool borrowing
|
2020-08-05 22:46:22 +00:00
|
|
|
) internal {
|
2020-08-21 12:52:10 +00:00
|
|
|
self.data =
|
|
|
|
(self.data & ~(1 << (reserveIndex * 2))) |
|
|
|
|
(uint256(borrowing ? 1 : 0) << (reserveIndex * 2));
|
2020-08-05 10:40:24 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 07:57:12 +00:00
|
|
|
/**
|
2020-08-21 12:52:10 +00:00
|
|
|
* @dev sets if the user is using as collateral the reserve identified by reserveIndex
|
|
|
|
* @param self the configuration object
|
|
|
|
* @param reserveIndex the index of the reserve in the bitmap
|
2020-08-19 15:56:51 +00:00
|
|
|
* @param _usingAsCollateral true if the user is usin the reserve as collateral, false otherwise
|
|
|
|
**/
|
2020-08-05 22:46:22 +00:00
|
|
|
function setUsingAsCollateral(
|
2020-08-21 12:52:10 +00:00
|
|
|
UserConfiguration.Map storage self,
|
|
|
|
uint256 reserveIndex,
|
2020-08-05 22:46:22 +00:00
|
|
|
bool _usingAsCollateral
|
|
|
|
) internal {
|
2020-08-21 12:52:10 +00:00
|
|
|
self.data =
|
|
|
|
(self.data & ~(1 << (reserveIndex * 2 + 1))) |
|
|
|
|
(uint256(_usingAsCollateral ? 1 : 0) << (reserveIndex * 2 + 1));
|
2020-08-05 10:40:24 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 07:57:12 +00:00
|
|
|
/**
|
2020-08-19 15:56:51 +00:00
|
|
|
* @dev used to validate if a user has been using the reserve for borrowing or as collateral
|
2020-08-21 12:52:10 +00:00
|
|
|
* @param self the configuration object
|
|
|
|
* @param reserveIndex the index of the reserve in the bitmap
|
2020-08-19 15:56:51 +00:00
|
|
|
* @return true if the user has been using a reserve for borrowing or as collateral, false otherwise
|
|
|
|
**/
|
2020-08-21 12:52:10 +00:00
|
|
|
function isUsingAsCollateralOrBorrowing(UserConfiguration.Map memory self, uint256 reserveIndex)
|
2020-08-05 22:46:22 +00:00
|
|
|
internal
|
2020-08-21 12:52:10 +00:00
|
|
|
pure
|
2020-08-05 22:46:22 +00:00
|
|
|
returns (bool)
|
|
|
|
{
|
2020-08-21 12:52:10 +00:00
|
|
|
return (self.data >> (reserveIndex * 2)) & 3 != 0;
|
2020-08-05 10:40:24 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 07:57:12 +00:00
|
|
|
/**
|
2020-08-19 15:56:51 +00:00
|
|
|
* @dev used to validate if a user has been using the reserve for borrowing
|
2020-08-21 12:52:10 +00:00
|
|
|
* @param self the configuration object
|
|
|
|
* @param reserveIndex the index of the reserve in the bitmap
|
2020-08-19 15:56:51 +00:00
|
|
|
* @return true if the user has been using a reserve for borrowing, false otherwise
|
|
|
|
**/
|
2020-08-21 12:52:10 +00:00
|
|
|
function isBorrowing(UserConfiguration.Map memory self, uint256 reserveIndex)
|
2020-08-05 22:46:22 +00:00
|
|
|
internal
|
2020-08-21 12:52:10 +00:00
|
|
|
pure
|
2020-08-05 22:46:22 +00:00
|
|
|
returns (bool)
|
|
|
|
{
|
2020-08-21 12:52:10 +00:00
|
|
|
return (self.data >> (reserveIndex * 2)) & 1 != 0;
|
2020-08-05 10:40:24 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 07:57:12 +00:00
|
|
|
/**
|
2020-08-19 15:56:51 +00:00
|
|
|
* @dev used to validate if a user has been using the reserve as collateral
|
2020-08-21 12:52:10 +00:00
|
|
|
* @param self the configuration object
|
|
|
|
* @param reserveIndex the index of the reserve in the bitmap
|
2020-08-19 15:56:51 +00:00
|
|
|
* @return true if the user has been using a reserve as collateral, false otherwise
|
|
|
|
**/
|
2020-08-21 12:52:10 +00:00
|
|
|
function isUsingAsCollateral(UserConfiguration.Map memory self, uint256 reserveIndex)
|
2020-08-05 22:46:22 +00:00
|
|
|
internal
|
2020-08-21 12:52:10 +00:00
|
|
|
pure
|
2020-08-05 22:46:22 +00:00
|
|
|
returns (bool)
|
|
|
|
{
|
2020-08-21 12:52:10 +00:00
|
|
|
return (self.data >> (reserveIndex * 2 + 1)) & 1 != 0;
|
2020-08-05 10:40:24 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 07:57:12 +00:00
|
|
|
/**
|
2020-08-19 15:56:51 +00:00
|
|
|
* @dev used to validate if a user has been borrowing from any reserve
|
2020-08-21 12:52:10 +00:00
|
|
|
* @param self the configuration object
|
2020-08-19 15:56:51 +00:00
|
|
|
* @return true if the user has been borrowing any reserve, false otherwise
|
|
|
|
**/
|
2020-08-21 12:52:10 +00:00
|
|
|
function isBorrowingAny(UserConfiguration.Map memory self) internal pure returns (bool) {
|
|
|
|
return self.data & BORROWING_MASK != 0;
|
2020-08-05 10:40:24 +00:00
|
|
|
}
|
2020-08-05 22:46:22 +00:00
|
|
|
|
2020-08-06 07:57:12 +00:00
|
|
|
/**
|
2020-08-19 15:56:51 +00:00
|
|
|
* @dev used to validate if a user has not been using any reserve
|
2020-08-21 12:52:10 +00:00
|
|
|
* @param self the configuration object
|
2020-08-19 15:56:51 +00:00
|
|
|
* @return true if the user has been borrowing any reserve, false otherwise
|
|
|
|
**/
|
2020-08-21 12:52:10 +00:00
|
|
|
function isEmpty(UserConfiguration.Map memory self) internal pure returns (bool) {
|
|
|
|
return self.data == 0;
|
2020-08-05 22:46:22 +00:00
|
|
|
}
|
2020-08-05 10:40:24 +00:00
|
|
|
}
|