This code is going to create a provebly random lottery smart contract!
- Users can take part in the lottery by paying for the ticket
- The cumulative ticket fees of all participants will go to the winner of the lottery.
- After X period of time, the lottery will automatically draw a winner
- And this will happen programatically.
- The contract will be using Chainlink VRF & Automation
- Chainlink VRF -> Randomness
- Chainlink Automation -> Time based trigger for lottery draw
git clone https://github.com/alfheimrShiven/lottery-smart-contract.git
cd lottery-smart-contract
forge build
forge test
or
forge test --fork-url $SEPOLIA_RPC_URL
forge coverage
make deploy
- Setup environment variables
You'll want to set your SEPOLIA_RPC_URL
and PRIVATE_KEY
as environment variables. You can add them to a .env
file, similar to what you see in .env.example
.
PRIVATE_KEY
: The private key of your account (like from metamask). NOTE: FOR DEVELOPMENT, PLEASE USE A KEY THAT DOESN'T HAVE ANY REAL FUNDS ASSOCIATED WITH IT.- You can learn how to export it here.
SEPOLIA_RPC_URL
: This is url of the sepolia testnet node you're working with. You can get setup with one for free from Alchemy
Optionally, add your ETHERSCAN_API_KEY
if you want to verify your contract on Etherscan.
- Get testnet ETH
Head over to faucets.chain.link and get some testnet ETH. You should see the ETH show up in your metamask.
- Deploy
make deploy ARGS="--network sepolia"
This will setup a ChainlinkVRF Subscription for you. If you already have one, update it in the scripts/HelperConfig.s.sol
file. It will also automatically add your contract as a consumer.
- Register a Chainlink Automation Upkeep
You can follow the documentation if you get lost.
Go to automation.chain.link and register a new upkeep. Choose Custom logic
as your trigger mechanism for automation.
- Once deployed and Upkeep registered with Chainlink Automation, you can use EtherScan UI to interact with your lottery.
If you appreciated this, feel free to follow me: