Skip to content

Commit

Permalink
feat: simplifying code base
Browse files Browse the repository at this point in the history
  • Loading branch information
wellitongervickas committed Nov 10, 2023
1 parent a9157ea commit c57c89a
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 433 deletions.
10 changes: 9 additions & 1 deletion contracts/AccessManagement.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ pragma solidity 0.8.21;

import "@openzeppelin/contracts/access/manager/AccessManager.sol";

uint64 constant ADMIN_ROLE = type(uint64).min;
uint64 constant PUBLIC_ROLE = type(uint64).max;

/**
* @title AccessManagement
* @notice This contract is used to manage access to the protocol
*/

contract AccessManagement is AccessManager {
constructor(address initiaAdmin_) AccessManager(initiaAdmin_) {}
constructor(address admin) AccessManager(admin) {}
}
72 changes: 44 additions & 28 deletions contracts/Hub.sol
Original file line number Diff line number Diff line change
@@ -1,49 +1,65 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.21;

import {IHub} from "./interfaces/IHub.sol";
import {IRegistry} from "./interfaces/IRegistry.sol";
import {App} from "./interfaces/IApp.sol";
import {Adapter} from "./interfaces/IAdapter.sol";
import {AccessManaged} from "@openzeppelin/contracts/access/manager/AccessManaged.sol";

contract Hub is IHub, AccessManaged {
IRegistry public immutable registryAddress;
uint256 private _appIdSalt;
/**
* @title Hub
* @notice This contract is used to manage apps in the protocol
*/

mapping(bytes32 => App) private _apps;

constructor(IRegistry registryAddress_, address accessManagement_) AccessManaged(accessManagement_) {
registryAddress = registryAddress_;
contract Hub is AccessManaged {
struct App {
address appAddress;
}

modifier checkIsRegistryAdapter(bytes32 adapterId_) {
if (!registryAddress.isAdapter(adapterId_)) {
revert IHub.Hub_AdapterNotFound(adapterId_);
}
_;
}
/// @notice The salt used to generate appIds
uint256 private s_appIdSalt;

mapping(bytes32 => App) private s_apps;

function createApp(
bytes32 adapterId_,
address appAddress_
) external checkIsRegistryAdapter(adapterId_) restricted returns (bytes32) {
App memory app = App({adapter: _getRegistryAdapter(adapterId_), appAddress: appAddress_});
/// @notice Emitted when an app is added to the hub
event Hub_AppAdded(bytes32 indexed appId_);

bytes32 appId = keccak256(abi.encodePacked(_appIdSalt++, adapterId_, appAddress_, msg.sender));
constructor(address accessManagement_) AccessManaged(accessManagement_) {}

_apps[appId] = app;
/**
* @notice Adds an app to the hub
* @param appAddress_ The address of the app contract
* @return The id of the app
*/
function addApp(address appAddress_) external restricted returns (bytes32) {
bytes32 appId = _getNextAppId(appAddress_);
s_apps[appId] = App({appAddress: appAddress_});

emit IHub.Hub_AppCreated(appId);
emit Hub_AppAdded(appId);

return appId;
}

function _getRegistryAdapter(bytes32 adapterId_) internal view returns (Adapter memory) {
return registryAddress.getAdapter(adapterId_);
/**
* @notice Gets the next app id
* @param appAddress_ The address of the app contract
* @return The id of the app
*/
function _getNextAppId(address appAddress_) private returns (bytes32) {
return keccak256(abi.encodePacked(_getNextAppIdSalt(), msg.sender, appAddress_));
}

/**
* @notice Gets the next app id salt
* @return The next app id salt
*/
function _getNextAppIdSalt() private returns (uint256) {
return s_appIdSalt++;
}

/**
* @notice Gets an app from the hub
* @param appId_ The id of the app
* @return The app
*/
function getApp(bytes32 appId_) external view returns (App memory) {
return _apps[appId_];
return s_apps[appId_];
}
}
33 changes: 0 additions & 33 deletions contracts/Registry.sol

This file was deleted.

7 changes: 0 additions & 7 deletions contracts/interfaces/IAdapter.sol

This file was deleted.

9 changes: 0 additions & 9 deletions contracts/interfaces/IApp.sol

This file was deleted.

14 changes: 0 additions & 14 deletions contracts/interfaces/IHub.sol

This file was deleted.

14 changes: 0 additions & 14 deletions contracts/interfaces/IRegistry.sol

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { ethers } from 'hardhat'

export const ADMIN_ROLE = 0n
export const DEVELOPER_ROLE = 1n

export async function deployAccessManagementFixture() {
const [owner] = await ethers.getSigners()

Expand Down
Loading

0 comments on commit c57c89a

Please sign in to comment.