Misc changes on ReserveConfiguration:

- Fixed unprecise STABLE_BORROWING_MASK.
  - Added constant for start bit positions of the different configurations.
This commit is contained in:
eboado 2020-09-30 11:24:22 +02:00
parent f435b2fa0a
commit d56a7a2779

View File

@ -20,40 +20,32 @@ library ReserveConfiguration {
uint256 constant ACTIVE_MASK = 0xFFFFFEFFFFFFFFFFFFFF;
uint256 constant FROZEN_MASK = 0xFFFFFDFFFFFFFFFFFFFF;
uint256 constant BORROWING_MASK = 0xFFFFFBFFFFFFFFFFFFFF;
uint256 constant STABLE_BORROWING_MASK = 0xFFFF07FFFFFFFFFFFFFF;
uint256 constant STABLE_BORROWING_MASK = 0xFFFFF7FFFFFFFFFFFFFF;
uint256 constant RESERVE_FACTOR_MASK = 0xFFFFFFFFFFFFFFFF;
/// @dev For the LTV, the start bit is 0 (up to 15), but we don't declare it as for 0 no bit movement is needed
uint256 constant LIQUIDATION_THRESHOLD_START_BIT_POSITION = 16;
uint256 constant LIQUIDATION_BONUS_START_BIT_POSITION = 32;
uint256 constant RESERVE_DECIMALS_START_BIT_POSITION = 48;
uint256 constant IS_ACTIVE_START_BIT_POSITION = 56;
uint256 constant IS_FROZEN_START_BIT_POSITION = 57;
uint256 constant BORROWING_ENABLED_START_BIT_POSITION = 58;
uint256 constant STABLE_BORROWING_ENABLED_START_BIT_POSITION = 59;
uint256 constant RESERVE_FACTOR_START_BIT_POSITION = 64;
struct Map {
//bit 0-15: LTV
//bit 16-31: Liq. threshold
//bit 32-47: Liq. bonus
//bit 48-55: Decimals
//bit 56: Reserve is active
//bit 57: reserve is freezed
//bit 57: reserve is frozen
//bit 58: borrowing is enabled
//bit 59: stable rate borrowing enabled
//bit 64-79: reserve factor
uint256 data;
}
/**
* @dev sets the reserve factor of the reserve
* @param self the reserve configuration
* @param reserveFactor the reserve factor
**/
function setReserveFactor(ReserveConfiguration.Map memory self, uint256 reserveFactor) internal pure {
self.data = (self.data & RESERVE_FACTOR_MASK) | reserveFactor << 64;
}
/**
* @dev gets the reserve factor of the reserve
* @param self the reserve configuration
* @return the reserve factor
**/
function getReserveFactor(ReserveConfiguration.Map storage self) internal view returns (uint256) {
return (self.data & ~RESERVE_FACTOR_MASK) >> 64;
}
/**
* @dev sets the Loan to Value of the reserve
* @param self the reserve configuration
@ -81,7 +73,9 @@ library ReserveConfiguration {
internal
pure
{
self.data = (self.data & LIQUIDATION_THRESHOLD_MASK) | (threshold << 16);
self.data =
(self.data & LIQUIDATION_THRESHOLD_MASK) |
(threshold << LIQUIDATION_THRESHOLD_START_BIT_POSITION);
}
/**
@ -94,7 +88,7 @@ library ReserveConfiguration {
view
returns (uint256)
{
return (self.data & ~LIQUIDATION_THRESHOLD_MASK) >> 16;
return (self.data & ~LIQUIDATION_THRESHOLD_MASK) >> LIQUIDATION_THRESHOLD_START_BIT_POSITION;
}
/**
@ -103,7 +97,9 @@ library ReserveConfiguration {
* @param bonus the new liquidation bonus
**/
function setLiquidationBonus(ReserveConfiguration.Map memory self, uint256 bonus) internal pure {
self.data = (self.data & LIQUIDATION_BONUS_MASK) | (bonus << 32);
self.data =
(self.data & LIQUIDATION_BONUS_MASK) |
(bonus << LIQUIDATION_BONUS_START_BIT_POSITION);
}
/**
@ -116,7 +112,7 @@ library ReserveConfiguration {
view
returns (uint256)
{
return (self.data & ~LIQUIDATION_BONUS_MASK) >> 32;
return (self.data & ~LIQUIDATION_BONUS_MASK) >> LIQUIDATION_BONUS_START_BIT_POSITION;
}
/**
@ -125,7 +121,7 @@ library ReserveConfiguration {
* @param decimals the decimals
**/
function setDecimals(ReserveConfiguration.Map memory self, uint256 decimals) internal pure {
self.data = (self.data & DECIMALS_MASK) | (decimals << 48);
self.data = (self.data & DECIMALS_MASK) | (decimals << RESERVE_DECIMALS_START_BIT_POSITION);
}
/**
@ -134,7 +130,7 @@ library ReserveConfiguration {
* @return the decimals of the asset
**/
function getDecimals(ReserveConfiguration.Map storage self) internal view returns (uint256) {
return (self.data & ~DECIMALS_MASK) >> 48;
return (self.data & ~DECIMALS_MASK) >> RESERVE_DECIMALS_START_BIT_POSITION;
}
/**
@ -143,7 +139,9 @@ library ReserveConfiguration {
* @param active the active state
**/
function setActive(ReserveConfiguration.Map memory self, bool active) internal pure {
self.data = (self.data & ACTIVE_MASK) | (uint256(active ? 1 : 0) << 56);
self.data =
(self.data & ACTIVE_MASK) |
(uint256(active ? 1 : 0) << IS_ACTIVE_START_BIT_POSITION);
}
/**
@ -152,7 +150,7 @@ library ReserveConfiguration {
* @return the active state
**/
function getActive(ReserveConfiguration.Map storage self) internal view returns (bool) {
return ((self.data & ~ACTIVE_MASK) >> 56) != 0;
return ((self.data & ~ACTIVE_MASK) >> IS_ACTIVE_START_BIT_POSITION) != 0;
}
/**
@ -161,7 +159,9 @@ library ReserveConfiguration {
* @param frozen the frozen state
**/
function setFrozen(ReserveConfiguration.Map memory self, bool frozen) internal pure {
self.data = (self.data & FROZEN_MASK) | (uint256(frozen ? 1 : 0) << 57);
self.data =
(self.data & FROZEN_MASK) |
(uint256(frozen ? 1 : 0) << IS_FROZEN_START_BIT_POSITION);
}
/**
@ -170,7 +170,7 @@ library ReserveConfiguration {
* @return the frozen state
**/
function getFrozen(ReserveConfiguration.Map storage self) internal view returns (bool) {
return ((self.data & ~FROZEN_MASK) >> 57) != 0;
return ((self.data & ~FROZEN_MASK) >> IS_FROZEN_START_BIT_POSITION) != 0;
}
/**
@ -179,7 +179,9 @@ library ReserveConfiguration {
* @param enabled true if the borrowing needs to be enabled, false otherwise
**/
function setBorrowingEnabled(ReserveConfiguration.Map memory self, bool enabled) internal pure {
self.data = (self.data & BORROWING_MASK) | (uint256(enabled ? 1 : 0) << 58);
self.data =
(self.data & BORROWING_MASK) |
(uint256(enabled ? 1 : 0) << BORROWING_ENABLED_START_BIT_POSITION);
}
/**
@ -188,7 +190,7 @@ library ReserveConfiguration {
* @return the borrowing state
**/
function getBorrowingEnabled(ReserveConfiguration.Map storage self) internal view returns (bool) {
return ((self.data & ~BORROWING_MASK) >> 58) != 0;
return ((self.data & ~BORROWING_MASK) >> BORROWING_ENABLED_START_BIT_POSITION) != 0;
}
/**
@ -197,9 +199,12 @@ library ReserveConfiguration {
* @param enabled true if the stable rate borrowing needs to be enabled, false otherwise
**/
function setStableRateBorrowingEnabled(ReserveConfiguration.Map memory self, bool enabled)
internal pure
internal
pure
{
self.data = (self.data & STABLE_BORROWING_MASK) | (uint256(enabled ? 1 : 0) << 59);
self.data =
(self.data & STABLE_BORROWING_MASK) |
(uint256(enabled ? 1 : 0) << STABLE_BORROWING_ENABLED_START_BIT_POSITION);
}
/**
@ -212,7 +217,31 @@ library ReserveConfiguration {
view
returns (bool)
{
return ((self.data & ~STABLE_BORROWING_MASK) >> 59) != 0;
return
((self.data & ~STABLE_BORROWING_MASK) >> STABLE_BORROWING_ENABLED_START_BIT_POSITION) != 0;
}
/**
* @dev sets the reserve factor of the reserve
* @param self the reserve configuration
* @param reserveFactor the reserve factor
**/
function setReserveFactor(ReserveConfiguration.Map memory self, uint256 reserveFactor)
internal
pure
{
self.data =
(self.data & RESERVE_FACTOR_MASK) |
(reserveFactor << RESERVE_FACTOR_START_BIT_POSITION);
}
/**
* @dev gets the reserve factor of the reserve
* @param self the reserve configuration
* @return the reserve factor
**/
function getReserveFactor(ReserveConfiguration.Map storage self) internal view returns (uint256) {
return (self.data & ~RESERVE_FACTOR_MASK) >> RESERVE_FACTOR_START_BIT_POSITION;
}
/**
@ -233,10 +262,10 @@ library ReserveConfiguration {
uint256 dataLocal = self.data;
return (
(dataLocal & ~ACTIVE_MASK) >> 56 != 0,
(dataLocal & ~FROZEN_MASK) >> 57 != 0,
(dataLocal & ~BORROWING_MASK) >> 58 != 0,
(dataLocal & ~STABLE_BORROWING_MASK) >> 59 != 0
(dataLocal & ~ACTIVE_MASK) >> IS_ACTIVE_START_BIT_POSITION != 0,
(dataLocal & ~FROZEN_MASK) >> IS_FROZEN_START_BIT_POSITION != 0,
(dataLocal & ~BORROWING_MASK) >> BORROWING_ENABLED_START_BIT_POSITION != 0,
(dataLocal & ~STABLE_BORROWING_MASK) >> STABLE_BORROWING_ENABLED_START_BIT_POSITION != 0
);
}
@ -259,9 +288,9 @@ library ReserveConfiguration {
return (
dataLocal & ~LTV_MASK,
(dataLocal & ~LIQUIDATION_THRESHOLD_MASK) >> 16,
(dataLocal & ~LIQUIDATION_BONUS_MASK) >> 32,
(dataLocal & ~DECIMALS_MASK) >> 48
(dataLocal & ~LIQUIDATION_THRESHOLD_MASK) >> LIQUIDATION_THRESHOLD_START_BIT_POSITION,
(dataLocal & ~LIQUIDATION_BONUS_MASK) >> LIQUIDATION_BONUS_START_BIT_POSITION,
(dataLocal & ~DECIMALS_MASK) >> RESERVE_DECIMALS_START_BIT_POSITION
);
}
}