Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Roles feature #5

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
30 changes: 18 additions & 12 deletions contracts/AssetParameters.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import "./interfaces/IAssetParameters.sol";
import "./interfaces/ISystemPoolsRegistry.sol";
import "./interfaces/IBasicPool.sol";
import "./interfaces/IPriceManager.sol";
import "./interfaces/IRoleManager.sol";

import "./libraries/PureParameters.sol";

Expand Down Expand Up @@ -50,6 +51,7 @@ contract AssetParameters is IAssetParameters, AbstractDependant {
address internal _systemOwnerAddr;
ISystemParameters internal _systemParameters;
ISystemPoolsRegistry internal _systemPoolsRegistry;
IRoleManager internal _roleManager;

mapping(bytes32 => mapping(bytes32 => PureParameters.Param)) internal _parameters;

Expand All @@ -61,11 +63,8 @@ contract AssetParameters is IAssetParameters, AbstractDependant {
_;
}

modifier onlySystemOwner() {
require(
msg.sender == _systemOwnerAddr,
"AssetParameters: Only system owner can call this function."
);
modifier onlyAssetParametersManager() {
_onlyAssetParametersManager();
_;
}

Expand All @@ -75,6 +74,7 @@ contract AssetParameters is IAssetParameters, AbstractDependant {
_systemOwnerAddr = registry_.getSystemOwner();
_systemParameters = ISystemParameters(registry_.getSystemParametersContract());
_systemPoolsRegistry = ISystemPoolsRegistry(registry_.getSystemPoolsRegistryContract());
_roleManager = IRoleManager(registry_.getRoleManagerContract());
}

function setPoolInitParams(
Expand Down Expand Up @@ -103,7 +103,7 @@ contract AssetParameters is IAssetParameters, AbstractDependant {
function setupAnnualBorrowRate(
bytes32 assetKey_,
uint256 newAnnualBorrowRate_
) external override onlySystemOwner onlyExists(assetKey_) {
) external override onlyAssetParametersManager onlyExists(assetKey_) {
require(
_systemParameters.getStablePoolsAvailability(),
"AssetParameters: Stable pools unavailable."
Expand Down Expand Up @@ -135,34 +135,36 @@ contract AssetParameters is IAssetParameters, AbstractDependant {
function setupMainParameters(
bytes32 assetKey_,
MainPoolParams calldata mainParams_
) external override onlySystemOwner onlyExists(assetKey_) {
) external override onlyAssetParametersManager onlyExists(assetKey_) {
_setupMainParameters(assetKey_, mainParams_);
}

function setupInterestRateModel(
bytes32 assetKey_,
InterestRateParams calldata interestParams_
) external override onlySystemOwner onlyExists(assetKey_) {
) external override onlyAssetParametersManager onlyExists(assetKey_) {
_setupInterestRateParams(assetKey_, interestParams_);
}

function setupDistributionsMinimums(
bytes32 assetKey_,
DistributionMinimums calldata distrMinimums_
) external override onlySystemOwner onlyExists(assetKey_) {
) external override onlyAssetParametersManager onlyExists(assetKey_) {
_setupDistributionsMinimums(assetKey_, distrMinimums_);
}

function setupAllParameters(
bytes32 assetKey_,
AllPoolParams calldata poolParams_
) external override onlySystemOwner onlyExists(assetKey_) {
) external override onlyAssetParametersManager onlyExists(assetKey_) {
_setupInterestRateParams(assetKey_, poolParams_.interestRateParams);
_setupMainParameters(assetKey_, poolParams_.mainParams);
_setupDistributionsMinimums(assetKey_, poolParams_.distrMinimums);
}

function freeze(bytes32 assetKey_) external override onlySystemOwner onlyExists(assetKey_) {
function freeze(
bytes32 assetKey_
) external override onlyAssetParametersManager onlyExists(assetKey_) {
_parameters[assetKey_][FREEZE_KEY] = PureParameters.makeBoolParam(true);

emit FreezeParamUpdated(assetKey_, true);
Expand All @@ -171,7 +173,7 @@ contract AssetParameters is IAssetParameters, AbstractDependant {
function enableCollateral(
bytes32 assetKey_,
bool forPRT_
) external override onlySystemOwner onlyExists(assetKey_) {
) external override onlyAssetParametersManager onlyExists(assetKey_) {
forPRT_
? _parameters[assetKey_][ENABLE_COLLATERAL_WITH_PRT_KEY] = PureParameters
.makeBoolParam(true)
Expand Down Expand Up @@ -255,6 +257,10 @@ contract AssetParameters is IAssetParameters, AbstractDependant {
return _getParam(assetKey_, MAX_UTILIZATION_RATIO_KEY).getUintFromParam();
}

function _onlyAssetParametersManager() internal {
_roleManager.isAssetParametersManager(msg.sender);
}

function _setupInterestRateParams(
bytes32 assetKey_,
InterestRateParams calldata interestParams_
Expand Down
18 changes: 11 additions & 7 deletions contracts/DefiCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import "./interfaces/ISystemPoolsRegistry.sol";
import "./interfaces/IRewardsDistribution.sol";
import "./interfaces/IBasicPool.sol";
import "./interfaces/IPRT.sol";
import "./interfaces/IRoleManager.sol";

import "./libraries/AssetsHelperLibrary.sol";
import "./libraries/MathHelper.sol";
Expand All @@ -43,14 +44,12 @@ contract DefiCore is
ISystemPoolsRegistry internal _systemPoolsRegistry;
IRewardsDistribution internal _rewardsDistribution;
IPRT internal _prt;
IRoleManager internal _roleManager;

mapping(address => mapping(bytes32 => bool)) public override disabledCollateralAssets;

modifier onlySystemOwner() {
require(
msg.sender == _systemOwnerAddr,
"DefiCore: Only system owner can call this function."
);
modifier onlyDefiCorePauser() {
_onlyDefiCorePauser();
_;
}

Expand All @@ -69,13 +68,14 @@ contract DefiCore is
_rewardsDistribution = IRewardsDistribution(registry_.getRewardsDistributionContract());
_systemPoolsRegistry = ISystemPoolsRegistry(registry_.getSystemPoolsRegistryContract());
_prt = IPRT(registry_.getPRTContract());
_roleManager = IRoleManager(registry_.getRoleManagerContract());
}

function pause() external override onlySystemOwner {
function pause() external override onlyDefiCorePauser {
_pause();
}

function unpause() external override onlySystemOwner {
function unpause() external override onlyDefiCorePauser {
_unpause();
}

Expand Down Expand Up @@ -645,6 +645,10 @@ contract DefiCore is
}
}

function _onlyDefiCorePauser() internal {
_roleManager.isDefiCorePauser(msg.sender);
}

function _borrowInternal(
bytes32 assetKey_,
uint256 borrowAmount_,
Expand Down
15 changes: 10 additions & 5 deletions contracts/PRT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import "./interfaces/IPRT.sol";
import "./interfaces/IRegistry.sol";
import "./interfaces/IDefiCore.sol";
import "./interfaces/IUserInfoRegistry.sol";

import "./common/Globals.sol";
import "./interfaces/IRoleManager.sol";

contract PRT is IPRT, ERC721Upgradeable, AbstractDependant, ReentrancyGuardUpgradeable {
uint256 internal _tokenIdCounter;
address internal _systemOwnerAddr;
IDefiCore internal _defiCore;
IUserInfoRegistry internal _userInfoRegistry;
IRoleManager internal _roleManager;

PRTParams internal _prtParams;

modifier onlySystemOwner() {
require(msg.sender == _systemOwnerAddr, "PRT: Only system owner can call this function");
modifier onlyPRTParamUpdater() {
_onlyPRTParamUpdater();
_;
}

Expand All @@ -42,9 +42,10 @@ contract PRT is IPRT, ERC721Upgradeable, AbstractDependant, ReentrancyGuardUpgra
_systemOwnerAddr = registry_.getSystemOwner();
_defiCore = IDefiCore(registry_.getDefiCoreContract());
_userInfoRegistry = IUserInfoRegistry(registry_.getUserInfoRegistryContract());
_roleManager = IRoleManager(registry_.getRoleManagerContract());
}

function updatePRTParams(PRTParams calldata prtParams_) external override onlySystemOwner {
function updatePRTParams(PRTParams calldata prtParams_) external override onlyPRTParamUpdater {
_prtParams = prtParams_;
}

Expand Down Expand Up @@ -100,6 +101,10 @@ contract PRT is IPRT, ERC721Upgradeable, AbstractDependant, ReentrancyGuardUpgra
_burn(tokenId_);
}

function _onlyPRTParamUpdater() internal {
_roleManager.isPRTParamUpdater(msg.sender);
}

function _checkUserPRTStats(
address userAddr_,
IUserInfoRegistry.LastSavedUserPosition memory userPosition_,
Expand Down
6 changes: 6 additions & 0 deletions contracts/Registry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ contract Registry is IRegistry, OwnableContractsRegistry {

string public constant PRT_NAME = "PLATFORM_REPUTATION_TOKEN";

string public constant ROLE_MANAGER_NAME = "ROLE_MANAGER_NAME";

function transferOwnershipAndInject(
address newOwner_,
string[] calldata names_
Expand Down Expand Up @@ -83,4 +85,8 @@ contract Registry is IRegistry, OwnableContractsRegistry {
function getPRTContract() external view override returns (address) {
return getContract(PRT_NAME);
}

function getRoleManagerContract() external view override returns (address) {
return getContract(ROLE_MANAGER_NAME);
}
}
16 changes: 10 additions & 6 deletions contracts/RewardsDistribution.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import "./interfaces/IAssetParameters.sol";
import "./interfaces/IRewardsDistribution.sol";
import "./interfaces/ISystemPoolsRegistry.sol";
import "./interfaces/IBasicPool.sol";
import "./interfaces/IRoleManager.sol";

import "./libraries/MathHelper.sol";

Expand All @@ -22,6 +23,7 @@ contract RewardsDistribution is IRewardsDistribution, AbstractDependant {
address internal _defiCoreAddr;
IAssetParameters internal _assetParameters;
ISystemPoolsRegistry internal _systemPoolsRegistry;
IRoleManager internal _roleManager;

mapping(bytes32 => LiquidityPoolInfo) public liquidityPoolsInfo;
mapping(bytes32 => mapping(address => UserDistributionInfo)) public usersDistributionInfo;
Expand All @@ -34,11 +36,8 @@ contract RewardsDistribution is IRewardsDistribution, AbstractDependant {
_;
}

modifier onlySystemOwner() {
require(
msg.sender == _systemOwnerAddr,
"RewardsDistribution: Only system owner can call this function."
);
modifier onlyRewardsDistributionManager() {
_onlyRewardsDistributionManager();
_;
}

Expand All @@ -49,6 +48,7 @@ contract RewardsDistribution is IRewardsDistribution, AbstractDependant {
_defiCoreAddr = registry_.getDefiCoreContract();
_assetParameters = IAssetParameters(registry_.getAssetParametersContract());
_systemPoolsRegistry = ISystemPoolsRegistry(registry_.getSystemPoolsRegistryContract());
_roleManager = IRoleManager(registry_.getRoleManagerContract());
}

function updateCumulativeSums(
Expand Down Expand Up @@ -85,7 +85,7 @@ contract RewardsDistribution is IRewardsDistribution, AbstractDependant {
function setupRewardsPerBlockBatch(
bytes32[] calldata assetKeys_,
uint256[] calldata rewardsPerBlock_
) external override onlySystemOwner {
) external override onlyRewardsDistributionManager {
require(
_onlyExistingRewardsAssetKey(),
"RewardsDistributionL Unable to setup rewards per block."
Expand Down Expand Up @@ -183,6 +183,10 @@ contract RewardsDistribution is IRewardsDistribution, AbstractDependant {
}
}

function _onlyRewardsDistributionManager() internal {
_roleManager.isRewardsDistributionManager(msg.sender);
}

function _updateSumsWithUserReward(
address userAddr_,
bytes32 assetKey_,
Expand Down
92 changes: 92 additions & 0 deletions contracts/RoleManager.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
pragma solidity 0.8.17;

import "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";

import "@openzeppelin/contracts-upgradeable/utils/StringsUpgradeable.sol";

contract RoleManager is AccessControlUpgradeable {
bytes32 constant ROLE_MANAGER_ADMIN = keccak256("ROLE_MANAGER_ADMIN");

bytes32 constant ROLE_MANAGER_ROLE_GOVERNOR = keccak256("ROLE_MANAGER_ROLE_GOVERNOR");

bytes32 constant ASSET_PARAMETERS_MANAGER = keccak256("ASSET_PARAMETERS_MANAGER");

bytes32 constant DEFI_CORE_PAUSER = keccak256("DEFI_CORE_PAUSER");

bytes32 constant PRT_PARAM_UPDATER = keccak256("PRT_PARAM_UPDATER");

bytes32 constant REWARDS_DISTRIBUTION_MANAGER = keccak256("REWARDS_DISTRIBUTION_MANAGER");

bytes32 constant SYSTEM_PARAMETERS_MANAGER = keccak256("SYSTEM_PARAMETERS_MANAGER");

bytes32 constant SYSTEM_POOLS_MANAGER = keccak256("SYSTEM_POOLS_MANAGER");

bytes32 constant SYSTEM_POOLS_RESERVE_FUNDS_MANAGER =
keccak256("SYSTEM_POOLS_RESERVE_FUNDS_MANAGER");

function roleManagerInitialize(
bytes32[] calldata roles_,
address[] calldata accounts_
) external initializer {
require(
roles_.length == accounts_.length,
"RoleManager: passed arrays are of different sizes"
);
for (uint256 i = 0; i < roles_.length; ++i) {
_setupRole(roles_[i], accounts_[i]);
}
_setupRole(ROLE_MANAGER_ADMIN, msg.sender);
}

function isAssetParametersManager(address account_) external view {
_hasRoleOrAdmin(ASSET_PARAMETERS_MANAGER, account_);
}

function isDefiCorePauser(address account_) external view {
_hasRoleOrAdmin(DEFI_CORE_PAUSER, account_);
}

function isPRTParamUpdater(address account_) external view {
_hasRoleOrAdmin(PRT_PARAM_UPDATER, account_);
}

function isRewardsDistributionManager(address account_) external view {
_hasRoleOrAdmin(REWARDS_DISTRIBUTION_MANAGER, account_);
}

function isSystemParametersManager(address account_) external view {
_hasRoleOrAdmin(SYSTEM_PARAMETERS_MANAGER, account_);
}

function isSystemPoolsManager(address account_) external view {
_hasRoleOrAdmin(SYSTEM_POOLS_MANAGER, account_);
}

function isSystemPoolsReserveFundsManager(address account_) external view {
_hasRoleOrAdmin(SYSTEM_POOLS_RESERVE_FUNDS_MANAGER, account_);
}

function grantRole(bytes32 role_, address account_) public override {
_hasRoleOrAdmin(ROLE_MANAGER_ROLE_GOVERNOR, msg.sender);

_grantRole(role_, account_);
}

function revokeRole(bytes32 role_, address account_) public override {
_hasRoleOrAdmin(ROLE_MANAGER_ROLE_GOVERNOR, msg.sender);

_revokeRole(role_, account_);
}

function _hasRoleOrAdmin(bytes32 role_, address account_) internal view virtual {
require(
hasRole(role_, account_) || hasRole(ROLE_MANAGER_ADMIN, account_),
string(
abi.encodePacked(
"RoleManager: account is missing role ",
StringsUpgradeable.toHexString(uint256(role_), 32)
)
)
);
}
}
Loading