Skip to content

DevAyomi/Lottery-Smart-Contract

Repository files navigation

Lottery Raffle Smart Contract (Foundry)

Decentralized lottery (raffle) smart contract built with Solidity and Foundry, using Chainlink VRF v2.5 for provably random winner selection. The project includes deployment and interaction scripts plus unit tests.


Features

  • Provably fair randomness via Chainlink VRF v2.5
  • Configurable raffle: entrance fee, interval, gas lane, gas limit
  • Network-aware config using HelperConfig (local Anvil + Sepolia)
  • Automation-ready architecture for periodic winner selection
  • Foundry test suite for core raffle behavior

Tech Stack

  • Solidity ^0.8.x
  • Foundry (Forge, Cast, Anvil)
  • Chainlink VRF v2.5
  • Solmate (ERC20 for LINK mock)
  • Cyfrin foundry-devops (for reading latest deployment)

Getting Started

Prerequisites

  • Foundry installed (forge, cast, anvil)
  • Node / pnpm/yarn/npm only if you want extra tooling (optional)
  • A funded EOA and RPC URL for testnet deployments (e.g. Sepolia)

Install

Clone and install dependencies:

git clone https://github.com/<your-username>/lottery_smart_contract.git
cd lottery_smart_contract

# Install Solidity libraries
forge install

Build and run tests:

forge build
forge test

Project Structure

  • src/Rafflle.sol – main raffle contract
  • script/HelperConfig.s.sol – per-chain config + mock deployment
  • script/DeployRaffle.s.sol – deploys Raffle using HelperConfig
  • script/Interactions.s.sol
    • CreateSubscription – creates Chainlink VRF subscription
    • FundSubscription – funds subscription with LINK (mock or real)
    • AddConsumer – adds the raffle as VRF consumer
  • test/unit/RaffleTest.t.sol – unit tests for the raffle logic

Running Locally (Anvil)

  1. Start a local node:
anvil
  1. In a new terminal, deploy the raffle to Anvil:
forge script script/DeployRaffle.s.sol:DeployRaffle \
  --rpc-url http://127.0.0.1:8545 \
  --private-key <ANVIL_PRIVATE_KEY> \
  --broadcast
  1. Use the interaction scripts if needed (subscription creation/funding, adding consumer) – typically only needed for live/test networks.

Deploying to Testnet (e.g. Sepolia)

Update HelperConfig.s.sol with your desired network parameters if needed (subscription id, LINK token, VRF coordinator, etc.).

Then deploy:

forge script script/DeployRaffle.s.sol:DeployRaffle \
  --rpc-url $SEPOLIA_RPC_URL \
  --private-key $PRIVATE_KEY \
  --broadcast \
  --verify --etherscan-api-key $ETHERSCAN_API_KEY

Make sure $SEPOLIA_RPC_URL, $PRIVATE_KEY, and $ETHERSCAN_API_KEY are set in your shell environment.


Testing

Run the whole test suite:

forge test

Run a single test file or test:

forge test --match-path test/unit/RaffleTest.t.sol
# or
forge test --match-test test_EnterRaffleUpdatesState

Notes

  • The project uses foundry-devops to fetch the latest Raffle deployment from the broadcast/ folder.
  • Local LINK and VRF coordinator mocks are deployed automatically for the local chain in HelperConfig.

License

SPDX-License-Identifier: UNLICENSED (adjust to your preferred license if needed).

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors