Skip to content

NexTechArchitect/Fair-Raffle-Lottery

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

14 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation



Raffle Logo

Automated Provably Fair Raffle

A decentralized, autonomous gaming protocol secured by Cryptographic Randomness.
Powered by Chainlink VRF for fairness and Chainlink Automation for self-execution.

ย  ย 



๐Ÿ“‘ Table of Contents


๐Ÿง  Executive Summary

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:

  1. Immutable: The rules are codified on-chain and cannot be altered by an admin.
  2. Autonomous: Chainlink Automation triggers the draw based on time intervals, removing manual intervention.
  3. Provably Fair: Winners are selected using Verifiable Random Functions (VRF), making the outcome mathematically tamper-proof.

๐Ÿ—๏ธ System Architecture

The system operates as a finite State Machine (OPEN $\rightarrow$ CALCULATING $\rightarrow$ 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

Loading

โš™๏ธ Core Logic & Modules

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

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.

๐Ÿ’Ž Key Features

๐Ÿค– Automation Layer

  • Self-Sustaining: No admin required to start/end rounds.
  • Gas Optimized: Uses `checkUpkeep` (view function) to validate conditions off-chain before spending gas on-chain.

๐ŸŽฒ Randomness Layer

  • Tamper-Proof: Uses Chainlink VRF Direct Funding.
  • Verifiable: Proof of randomness is verified on-chain by the VRF Coordinator before fulfillment.

๐Ÿ›  Engineering Standards

  • Custom Errors: Gas-optimized alternatives to long require strings.
  • Indexed Events: Fully compatible with The Graph for frontend data fetching.

๐Ÿ›ก Defense Mechanisms

  • Reentrancy Protection: Strict CEI Pattern implementation.
  • State Locking: Prevents new players from joining while a winner is being calculated.

๐Ÿ›ก๏ธ Security & Testing

The project employs a Test-Driven Development (TDD) approach with a comprehensive Foundry suite.

Testing Strategy

  1. Unit Tests: Validate entrance fees, state transitions, and array recording.
  2. Mock Simulation: Uses VRFCoordinatorV2Mock to simulate random number generation locally on Anvil.
  3. Fuzz Testing: Random input generation to ensure the contract handles unexpected data (e.g., 0 fees, massive arrays).
  4. Invariant Analysis: Ensures contract balance == players * fee at all times during the OPEN state.

Security Patterns

  • 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.

๐Ÿš€ Usage & Workflow

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.

๐Ÿ’ก Use Cases

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

GitHub โ€ข Twitter


About

A complete decentralized lottery (raffle) system built with Solidity & Foundry, integrated with Chainlink VRF.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors