A decentralized and verifiably random raffle system built on Ethereum using Solidity, Foundry, and Chainlink VRF + Automation.
This project implements a trustless raffle where:
- Users can enter by paying an entrance fee
- A verifiably random winner is selected after a time interval
- The winner receives all the accumulated ETH
- The drawing process is fully automated and tamper-proof
- Transparent: All code is open source and verifiable
- Fair: Uses Chainlink VRF (Verifiable Random Function) for true randomness
- Automated: Leverages Chainlink Automation for time-based execution
- Gas-efficient: Optimized for minimal gas usage
- Solidity - Smart contract development
- Foundry - Testing framework
- Chainlink VRF - Verifiable randomness
- Chainlink Automation - Decentralized execution
src/Raffle.sol- Main raffle contract implementation
script/DeployRaffle.s.sol- Deployment script for the Raffle contractscript/HelperConfig.s.sol- Configuration helper for different networksscript/Interactions.s.sol- Scripts for interacting with the deployed contract
test/unit/RaffleTest.t.sol- Comprehensive unit tests for the Raffle contract- Tests for entering the raffle
- Tests for checkUpkeep functionality
- Tests for performUpkeep functionality
- Tests for winner selection and prize distribution
- and more...
- Clone the repository
git clone https://github.com/chauhan-varun/foundry-lottery.git
cd raffle- Install dependencies
forge install- Build the project
forge buildRun the test suite:
forge testFor verbose output:
forge test -vvvFor specific test files:
forge test --match-path test/unit/RaffleTest.t.sol -vvv- Setup environment variables (create a
.envfile):
PRIVATE_KEY=your_private_key
SEPOLIA_RPC_URL=your_sepolia_rpc_url
ETHERSCAN_API_KEY=your_etherscan_api_key
- Deploy to testnet:
forge script script/DeployRaffle.s.sol:DeployRaffle --rpc-url $SEPOLIA_RPC_URL --private-key $PRIVATE_KEY --broadcast --verify --etherscan-api-key $ETHERSCAN_API_KEYThe main contract with the following functionality:
enterRaffle(): Enter the raffle by sending ETHcheckUpkeep(): Check if it's time to perform a drawingperformUpkeep(): Initiate the random number requestfulfillRandomWords(): Process the random result and select a winner
Handles the deployment of the Raffle contract with proper configuration based on the network.
Provides network-specific configurations including:
- VRF Coordinator addresses
- Gas lane values
- Subscription IDs
- Entrance fees
- Interval settings
Contains scripts for interacting with the deployed contract:
- Creating VRF subscriptions
- Funding subscriptions
- Adding consumers to subscriptions
The test suite covers:
- Contract initialization state
- Raffle entrance requirements and validations
- Player recording functionality
- Event emission verification
- Upkeep checks under various conditions
- Random number fulfillment and winner selection
- Prize distribution
The contract is configurable with:
- Entrance fee
- Drawing interval
- VRF parameters (gas lane, callback gas limit, etc.)
This project is licensed under the MIT License - see the LICENSE file for details.
Varun Chauhan