From 47c0648b05bd6d10d1f936458ff2cd73cdda2abb Mon Sep 17 00:00:00 2001 From: Korbinian Date: Fri, 13 Dec 2024 12:47:59 +0100 Subject: [PATCH] feat(nfts): eventRegister contract update (#18547) --- .../contracts/eventRegister/EventRegister.sol | 35 +++++++++++++------ .../nfts/deployments/eventRegister/hekla.json | 3 ++ .../deployments/eventRegister/mainnet.json | 3 ++ .../{Deploy.sol => Deploy.s.sol} | 5 +++ .../eventRegister/EventRegister.t.sol | 8 ++--- 5 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 packages/nfts/deployments/eventRegister/hekla.json create mode 100644 packages/nfts/deployments/eventRegister/mainnet.json rename packages/nfts/script/trailblazer/eventRegister/{Deploy.sol => Deploy.s.sol} (83%) diff --git a/packages/nfts/contracts/eventRegister/EventRegister.sol b/packages/nfts/contracts/eventRegister/EventRegister.sol index 72b01d77e8b..779bb94b543 100644 --- a/packages/nfts/contracts/eventRegister/EventRegister.sol +++ b/packages/nfts/contracts/eventRegister/EventRegister.sol @@ -1,8 +1,10 @@ // 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, @@ -10,7 +12,8 @@ import "@openzeppelin/contracts/access/AccessControl.sol"; * @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. @@ -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. @@ -81,6 +83,18 @@ 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 @@ -88,7 +102,6 @@ contract EventRegister is AccessControl { */ constructor() { _grantRole(DEFAULT_ADMIN_ROLE, _msgSender()); - _grantRole(EVENT_MANAGER_ROLE, _msgSender()); } /** @@ -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); } @@ -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); } @@ -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++; } diff --git a/packages/nfts/deployments/eventRegister/hekla.json b/packages/nfts/deployments/eventRegister/hekla.json new file mode 100644 index 00000000000..b40d00f89cb --- /dev/null +++ b/packages/nfts/deployments/eventRegister/hekla.json @@ -0,0 +1,3 @@ +{ + "EventRegister": "0x6439Bb31dE508Dc36B8D3A39C389EB8E6C214ebE" +} diff --git a/packages/nfts/deployments/eventRegister/mainnet.json b/packages/nfts/deployments/eventRegister/mainnet.json new file mode 100644 index 00000000000..32df83a78ec --- /dev/null +++ b/packages/nfts/deployments/eventRegister/mainnet.json @@ -0,0 +1,3 @@ +{ + "EventRegister": "0x7cbec64D099D49eA91062746B38182455c94862C" +} diff --git a/packages/nfts/script/trailblazer/eventRegister/Deploy.sol b/packages/nfts/script/trailblazer/eventRegister/Deploy.s.sol similarity index 83% rename from packages/nfts/script/trailblazer/eventRegister/Deploy.sol rename to packages/nfts/script/trailblazer/eventRegister/Deploy.s.sol index 647511ea4d6..4a8d576803f 100644 --- a/packages/nfts/script/trailblazer/eventRegister/Deploy.sol +++ b/packages/nfts/script/trailblazer/eventRegister/Deploy.s.sol @@ -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(); } } diff --git a/packages/nfts/test/trailblazer/eventRegister/EventRegister.t.sol b/packages/nfts/test/trailblazer/eventRegister/EventRegister.t.sol index 702165d3b2e..2176e69830a 100644 --- a/packages/nfts/test/trailblazer/eventRegister/EventRegister.t.sol +++ b/packages/nfts/test/trailblazer/eventRegister/EventRegister.t.sol @@ -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 { @@ -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 { @@ -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 {