Skip to content

Commit

Permalink
update comments
Browse files Browse the repository at this point in the history
  • Loading branch information
benr-ml committed Mar 17, 2024
1 parent cb75d6d commit 817695a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ module sui::random {
let epoch = tx_context::epoch(ctx);
let inner = load_inner_mut(self);
if (inner.randomness_round == 0 && inner.epoch == 0 &&
vector::is_empty(&inner.random_bytes)) {
vector::is_empty(&inner.random_bytes)) {
// First update should be for round zero.
assert!(new_round == 0, EInvalidRandomnessUpdate);
} else {
Expand Down Expand Up @@ -272,15 +272,15 @@ module sui::random {
(u128_in_range(g, (min as u128), (max as u128), 9) as u8)
}

/// Shuffle a vector using the random generator.
/// Shuffle a vector using the random generator (Fisher–Yates/Knuth shuffle).
public fun shuffle<T>(g: &mut RandomGenerator, v: &mut vector<T>) {
let n = (vector::length(v) as u32);
if (n == 0) {
return
};
let i: u32 = 0;
while (i < (n - 1)) {
let j = generate_u32_in_range(g, i, n-1);
let j = generate_u32_in_range(g, i, n - 1);
vector::swap(v, (i as u64), (j as u64));
i = i + 1;
};
Expand All @@ -300,5 +300,4 @@ module sui::random {
public fun generator_buffer(r: &RandomGenerator): &vector<u8> {
&r.buffer
}

}
11 changes: 7 additions & 4 deletions sui_programmability/examples/games/sources/raffles.move
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/// Basic raffles games that depends on Sui randomness.
///
/// Anyone can create a new lottery game with an end time and a price. After the end time, anyone can trigger
/// Anyone can create a new raffle game with an end time and a price. After the end time, anyone can trigger
/// a function to determine the winner, and the winner gets the entire balance of the game.
///
/// - raffle_with_tickets uses tickets which could be transferred to other accounts, used as NFTs, etc.
Expand Down Expand Up @@ -104,7 +104,7 @@ module games::raffle_with_tickets {
}

public fun destroy_ticket(ticket: Ticket) {
let Ticket { id, game_id: _, participant_index: _} = ticket;
let Ticket { id, game_id: _, participant_index: _ } = ticket;
object::delete(id);
}

Expand Down Expand Up @@ -150,6 +150,9 @@ module games::small_raffle {
const EGameInProgress: u64 = 0;
const EGameAlreadyCompleted: u64 = 1;
const EInvalidAmount: u64 = 2;
const EReachedMaxParticipants: u64 = 3;

const MaxParticipants: u32 = 500;

/// Game represents a set of parameters of a single game.
struct Game has key {
Expand Down Expand Up @@ -181,7 +184,7 @@ module games::small_raffle {
/// Gas based attacks are not possible since the gas cost of this function is independent of the winner.
entry fun close(game: Game, r: &Random, clock: &Clock, ctx: &mut TxContext) {
assert!(game.end_time <= clock::timestamp_ms(clock), EGameInProgress);
let Game { id, cost_in_sui: _, participants, end_time: _, balance , participants_table } = game;
let Game { id, cost_in_sui: _, participants, end_time: _, balance, participants_table } = game;
if (participants > 0) {
let generator = new_generator(r, ctx);
let winner = random::generate_u32_in_range(&mut generator, 1, participants);
Expand All @@ -192,7 +195,6 @@ module games::small_raffle {
balance::destroy_zero(balance);
};

// TODO: will this work with 10K objects?
let i = 1;
while (i <= participants) {
table::remove(&mut participants_table, i);
Expand All @@ -206,6 +208,7 @@ module games::small_raffle {
public fun play(game: &mut Game, coin: Coin<SUI>, clock: &Clock, ctx: &mut TxContext) {
assert!(game.end_time > clock::timestamp_ms(clock), EGameAlreadyCompleted);
assert!(coin::value(&coin) == game.cost_in_sui, EInvalidAmount);
assert!(game.participants < MaxParticipants, EReachedMaxParticipants);

game.participants = game.participants + 1;
coin::put(&mut game.balance, coin);
Expand Down

0 comments on commit 817695a

Please sign in to comment.