From 0d60d29f80a0cf01d46a445f71f4be73dcbbaf2e Mon Sep 17 00:00:00 2001 From: Thrilok Kumar Date: Tue, 4 May 2021 17:17:04 +0530 Subject: [PATCH] Added InstaMappings --- contracts/mapping/InstaMappings.sol | 58 +++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 contracts/mapping/InstaMappings.sol diff --git a/contracts/mapping/InstaMappings.sol b/contracts/mapping/InstaMappings.sol new file mode 100644 index 00000000..145383ec --- /dev/null +++ b/contracts/mapping/InstaMappings.sol @@ -0,0 +1,58 @@ +pragma solidity ^0.7.0; +pragma experimental ABIEncoderV2; + +import { AccessControl } from "@openzeppelin/contracts/access/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"); + + function setMaster() public { + require(msg.sender == address(this), "msg.sender is not this contract"); + uint256 adminCount = getRoleMemberCount(DEFAULT_ADMIN_ROLE); + require(adminCount == 1, "setMaster::Wrong-admin-count"); + address currentMaster = getRoleMember(DEFAULT_ADMIN_ROLE, 0); + address master = instaIndex.master(); + + if (currentMaster != master) { + grantRole(DEFAULT_ADMIN_ROLE, master); + revokeRole(DEFAULT_ADMIN_ROLE, currentMaster); + } + + adminCount = getRoleMemberCount(DEFAULT_ADMIN_ROLE); + require(adminCount == 1, "setMaster::Wrong-admin-count"); + require(hasRole(DEFAULT_ADMIN_ROLE, master), "setMaster::InstaIndex-master-not-set"); + } + + constructor() { + _setupRole(DEFAULT_ADMIN_ROLE, instaIndex.master()); + _setRoleAdmin(DEFAULT_ADMIN_ROLE, ADMIN_ROLE); + _setupRole(ADMIN_ROLE, address(this)); + } + + function getMappingContractRole(address mappingContract) public pure returns (bytes32 role){ + assembly { + role := mload(add(mappingContract, 32)) + } + } + + function hasRole(address mappingAddr, address account) public view returns (bool) { + return super.hasRole(getMappingContractRole(mappingAddr), account); + } + + function grantRole(address mappingAddr, address account) public { + super.grantRole(getMappingContractRole(mappingAddr), account); + } + + function revokeRole(address mappingAddr, address account) public { + super.revokeRole(getMappingContractRole(mappingAddr), account); + } + + function renounceRole(address mappingAddr, address account) public { + super.renounceRole(getMappingContractRole(mappingAddr), account); + } +} \ No newline at end of file