Skip to content

Commit

Permalink
Feat: Unregister event (#64)
Browse files Browse the repository at this point in the history
* feat: fn that allows a user to unregister for the event

* test: unit test for the function

* fix: fmt

* fix: mismatch fn name

* fix

* fix: unregister function

* test: unregister test
  • Loading branch information
raizo07 authored Nov 28, 2024
1 parent 31ea92b commit 4b239c9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/events/events.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub mod Events {
OwnableEvent: OwnableComponent::Event,
#[flat]
UpgradeableEvent: UpgradeableComponent::Event,
UnregisteredEvent: UnregisteredEvent,
}

/// @notice Event emitted when a new event is created
Expand Down Expand Up @@ -116,6 +117,13 @@ pub mod Events {
pub event_type: EventType
}

#[derive(Drop, starknet::Event)]
pub struct UnregisteredEvent {
pub event_id: u256,
pub user_address: ContractAddress
}


#[derive(Drop, starknet::Event)]
pub struct EventAttendanceMark {
pub event_id: u256,
Expand Down Expand Up @@ -219,6 +227,38 @@ pub mod Events {
);
}

fn unregister_from_event(ref self: ContractState, event_id: u256) {
let caller = get_caller_address();
let event = self.event_details.read(event_id);
assert(!event.is_closed, CLOSED_EVENT);

let attendee_registration = self.attendee_event_details.read((event_id, caller));
assert(attendee_registration.attendee_address == caller, NOT_REGISTERED);

let zero_address: ContractAddress = 0.try_into().unwrap();
self
.attendee_event_details
.write(
(event_id, caller),
EventRegistration {
attendee_address: zero_address,
amount_paid: 0,
has_rsvp: false,
nft_contract_address: zero_address,
nft_token_id: 0,
organizer: zero_address
}
);

self.event_registrations.write(caller, 0);

self.registered_attendees.write(event_id, self.registered_attendees.read(event_id) - 1);
let current_count = self.attendee_event_registration_counts.read(event_id);
self.attendee_event_registration_counts.write(event_id, current_count - 1);

self.emit(UnregisteredEvent { event_id, user_address: caller });
}


/// @notice Ends registration for an event
/// @param event_id The ID of the event to close registration for
Expand Down Expand Up @@ -385,3 +425,4 @@ pub mod Events {
}
}
}

1 change: 1 addition & 0 deletions src/interfaces/IEvent.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub trait IEvent<TContractState> {
); // only owner can closed an event
fn rsvp_for_event(ref self: TContractState, event_id: u256);
fn upgrade_event(ref self: TContractState, event_id: u256, paid_amount: u256);
fn unregister_from_event(ref self: TContractState, event_id: u256);

// GETTER FUNCTION
fn event_details(self: @TContractState, event_id: u256) -> EventDetails;
Expand Down
23 changes: 23 additions & 0 deletions tests/test_contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,26 @@ fn test_upgrade_event_with_wrong_owner() {
start_cheat_caller_address(event_contract_address, USER_TWO.try_into().unwrap());
event_dispatcher.upgrade_event(event_id, 20);
}

#[test]
fn test_unregister_from_event() {
let event_contract_address = __setup__();
let event_dispatcher = IEventDispatcher { contract_address: event_contract_address };

start_cheat_caller_address(event_contract_address, USER_ONE.try_into().unwrap());
let event_id = event_dispatcher.add_event("bitcoin dev meetup", "Dan Marna road");
stop_cheat_caller_address(event_contract_address);

start_cheat_caller_address(event_contract_address, USER_TWO.try_into().unwrap());
event_dispatcher.register_for_event(event_id);

let mut spy = spy_events();
event_dispatcher.unregister_from_event(event_id);

let expected_event = Events::Event::UnregisteredEvent(
Events::UnregisteredEvent { event_id, user_address: USER_TWO.try_into().unwrap() }
);
spy.assert_emitted(@array![(event_contract_address, expected_event)]);

stop_cheat_caller_address(event_contract_address);
}

0 comments on commit 4b239c9

Please sign in to comment.