From 169b411142b19e6d4c8e31810c72c550b756ff37 Mon Sep 17 00:00:00 2001 From: Daksh Miglani Date: Mon, 10 May 2021 00:45:52 +0530 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20separate=20out=20contrac?= =?UTF-8?q?ts=20and=20fit=20them=20for=20our=20usecase?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...cessControl.sol => InstaAccessControl.sol} | 105 ++++++------------ contracts/mapping/InstaMappings.sol | 21 +--- 2 files changed, 39 insertions(+), 87 deletions(-) rename contracts/mapping/{AccessControl.sol => InstaAccessControl.sol} (68%) diff --git a/contracts/mapping/AccessControl.sol b/contracts/mapping/InstaAccessControl.sol similarity index 68% rename from contracts/mapping/AccessControl.sol rename to contracts/mapping/InstaAccessControl.sol index 9c8b8fb7..d1d3a0d5 100644 --- a/contracts/mapping/AccessControl.sol +++ b/contracts/mapping/InstaAccessControl.sol @@ -41,50 +41,29 @@ import "@openzeppelin/contracts/utils/Context.sol"; * grant and revoke this role. Extra precautions should be taken to secure * accounts that have been granted it. */ -abstract contract AccessControl is Context { +interface IndexInterface { + function master() external view returns (address); +} + +contract InstaAccessControl is Context { using EnumerableSet for EnumerableSet.AddressSet; using Address for address; - struct RoleData { - EnumerableSet.AddressSet members; - bytes32 adminRole; - } + mapping(bytes32 => EnumerableSet.AddressSet) private _roles; - mapping(bytes32 => RoleData) private _roles; - - bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00; - - /** - * @dev Emitted when `newAdminRole` is set as ``role``'s admin role, replacing `previousAdminRole` - * - * `DEFAULT_ADMIN_ROLE` is the starting admin for all roles, despite - * {RoleAdminChanged} not being emitted signaling this. - * - * _Available since v3.1._ - */ - event RoleAdminChanged( - bytes32 indexed role, - bytes32 indexed previousAdminRole, - bytes32 indexed newAdminRole - ); + IndexInterface public constant instaIndex = + IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723); /** * @dev Emitted when `account` is granted `role`. - * - * `sender` is the account that originated the contract call, an admin role - * bearer except when using {_setupRole}. */ - event RoleGranted( - bytes32 indexed role, - address indexed account, - address indexed sender - ); + event RoleGranted(bytes32 indexed role, address indexed account); /** * @dev Emitted when `account` is revoked `role`. * * `sender` is the account that originated the contract call: - * - if using `revokeRole`, it is the admin role bearer + * - if using `revokeRole`, it is the insta master * - if using `renounceRole`, it is the role bearer (i.e. `account`) */ event RoleRevoked( @@ -93,11 +72,19 @@ abstract contract AccessControl is Context { address indexed sender ); + modifier onlyMaster { + require( + instaIndex.master() == _msgSender(), + "AccessControl: sender must be master" + ); + _; + } + /** * @dev Returns `true` if `account` has been granted `role`. */ function hasRole(bytes32 role, address account) public view returns (bool) { - return _roles[role].members.contains(account); + return _roles[role].contains(account); } /** @@ -105,7 +92,7 @@ abstract contract AccessControl is Context { * together with {getRoleMember} to enumerate all bearers of a role. */ function getRoleMemberCount(bytes32 role) public view returns (uint256) { - return _roles[role].members.length(); + return _roles[role].length(); } /** @@ -125,17 +112,7 @@ abstract contract AccessControl is Context { view returns (address) { - return _roles[role].members.at(index); - } - - /** - * @dev Returns the admin role that controls `role`. See {grantRole} and - * {revokeRole}. - * - * To change a role's admin, use {_setRoleAdmin}. - */ - function getRoleAdmin(bytes32 role) public view returns (bytes32) { - return _roles[role].adminRole; + return _roles[role].at(index); } /** @@ -146,13 +123,13 @@ abstract contract AccessControl is Context { * * Requirements: * - * - the caller must have `DEFAULT_ADMIN_ROLE` role. + * - the caller must be the master. */ - function grantRole(bytes32 role, address account) public virtual { - require( - hasRole(DEFAULT_ADMIN_ROLE, _msgSender()), - "AccessControl: sender must be the master admin" - ); + function grantRole(bytes32 role, address account) + public + virtual + onlyMaster + { _grantRole(role, account); } @@ -163,13 +140,13 @@ abstract contract AccessControl is Context { * * Requirements: * - * - the caller must have `DEFAULT_ADMIN_ROLE` role. + * - the caller must be the master. */ - function revokeRole(bytes32 role, address account) public virtual { - require( - hasRole(DEFAULT_ADMIN_ROLE, _msgSender()), - "AccessControl: sender must be the master admin" - ); + function revokeRole(bytes32 role, address account) + public + virtual + onlyMaster + { _revokeRole(role, account); } @@ -216,24 +193,14 @@ abstract contract AccessControl is Context { _grantRole(role, account); } - /** - * @dev Sets `adminRole` as ``role``'s admin role. - * - * Emits a {RoleAdminChanged} event. - */ - function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual { - emit RoleAdminChanged(role, _roles[role].adminRole, adminRole); - _roles[role].adminRole = adminRole; - } - function _grantRole(bytes32 role, address account) private { - if (_roles[role].members.add(account)) { - emit RoleGranted(role, account, _msgSender()); + if (_roles[role].add(account)) { + emit RoleGranted(role, account); } } function _revokeRole(bytes32 role, address account) private { - if (_roles[role].members.remove(account)) { + if (_roles[role].remove(account)) { emit RoleRevoked(role, account, _msgSender()); } } diff --git a/contracts/mapping/InstaMappings.sol b/contracts/mapping/InstaMappings.sol index da7b1e0f..17147510 100644 --- a/contracts/mapping/InstaMappings.sol +++ b/contracts/mapping/InstaMappings.sol @@ -1,29 +1,14 @@ pragma solidity ^0.7.0; pragma experimental ABIEncoderV2; -import {AccessControl} from "./AccessControl.sol"; - -interface IndexInterface { - function master() external view returns (address); -} - -contract InstaMappings is AccessControl { - IndexInterface public constant instaIndex = - IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723); - - bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE"); - - constructor() { - _setupRole(DEFAULT_ADMIN_ROLE, instaIndex.master()); - _setRoleAdmin(DEFAULT_ADMIN_ROLE, ADMIN_ROLE); - _setupRole(ADMIN_ROLE, address(this)); - } +import {InstaAccessControl} from "./InstaAccessControl.sol"; +contract InstaMappings is InstaAccessControl { function getMappingContractRole(address mappingContract) public pure returns (bytes32 role) - { + { bytes memory encoded = abi.encode(mappingContract); assembly { role := mload(add(encoded, 32))