Skip to content

Commit

Permalink
fix apocalypse mode unclaimedPayouts calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincharm committed Aug 29, 2024
1 parent d75474b commit 9b4b0b4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
37 changes: 26 additions & 11 deletions contracts/Lootery.sol
Original file line number Diff line number Diff line change
Expand Up @@ -537,12 +537,37 @@ contract Lootery is
/// as necessary.
function _setupNextGame() internal {
uint248 gameId = currentGame.id;

// Ready for next game
currentGame = CurrentGame({state: GameState.Purchase, id: gameId + 1});

// Set up next game
gameData[gameId + 1] = Game({
ticketsSold: 0,
startedAt: uint64(block.timestamp),
winningPickId: 0
});

// Roll over jackpot if no winner
uint256 winningPickId = gameData[gameId].winningPickId;
uint256 numWinners = tokenByPickIdentity[gameId][winningPickId].length;
uint256 currentUnclaimedPayouts = unclaimedPayouts;
uint256 currentJackpot = jackpot;
if (numWinners == 0) {
if (numWinners == 0 && !isGameActive()) {
// No winners, but apocalypse mode
uint256 nextJackpot = 0;
uint256 nextUnclaimedPayouts = currentUnclaimedPayouts +
currentJackpot;
jackpot = 0;
unclaimedPayouts = nextUnclaimedPayouts;
emit JackpotRollover(
gameId,
currentUnclaimedPayouts,
currentJackpot,
nextUnclaimedPayouts,
nextJackpot
);
} else if (numWinners == 0) {
// No winners, current jackpot and unclaimed payouts are rolled
// over to the next game
uint256 nextJackpot = currentUnclaimedPayouts + currentJackpot;
Expand Down Expand Up @@ -570,16 +595,6 @@ contract Lootery is
0
);
}

// Ready for next game
currentGame = CurrentGame({state: GameState.Purchase, id: gameId + 1});

// Set up next game
gameData[gameId + 1] = Game({
ticketsSold: 0,
startedAt: uint64(block.timestamp),
winningPickId: 0
});
}

/// @notice Claim a share of the jackpot with a winning ticket.
Expand Down
1 change: 1 addition & 0 deletions test/Lootery.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,7 @@ describe('Lootery', () => {

// No winners -> each ticket claims share of jackpot
const unclaimedPayouts = await lotto.unclaimedPayouts()
expect(unclaimedPayouts).to.be.gt(0n)
const expectedShare = unclaimedPayouts / BigInt(tickets.length)
for (const ticket of tickets) {
const balanceBefore = await testERC20.balanceOf(ticket.owner)
Expand Down

0 comments on commit 9b4b0b4

Please sign in to comment.