-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(nfts): trailblazers "registerEvent" contract #18198
Open
KorbinianK
wants to merge
5
commits into
main
Choose a base branch
from
feat/trailblazer--event-register-contract
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+685
−14
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
241 changes: 241 additions & 0 deletions
241
packages/nfts/contracts/eventRegister/EventRegister.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,241 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24.0; | ||
|
||
import "@openzeppelin/contracts/access/AccessControl.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 { | ||
/** | ||
* @dev The role identifier for event managers. This role allows accounts to create events | ||
* and manage registrations. | ||
*/ | ||
bytes32 public constant EVENT_MANAGER_ROLE = keccak256("EVENT_MANAGER_ROLE"); | ||
|
||
/** | ||
* @dev Represents an event with its associated details. | ||
*/ | ||
struct Event { | ||
uint256 id; | ||
///< Unique identifier for the event. | ||
string name; | ||
///< Name of the event. | ||
bool exists; | ||
///< Flag indicating whether the event exists. | ||
bool registrationOpen; | ||
} | ||
///< Flag indicating whether registrations are open for the event. | ||
|
||
/** | ||
* @dev Mapping from event ID to Event details. | ||
*/ | ||
mapping(uint256 => Event) public events; | ||
|
||
/** | ||
* @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 => mapping(address => bool)) public registrations; | ||
|
||
/** | ||
* @dev Emitted when a new event is created. | ||
* @param id The unique identifier of the created event. | ||
* @param name The name of the created event. | ||
*/ | ||
event EventCreated(uint256 id, string name); | ||
|
||
/** | ||
* @dev Emitted when a user registers for an event. | ||
* @param registrant The address of the user who registered. | ||
* @param eventId The unique identifier of the event for which the user registered. | ||
*/ | ||
event Registered(address indexed registrant, uint256 eventId); | ||
|
||
/** | ||
* @dev Emitted when registrations are opened for an event. | ||
* @param eventId The unique identifier of the event whose registrations are opened. | ||
*/ | ||
event RegistrationOpened(uint256 eventId); | ||
|
||
/** | ||
* @dev Emitted when registrations are closed for an event. | ||
* @param eventId The unique identifier of the event whose registrations are closed. | ||
*/ | ||
event RegistrationClosed(uint256 eventId); | ||
|
||
/** | ||
* @dev Counter for assigning unique event IDs. | ||
*/ | ||
uint256 private nextEventId; | ||
|
||
/** | ||
* @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()); | ||
} | ||
|
||
/** | ||
* @notice Grants the EVENT_MANAGER_ROLE to a specified account. | ||
* @dev Only accounts with the DEFAULT_ADMIN_ROLE can call this function. | ||
* @param account The address to be granted the EVENT_MANAGER_ROLE. | ||
* | ||
* Requirements: | ||
* | ||
* - The caller must have the DEFAULT_ADMIN_ROLE. | ||
*/ | ||
function grantEventManagerRole(address account) external onlyRole(DEFAULT_ADMIN_ROLE) { | ||
grantRole(EVENT_MANAGER_ROLE, account); | ||
} | ||
|
||
/** | ||
* @notice Revokes the EVENT_MANAGER_ROLE from a specified account. | ||
* @dev Only accounts with the DEFAULT_ADMIN_ROLE can call this function. | ||
* @param account The address from which the EVENT_MANAGER_ROLE will be revoked. | ||
* | ||
* Requirements: | ||
* | ||
* - The caller must have the DEFAULT_ADMIN_ROLE. | ||
*/ | ||
function revokeEventManagerRole(address account) external onlyRole(DEFAULT_ADMIN_ROLE) { | ||
revokeRole(EVENT_MANAGER_ROLE, account); | ||
} | ||
|
||
/** | ||
* @notice Creates a new event with the given name. | ||
* @dev Only accounts with the EVENT_MANAGER_ROLE can call this function. | ||
* Emits EventCreated and RegistrationOpened events upon successful creation. | ||
* @param _name The name of the event to be created. | ||
* | ||
* Requirements: | ||
* | ||
* - The caller must have the EVENT_MANAGER_ROLE. | ||
*/ | ||
function createEvent(string memory _name) external onlyRole(EVENT_MANAGER_ROLE) { | ||
uint256 eventId = nextEventId; | ||
events[eventId] = Event({ id: eventId, name: _name, exists: true, registrationOpen: true }); | ||
emit EventCreated(eventId, _name); | ||
emit RegistrationOpened(eventId); // Emit event indicating registrations are open | ||
nextEventId++; | ||
} | ||
|
||
/** | ||
* @notice Opens registrations for a specific event. | ||
* @dev Only accounts with the EVENT_MANAGER_ROLE can call this function. | ||
* Emits a RegistrationOpened event upon successful operation. | ||
* @param _eventId The unique identifier of the event for which to open registrations. | ||
* | ||
* Requirements: | ||
* | ||
* - The event with `_eventId` must exist. | ||
* - Registrations for the event must currently be closed. | ||
* - The caller must have the EVENT_MANAGER_ROLE. | ||
*/ | ||
function openRegistration(uint256 _eventId) external onlyRole(EVENT_MANAGER_ROLE) { | ||
require(events[_eventId].exists, "Event does not exist"); | ||
require(!events[_eventId].registrationOpen, "Registrations are already open for this event"); | ||
|
||
events[_eventId].registrationOpen = true; | ||
emit RegistrationOpened(_eventId); | ||
} | ||
|
||
/** | ||
* @notice Closes registrations for a specific event. | ||
* @dev Only accounts with the EVENT_MANAGER_ROLE can call this function. | ||
* Emits a RegistrationClosed event upon successful operation. | ||
* @param _eventId The unique identifier of the event for which to close registrations. | ||
* | ||
* Requirements: | ||
* | ||
* - The event with `_eventId` must exist. | ||
* - Registrations for the event must currently be open. | ||
* - The caller must have the EVENT_MANAGER_ROLE. | ||
*/ | ||
function closeRegistration(uint256 _eventId) external onlyRole(EVENT_MANAGER_ROLE) { | ||
require(events[_eventId].exists, "Event does not exist"); | ||
require( | ||
events[_eventId].registrationOpen, "Registrations are already closed for this event" | ||
); | ||
|
||
events[_eventId].registrationOpen = false; | ||
emit RegistrationClosed(_eventId); | ||
} | ||
|
||
/** | ||
* @notice Allows a user to register for a specific event. | ||
* @dev Emits a Registered event upon successful registration. | ||
* @param _eventId The unique identifier of the event to register for. | ||
* | ||
* Requirements: | ||
* | ||
* - The event with `_eventId` must exist. | ||
* - Registrations for the event must be open. | ||
* - The caller must not have already registered for the event. | ||
*/ | ||
function register(uint256 _eventId) external { | ||
Event memory currentEvent = events[_eventId]; | ||
require(currentEvent.exists, "Event does not exist"); | ||
require(currentEvent.registrationOpen, "Registrations for this event are closed"); | ||
require(!registrations[_eventId][msg.sender], "Already registered for this event"); | ||
|
||
registrations[_eventId][msg.sender] = true; | ||
|
||
emit Registered(msg.sender, _eventId); | ||
} | ||
|
||
/** | ||
* @notice Retrieves all event IDs for which a user has registered. | ||
* @dev Iterates through all existing events to compile a list of registrations. | ||
* @param _user The address of the user whose registrations are to be retrieved. | ||
* @return An array of event IDs that the user has registered for. | ||
* | ||
*/ | ||
function getRegisteredEvents(address _user) external view returns (uint256[] memory) { | ||
uint256[] memory temp = new uint256[](nextEventId); | ||
uint256 count = 0; | ||
|
||
for (uint256 i = 0; i < nextEventId; i++) { | ||
if (registrations[i][_user]) { | ||
temp[count] = i; | ||
count++; | ||
} | ||
} | ||
|
||
// Create a fixed-size array to return | ||
uint256[] memory registeredEvents = new uint256[](count); | ||
for (uint256 j = 0; j < count; j++) { | ||
registeredEvents[j] = temp[j]; | ||
} | ||
|
||
return registeredEvents; | ||
} | ||
|
||
/** | ||
* @notice Retrieves the details of a specific event. | ||
* @dev Returns the event's ID, name, and registration status. | ||
* @param _eventId The unique identifier of the event to retrieve. | ||
* @return id The unique identifier of the event. | ||
* @return name The name of the event. | ||
* @return registrationOpen_ A boolean indicating whether registrations are open for the event. | ||
* | ||
* Requirements: | ||
* | ||
* - The event with `_eventId` must exist. | ||
*/ | ||
function getEvent(uint256 _eventId) | ||
external | ||
view | ||
returns (uint256 id, string memory name, bool registrationOpen_) | ||
{ | ||
require(events[_eventId].exists, "Event does not exist"); | ||
Event memory e = events[_eventId]; | ||
return (e.id, e.name, e.registrationOpen); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
|
||
import { Script, console } from "forge-std/src/Script.sol"; | ||
import { EventRegister } from "../../../contracts/eventRegister/EventRegister.sol"; | ||
|
||
contract DeployEventRegisterScript is Script { | ||
function run() public { | ||
uint256 deployerPrivateKey = vm.envUint("MAINNET_PRIVATE_KEY"); | ||
address deployerAddress = vm.addr(deployerPrivateKey); | ||
|
||
vm.startBroadcast(deployerPrivateKey); | ||
|
||
EventRegister eventRegister = new EventRegister(); | ||
|
||
console.log("Deployed EventRegister to:", address(eventRegister), "from", deployerAddress); | ||
|
||
vm.stopBroadcast(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor reorder comments