Skip to content

Commit

Permalink
♻️ Make AccessControl Modules-Ready
Browse files Browse the repository at this point in the history
Signed-off-by: Pascal Marco Caversaccio <[email protected]>
  • Loading branch information
pcaversaccio committed Mar 20, 2024
1 parent 19ed1e5 commit 9c4f260
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 20 deletions.
18 changes: 0 additions & 18 deletions src/snekmate/auth/AccessControl.vy
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,6 @@ implements: IAccessControl
DEFAULT_ADMIN_ROLE: public(constant(bytes32)) = empty(bytes32)


# @dev An additional 32-byte access role.
# @notice Please adjust the naming of the variable
# according to your specific requirement,
# e.g. `MINTER_ROLE`.
ADDITIONAL_ROLE_1: public(constant(bytes32)) = keccak256("ADDITIONAL_ROLE_1")


# @dev An additional 32-byte access role.
# @notice Please adjust the naming of the variable
# according to your specific requirement,
# e.g. `PAUSER_ROLE`. Also, feel free to add more
# roles if necessary. In this case, it is important
# to extend the constructor accordingly.
ADDITIONAL_ROLE_2: public(constant(bytes32)) = keccak256("ADDITIONAL_ROLE_2")


# @dev Stores the ERC-165 interface identifier for each
# imported interface. The ERC-165 interface identifier
# is defined as the XOR of all function selectors in the
Expand Down Expand Up @@ -147,8 +131,6 @@ def __init__():
the `msg.sender`.
"""
self._grant_role(DEFAULT_ADMIN_ROLE, msg.sender)
self._grant_role(ADDITIONAL_ROLE_1, msg.sender)
self._grant_role(ADDITIONAL_ROLE_2, msg.sender)


@external
Expand Down
63 changes: 63 additions & 0 deletions src/snekmate/auth/mocks/AccessControlMock.vy
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# pragma version ~=0.4.0b5
"""
@title Wrapper Contract for Multi-Role-Based Access Control Functions
@custom:contract-name AccessControlMock
@license GNU Affero General Public License v3.0 only
@author pcaversaccio
"""


# @dev We import and initialise the `AccessControl` module.
from .. import AccessControl as ac
initializes: ac


# @dev We import and implement the `IERC165` interface,
# which is a built-in interface of the Vyper compiler.
from ethereum.ercs import IERC165
implements: IERC165


# @dev We import and implement the `IAccessControl`
# interface, which is written using standard Vyper
# syntax.
from ..interfaces import IAccessControl
implements: IAccessControl


# @dev An additional 32-byte access role.
ADDITIONAL_ROLE_1: public(constant(bytes32)) = keccak256("ADDITIONAL_ROLE_1")


# @dev An additional 32-byte access role.
ADDITIONAL_ROLE_2: public(constant(bytes32)) = keccak256("ADDITIONAL_ROLE_2")


# @dev We export all public functions from the `AccessControl` module.
# @notice It's important to also export public `immutable` and state
# variables.
exports: (
ac.supportsInterface,
ac.DEFAULT_ADMIN_ROLE,
ac.hasRole,
ac.getRoleAdmin,
ac.grantRole,
ac.revokeRole,
ac.renounceRole,
ac.set_role_admin
)


@deploy
@payable
def __init__():
"""
@dev To omit the opcodes for checking the `msg.value`
in the creation-time EVM bytecode, the constructor
is declared as `payable`.
@notice All predefined roles will be assigned to
the `msg.sender`.
"""
ac.__init__() # Assigns the `DEFAULT_ADMIN_ROLE` to the `msg.sender`.
ac._grant_role(ADDITIONAL_ROLE_1, msg.sender)
ac._grant_role(ADDITIONAL_ROLE_2, msg.sender)
4 changes: 2 additions & 2 deletions test/auth/AccessControl.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ contract AccessControlTest is Test {

function setUp() public {
accessControl = IAccessControlExtended(
vyperDeployer.deployContract("src/snekmate/auth/", "AccessControl")
vyperDeployer.deployContract("src/snekmate/auth/mocks/", "AccessControlMock")
);
}

Expand Down Expand Up @@ -54,7 +54,7 @@ contract AccessControlTest is Test {
vm.expectEmit(true, true, true, false);
emit IAccessControl.RoleGranted(ADDITIONAL_ROLE_2, deployer, deployer);
accessControlInitialEvent = IAccessControlExtended(
vyperDeployer.deployContract("src/snekmate/auth/", "AccessControl")
vyperDeployer.deployContract("src/snekmate/auth/mocks/", "AccessControlMock")
);
assertEq(
accessControlInitialEvent.DEFAULT_ADMIN_ROLE(),
Expand Down

0 comments on commit 9c4f260

Please sign in to comment.