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.
- 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
- Solidity ^0.8.x
- Foundry (Forge, Cast, Anvil)
- Chainlink VRF v2.5
- Solmate (ERC20 for LINK mock)
- Cyfrin foundry-devops (for reading latest deployment)
- 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)
Clone and install dependencies:
git clone https://github.com/<your-username>/lottery_smart_contract.git
cd lottery_smart_contract
# Install Solidity libraries
forge installBuild and run tests:
forge build
forge testsrc/Rafflle.sol– main raffle contractscript/HelperConfig.s.sol– per-chain config + mock deploymentscript/DeployRaffle.s.sol– deploysRaffleusingHelperConfigscript/Interactions.s.solCreateSubscription– creates Chainlink VRF subscriptionFundSubscription– 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
- Start a local node:
anvil- 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- Use the interaction scripts if needed (subscription creation/funding, adding consumer) – typically only needed for live/test networks.
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_KEYMake sure
$SEPOLIA_RPC_URL,$PRIVATE_KEY, and$ETHERSCAN_API_KEYare set in your shell environment.
Run the whole test suite:
forge testRun a single test file or test:
forge test --match-path test/unit/RaffleTest.t.sol
# or
forge test --match-test test_EnterRaffleUpdatesState- The project uses
foundry-devopsto fetch the latestRaffledeployment from thebroadcast/folder. - Local LINK and VRF coordinator mocks are deployed automatically for the local chain in
HelperConfig.
SPDX-License-Identifier: UNLICENSED (adjust to your preferred license if needed).