Skip to content

Commit

Permalink
Merge pull request #88 from multiversx/launchpad-v2-audit-fixes-unit-…
Browse files Browse the repository at this point in the history
…tests

Audit fixes unit tests
  • Loading branch information
psorinionut authored Dec 3, 2024
2 parents b7064ef + ddf0836 commit 0799d90
Show file tree
Hide file tree
Showing 5 changed files with 601 additions and 25 deletions.
4 changes: 4 additions & 0 deletions launchpad-common/src/winner_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ pub trait WinnerSelectionModule:

let (mut rng, mut ticket_position) = self.load_select_winners_operation();
let run_result = self.run_while_it_has_gas(|| {
if nr_winning_tickets == 0 {
return STOP_OP;
}

self.shuffle_single_ticket(&mut rng, ticket_position, last_ticket_position);

if ticket_position == nr_winning_tickets {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ pub trait GuaranteedTicketWinnersModule:
op.leftover_tickets += remaining_tickets;
}

// TODO - add a check if current_ticket_pos > last_ticket_pos
fn distribute_leftover_tickets(
&self,
op: &mut GuaranteedTicketsSelectionOperation<Self::Api>,
Expand Down
52 changes: 29 additions & 23 deletions launchpad-guaranteed-tickets-v2/src/guaranteed_tickets_init.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

pub const MAX_TICKETS_ALLOWANCE: usize = 255;
pub const MAX_GUARANTEED_TICKETS_ENTRIES: usize = 10;

#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, TypeAbi, ManagedVecItem)]
Expand Down Expand Up @@ -55,6 +56,18 @@ pub trait GuaranteedTicketsInitModule:

for multi_arg in address_number_pairs.into_iter() {
let (buyer, total_tickets_allowance, guaranteed_ticket_raw) = multi_arg.into_tuple();
if total_tickets_allowance == 0 {
continue;
}

require!(
!self.blockchain().is_smart_contract(&buyer),
"Only user accounts can participate"
);
require!(
total_tickets_allowance <= MAX_TICKETS_ALLOWANCE,
"Total number of tickets exceeds maximum allowed"
);
require!(
guaranteed_ticket_raw.len() <= MAX_GUARANTEED_TICKETS_ENTRIES,
"Number of guaranteed tickets entries exceeds maximum allowed"
Expand Down Expand Up @@ -118,19 +131,17 @@ pub trait GuaranteedTicketsInitModule:
let mut nr_winning_tickets = self.nr_winning_tickets().get();
let mut total_guaranteed_tickets = self.total_guaranteed_tickets().get();
for user in users {
let was_whitelisted = whitelist.swap_remove(&user);
if was_whitelisted {
let user_ticket_status = self.user_ticket_status(&user).take();
let guaranteed_tickets_recovered = user_ticket_status
.guaranteed_tickets_info
.iter()
.fold(0, |acc, info| acc + info.guaranteed_tickets);

nr_winning_tickets += guaranteed_tickets_recovered;
total_guaranteed_tickets -= guaranteed_tickets_recovered;
self.blacklist_user_ticket_status(&user)
.set(user_ticket_status);
}
let _ = whitelist.swap_remove(&user);
let user_ticket_status = self.user_ticket_status(&user).take();
let guaranteed_tickets_recovered = user_ticket_status
.guaranteed_tickets_info
.iter()
.fold(0, |acc, info| acc + info.guaranteed_tickets);

nr_winning_tickets += guaranteed_tickets_recovered;
total_guaranteed_tickets -= guaranteed_tickets_recovered;
self.blacklist_user_ticket_status(&user)
.set(user_ticket_status);
}

self.nr_winning_tickets().set(nr_winning_tickets);
Expand All @@ -143,18 +154,11 @@ pub trait GuaranteedTicketsInitModule:
let mut total_guaranteed_tickets = self.total_guaranteed_tickets().get();
let mut whitelist = self.users_with_guaranteed_ticket();
for user in users {
let user_ticket_status_mapper = self.user_ticket_status(&user);
if !user_ticket_status_mapper.is_empty()
|| self.ticket_range_for_address(&user).is_empty()
{
if self.ticket_range_for_address(&user).is_empty() {
continue;
}

let blacklist_user_ticket_status_mapper = self.blacklist_user_ticket_status(&user);
if blacklist_user_ticket_status_mapper.is_empty() {
continue;
}
let blacklist_user_ticket_status = blacklist_user_ticket_status_mapper.take();
let blacklist_user_ticket_status = self.blacklist_user_ticket_status(&user).take();
let guaranteed_tickets_added = blacklist_user_ticket_status
.guaranteed_tickets_info
.iter()
Expand All @@ -168,8 +172,10 @@ pub trait GuaranteedTicketsInitModule:
whitelist.insert(user.clone());
nr_winning_tickets -= guaranteed_tickets_added;
total_guaranteed_tickets += guaranteed_tickets_added;
user_ticket_status_mapper.set(blacklist_user_ticket_status);
}

self.user_ticket_status(&user)
.set(blacklist_user_ticket_status);
}

self.nr_winning_tickets().set(nr_winning_tickets);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ where
);
sc.add_tickets_endpoint(args);

// 1 ticket for the max tier gets removed
// 1 ticket for the guaranteed entry gets removed
assert_eq!(sc.nr_winning_tickets().get(), nr_winning_tickets - 1);
assert_eq!(sc.users_with_guaranteed_ticket().len(), 1);
assert!(sc
Expand Down
Loading

0 comments on commit 0799d90

Please sign in to comment.