Lottery that can select a verifiably random winner...
View on Etherscan »
About
·
Local Development
·
Usage
Table of Contents
Bingo game vector created by katemangostar - www.freepik.com
This Lottery Smart Contract is scheduled to start automatically every 24 hrs ( *if the conditions are met ). This is done by using openzeppelin's defender autotasks. After being started players can enter the Lottery. They have to enter a minimum threshold of 50 USD. The price of 50 USD in ETH is calculated in real-time by using chainlink data feed oracles.
When a player enters the lottery an event is emitted from the smart contract. I have set up a defender sentinel to listen for this event. The maximum limit of players is 3. ( I have set this to 3, So it's easy to test with fewer accounts ). The sentinel (event listener) triggers the endLottery
task when the max limit of players is reached.
To end the lottery a random winner has to be selected. But getting a truly random number is not a task for a deterministic system like blockchain. So the random number is fetched using chainlink VRF (Verifiable Random Function).
This repo includes deploy scripts, unit tests and staging tests and scripts for executing transactions. You can test locally or in Gitpod.
-
env variables
- Etherscan API key : for verification of contracts
- CoinMarketCap API key : for gas estimation in USD
- VRF v2 Subscription Id : for using chainlink VRF
- Truffle dashboard : for using metamask instaed of private keys
-
Run Unit Tests
- Spin up a local blockchain
npx hardhat node
- Run tests in local network
npx hardhat test --network localhost
- See code coverage
npx hardhat coverage
- Spin up a local blockchain
-
Run Staging Tests
- Open truffle dashboard
truffle dashboard
- Deploy to a live network
npx hardhat deploy --network truffle
configured to use Rinkeby network addresses by default (for vrf & price feed addresses ) - Run staging test.
npx hardhat test --network truffle
- Open truffle dashboard
- Go to Etherscan.
- Check the lottery state ( s_lotteryState : "0" means open, "1" means closed, "2" means selectingWinner)
- If the lottery is open you can enter. Otherwise has to wait for 24 hrs. Lottery automatically starts every 24 hrs if the lottery is closed.
- Check the Entrance Fee ( getEntranceFee )
- Entrance fee is in wei. Convert it to ether.
- Enter by sending entrance fee. ( enter function )
- Check the number of participants ( getParticipantsLen )
- When the number of participants hit the max limit ( s_maxParticipantsLimit ) the lottery will automatically end and select a winner.
- Visit this page to see pending radnomness requests to VRF.
- Automate Lottery using Openzeppelin defender
- Use chainlink keepers to automate the lottery in a decentralized manner
- Fuzz Testing
- Build a user interface with React