Raffle is a smart contract lottery that utilizes Chainlink VRF to randomly select winners and Chainlink Automation to initialize a winner pick and restart of the lottery
This project demonstrates the implementation of a decentralized lottery system using:
- Chainlink VRF v2.5 for provably fair random number generation
- Chainlink Automation for automated lottery execution
- Foundry for development, testing, and deployment
- Solidity 0.8.19 for smart contract development
- 🎰 Decentralized Lottery: Fair and transparent lottery system
- 🔗 Chainlink Integration: Uses VRF for randomness and Automation for execution
- 🛡️ Security: Implements CEI (Checks, Effects, Interactions) pattern
- 🧪 Comprehensive Testing: Unit tests with Foundry
- 🚀 Easy Deployment: Automated deployment scripts for multiple networks
- Foundry installed
- Node.js and npm (for additional tooling if needed)
- Access to Ethereum RPC endpoints (Sepolia for testnet deployment)
- Clone the repository:
git clone <repository-url>
cd foundry-smart-contract-lottery
- Install dependencies:
make install
This will install:
cyfrin/[email protected]
smartcontractkit/[email protected]
foundry-rs/[email protected]
transmissions11/solmate@v6
make build
make test
make fmt
make clean
make snapshot
Create a .env
file with the following variables:
SEPOLIA_RPC_URL=your_sepolia_rpc_url
ETHERSCAN_API_KEY=your_etherscan_api_key
PRIVATE_KEY=your_private_key
make deploy
make deploy-sepolia
├── src/
│ └── Raffle.sol # Main lottery contract
├── test/
│ ├── unit/
│ │ └── RaffleTest.t.sol # Unit tests
│ ├── integration/ # Integration tests
│ └── mocks/
│ └── LinkToken.sol # Mock Chainlink token
├── script/
│ ├── DeployRaffle.s.sol # Deployment script
│ ├── Interactions.s.sol # Interaction script
│ └── HelperConfig.s.sol # Configuration helper
├── lib/ # Dependencies
├── foundry.toml # Foundry configuration
├── Makefile # Build automation
└── README.md # This file
- Entrance Fee: Configurable fee to enter the lottery
- Time Interval: Configurable time between lottery rounds
- VRF Integration: Uses Chainlink VRF for random winner selection
- Automation: Uses Chainlink Automation for automated execution
- State Management: Tracks lottery state (OPEN/CALCULATING)
- Winner Selection: Fair random selection using VRF
- Prize Distribution: Automatic prize transfer to winner
enterRaffle()
: Enter the lottery by paying the entrance feecheckUpkeep()
: Check if lottery is ready for winner selectionperformUpkeep()
: Trigger winner selection processfulfillRandomWords()
: Handle VRF callback and select winner
The project includes comprehensive unit tests covering:
- Lottery entry functionality
- Winner selection process
- VRF integration
- Automation triggers
- Error handling
- State management
Run tests with:
make test
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
This project is licensed under the MIT License.