This project creates a provably random lottery via smart contracts that allows users to participate in the lottery in a trustless and decentralised manner. Additionally the users can verify themselves that the contracts are provably random as they are powered by chainlink VRF. The lottery is also compatible with chainlink keepers to run the lottery automatically.
- Users can enter by paying for a ticket
- The ticket fees are going to go to the winer during the draw
- After a period of time, the lottery will automatically draw a winner at random
- Chainlink Automation will be used to create a time based trigger
- Chainlink VRF will be used to select the winner from the participant pool
The following must be installed on your machine:
git clone https://github.com/MGM103/on-chain_lottery.git
cd on-chain_lottery
make build
In one terminal run anvil. Anvil is a local Ethereum node, designed for development with Forge, akin to Ganache.
anvil
In another terminal run the following command to deploy:
make deploy
1. Setup environment variables
You'll want to set your SEPOLIA_RPC_URL
, PRIVATE_KEY
& ETHERSCAN_API_KEY
as environment variables. You can add them to a .env
file.
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 AlchemyETHERSCAN_API_KEY
to verify your contract on Etherscan.
2. Get testnet ETH
Head over to faucets.chain.link and get some testnet ETH. You should see the ETH show up in your metamask.
3. 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.
4. 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.
After deploying to a testnet or local net, you can run the scripts.
Using cast deployed locally example:
cast send <RAFFLE_CONTRACT_ADDRESS> "enterRaffle()" --value 0.1ether --private-key <PRIVATE_KEY> --rpc-url $SEPOLIA_RPC_URL
or, to create a ChainlinkVRF Subscription:
make createSubscription ARGS="--network sepolia"
To run the unit tests for this project you can run the following commands:
forge test // run all tests
forge test --mt <testFunctionName> // run specific test
To see the amount of coverage the tests in the project cover of the codebase you can run the following command:
forge coverage
If you are unfamiliar with the foundry smart contract development framework please refer to: https://book.getfoundry.sh/
Full credit must go to Patrick Collins. He is a fantastic teacher of blockchain development and this project came from his course. If you are interested in learning more please checkout his youtube.