Skip to content

Commit

Permalink
feat(nfts): eventRegister contract update (#18547)
Browse files Browse the repository at this point in the history
  • Loading branch information
KorbinianK authored Dec 13, 2024
1 parent 3966bb1 commit 47c0648
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 15 deletions.
35 changes: 24 additions & 11 deletions packages/nfts/contracts/eventRegister/EventRegister.sol
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24.0;

import "@openzeppelin/contracts/access/AccessControl.sol";

import { Ownable2StepUpgradeable } from
"@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol";
import { AccessControlUpgradeable } from
"@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
/**
* @title EventRegister
* @notice A contract that allows authorized managers to create events, manage user registrations,
* and track user participation using role-based access control.
* @dev Utilizes OpenZeppelin's AccessControl for role management. The contract does not hold any
* Ether.
*/
contract EventRegister is AccessControl {

contract EventRegister is Ownable2StepUpgradeable, AccessControlUpgradeable {
/**
* @dev The role identifier for event managers. This role allows accounts to create events
* and manage registrations.
Expand Down Expand Up @@ -40,8 +43,7 @@ contract EventRegister is AccessControl {
* @dev Mapping from event ID to a mapping of user addresses to their registration status.
* Indicates whether a user has registered for a specific event.
*/
mapping(uint256 eventId => mapping(address registrant => bool isRegistered)) public
registrations;
mapping(uint256 eventId => mapping(address registrant => bool isRegistered)) public isRegistered;

/**
* @dev Emitted when a new event is created.
Expand Down Expand Up @@ -81,14 +83,25 @@ contract EventRegister is AccessControl {
*/
uint256 private nextEventId;

/**
* @notice Contract initializer
* @dev Initializes the contract by granting the deployer the default admin role.
* The deployer is also granted the EVENT_MANAGER_ROLE.
* The deployer is set as the owner of the contract.
*/
function initialize() external initializer {
__Context_init();
_grantRole(EVENT_MANAGER_ROLE, _msgSender());
_transferOwnership(_msgSender());
}

/**
* @notice Initializes the contract by granting the deployer the default admin role.
* @dev The deployer of the contract is granted the DEFAULT_ADMIN_ROLE, allowing them to manage
* roles.
*/
constructor() {
_grantRole(DEFAULT_ADMIN_ROLE, _msgSender());
_grantRole(EVENT_MANAGER_ROLE, _msgSender());
}

/**
Expand Down Expand Up @@ -190,9 +203,9 @@ contract EventRegister is AccessControl {
Event memory currentEvent = events[_eventId];
require(currentEvent.exists, "Event not found");
require(currentEvent.registrationOpen, "Registrations closed");
require(!registrations[_eventId][msg.sender], "Already registered");
require(!isRegistered[_eventId][msg.sender], "Already registered");

registrations[_eventId][msg.sender] = true;
isRegistered[_eventId][msg.sender] = true;

emit Registered(msg.sender, _eventId);
}
Expand All @@ -212,9 +225,9 @@ contract EventRegister is AccessControl {
Event memory currentEvent = events[_eventId];
require(currentEvent.exists, "Event not found");
require(currentEvent.registrationOpen, "Registrations closed");
require(registrations[_eventId][_user], "Not registered");
require(isRegistered[_eventId][_user], "Not registered");

registrations[_eventId][_user] = false;
isRegistered[_eventId][_user] = false;
emit Unregistered(_user, _eventId);
}

Expand All @@ -230,7 +243,7 @@ contract EventRegister is AccessControl {
uint256 count = 0;

for (uint256 i = 0; i < nextEventId; i++) {
if (registrations[i][_user]) {
if (isRegistered[i][_user]) {
temp[count] = i;
count++;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/nfts/deployments/eventRegister/hekla.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"EventRegister": "0x6439Bb31dE508Dc36B8D3A39C389EB8E6C214ebE"
}
3 changes: 3 additions & 0 deletions packages/nfts/deployments/eventRegister/mainnet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"EventRegister": "0x7cbec64D099D49eA91062746B38182455c94862C"
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ contract DeployEventRegisterScript is Script {

console.log("Deployed EventRegister to:", address(eventRegister), "from", deployerAddress);

// Initialize the contract
eventRegister.initialize();

console.log("Initialized EventRegister contract.");

vm.stopBroadcast();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ contract EventRegisterTest is Test {
eventRegister.register(0);
vm.stopPrank();

assertTrue(eventRegister.registrations(0, user1), "User1 should be registered");
assertTrue(eventRegister.isRegistered(0, user1), "User1 should be registered");
}

function testOnlyEventManagersCanOpenCloseRegistrations() public {
Expand Down Expand Up @@ -125,7 +125,7 @@ contract EventRegisterTest is Test {
eventRegister.register(0);
vm.stopPrank();

assertTrue(eventRegister.registrations(0, user1), "User1 should be registered for event 0");
assertTrue(eventRegister.isRegistered(0, user1), "User1 should be registered for event 0");
}

function testUserCannotRegisterTwice() public {
Expand Down Expand Up @@ -235,8 +235,8 @@ contract EventRegisterTest is Test {
eventRegister.register(0);
vm.stopPrank();

assertTrue(eventRegister.registrations(0, user1), "User1 should be registered");
assertTrue(eventRegister.registrations(0, user2), "User2 should be registered");
assertTrue(eventRegister.isRegistered(0, user1), "User1 should be registered");
assertTrue(eventRegister.isRegistered(0, user2), "User2 should be registered");
}

function testGetRegisteredEvents() public {
Expand Down

0 comments on commit 47c0648

Please sign in to comment.