aave-protocol-v2/contracts/protocol/libraries/configuration/UserConfiguration.sol

112 lines
4.0 KiB
Solidity
Raw Normal View History

2020-08-05 10:40:24 +00:00
// SPDX-License-Identifier: agpl-3.0
2020-11-20 10:45:20 +00:00
pragma solidity 0.6.12;
2020-08-05 10:40:24 +00:00
2020-11-10 16:21:59 +00:00
import {Errors} from '../helpers/Errors.sol';
import {DataTypes} from '../types/DataTypes.sol';
2020-08-05 10:40:24 +00:00
/**
* @title UserConfiguration library
* @author Aave
* @notice Implements the bitmap logic to handle the user configuration
*/
library UserConfiguration {
uint256 internal constant BORROWING_MASK =
0x5555555555555555555555555555555555555555555555555555555555555555;
2020-08-05 10:40:24 +00:00
2020-08-06 07:57:12 +00:00
/**
2020-11-25 17:33:49 +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-05 22:46:22 +00:00
function setBorrowing(
2020-11-24 15:17:27 +00:00
DataTypes.UserConfigurationMap storage self,
2020-08-21 12:52:10 +00:00
uint256 reserveIndex,
bool borrowing
2020-08-05 22:46:22 +00:00
) internal {
2020-11-10 16:21:59 +00:00
require(reserveIndex < 128, Errors.UL_INVALID_INDEX);
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-11-25 17:33:49 +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
* @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-11-24 15:17:27 +00:00
DataTypes.UserConfigurationMap storage self,
2020-08-21 12:52:10 +00:00
uint256 reserveIndex,
2020-11-25 17:33:49 +00:00
bool usingAsCollateral
2020-08-05 22:46:22 +00:00
) internal {
2020-11-10 16:21:59 +00:00
require(reserveIndex < 128, Errors.UL_INVALID_INDEX);
2020-08-21 12:52:10 +00:00
self.data =
(self.data & ~(1 << (reserveIndex * 2 + 1))) |
2020-11-25 17:33:49 +00:00
(uint256(usingAsCollateral ? 1 : 0) << (reserveIndex * 2 + 1));
2020-08-05 10:40:24 +00:00
}
2020-08-06 07:57:12 +00:00
/**
2020-11-25 17:33:49 +00:00
* @dev Used to validate if a user has been using the reserve for borrowing or as collateral
* @param self The configuration object
* @param reserveIndex The index of the reserve in the bitmap
* @return True if the user has been using a reserve for borrowing or as collateral, false otherwise
**/
function isUsingAsCollateralOrBorrowing(
DataTypes.UserConfigurationMap memory self,
uint256 reserveIndex
) internal pure returns (bool) {
2020-11-10 16:21:59 +00:00
require(reserveIndex < 128, Errors.UL_INVALID_INDEX);
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-11-25 17:33:49 +00:00
* @dev Used to validate if a user has been using the reserve for borrowing
* @param self The configuration object
* @param reserveIndex The index of the reserve in the bitmap
* @return True if the user has been using a reserve for borrowing, false otherwise
**/
2020-11-24 15:17:27 +00:00
function isBorrowing(DataTypes.UserConfigurationMap 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-11-10 16:21:59 +00:00
require(reserveIndex < 128, Errors.UL_INVALID_INDEX);
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-11-25 17:33:49 +00:00
* @dev Used to validate if a user has been using the reserve as collateral
* @param self The configuration object
* @param reserveIndex The index of the reserve in the bitmap
* @return True if the user has been using a reserve as collateral, false otherwise
**/
2020-11-24 15:17:27 +00:00
function isUsingAsCollateral(DataTypes.UserConfigurationMap 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-11-10 16:21:59 +00:00
require(reserveIndex < 128, Errors.UL_INVALID_INDEX);
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-11-25 17:33:49 +00:00
* @dev Used to validate if a user has been borrowing from any reserve
* @param self The configuration object
* @return True if the user has been borrowing any reserve, false otherwise
**/
2020-11-24 15:17:27 +00:00
function isBorrowingAny(DataTypes.UserConfigurationMap memory self) internal pure returns (bool) {
2020-08-21 12:52:10 +00:00
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-11-25 17:33:49 +00:00
* @dev Used to validate if a user has not been using any reserve
* @param self The configuration object
* @return True if the user has been borrowing any reserve, false otherwise
**/
2020-11-24 15:17:27 +00:00
function isEmpty(DataTypes.UserConfigurationMap memory self) internal pure returns (bool) {
2020-08-21 12:52:10 +00:00
return self.data == 0;
2020-08-05 22:46:22 +00:00
}
2020-08-05 10:40:24 +00:00
}