Skip to content

Commit

Permalink
fix: move manager to admin and update files structure
Browse files Browse the repository at this point in the history
  • Loading branch information
wellitongervickas committed Nov 1, 2023
1 parent 0ecf742 commit e14689a
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 18 deletions.
3 changes: 3 additions & 0 deletions constants/roles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ethers } from 'hardhat'

export const MANAGER_ROLE = ethers.keccak256(ethers.toUtf8Bytes('MANAGER_ROLE'))
2 changes: 1 addition & 1 deletion contracts/Hub.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;

import {IHub} from "./interfaces/IHub.sol";
Expand Down
4 changes: 2 additions & 2 deletions contracts/Registry.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;

import {IRegistry, Adapter} from "./interfaces/IRegistry.sol";
Expand All @@ -8,7 +8,7 @@ import {Roles} from "./Roles.sol";
contract Registry is IRegistry, Roles {
mapping(bytes32 => Adapter) private _adapters;

function createAdapter(bytes32 adapterType_, address adapterAddress_) external OnlyManager returns (bytes32) {
function createAdapter(bytes32 adapterType_, address adapterAddress_) external onlyManager returns (bytes32) {
Adapter memory adapter = Adapter({adapterType: adapterType_, adapterAddress: adapterAddress_});

bytes32 adapterId = keccak256(abi.encodePacked(adapterType_, adapterAddress_));
Expand Down
27 changes: 21 additions & 6 deletions contracts/Roles.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;

import {IRoles} from "./interfaces/IRoles.sol";
Expand All @@ -7,22 +7,37 @@ import "@openzeppelin/contracts/access/AccessControl.sol";
bytes32 constant MANAGER_ROLE = keccak256("MANAGER_ROLE");

contract Roles is AccessControl {
modifier OnlyManager() {
modifier onlyAdmin() {
if (!isAdmin(msg.sender)) {
revert IRoles.Roles_NotAdmin();
}
_;
}

modifier onlyManager() {
if (!isManager(msg.sender)) {
revert IRoles.Roles_NotRoleManager();
revert IRoles.Roles_NotManager();
}
_;
}

constructor() {
_setManagerRole(msg.sender);
_setAdminRole(msg.sender);
}

function _setManagerRole(address _address) internal {
_grantRole(MANAGER_ROLE, _address);
function _setAdminRole(address _address) private {
_grantRole(DEFAULT_ADMIN_ROLE, _address);
}

function isAdmin(address _address) public view returns (bool) {
return hasRole(DEFAULT_ADMIN_ROLE, _address);
}

function isManager(address _address) public view returns (bool) {
return hasRole(MANAGER_ROLE, _address);
}

function setManager(address _address) external onlyAdmin {
_grantRole(MANAGER_ROLE, _address);
}
}
2 changes: 1 addition & 1 deletion contracts/interfaces/IAdapter.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;

struct Adapter {
Expand Down
2 changes: 1 addition & 1 deletion contracts/interfaces/IApp.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;

import {Adapter} from "./IAdapter.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/interfaces/IHub.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;

import {App} from "./IApp.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/interfaces/IRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;

import {Adapter} from "./IAdapter.sol";
Expand Down
10 changes: 8 additions & 2 deletions contracts/interfaces/IRoles.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
// SPDX-License-Identifier: UNLICENSED
// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;

interface IRoles {
error Roles_NotRoleManager();
error Roles_NotAdmin();

error Roles_NotManager();

function setManager(address _address) external;

function isManager(address _address) external view returns (bool);

function isAdmin(address _address) external view returns (bool);
}
5 changes: 4 additions & 1 deletion test/hub/Hub.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import { ethers } from 'hardhat'
import { deployHubFixture } from './fixture'
import { VAULT_V1 } from '../registry/fixture'
import { deployRegistryFixture } from '../registry/fixture'
import { MANAGER_ROLE } from '../../constants/roles'

describe('Hub', function () {
it('should set app data on create', async function () {
const { registryAddress, registry } = await loadFixture(
const { registryAddress, registry, owner } = await loadFixture(
deployRegistryFixture
)

await registry.grantRole(MANAGER_ROLE, owner.address)

const { hub } = await loadFixture(
deployHubFixture.bind(this, registryAddress)
)
Expand Down
7 changes: 5 additions & 2 deletions test/registry/Registry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import { expect } from 'chai'
import { ethers } from 'hardhat'

import { deployRegistryFixture, VAULT_V1 } from './fixture'
import { MANAGER_ROLE } from '../../constants/roles'

describe('Registry', function () {
it('should set adapter data on create', async function () {
const { registry } = await loadFixture(deployRegistryFixture)
const { registry, owner } = await loadFixture(deployRegistryFixture)

await registry.grantRole(MANAGER_ROLE, owner.address)

const adapterType = VAULT_V1
const adapterAddress = ethers.ZeroAddress
Expand Down Expand Up @@ -38,6 +41,6 @@ describe('Registry', function () {

await expect(
registry.connect(notManager).createAdapter(adapterType, adapterAddress)
).to.be.revertedWithCustomError(registry, 'Roles_NotRoleManager')
).to.be.revertedWithCustomError(registry, 'Roles_NotManager')
})
})

0 comments on commit e14689a

Please sign in to comment.