Staking ProtocolgenericFacetsaccessControlFacet

AccessControlStakingFacetStorage

Inspired by the OpenZeppelin implementation https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/AccessControl.sol

_This library manages and allows for role-based access control mechanisms for both the staking platform and its individual staking campaigns.

Roles are referred to by their bytes32 identifier.

IMPORTANT: The Admin of the staking platform is not the owner of the StakingDiamond.sol and does not possess the same privileges.

NOTE:

  • Only the Admin can grant and revoke all roles except for ADMIN_ROLE and OPEN_ROLE to/from users, and designate or dismiss users as role handlers (see addRoleHandler() & removeRoleHandler()).
  • Designated role handlers can only grant and revoke roles they are assigned to manage from/to users.
  • OPEN_ROLE is the default role; all accounts inherently have this role.
  • By default, the Admin is a handler of all roles except for ADMIN_ROLE and OPEN_ROLE.
  • This version supports only one Admin per staking platform._

AlreadyInitialized

error AlreadyInitialized()

Thrown when attempting to re-initialize.

RestrictedDefaultRole

error RestrictedDefaultRole(bytes32 role)

Thrown when attempting to handle (grant or revoke) a default role (either OPEN_ROLE or ADMIN_ROLE).

UnauthorizedRoleHandling

error UnauthorizedRoleHandling(address account, bytes32 role)

Thrown when an account attempts to handle (grant or revoke) a role it is not permitted to manage.

AccountMissingRole

error AccountMissingRole(address account, bytes32 role)

Thrown when an account attempts an action without the required role.

InvalidZeroAddressForAdmin

error InvalidZeroAddressForAdmin()

Thrown when attempting to assign the zero address as the admin.

InvalidCampaignIdZero

error InvalidCampaignIdZero()

Thrown when a campaignId of 0 is provided, which is invalid for role checks.

ZeroAccountsToGrant

error ZeroAccountsToGrant()

Thrown when no accounts provided for role granting.

ZeroAccountsToRevoke

error ZeroAccountsToRevoke()

Thrown when no accounts provided for role revocation.

CannotHandleAdminRole

error CannotHandleAdminRole()

Thrown when an attempt is made to handle ADMIN_ROLE.

STORAGE_SLOT

bytes32 STORAGE_SLOT

Unique identifier for the storage slot where the Layout struct is stored.

ADMIN_ROLE

bytes32 ADMIN_ROLE

The unique identifier of the admin role.

OPEN_ROLE

bytes32 OPEN_ROLE

The unique identifier of the open role (all users inherently possess this role).

Layout

Struct for managing access control related data.

struct Layout {
  bool isInitialized;
  mapping(address => mapping(bytes32 => bool)) hasRole_;
  mapping(uint256 => mapping(address => mapping(bytes32 => bool))) hasRoleForId_;
  mapping(address => mapping(bytes32 => bool)) canHandleRole;
}

onlyRoleHandler

modifier onlyRoleHandler(bytes32 role)

Restricts function execution to designated role handlers for a specified role.

Checks that the specified role is not OPEN_ROLE or ADMIN_ROLE, and verifies whether the caller (msg.sender) is authorized to handle the specified role. If the caller (msg.sender) is neither a designated role handler nor the Admin, the function reverts.

Parameters

NameTypeDescription
rolebytes32The identifier of the role for which the caller must be a designated handler

onlyRole

modifier onlyRole(bytes32 role)

Rrestricts function execution to accounts with the specified role.

Checks whether the caller (msg.sender) has the specified role, else it reverts. If the specified role is the OPEN_ROLE then function proceeds with the execution.

Parameters

NameTypeDescription
rolebytes32The identifier of the role that the caller must possess to proceed.

layout

function layout() internal pure returns (struct AccessControlStakingFacetStorage.Layout l)

Retrieves a reference to the Layout struct stored at the slot specified by STORAGE_SLOT unique identifier.

initAccessControlFacet

function initAccessControlFacet(struct AccessControlStakingFacetStorage.Layout l, address account) internal

Initializes the AccessControlStakingFacetStorage by setting the Admin of the staking platform.

account cannot be address(0).

Parameters

NameTypeDescription
lstruct AccessControlStakingFacetStorage.LayoutA reference to the Layout struct in storage.
accountaddressThe address to be the designated Admin of the staking platform, responsible for managing permissions.

hasRole

function hasRole(struct AccessControlStakingFacetStorage.Layout l, bytes32 role, address account) internal view returns (bool)

Checks whether a specific account possesses a specified role within the staking platform.

This function verifies role-based permissions within the context of the staking platform.

Parameters

NameTypeDescription
lstruct AccessControlStakingFacetStorage.LayoutA reference to the Layout struct in storage.
rolebytes32The role identifier to check for.
accountaddressThe address being checked for the specified role.

Return Values

NameTypeDescription
[0]booltrue if account possesses role; false otherwise.

hasRoleForId

function hasRoleForId(struct AccessControlStakingFacetStorage.Layout l, uint256 campaignId, bytes32 role, address account) internal view returns (bool)

Checks whether a specific account possesses a specified role within a given staking campaign.

This function verifies role-based permissions within the context of a specific campaign.

Parameters

NameTypeDescription
lstruct AccessControlStakingFacetStorage.LayoutA reference to the Layout struct in storage.
campaignIduint256The unique identifier of the targeted staking campaign.
rolebytes32The role identifier to check for.
accountaddressThe address being checked for the specified role in the campaign.

Return Values

NameTypeDescription
[0]booltrue if account possesses role within the specified campaignId; false otherwise.

grantRole

function grantRole(struct AccessControlStakingFacetStorage.Layout l, bytes32 role, address account) internal

Grants role to account within the context of the staking platform.

Only callable by the Admin or a handler of role.

Parameters

NameTypeDescription
lstruct AccessControlStakingFacetStorage.LayoutA reference to the Layout struct in storage.
rolebytes32The identifier of the role to grant.
accountaddressThe address to grant role to.

grantRoleForId

function grantRoleForId(struct AccessControlStakingFacetStorage.Layout l, uint256 campaignId, bytes32 role, address account) internal

Grants role to account within the context of a specified staking campaign.

Only callable by the Admin or a handler of role.

Parameters

NameTypeDescription
lstruct AccessControlStakingFacetStorage.LayoutA reference to the Layout struct in storage.
campaignIduint256The unique identifier of the targeted staking campaign.
rolebytes32The identifier of the role to grant.
accountaddressThe address to grant role to.

grantRoleMultiple

function grantRoleMultiple(struct AccessControlStakingFacetStorage.Layout l, bytes32 role, address[] accounts) internal

Grants role to multiple accounts within the context of the staking platform.

Only callable by the Admin or a handler of role.

Parameters

NameTypeDescription
lstruct AccessControlStakingFacetStorage.LayoutA reference to the Layout struct in storage.
rolebytes32The identifier of the role to grant.
accountsaddress[]An array of addresses to grant role to.

grantRoleMultipleForId

function grantRoleMultipleForId(struct AccessControlStakingFacetStorage.Layout l, uint256 campaignId, bytes32 role, address[] accounts) internal

Grants role to multiple accounts within the context of a specified staking campaign.

Only callable by the Admin or a handler of role.

Parameters

NameTypeDescription
lstruct AccessControlStakingFacetStorage.LayoutA reference to the Layout struct in storage.
campaignIduint256The unique identifier of the targeted staking campaign.
rolebytes32The identifier of the role to grant.
accountsaddress[]An array of addresses to grant role to.

revokeRole

function revokeRole(struct AccessControlStakingFacetStorage.Layout l, bytes32 role, address account) internal

Revokes role from account within the context of the staking platform.

Only callable by the Admin or a handler of role.

Parameters

NameTypeDescription
lstruct AccessControlStakingFacetStorage.LayoutA reference to the Layout struct in storage.
rolebytes32The identifier of the role to revoke.
accountaddressThe address to revoke role from.

revokeRoleForId

function revokeRoleForId(struct AccessControlStakingFacetStorage.Layout l, uint256 campaignId, bytes32 role, address account) internal

Revokes role from account within the context of a specified staking campaign.

Only callable by the Admin or a handler of role.

Parameters

NameTypeDescription
lstruct AccessControlStakingFacetStorage.LayoutA reference to the Layout struct in storage.
campaignIduint256The unique identifier of the targeted staking campaign.
rolebytes32The identifier of the role to revoke.
accountaddressThe address to revoke role from.

revokeRoleMultiple

function revokeRoleMultiple(struct AccessControlStakingFacetStorage.Layout l, bytes32 role, address[] accounts) internal

Revokes role from multiple accounts within the context of the staking platform.

Only callable by the Admin or a handler of role.

Parameters

NameTypeDescription
lstruct AccessControlStakingFacetStorage.LayoutA reference to the Layout struct in storage.
rolebytes32The identifier of the role to revoke.
accountsaddress[]An array of addresses to revoke role from.

revokeRoleMultipleForId

function revokeRoleMultipleForId(struct AccessControlStakingFacetStorage.Layout l, uint256 campaignId, bytes32 role, address[] accounts) internal

Revoke role from multiple accounts within the context of a specified staking campaign.

Only callable by the Admin or a handler of role.

Parameters

NameTypeDescription
lstruct AccessControlStakingFacetStorage.LayoutA reference to the Layout struct in storage.
campaignIduint256The unique identifier of the targeted staking campaign.
rolebytes32The identifier of the role to revoke.
accountsaddress[]An array of addresses to revoke role from.

addRoleHandler

function addRoleHandler(struct AccessControlStakingFacetStorage.Layout l, bytes32 role, address account) internal

Delegates the handling of role to the specified account, allowing account to manage the granting and revocation of this role.

Only callable by the Admin. Designated role handlers can manage the associated roles both at the platform level and within individual campaigns.

Parameters

NameTypeDescription
lstruct AccessControlStakingFacetStorage.LayoutA reference to the Layout struct in storage.
rolebytes32The identifier of the role to delegate the handling of.
accountaddressThe address to be the designated role handler of role.

removeRoleHandler

function removeRoleHandler(struct AccessControlStakingFacetStorage.Layout l, bytes32 role, address account) internal

Dismisses the handling of role from the specified account, preventing account from managing the granting and revocation of this role.

Only callable by the Admin.

Parameters

NameTypeDescription
lstruct AccessControlStakingFacetStorage.LayoutA reference to the Layout struct in storage.
rolebytes32The identifier of the role to dismiss handling for.
accountaddressThe address to be removed as a role handler for role.

changeAdmin

function changeAdmin(struct AccessControlStakingFacetStorage.Layout l, address account) internal

Replaces the current Admin with a new Admin.

Only callable by the (current) Admin. In this version, only one Admin is supported per staking platform.

Parameters

NameTypeDescription
lstruct AccessControlStakingFacetStorage.LayoutA reference to the Layout struct in storage.
accountaddressThe address of the new Admin.