Skip to content

Commit

Permalink
Extend Play event with jackpot_amount (#7)
Browse files Browse the repository at this point in the history
* Extend Play event with jackpot_amount
  • Loading branch information
ZhmakAS authored Jun 3, 2024
1 parent 1ee9138 commit 5d5d77b
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions smart-contract/src/lottery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ pub struct Play {
/// Timestamp of the entry
pub timestamp: u64,
/// Prize won by the player (if any)
pub prize: U512,
pub prize_amount: U512,
/// Flag indicating if the entry won the jackpot
pub is_jackpot: bool,
/// Current jackpot amount after play
pub jackpot_amount: U512,
}

/// Possible outcomes of a lottery play
Expand Down Expand Up @@ -188,7 +190,7 @@ impl Lottery {
.add(self.env().attached_value() - self.lottery_fee.get_or_default());

let mut is_jackpot = false;
let mut prize = U512::zero();
let mut prize_amount = U512::zero();

match self.determine_outcome() {
Outcome::Jackpot => {
Expand All @@ -198,7 +200,7 @@ impl Lottery {

// update play event state
is_jackpot = true;
prize = prize_value;
prize_amount = prize_value;

// start the next round
self.current_round_id.add(1);
Expand All @@ -208,7 +210,7 @@ impl Lottery {
self.prize_pool.subtract(prize_value);

// update play event state
prize = prize_value;
prize_amount = prize_value;
}
_ => {}
}
Expand All @@ -220,7 +222,8 @@ impl Lottery {
play_id,
timestamp: self.env().get_block_time(),
is_jackpot,
prize,
prize_amount,
jackpot_amount: self.prize_pool.get_or_default(),
});
}

Expand Down Expand Up @@ -358,9 +361,10 @@ mod tests {
round_id: expected_round,
player: alice,
play_id: expected_play,
prize: U512::from(49 * ONE_CSPR_IN_MOTES),
prize_amount: U512::from(49 * ONE_CSPR_IN_MOTES),
is_jackpot: true,
timestamp: ONE_HOUR_IN_MILLISECONDS,
jackpot_amount: U512::zero(),
},
));
assert_eq!(env.events_count(contract.address()), 2);
Expand All @@ -378,9 +382,10 @@ mod tests {
round_id: expected_round,
player: bob,
play_id: expected_play,
prize: U512::from(49 * ONE_CSPR_IN_MOTES),
prize_amount: U512::from(49 * ONE_CSPR_IN_MOTES),
is_jackpot: true,
timestamp: ONE_HOUR_IN_MILLISECONDS,
jackpot_amount: U512::zero(),
},
));
assert_eq!(env.events_count(contract.address()), 3);
Expand All @@ -398,9 +403,10 @@ mod tests {
round_id: expected_round,
player: charlie,
play_id: expected_play,
prize: U512::from(49 * ONE_CSPR_IN_MOTES),
prize_amount: U512::from(49 * ONE_CSPR_IN_MOTES),
is_jackpot: true,
timestamp: ONE_HOUR_IN_MILLISECONDS,
jackpot_amount: U512::zero(),
},
));
assert_eq!(env.events_count(contract.address()), 4);
Expand Down Expand Up @@ -431,9 +437,10 @@ mod tests {
round_id: expected_round,
player: charlie,
play_id: expected_play,
prize: U512::zero(),
prize_amount: U512::zero(),
is_jackpot: false,
timestamp: ONE_HOUR_IN_MILLISECONDS,
jackpot_amount: U512::from(49 * ONE_CSPR_IN_MOTES),
},
));
assert_eq!(env.events_count(contract.address()), 5);
Expand Down Expand Up @@ -463,9 +470,10 @@ mod tests {
round_id: expected_round,
player: charlie,
play_id: expected_play,
prize: U512::from(2 * ONE_CSPR_IN_MOTES),
prize_amount: U512::from(2 * ONE_CSPR_IN_MOTES),
is_jackpot: false,
timestamp: ONE_HOUR_IN_MILLISECONDS,
jackpot_amount: U512::from(96 * ONE_CSPR_IN_MOTES),
},
));
assert_eq!(env.events_count(contract.address()), 6);
Expand Down

0 comments on commit 5d5d77b

Please sign in to comment.