From dc1090b478dfebedfb42569efff0c77b16172460 Mon Sep 17 00:00:00 2001 From: Stephanie Nwankwo Date: Sat, 26 Oct 2024 16:21:43 +0100 Subject: [PATCH 1/4] feat: add end_event_registration and test --- src/base/errors.cairo | 2 ++ src/base/types.cairo | 2 +- src/events/events.cairo | 48 +++++++++++++++++++++++++++---- src/interfaces/IEvent.cairo | 10 +++---- tests/test_contract.cairo | 56 +++++++++++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+), 12 deletions(-) diff --git a/src/base/errors.cairo b/src/base/errors.cairo index 2849192..5752832 100644 --- a/src/base/errors.cairo +++ b/src/base/errors.cairo @@ -2,4 +2,6 @@ pub mod Errors { pub const ZERO_ADDRESS_OWNER: felt252 = 'Owner cannot be zero addr'; pub const ZERO_ADDRESS_CALLER: felt252 = 'Caller cannot be zero addr'; pub const NOT_OWNER: felt252 = 'Caller Not Owner'; + pub const INVALID_EVENT: felt252 = 'Invalid event'; + pub const EVENT_CLOSED: felt252 = 'Event closed'; } diff --git a/src/base/types.cairo b/src/base/types.cairo index 5381239..6d2f40f 100644 --- a/src/base/types.cairo +++ b/src/base/types.cairo @@ -2,7 +2,7 @@ use core::serde::Serde; use core::option::OptionTrait; use core::starknet::ContractAddress; -#[derive(Drop, Serde, starknet::Store, Clone)] +#[derive(Debug, Drop, Serde, starknet::Store, Clone)] pub struct EventDetails { pub event_id: u256, pub name: ByteArray, diff --git a/src/events/events.cairo b/src/events/events.cairo index ad48bb5..372d796 100644 --- a/src/events/events.cairo +++ b/src/events/events.cairo @@ -1,9 +1,12 @@ #[starknet::contract] pub mod Events { use core::num::traits::zero::Zero; + use core::clone::Clone; + + use chainevents_contracts::base::types::{EventDetails, EventRegistration, EventType}; use chainevents_contracts::base::errors::Errors::{ - ZERO_ADDRESS_OWNER, ZERO_ADDRESS_CALLER, NOT_OWNER + ZERO_ADDRESS_OWNER, ZERO_ADDRESS_CALLER, NOT_OWNER, INVALID_EVENT, EVENT_CLOSED }; use chainevents_contracts::interfaces::IEvent::IEvent; use core::starknet::{ @@ -63,7 +66,7 @@ pub mod Events { #[derive(Drop, starknet::Event)] pub struct EndEventRegistration { pub event_id: u256, - pub event_name: felt252, + pub event_name: ByteArray, pub event_owner: ContractAddress } @@ -133,9 +136,42 @@ pub mod Events { event_id } fn register_for_event(ref self: ContractState, event_id: u256, event_fee: u256) {} - fn end_event_registration( - ref self: ContractState, event_id: u256 - ) {} // only owner can closed an event + + fn end_event_registration(ref self: ContractState, event_id: u256) { + let caller = get_caller_address(); + let event_owner = self.event_owners.read(event_id); + + // Read event details + let event_details = self.event_details.read(event_id); + + assert(!event_owner.is_zero(), INVALID_EVENT); + assert(caller == event_owner, NOT_OWNER); + assert(!event_details.is_closed, EVENT_CLOSED); + + // Create new EventDetails with updated is_closed field + let updated_event_details = EventDetails { + event_id: event_details.event_id, + name: event_details.name.clone(), + location: event_details.location, + organizer: event_details.organizer, + total_register: event_details.total_register, + total_attendees: event_details.total_attendees, + event_type: event_details.event_type, + is_closed: true, + paid_amount: event_details.paid_amount, + }; + + // Write the updated event details + self.event_details.write(event_id, updated_event_details); + + self.emit(EndEventRegistration { + event_id, + event_name: event_details.name, + event_owner: caller, + }); + } + } // only owner can closed an event + fn rsvp_for_event(ref self: ContractState, event_id: u256) {} fn upgrade_event(ref self: ContractState, event_id: u256, paid_amount: u256) {} @@ -170,4 +206,4 @@ pub mod Events { event_attendance_details } } -} + diff --git a/src/interfaces/IEvent.cairo b/src/interfaces/IEvent.cairo index cfe57c9..2e318bf 100644 --- a/src/interfaces/IEvent.cairo +++ b/src/interfaces/IEvent.cairo @@ -8,12 +8,12 @@ pub trait IEvent { fn end_event_registration( ref self: TContractState, event_id: u256 ); // 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 rsvp_for_event(ref self: TContractState, event_id: u256); + // fn upgrade_event(ref self: TContractState, event_id: u256, paid_amount: u256); // GETTER FUNCTION - fn event_details(self: @TContractState, event_id: u256) -> EventDetails; - fn event_owner(self: @TContractState, event_id: u256) -> ContractAddress; - fn attendee_event_details(self: @TContractState, event_id: u256) -> EventRegistration; + // fn event_details(self: @TContractState, event_id: u256) -> EventDetails; + // fn event_owner(self: @TContractState, event_id: u256) -> ContractAddress; + // fn attendee_event_details(self: @TContractState, event_id: u256) -> EventRegistration; } diff --git a/tests/test_contract.cairo b/tests/test_contract.cairo index e0701f3..8cbeca1 100644 --- a/tests/test_contract.cairo +++ b/tests/test_contract.cairo @@ -45,3 +45,59 @@ fn test_add_event() { stop_cheat_caller_address(event_contract_address); } +#[test] +fn test_end_event_registration() { + 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"); + assert(event_id == 1, 'Event was not created'); + + event_dispatcher.end_event_registration(event_id); + let event_details = event_dispatcher.event_details(event_id); + + assert(event_details.is_closed == true, 'Event was not closed'); + stop_cheat_caller_address(event_contract_address); +} + +#[test] +#[should_panic(expected: ('NOT_OWNER',))] +fn test_end_event_registration_not_owner() { + 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.end_event_registration(event_id); + stop_cheat_caller_address(event_contract_address); +} + +#[test] +#[should_panic(expected: ('INVALID_EVENT',))] +fn test_end_event_registration_invalid_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()); + event_dispatcher.end_event_registration(999); + stop_cheat_caller_address(event_contract_address); +} + +#[test] +#[should_panic(expected: ('EVENT_CLOSED',))] +fn test_end_event_registration_already_closed() { + 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"); + + event_dispatcher.end_event_registration(event_id); + + event_dispatcher.end_event_registration(event_id); + stop_cheat_caller_address(event_contract_address); +} From d4bbb36b4679fbdf6b5546cf274da75ad3f476fc Mon Sep 17 00:00:00 2001 From: Stephanie Nwankwo Date: Sat, 26 Oct 2024 16:44:28 +0100 Subject: [PATCH 2/4] feat: add end_event_registration and test --- src/interfaces/IEvent.cairo | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/interfaces/IEvent.cairo b/src/interfaces/IEvent.cairo index 2e318bf..cfe57c9 100644 --- a/src/interfaces/IEvent.cairo +++ b/src/interfaces/IEvent.cairo @@ -8,12 +8,12 @@ pub trait IEvent { fn end_event_registration( ref self: TContractState, event_id: u256 ); // 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 rsvp_for_event(ref self: TContractState, event_id: u256); + fn upgrade_event(ref self: TContractState, event_id: u256, paid_amount: u256); // GETTER FUNCTION - // fn event_details(self: @TContractState, event_id: u256) -> EventDetails; - // fn event_owner(self: @TContractState, event_id: u256) -> ContractAddress; - // fn attendee_event_details(self: @TContractState, event_id: u256) -> EventRegistration; + fn event_details(self: @TContractState, event_id: u256) -> EventDetails; + fn event_owner(self: @TContractState, event_id: u256) -> ContractAddress; + fn attendee_event_details(self: @TContractState, event_id: u256) -> EventRegistration; } From 53b8d794d85eecac25615402df642acc4eaa47e7 Mon Sep 17 00:00:00 2001 From: Stephanie Nwankwo Date: Sat, 26 Oct 2024 16:46:45 +0100 Subject: [PATCH 3/4] feat: add end_event_registration and test --- Scarb.lock | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Scarb.lock b/Scarb.lock index 777e152..dbb367e 100644 --- a/Scarb.lock +++ b/Scarb.lock @@ -95,7 +95,15 @@ name = "openzeppelin_utils" version = "0.15.0" source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.15.0#f57642960f1c8cffafefb88bfff418eca8510634" +[[package]] +name = "snforge_scarb_plugin" +version = "0.31.0" +source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.31.0#72ea785ca354e9e506de3e5d687da9fb2c1b3c67" + [[package]] name = "snforge_std" -version = "0.27.0" -source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.27.0#2d99b7c00678ef0363881ee0273550c44a9263de" +version = "0.31.0" +source = "git+https://github.com/foundry-rs/starknet-foundry?tag=v0.31.0#72ea785ca354e9e506de3e5d687da9fb2c1b3c67" +dependencies = [ + "snforge_scarb_plugin", +] From 96aa790e9c4579a606b3f632434c7afaf1503ece Mon Sep 17 00:00:00 2001 From: Stephanie Nwankwo Date: Sat, 26 Oct 2024 16:48:50 +0100 Subject: [PATCH 4/4] feat: resolve conflict --- src/events/events.cairo | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/events/events.cairo b/src/events/events.cairo index 13d501f..6f0be32 100644 --- a/src/events/events.cairo +++ b/src/events/events.cairo @@ -6,10 +6,9 @@ pub mod Events { use chainevents_contracts::base::types::{EventDetails, EventRegistration, EventType}; use chainevents_contracts::base::errors::Errors::{ - ZERO_ADDRESS_OWNER, ZERO_ADDRESS_CALLER, NOT_OWNER, INVALID_EVENT, EVENT_CLOSED + ZERO_ADDRESS_OWNER, ZERO_ADDRESS_CALLER, NOT_OWNER, INVALID_EVENT, EVENT_CLOSED, - ZERO_ADDRESS_OWNER, ZERO_ADDRESS_CALLER, NOT_OWNER, CLOSED_EVENT, ALREADY_REGISTERED, - NOT_REGISTERED, ALREADY_RSVP + }; use chainevents_contracts::interfaces::IEvent::IEvent; use core::starknet::{