Skip to main content

Lottery

warning

These docs are for V1 of Eat The Pie, which is now deprecated. For the latest documentation for V2 on World Chain, please visit docs.eatthepie.xyz.

Github Link: https://github.com/eatthepie/contracts/blob/main/src/Lottery.sol

This contract implements the core features of the Eat The Pie lottery.

Key Features

  • 🎟️ Multiple ticket purchase support (up to 100 tickets per transaction)
  • 🔢 Three difficulty levels affecting number ranges (Easy, Medium, Hard)
  • 🔐 Secure random number generation using VDFs and RANDAO
  • 💰 Three-tiered prize system (Gold, Silver, Bronze)
  • 🏆 NFT minting for jackpot winners
  • 🔄 Automatic difficulty adjustment based on win patterns
  • 💸 Configurable ticket pricing with scheduled changes
  • 🛡️ Comprehensive security measures
  • 💎 Prize pool management with excess handling
  • 🔍 Detailed game information tracking and querying

Contract Structure

Inheritance

  • Ownable: Provides basic authorization control functions
  • ReentrancyGuard: Prevents reentrant calls to functions

Dependencies

  • VDFPietrzak: Contract for VDF calculations and verification
  • NFTPrize: Contract for minting prize NFTs
  • BigNumbers: Library for handling large number operations

Constants

  • BASIS_POINTS: 10000 (for percentage calculations)
  • GOLD_PERCENTAGE: 6000 (60% of prize pool)
  • SILVER_PLACE_PERCENTAGE: 2500 (25% of prize pool)
  • BRONZE_PLACE_PERCENTAGE: 1400 (14% of prize pool)
  • FEE_PERCENTAGE: 100 (1% of prize pool)
  • FEE_MAX_IN_ETH: 100 ether
  • Difficulty-based number ranges:
    • Easy: 1-50 (main), 1-5 (etherball)
    • Medium: 1-100 (main), 1-10 (etherball)
    • Hard: 1-150 (main), 1-15 (etherball)
  • DRAW_MIN_PRIZE_POOL: 500 ether
  • DRAW_MIN_TIME_PERIOD: 1 week
  • DRAW_DELAY_SECURITY_BUFFER: 128 blocks

Key State Variables

  • currentGameNumber: Current game identifier
  • ticketPrice: Price of a single ticket
  • lastDrawTime: Timestamp of the last draw
  • consecutiveJackpotGames: Counter for consecutive games with jackpot winners
  • consecutiveNonJackpotGames: Counter for consecutive games without jackpot winners
  • Prize pool and game state mappings
  • Winner tracking mappings for each prize tier

Core Functionality

Ticket Purchase

function buyTickets(uint256[4][] calldata tickets) external payable
  • Allows purchase of up to 100 tickets in one transaction
  • Each ticket requires 4 numbers (3 main numbers + 1 etherball)
  • Automatically tracks tickets for all prize tiers
  • Emits TicketPurchased and TicketsPurchased events

Drawing Process

  1. Draw Initiation
function initiateDraw() external
  • Requires minimum prize pool (500 ETH)
  • Requires minimum time period (1 week)
  • Sets up security delay buffer
  • Starts the next game
  1. Random Value Setting
function setRandom(uint256 gameNumber) external
  • Uses Ethereum's block.prevrandao for randomness
  • Must be called after security buffer period
  1. VDF Proof Submission
function submitVDFProof(uint256 gameNumber, BigNumber[] memory v, BigNumber memory y) external
  • Verifies VDF proof by calling the VDF contract
  • Generates final winning numbers
  • Sets game status to completed
  1. Payout Calculation
function calculatePayouts(uint256 gameNumber) external
  • Calculates prizes for each tier
  • Handles excess fee management
  • Transfers remaining prize pool to next game
  • Emits payout information

Prize Claiming

function claimPrize(uint256 gameNumber) external
  • Allows winners to claim their prizes
  • Prevents double claiming
  • Emits PrizeClaimed event
function mintWinningNFT(uint256 gameNumber) external
  • Available only to gold tier winners
  • Mints unique NFT by calling the NFT contract
  • Emits NFTMinted event

Difficulty Management

function changeDifficulty() external
  • Automatically adjusts difficulty based on win patterns
  • Requires 3 consecutive games for adjustment
  • Changes take effect on the next game
  • Emits DifficultyChanged event

Administrative Functions

  • setTicketPrice: Schedules ticket price changes
  • setNewVDFContract: Updates VDF contract with delay
  • setFeeRecipient: Updates fee recipient address

Query Functions

function getCurrentGameInfo() external view returns (
uint256 gameNumber,
Difficulty difficulty,
uint256 prizePool,
uint256 drawTime,
uint256 timeUntilDraw
)
function getBasicGameInfo(uint256 startGameId, uint256 endGameId) external view returns (GameBasicInfo[] memory)
function getDetailedGameInfo(uint256 gameId) external view returns (GameDetailedInfo memory)
function getUserGameWinnings(uint256 gameNumber, address user) external view

Events

  • TicketPurchased: Individual ticket purchase details
  • TicketsPurchased: Bulk ticket purchase information
  • DrawInitiated: Start of drawing process
  • RandomSet: Initial random value set
  • VDFProofSubmitted: VDF verification complete
  • WinningNumbersSet: Final winning numbers
  • DifficultyChanged: Difficulty level updates
  • TicketPriceChangeScheduled: Future price changes
  • ExcessPrizePoolTransferred: Prize pool management
  • GamePrizePayoutInfo: Prize distribution details
  • FeeRecipientChanged: Fee recipient updates
  • PrizeClaimed: Winner prize claims
  • NFTMinted: NFT prize creation