Skip to content
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: End event registration Function #38

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/base/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ 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';

pub const CLOSED_EVENT: felt252 = 'Event is closed';
pub const ALREADY_REGISTERED: felt252 = 'Caller already registered';
pub const NOT_REGISTERED: felt252 = 'rsvp only for registered event';
Expand Down
2 changes: 1 addition & 1 deletion src/base/types.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
50 changes: 46 additions & 4 deletions src/events/events.cairo
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#[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, CLOSED_EVENT, ALREADY_REGISTERED,
NOT_REGISTERED, ALREADY_RSVP
ZERO_ADDRESS_OWNER, ZERO_ADDRESS_CALLER, NOT_OWNER, INVALID_EVENT, EVENT_CLOSED,


};
use chainevents_contracts::interfaces::IEvent::IEvent;
use core::starknet::{
Expand Down Expand Up @@ -64,7 +68,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
}

Expand Down Expand Up @@ -135,6 +139,44 @@ 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) {
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 register_for_event(ref self: ContractState, event_id: u256) {
let caller = get_caller_address();
Expand Down Expand Up @@ -229,4 +271,4 @@ pub mod Events {
attendee_event_details
}
}
}

140 changes: 20 additions & 120 deletions tests/test_contract.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ use starknet::{ContractAddress};

use snforge_std::{
declare, start_cheat_caller_address, stop_cheat_caller_address, ContractClassTrait,
DeclareResultTrait, spy_events, EventSpyAssertionsTrait,
DeclareResultTrait,
};

use chainevents_contracts::interfaces::IEvent::{IEventDispatcher, IEventDispatcherTrait};

use chainevents_contracts::events::events::Events;
use chainevents_contracts::base::types::EventType;

Expand Down Expand Up @@ -47,6 +48,7 @@ fn test_add_event() {
}

#[test]
fn test_end_event_registration() {
fn test_event_registration() {
let event_contract_address = __setup__();
let event_dispatcher = IEventDispatcher { contract_address: event_contract_address };
Expand Down Expand Up @@ -83,159 +85,57 @@ fn test_event_registration() {

fn test_register_for_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");

assert(event_id == 1, 'Event was not created');

event_dispatcher.end_event_registration(event_id);
let event_details = event_dispatcher.event_details(event_id);

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);

stop_cheat_caller_address(event_contract_address);
}

#[test]
#[should_panic(expected: 'rsvp only for registered event')]
fn test_should_panic_on_rsvp_for_event_that_was_not_registered_for() {
let event_contract_address = __setup__();

let event_dispatcher = IEventDispatcher { contract_address: event_contract_address };

let caller: ContractAddress = starknet::contract_address_const::<0x123626789>();

start_cheat_caller_address(event_contract_address, caller);

let event_id: u256 = 1;

event_dispatcher.rsvp_for_event(event_id);

stop_cheat_caller_address(event_contract_address);
}

#[test]
fn test_rsvp_for_event_should_emit_event_on_success() {
let event_contract_address = __setup__();

let event_dispatcher = IEventDispatcher { contract_address: event_contract_address };

// USER_ONE adds event
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');
stop_cheat_caller_address(event_contract_address);

// Use a new user(caller) to register for event & rsvp for event
let caller: ContractAddress = starknet::contract_address_const::<0x123626789>();

start_cheat_caller_address(event_contract_address, caller);

event_dispatcher.register_for_event(event_id);

let mut spy = spy_events();

event_dispatcher.rsvp_for_event(event_id);

let expected_event = Events::Event::RSVPForEvent(
Events::RSVPForEvent { event_id: 1, attendee_address: caller }
);
spy.assert_emitted(@array![(event_contract_address, expected_event)]);

assert(event_details.is_closed == true, 'Event was not closed');
stop_cheat_caller_address(event_contract_address);
}

#[test]
#[should_panic(expected: 'rsvp already exist')]
fn test_should_panic_on_rsvp_for_event_twice() {
#[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 };

// USER_ONE adds event
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');
stop_cheat_caller_address(event_contract_address);

// Use a new user(caller) to register for event & rsvp for event
let caller: ContractAddress = starknet::contract_address_const::<0x123626789>();

start_cheat_caller_address(event_contract_address, caller);

event_dispatcher.register_for_event(event_id);

// first rsvp for event
event_dispatcher.rsvp_for_event(event_id);

// second rsvp for the same event: should panic
event_dispatcher.rsvp_for_event(event_id);

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]
fn test_event_count_increase() {
#[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());

let initial_event_id = event_dispatcher.add_event("Blockchain Conference", "Tech Park");
assert(initial_event_id == 1, 'First event ID incorrect');

let second_event_id = event_dispatcher.add_event("Ethereum Workshop", "Innovation Hub");
assert(second_event_id == 2, 'Second event ID incorrect');

event_dispatcher.end_event_registration(999);
stop_cheat_caller_address(event_contract_address);
}

#[test]
fn test_event_emission() {
#[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());

// Add event with string literals
let event_id = event_dispatcher.add_event("Devcon", "Barcelona");
assert(event_id == 1, 'Event ID mismatch');

// Get event details and compare them separately
let event_details = event_dispatcher.event_details(event_id);

// Compare each field independently
let name_matches = event_details.name == "Devcon";
let location_matches = event_details.location == "Barcelona";

assert(name_matches, 'Event name mismatch');
assert(location_matches, 'Event location mismatch');
assert(event_details.event_id == event_id, 'Event ID mismatch in details');
assert(!event_details.is_closed, 'Event should not be closed');

stop_cheat_caller_address(event_contract_address);
}

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

let user_address: ContractAddress = USER_ONE.try_into().unwrap();

start_cheat_caller_address(event_contract_address, user_address);

let event_id = event_dispatcher.add_event("bitcoin dev meetup", "Dan Marna road");
assert(event_id == 1, 'Event was not created');

let retrieved_owner = event_dispatcher.event_owner(1);
assert(retrieved_owner == user_address, 'Wrong owner returned');

event_dispatcher.end_event_registration(event_id);

event_dispatcher.end_event_registration(event_id);
stop_cheat_caller_address(event_contract_address);
}

Expand Down
Loading