A decentralized, autonomous gaming protocol secured by Cryptographic Randomness.
Powered by Chainlink VRF for fairness and Chainlink Automation for self-execution.
- ๐ง Executive Summary
- ๐ System Architecture
- โ๏ธ Core Logic & Modules
- ๐ Key Features
- ๐ก Security & Testing
- ๐ Development Workflow
- ๐ก Use Cases
The Raffle Protocol is a robust smart contract system designed to eliminate trust from online gaming. Unlike traditional lotteries where the "house" controls the draw and holds custody of funds, this protocol is:
- Immutable: The rules are codified on-chain and cannot be altered by an admin.
- Autonomous: Chainlink Automation triggers the draw based on time intervals, removing manual intervention.
- Provably Fair: Winners are selected using Verifiable Random Functions (VRF), making the outcome mathematically tamper-proof.
The system operates as a finite State Machine (OPEN CALCULATING OPEN), ensuring atomic transitions and preventing new entries during the critical random number generation phase.
[Image of System Architecture Diagram]
graph TD
User((๐ค User))
Automation{๐ค Chainlink Automation}
VRF{๐ฒ Chainlink VRF}
subgraph "Raffle Protocol"
State[State: OPEN]
Check[checkUpkeep]
Perform[performUpkeep]
Fulfill[fulfillRandomWords]
end
User -- 1. Enter Raffle (ETH) --> State
Automation -- 2. Trigger Time Check --> Check
Check -- True --> Perform
Perform -- 3. Request Randomness --> VRF
VRF -- 4. Return Random Words --> Fulfill
Fulfill -- 5. Pick Winner & Payout --> User
style User fill:#1a1a1a,stroke:#b298dc
style Automation fill:#1a1a1a,stroke:#fff
style VRF fill:#1a1a1a,stroke:#9d4edd
style State fill:#2d1b4e,stroke:#9d4edd,stroke-width:2px
The repository is structured for modularity and environment-agnostic deployment (Local vs Testnet).
src/
โโโ Raffle.sol // [CORE] Main Lottery Logic & State Machine
script/
โโโ DeployRaffle.s.sol // [OPS] Deployment with Config Injection
โโโ HelperConfig.s.sol // [CONF] Multi-chain Config (Sepolia/Anvil)
โโโ Interactions.s.sol // [UTILS] Subscription Management
test/
โโโ unit/ // Isolated State Tests
โโโ fuzz/ // Stateless Edge-Case Testing
| Module | Responsibility |
|---|---|
Raffle.sol |
Manages player arrays, enforces entrance fees, tracks state, and processes atomic payouts. |
HelperConfig |
Abstracts network details. Automatically switches between local mocks (Anvil) and live addresses (Sepolia). |
Interactions |
Handles the heavy lifting of creating VRF Subscriptions and funding consumers programmatically. |
|
|
|
|
The project employs a Test-Driven Development (TDD) approach with a comprehensive Foundry suite.
- Unit Tests: Validate entrance fees, state transitions, and array recording.
- Mock Simulation: Uses
VRFCoordinatorV2Mockto simulate random number generation locally on Anvil. - Fuzz Testing: Random input generation to ensure the contract handles unexpected data (e.g., 0 fees, massive arrays).
- Invariant Analysis: Ensures
contract balance == players * feeat all times during theOPENstate.
- Checks-Effects-Interactions (CEI): State is updated before external calls (ETH transfer) to prevent reentrancy attacks.
- Atomic Transitions: The contract locks immediately upon requesting randomness, ensuring no "sniping" or front-running occurs.
This project utilizes a Makefile to streamline the development process.
| Command | Description |
|---|---|
make build |
Compiles the smart contracts. |
make test |
Runs the full unit test suite. |
make deploy |
Deploys the contract to the configured network. |
make anvil |
Starts a local blockchain node for testing. |
This architecture serves as a foundational blueprint for broader Web3 applications:
- ๐๏ธ NFT Whitelisting: Fairly selecting users for high-demand mints.
- ๐ณ๏ธ DAO Governance: Randomly selecting a sub-committee for dispute resolution.
- ๐ Reward Distribution: Automated weekly airdrops to community members.
- ๐ฎ GameFi Mechanics: Critical hit calculations or loot box openings.
Engineered by NEXTECHARHITECT
Smart Contract Developer ยท Solidity ยท Foundry ยท Web3 Engineering