Skip to content

Commit

Permalink
Add createRole func
Browse files Browse the repository at this point in the history
  • Loading branch information
tnkshuuhei committed Oct 13, 2024
1 parent 46228e0 commit fe0c041
Showing 1 changed file with 60 additions and 5 deletions.
65 changes: 60 additions & 5 deletions src/CEP.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ISchemaRegistry } from "eas-contracts/ISchemaRegistry.sol";
import { SchemaResolver } from "eas-contracts/resolver/SchemaResolver.sol";
import { ISchemaResolver } from "eas-contracts/resolver/ISchemaResolver.sol";
import { IHats } from "hats-contracts/interfaces/IHats.sol";
import { console } from "forge-std/console.sol";
// import { IHypercertToken } from "hypercerts/contracts/interfaces/IHypercertToken.sol"; // solidity 0.8.16

import "./gov/CEPGovernor.sol";
Expand Down Expand Up @@ -60,7 +61,12 @@ contract CEP is AccessControl, Errors, IERC1155Receiver {
}

// Mappings
// poolId => EvaluationPool
mapping(uint256 => EvaluationPool) public evaluations;

// hatId => Evaluation address
mapping(uint256 => address) public evaluationAddrByHatId;
// hatId => Profile
mapping(uint256 => Profile) public profilesById;

// Events
Expand All @@ -77,8 +83,11 @@ contract CEP is AccessControl, Errors, IERC1155Receiver {
event TreasuryUpdated(address treasury);
event PoolFunded(uint256 indexed id, uint256 amount);
event ProfileCreated(bytes32 indexed id, uint256 hatId, string name, string metadata, address owner);

event RoleCreated(
uint256 indexed projectHatid, uint256 roleHatId, address[] wearers, string metadata, string imageURL
);
// Modifiers

modifier onlyAdmin() {
_checkAdmin();
_;
Expand All @@ -91,7 +100,8 @@ contract CEP is AccessControl, Errors, IERC1155Receiver {
address _token,
IEAS _eas,
address _schemaRegistry,
address _hats
address _hats,
string memory _imageURL
) {
_grantRole(DEFAULT_ADMIN_ROLE, _owner);

Expand All @@ -116,7 +126,7 @@ contract CEP is AccessControl, Errors, IERC1155Receiver {
uint256 hatId = IHats(_hats).mintTopHat(
address(this), // target: Tophat's wearer address. The address that will be granted the hat.
"Impact Evaluation DAO", // name
"imageURL" // TODO: add the default image URL
_imageURL
);
topHatId = hatId;

Expand Down Expand Up @@ -173,8 +183,6 @@ contract CEP is AccessControl, Errors, IERC1155Receiver {
return profileId;
}

// TODO: create new hat for each report
// TODO: create a new role hat
function createReport(
uint256 _hatId, // the hatId of the project that the report is created for
address[] calldata _contributors,
Expand Down Expand Up @@ -207,6 +215,8 @@ contract CEP is AccessControl, Errors, IERC1155Receiver {
(, Evaluation evaluation) =
_createEvaluationWithPool(profile.id, reportHatsId, _amount, profile.metadata, _proposor, _contributors);

evaluationAddrByHatId[reportHatsId] = address(evaluation);

// mint the hat to the evaluation contract
hats.mintHat(reportHatsId, address(evaluation));

Expand Down Expand Up @@ -256,6 +266,39 @@ contract CEP is AccessControl, Errors, IERC1155Receiver {
emit ImpactReportCreated(_hatId, reportHatsId, proposalId);
}

function createRole(
uint256 _poolId,
string memory _metadata,
address[] memory _wearers,
string memory _imageURL
)
external
{
EvaluationPool memory evaluationPool = evaluations[_poolId];
// check the caller is the owner of the evaluation contract
require(Evaluation(evaluationPool.evaluation).owner() == msg.sender, INVALID());
// check the wearers array is not empty
require(_wearers.length > 0, INVALID());
// check the metadata is not empty
require(bytes(_metadata).length > 0, INVALID());

// create a new hat for the role
uint256 roleHatId = hats.createHat(
evaluationPool.projectHatId,
_metadata,
uint32(_wearers.length),
0x0000000000000000000000000000000000004A75,
0x0000000000000000000000000000000000004A75,
true,
_imageURL
);
for (uint256 i = 0; i < _wearers.length; i++) {
hats.mintHat(roleHatId, _wearers[i]);
}

emit RoleCreated(evaluationPool.projectHatId, roleHatId, _wearers, _metadata, _imageURL);
}

/// @dev deploy a new evaluation contract with create2 and initialize it
/// @dev create a new evaluation pool struct and store it in the evaluations mapping
function _createEvaluationWithPool(
Expand Down Expand Up @@ -406,6 +449,11 @@ contract CEP is AccessControl, Errors, IERC1155Receiver {
override
returns (bytes4)
{
console.logAddress(operator);
console.logAddress(from);
console.logBytes(data);
console.logUint(id);
console.logUint(value);
return this.onERC1155Received.selector;
}

Expand All @@ -422,6 +470,13 @@ contract CEP is AccessControl, Errors, IERC1155Receiver {
override
returns (bytes4)
{
console.logAddress(operator);
console.logAddress(from);
console.logBytes(data);
for (uint256 i = 0; i < ids.length; i++) {
console.logUint(ids[i]);
console.logUint(values[i]);
}
return this.onERC1155BatchReceived.selector;
}
}

0 comments on commit fe0c041

Please sign in to comment.