Skip to content

This is a project build to play a fair ,transparent lottery/raffle build to deploy on ethereum ,utilises chainlink vrf and other dependecies.

License

Notifications You must be signed in to change notification settings

jameslego-ctrl/foundry-smart-contract-lottery-raffle

Repository files navigation

🎟 Decentralized Raffle (Chainlink VRF v2.5 + Automation)

A fully automated, fair, and decentralized smart contract raffle system powered by Chainlink VRF v2.5 for provable randomness and Chainlink Automation for scheduling.

Players join by paying an entrance fee, and winners are selected at fixed intervals with auditable fairness. Includes complete deployment scripts, mock configurations for local testing, and VRF subscription management automation.


📑 Table of Contents


Overview

The Decentralized Raffle contract automates lottery rounds:

  • Players pay an entrance fee in ETH to join.
  • Every set interval, Chainlink Automation checks eligibility to pick a winner.
  • Chainlink VRF ensures winner selection is truly random and verifiable.
  • Funds are transferred automatically to the winner.

Seamlessly deployable to Sepolia or local development networks using Foundry scripts with configurable parameters.


Architecture

    graph TD
      A[Players] -->|Enter with Fee| B[Raffle Smart Contract]
      B -->|Automated Check| C[Chainlink Automation]
      C -->|Trigger| D[Chainlink VRF v2.5]
      D -->|Random Number| B
      B -->|Winner Selection + Payout| A
Loading

Features

  • Fully On-Chain Raffle Logic
  • 🎲 Provable Randomness via Chainlink VRF v2.5
  • Automated Intervals using Chainlink Automation
  • 🔧 Environment-Aware Deployment (Sepolia or Local Mock)
  • 🧪 Local Testing with VRF Mocks & LINK Token Mocks
  • 🛠 Helper Scripts for VRF Subscription Creation, Funding & Consumer Assignment
  • 🎯 Gas-Efficient Custom Errors & Events

Folder Structure

├── src/
│   └── Raffle.sol                # Main raffle contract
├── script/
│   ├── HelperConfig.s.sol        # Network configs and mocks
│   ├── DeployRaffle.s.sol        # Main deployment script
│   └── interactions.s.sol       # VRF Subscription interactions

├── test/                         # Unit and integration tests
│   ├── unit/
│   └── integration/
├── lib/                          # External dependencies (e.gChainlink, forge-std)
├── foundry.toml                  # Foundry project config
└── README.md

Contracts

Raffle.sol

Located in src/

Purpose:
Manages player entries, automates winner selection at fixed intervals using Chainlink VRF randomness.

Key Functions:

  • enterRaffle() — Player enters by paying ETH ≥ entrance fee.
  • checkUpKeep() — Checks if conditions are met for a draw.
  • performUpKeep() — Requests randomness via VRF.
  • fulfillRandomWords() — Selects winner & transfers prize.

Events:

  • raffleEntered(address player)
  • winnerPicked(address winner)
  • RequestedRaffleWinner(uint256 requestId)

Scripts

HelperConfig.s.sol

  • Defines NetworkConfig for Sepolia and local (Anvil) environments.
  • On local chain:
    • Deploys VRFCoordinatorV2_5Mock + LinkToken mocks.
  • Parameters include: entrance fee, interval, gas lane, callback gas limit, subscription ID, LINK token address, deployer account.

DeployRaffle.s.sol

  • Loads config from HelperConfig.
  • If no subscription exists:
    • Runs CreateSubscription and FundSubscription scripts.
  • Deploys Raffle contract with config values.
  • Adds it as a VRF consumer via AddConsumer script.

interactions.s.sol

Includes:

  • CreateSubscription — Creates VRF subscription.
  • FundSubscription — Funds subscription (local mock or actual LINK).
  • AddConsumer — Authorizes deployed contract to request VRF randomness.

Installation

Prerequisites:

  • Foundry installed.
  • Node.js (optional, if integrating frontend).
  • RPC URLs for Sepolia (Alchemy/Infura/etc.).
  • LINK tokens & VRF subscription for testnet.
  • git clone this repo and you will be ready to work with it
  • [To know more about the tools and the specific versions to work with please refer to the] (#Makefile)

THE DEMO DEPLOYED CONTRACT ON SEPOLIA

This is a link to the deployed version of this contract in sepolia: (https://sepolia.etherscan.io/address/0x0e92EA6f1E8Af8dBF12C94e8AB1fB2c7a93b4A83#code)

Contract address : 0x0e92EA6f1E8Af8dBF12C94e8AB1fB2c7a93b4A83

[Sepolia Etherscan] : (https://sepolia.etherscan.io)

About

This is a project build to play a fair ,transparent lottery/raffle build to deploy on ethereum ,utilises chainlink vrf and other dependecies.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published