A smart contract lottery implementation using Chainlink VRF and Automation for provably fair randomness and automatic winner selection.
This project implements a decentralized raffle system that ensures fairness through cryptographic randomness and automated execution. Built with Foundry and leveraging Chainlink's oracle services.
- Provably Fair: Uses Chainlink VRF (Verifiable Random Function) for cryptographically secure randomness
- Automated Execution: Chainlink Automation triggers winner selection after specified time intervals
- Gas Optimized: Efficient contract design with custom errors and optimized state management
- Multi-Network Support: Configured for Sepolia testnet and local Anvil development
- Comprehensive Testing: Full test suite with unit tests and fuzz testing
- Entry Phase: Users enter the raffle by paying the entrance fee
- Collection Period: The contract collects entries for a specified time interval
- Winner Selection: After the time period, Chainlink Automation triggers the lottery
- Random Selection: Chainlink VRF provides verifiable randomness to select the winner
- Prize Distribution: The winner receives the entire prize pool automatically
- Smart Contracts: Solidity ^0.8.19
- Development Framework: Foundry
- Randomness: Chainlink VRF v2
- Automation: Chainlink Keepers/Automation
- Testing: Forge with comprehensive unit tests
- Foundry
- Git
git clone https://github.com/yourusername/foundry-random-lottery
cd foundry-random-lottery
make install
make build
make test
make anvil # In one terminal
make deploy # In another terminal
make deploy ARGS="--network sepolia"
Create a .env
file with:
SEPOLIA_RPC_URL=your_sepolia_rpc_url
PRIVATE_KEY=your_private_key
ETHERSCAN_API_KEY=your_etherscan_api_key
- Sepolia: Pre-configured with Chainlink VRF Coordinator and LINK token addresses
- Anvil: Uses mock contracts for local development
- Raffle.sol: Main lottery contract with VRF and Automation integration
- DeployRaffle.s.sol: Deployment script with network-specific configuration
- HelperConfig.s.sol: Network configuration management
- Interactions.s.sol: Scripts for VRF subscription management
- Entrance Fee: 0.01 ETH
- Interval: 30 seconds
- Gas Lane: Chainlink VRF gas lane for transaction speed/cost balance
- Callback Gas Limit: 500,000 gas for VRF callback execution
The project includes comprehensive tests:
- Unit Tests: Individual function testing
- Integration Tests: End-to-end workflow testing
- Fuzz Tests: Random input testing for edge cases
- Fork Tests: Testing against live networks
Run specific test categories:
forge test --match-contract RaffleTest
forge test --fork-url $SEPOLIA_RPC_URL
- Custom error messages instead of require statements
- Efficient state variable packing
- Minimal external calls
- Optimized loops and conditionals
- Reentrancy protection through state changes before external calls
- Access control for critical functions
- Input validation and bounds checking
- Safe math operations (Solidity ^0.8.0)
Available Make commands:
make deploy
: Deploy to configured networkmake test
: Run full test suitemake format
: Format code with Forgemake snapshot
: Generate gas snapshotsmake clean
: Clean build artifacts
MIT License - see LICENSE file for details
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
- Make it a full-stack project (working on it with RainbowKit and wagmi)
For questions or issues, please open an issue in the repository or contact the maintainers.