Fix: Jackpot Calculation and Comprehensive Test Coverage for Prize Accumulation#550
Merged
kimcascante merged 8 commits intoFutureMindsTeam:mainfrom Oct 13, 2025
Conversation
…tions - Removed accumulatedPrize parameter from Initialize and CreateNewDraw functions. - Introduced GetVaultBalance function to calculate jackpot automatically from vault balance. - Added JackpotCalculated event to emit jackpot details upon draw creation. - Updated totalPrizesDistributed mapping to track prizes distributed per draw. - Enhanced jackpot calculation logic to ensure accurate prize distribution.
… users from the /dapp route to /dapp/dashboard. - Updated Navbar and HeroSection components to point to the new /dapp/dashboard route instead of /dapp. - Modified deployed contracts to reflect changes in jackpot calculation and prize distribution logic. - Enhanced lottery contract functions to streamline prize distribution and jackpot management. - Updated tests to align with the new jackpot calculation and removed unnecessary parameters from initialization functions.
…ze distribution handling. Updated jackpot determination based on previous draw's prize distribution status, ensuring accurate carryover of funds and safety checks for sufficient jackpot amounts. Enhanced comments for clarity.
…rove test coverage for jackpot calculations. Updated tests to validate jackpot accumulation across multiple draws and ensure accurate prize distribution handling. Introduced helper functions for ticket number generation and mock setups to streamline testing processes.
….com/davidmelendez/starklotto into fix/Jackpot-Validation-CreateNewDraw
drakkomaximo
approved these changes
Oct 12, 2025
xJeffx23
approved these changes
Oct 12, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix: Jackpot Calculation and Comprehensive Test Coverage for Prize Accumulation
📌 Description
This PR fixes critical issues in jackpot calculation logic and adds comprehensive test coverage for lottery fund accumulation across multiple draws. The main fix ensures that when creating a new lottery draw after prize distribution, the jackpot is calculated correctly based on the previous draw's accumulated prize minus distributed prizes, rather than using the entire vault balance which incorrectly included administrative fees (45%).
Additionally, this PR refactors the test suite to properly validate jackpot accumulation scenarios and makes the
Ticketstruct public to enable better testing capabilities.🎯 Motivation and Context
Problem 1: Jackpot Duplication
When creating a new lottery after distributing prizes, the jackpot was calculated as
vault_balance - prizes_distributed. This incorrectly included the 45% administrative fees stored in the vault, causing jackpot values to be inflated. For example:Problem 2: Insufficient Test Coverage
The existing tests only verified
jackpot >= 0without validating actual accumulation logic. Critical scenarios like prize distribution impact, carry-over between draws, and progressive accumulation were not tested.Closes #455
🛠️ How to Test the Change
Testing Jackpot Calculation Fix:
DrawNumbersandDistributePrizes(e.g., 1.65 STRKP distributed)CreateNewDrawWithDurationTesting New Test Suite:
Key tests to verify:
test_get_accumulated_prize_after_create_new_draw- Validates 2.75 STRKP carry-overtest_jackpot_accumulation_across_three_draws- Validates progression: 5.5 → 8.25 → 11 STRKPtest_jackpot_carryover_without_distribution- Validates full carry-over when prizes not distributedtest_jackpot_after_prize_distribution- Validates jackpot reduction after distributiontest_progressive_jackpot_five_draws- Validates 5-draw accumulationtest_jackpot_edge_cases- Validates zero jackpot scenarios🖼️ Screenshots
First lottery: two tickets were purchased, leaving 5.5 STRKP in the prize pool.
In the first lottery, there were no winners, so the pool rolled over to the second lottery, totaling 11 STRKP after purchasing two new tickets.
The following image shows the winning numbers for the second lottery.
Based on the winning numbers, this ticket has three matching numbers, earning a reward of 1.1 STRKP.
Finally, in the third lottery, the prize pool decreased from 11 STRKP to 9.9 STRKP due to the previous 1.1 STRKP payout, visually demonstrating that the pool accumulation and reward distribution logic works consistently.
Additionally, the modified tests implementing the new pool accumulation logic and the new test cases were executed successfully, with no errors or warnings.
🔍 Type of Change
✅ Checklist Before Merging
📌 Additional Notes
Core Contract Changes (
Lottery.cairo)1. Fixed Jackpot Calculation Logic (lines 788-815)
2. Made Ticket Struct Public (lines 17-29)
struct Tickettopub struct TicketTest Suite Refactoring
Deleted 5 Duplicate/Unnecessary Tests:
test_get_jackpot_history_performance(test_jackpot_history.cairo)test_jackpot_entry_getters_after_drawing(test_lottery_getters.cairo)test_get_jackpot_history_initial(test_lottery_getters.cairo)test_get_jackpot_history_multiple_draws(test_lottery_getters.cairo)test_get_jackpot_history_after_completing_draws(test_lottery_getters.cairo)Modified 6 Existing Tests:
test_get_accumulated_prize_after_create_new_draw- Now validates exact 2.75 STRKP carry-overtest_prize_distribution_with_updated_jackpot- Renamed and adapted to work with random winning numberstest_get_jackpot_entry_amount- Now validates exact 2.75 STRKP with ticket purchasetest_get_jackpot_entry_start_block- Changed from timestamps to blockstest_get_jackpot_entry_end_block- Changed from timestamps to blockstest_get_jackpot_history_multiple_draws- Added ticket purchases to validate progressive accumulationCreated 7 New Critical Scenario Tests:
test_jackpot_accumulation_across_three_draws- Validates 5.5 → 8.25 → 11 STRKP progressiontest_jackpot_multiple_purchases_same_draw- Validates 3 tickets = 8.25 STRKPtest_jackpot_carryover_without_distribution- Validates full carry-over (5.5 STRKP)test_jackpot_after_prize_distribution- Validates jackpot reduction after DistributePrizestest_progressive_jackpot_five_draws- Validates 5 consecutive draws: 2.75 → 5.5 → 8.25 → 11 → 13.75 STRKPtest_external_funds_addition_to_jackpot- Documents AddExternalFunds behaviortest_jackpot_edge_cases- Validates zero jackpot scenariostest_vault_balance_matches_jackpot_allocation- Validates 55% allocation (8.25 of 15 STRKP)Test Infrastructure Improvements
Added helper functions to
test_jackpot_history.cairo:create_valid_numbers()- Generates valid lottery numberscreate_valid_numbers_array(quantity)- Creates multiple ticket number setssetup_mocks_for_buy_ticket()- Configures ERC20 mocks for ticket purchasessetup_mocks_for_multiple_tickets()- Simplified mock setup for multiple ticketscleanup_mocks()- Cleans up mock callsdeploy_lottery()to return mock ERC20 address for proper test setupKey Validations