This project is a decentralised lottery game implemented as an Ethereum smart contract using Solidity. The goal is to simulate a fair lottery where players choose a sequence of numbers, and winnings are distributed based on how many numbers match the winning sequence in the correct order.
The contract is designed for educational purposes and demonstrates core Web3 concepts such as:
- Smart contracts
- Ether pooling and distribution
- On-chain game logic
- State management between lottery rounds
- Each ticket consists of 7 numbers
- Valid numbers range from 1 to 49
- Winning numbers are randomly generated by the contract
- Numbers must match the exact sequence to count as a win
The total prize pool is distributed as follows:
| Correct Numbers | Reward Percentage |
|---|---|
| 1 | 0% |
| 2 | 5% |
| 3 | 10% |
| 4 | 15% |
| 5 | 20% |
| 6 | 20% |
| 7 | 30% |
- If there are multiple winners in a category, the reward is split equally
- Any unclaimed funds roll over into the next lottery round
- A portion of the pool is reserved for the contract owner
Players are participants in the lottery game.
- Buy a lottery ticket by submitting exactly 7 numbers
- Pay a fixed entry fee in Ether
- Wait for the current lottery round to end
Outcomes:
- If a player’s selected numbers match the winning sequence in the correct order, they receive a portion of the prize pool
- If a player does not win, their contribution remains in the pool and may roll over into the next round
The contract owner acts as the lottery operator and does not participate as a player.
- Deploys the smart contract
- Starts and ends lottery rounds
- Triggers the request for random numbers
- Receives a predefined share of the prize pool as an operational fee
Restrictions:
- The owner cannot buy lottery tickets
- The owner cannot choose or influence winning numbers
- The owner cannot manually distribute rewards
The smart contract is the central authority that enforces all lottery rules automatically and without trust in any individual party.
- Validates all player inputs and ticket payments
- Manages lottery rounds and on-chain state
- Securely holds all Ether paid for tickets
- Generates verifiable random winning numbers
- Compares player tickets against the winning sequence
- Calculates rewards based on predefined percentages
- Automatically distributes winnings and the owner’s fee
- Rolls over any unclaimed funds into the next lottery round
- Deploys the contract
- Starts and ends lottery rounds
- Receives the owner’s share of the pool
- Secure Ether handling using Solidity best practices
- Validation of user input (number range & length)
- Deterministic prize allocation logic
- Rollover mechanism for leftover funds
- Owner-only administrative functions
- Solidity (Smart Contract)
- Hardhat (Development & Testing)
- Ethers.js (Contract interaction)
- Sepolia Testnet (Deployment)
git clone <repository-url>
cd web3-lottery-gamenpm installnpx hardhat compilenpx hardhat testnpx hardhat run scripts/deploy.js --network sepolia- This contract is for learning purposes only
- Pseudo-randomness on-chain is not suitable for production lotteries
- No external audits have been performed
By completing this project, you will understand:
- How to build and deploy smart contracts
- Ether flow and pool-based reward systems
- Solidity control structures (
require, conditionals, modifiers) - Basic decentralised game mechanics
This project is open-source and intended for educational use.
Built as part of a Web3 learning journey