How It Works
Step-by-step explanation of the Daily Lottery process from ticket purchase to winner payout.
The Complete Lottery Flow
This page walks through every step of a Daily Lottery, explaining what happens on-chain and how you can verify each phase.
Phase 1: Lottery Creation
An admin creates a new lottery by calling the program to create the Lottery and Vault PDAs.
This creates an on-chain lottery account with:
- Unique lottery ID
- Buy and upload windows
- Prize pool (starts at 0)
- Status: Active
Each lottery has a deterministic Program Derived Address (PDA) that stores all lottery data. This address is derived from the config + lottery ID and can be looked up by anyone.
Phase 2: Ticket Purchase
User Flow
Connect Wallet
User connects their Solana wallet to the Daily Lottery app.
Select Tickets
User chooses number of tickets and enters a lucky secret (kept private until upload).
Approve Transaction
User signs a transaction that transfers SOL and mints their tickets.
Receive Confirmation
Participant account is created or updated and linked to the user's wallet.
What Happens On-Chain
When a ticket is purchased:
- SOL is transferred from user to lottery pool account
- A participant account is created or updated (PDA from lottery + wallet)
- Proof-of-chance hash is stored on first purchase (lucky secret + random salt)
- Lottery account is updated: participant count, ticket totals, prize pool
Participant Data Structure
| Field | Type | Description |
|---|---|---|
| wallet | Pubkey | Participant wallet |
| proof_of_chance_hash | [u8; 32] | Hash of lucky secret + random salt |
| tickets_bought | u64 | Total tickets for participant |
| attested_uploaded | bool | Attested during upload window |
| voted_number_of_winners | u64 | Vote for winner count |
| reveal_score | u64 | Informational score derived from lucky secret |
Phase 3: Upload Window & Attestation
When the buy window ends, the upload window opens:
- Upload lucky secret: Participants send their lucky secret to the provider
- Attest on-chain: Participants submit an attestation with their winner-count vote
- Accountability: Winner count is locked from attester votes; reveal inclusion is tracked on-chain per participant
Phase 4: Provider Uploads & Verification
The provider uploads batched lucky secrets for attested participants with available reveal data. The program:
- Verifies each lucky secret against its on-chain hash
- Marks participants as reveal-included for settlement eligibility
- Computes a reveal score for analytics/transparency
- Computes the selected winner count from weighted attester votes
The result is a deterministic, public settlement input stored on-chain.
Phase 5: Winner Selection & Settlement
After uploads complete (or after upload deadline with partial reveals), winner selection runs from the reveal-included subset:
Selection Algorithm
S0 = SHA256(0x494B494741495F5250445F56325F53454544 || lottery_id || eligible_count || total_revealed_tickets || poc_aggregate_hash)
S = hash-chain over sorted reveal-included (wallet, tickets)
round_j = SHA256(0x494B494741495F5250445F56325F44524157 || S || j) % remaining_tickets
walk ticket ranges in wallet order to find winner
remove winner, repeat for k winners
Key properties of this algorithm:
- Deterministic: Same inputs always produce same outputs
- Reproducible: Anyone with the on-chain data can verify winners
- Weighted: Ticket counts define each participant's range
- Multi-winner: Winner count comes from weighted attester votes
See Winner Selection for the full algorithm, tie breaks, and worked examples.
After winners are selected:
- Winners are finalized and stored on-chain
- Prize amounts are calculated
- Payouts are processed in batches and can be audited
Prize Calculation
prize_per_winner = (total_pool * (1 - protocol_fee)) / selected_winner_count
Prizes remain claimable indefinitely. There's no expiration on claiming your winnings.
Verifying Each Step
Every step described above creates an on-chain event that can be audited:
| Event | What to Verify |
|---|---|
| Lottery Created | Parameters and windows match expectations |
| Tickets Purchased | Your commitment and ticket totals are correct |
| Upload Phase Began | Upload window timing is correct |
| Attestation Submitted | Your attestation and vote are recorded |
| Reveals Uploaded | Lucky secrets match on-chain hashes |
| Winners Finalized | Algorithm matches documented behavior |
| Payouts Complete | Correct amounts transferred |
See AI Audit Flow for detailed verification instructions.