A decentralized and automated raffle system built on blockchain technology, utilizing Chainlink VRF for verifiable randomness and Chainlink Automation for trustless execution.
This project implements a fully automated raffle system where:
- Users can enter by paying a set entrance fee
- A winner is automatically and fairly selected at regular intervals
- Uses Chainlink VRF (Verifiable Random Function) for provably fair winner selection
- Leverages Chainlink Automation for automatic draws
- Implements a robust testing suite
- Fair Entry System: Fixed entrance fee required to participate
- Automated Execution: No manual intervention needed for picking winners
- Verifiable Randomness: Chainlink VRF v2.5 ensures provably fair winner selection
- Time-Based Draws: Configurable intervals between raffles
- Transparent: Fully verifiable on-chain logic
- State Management: Proper handling of raffle states (OPEN and CALCULATING)
- Gas Efficient: Optimized for minimal gas consumption
The project consists of several key contracts:
Raffle.sol
: Main contract implementing the raffle logicDeployRaffle.s.sol
: Script for deploying the raffleHelperConfig.s.sol
: Configuration management for different networksInteractions.s.sol
: Helper scripts for contract interactions
- Solidity ^0.8.19
- Chainlink VRF V2.5
- Forge/Foundry for testing and deployment
- Solmate for optimized contract implementations
Comprehensive test suite available in test/unit/RaffleTest.t.sol
, covering:
- Initialization states
- Entry conditions
- Player recording
- Event emissions
- State transitions
- Upkeep checks
- Winner selection
- Foundry installed
- Node.js and npm (for additional tooling)
- An RPC URL for deployment
- LINK tokens for VRF funding (on mainnet/testnet)
- Clone the repository:
git clone <your-repo-url>
cd raffle-contract
- Install dependencies:
forge install
- Compile contracts:
forge build
- Run tests:
forge test
- Set up your environment variables:
cp .env.example .env
# Fill in your environment variables
- Deploy to a network:
forge script script/DeployRaffle.s.sol --rpc-url <your_rpc_url> --private-key <your_private_key>
Users can enter the raffle by calling the enterRaffle()
function with the required entrance fee:
raffle.enterRaffle{value: entranceFee}()
Several view functions are available:
getEntranceFee()
: Get the required entrance feegetRaffleState()
: Check if raffle is OPEN or CALCULATINGgetPlayer(uint256 index)
: Get player at specific indexgetNumberOfPlayers()
: Get total number of players
- Implements CEI (Checks-Effects-Interactions) pattern
- Uses custom errors for gas efficiency
- Proper access control mechanisms
- Comprehensive error handling
- VRF v2.5 for secure randomness
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
Saksham Gupta
- Chainlink VRF team for randomness solution
- Foundry team for development framework