|
| 1 | +// raffle contract |
| 2 | +// entering lottery by paying |
| 3 | +// pick a true random winner |
| 4 | +// winner to be selected every x minutes --> comp automated |
| 5 | +// chainlink oracle --> randomness, automated execution (chainlink keepers) |
| 6 | + |
| 7 | +// SPDX-License-Identifier: MIT |
| 8 | + |
| 9 | +pragma solidity ^0.8.7; |
| 10 | + |
| 11 | +import "@chainlink/contracts/src/v0.8/VRFConsumerBaseV2.sol"; |
| 12 | +import "@chainlink/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol"; |
| 13 | +import "@chainlink/contracts/src/v0.8/AutomationCompatible.sol"; |
| 14 | + |
| 15 | +error Raffle__NotEnoughETH(); |
| 16 | +error Raffle__TransferFailed(); |
| 17 | +error Raffle__NotOpen(); |
| 18 | +error Raffle__UpkeepNotNeeded(uint256 currentBalance, uint256 numPlayers, uint256 raffleState); |
| 19 | + |
| 20 | +/** @title A sample Raffle contract |
| 21 | + * @author Micle Platon |
| 22 | + * @notice This contract is for creating an untamperable decentralized smart contract |
| 23 | + * @dev This implements Chainlin VRF V2 and Chainlink KEEPERS |
| 24 | + */ |
| 25 | + |
| 26 | +contract Raffle is VRFConsumerBaseV2, AutomationCompatibleInterface { |
| 27 | + /* Type declarations */ |
| 28 | + enum RaffleState { |
| 29 | + OPEN, |
| 30 | + CALCULATING |
| 31 | + } // uint256 0 = OPEN, 1 = CALCULATING |
| 32 | + |
| 33 | + /* State variables */ |
| 34 | + VRFCoordinatorV2Interface private immutable i_vrfCoordinator; |
| 35 | + uint64 private immutable i_subscriptionId; |
| 36 | + bytes32 private immutable i_gasLane; |
| 37 | + uint32 private immutable i_callbackGasLimit; |
| 38 | + uint16 private constant REQUEST_CONFIRMATIONS = 3; |
| 39 | + uint32 private constant NUM_WORDS = 1; |
| 40 | + |
| 41 | + // Lottery variables |
| 42 | + uint256 private immutable i_interval; |
| 43 | + uint256 private immutable i_entranceFee; |
| 44 | + uint256 private s_lastTimeStamp; |
| 45 | + address private s_recentWinner; |
| 46 | + address payable[] private s_players; |
| 47 | + RaffleState private s_raffleState; |
| 48 | + |
| 49 | + /* Events */ |
| 50 | + event RequestedRaffleWinner(uint256 indexed requestId); |
| 51 | + event RaffleEnter(address indexed player); |
| 52 | + event WinnerPicked(address indexed winner); |
| 53 | + |
| 54 | + /* Functions */ |
| 55 | + constructor( |
| 56 | + address vrfCoordinatorV2, // contract --> need mocks for this |
| 57 | + uint64 subscriptionId, |
| 58 | + bytes32 gasLane, |
| 59 | + uint256 interval, |
| 60 | + uint256 entranceFee, |
| 61 | + uint32 callbackGasLimit |
| 62 | + ) VRFConsumerBaseV2(vrfCoordinatorV2) { |
| 63 | + i_vrfCoordinator = VRFCoordinatorV2Interface(vrfCoordinatorV2); |
| 64 | + i_gasLane = gasLane; |
| 65 | + i_interval = interval; |
| 66 | + i_subscriptionId = subscriptionId; |
| 67 | + i_entranceFee = entranceFee; |
| 68 | + s_raffleState = RaffleState.OPEN; |
| 69 | + s_lastTimeStamp = block.timestamp; |
| 70 | + i_callbackGasLimit = callbackGasLimit; |
| 71 | + } |
| 72 | + |
| 73 | + function enterRaffle() public payable { |
| 74 | + // instead of this: (msg.value > i_entranceFee, "Not enough ETH!") |
| 75 | + if (msg.value < i_entranceFee) revert Raffle__NotEnoughETH(); |
| 76 | + |
| 77 | + if (s_raffleState != RaffleState.OPEN) revert Raffle__NotOpen(); |
| 78 | + s_players.push(payable(msg.sender)); |
| 79 | + emit RaffleEnter(msg.sender); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @dev This is the function that the CHainlink Keeper nodes call |
| 84 | + * they look for the 'upkeepneeded' to return true |
| 85 | + * the following should be true in order to return true |
| 86 | + * 1. our time interval should have passed |
| 87 | + * 2. lottery should have at least 1 player an have some ETH |
| 88 | + * 3. our subscription is funded with LINK |
| 89 | + * 4. lottery should be in an "open" state --> enums to close # |
| 90 | + */ |
| 91 | + function checkUpkeep( |
| 92 | + bytes memory /*checkData*/ |
| 93 | + ) |
| 94 | + public |
| 95 | + override |
| 96 | + returns ( |
| 97 | + bool upkeepNeeded, |
| 98 | + bytes memory /* performData */ |
| 99 | + ) |
| 100 | + { |
| 101 | + bool isOpen = (RaffleState.OPEN == s_raffleState); |
| 102 | + bool timePassed = ((block.timestamp - s_lastTimeStamp) > i_interval); |
| 103 | + bool hasPlayers = (s_players.length > 0); |
| 104 | + bool hasBalance = address(this).balance > 0; |
| 105 | + upkeepNeeded = (isOpen && timePassed && hasPlayers && hasBalance); |
| 106 | + } |
| 107 | + |
| 108 | + function performUpkeep( |
| 109 | + bytes calldata /* performData*/ |
| 110 | + ) external override { |
| 111 | + (bool upkeepNeeded, ) = checkUpkeep(""); |
| 112 | + if (!upkeepNeeded) { |
| 113 | + revert Raffle__UpkeepNotNeeded( |
| 114 | + address(this).balance, |
| 115 | + s_players.length, |
| 116 | + uint256(s_raffleState) |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + s_raffleState = RaffleState.CALCULATING; |
| 121 | + uint256 requestId = i_vrfCoordinator.requestRandomWords( |
| 122 | + i_gasLane, |
| 123 | + i_subscriptionId, |
| 124 | + REQUEST_CONFIRMATIONS, |
| 125 | + i_callbackGasLimit, // SETS a cap for the gas price the function can run |
| 126 | + NUM_WORDS |
| 127 | + ); |
| 128 | + emit RequestedRaffleWinner(requestId); |
| 129 | + } |
| 130 | + |
| 131 | + function fulfillRandomWords( |
| 132 | + uint256, /* requestId */ |
| 133 | + uint256[] memory randomWords |
| 134 | + ) internal override { |
| 135 | + uint256 indexOfWinner = randomWords[0] % s_players.length; |
| 136 | + address payable recentWinner = s_players[indexOfWinner]; |
| 137 | + s_recentWinner = recentWinner; |
| 138 | + s_raffleState = RaffleState.OPEN; |
| 139 | + s_players = new address payable[](0); |
| 140 | + s_lastTimeStamp = block.timestamp; |
| 141 | + (bool success, ) = recentWinner.call{value: address(this).balance}(""); |
| 142 | + if (!success) { |
| 143 | + revert Raffle__TransferFailed(); |
| 144 | + } |
| 145 | + emit WinnerPicked(recentWinner); |
| 146 | + } |
| 147 | + |
| 148 | + /* views / pure functions */ |
| 149 | + function getEntranceFee() public view returns (uint256) { |
| 150 | + return i_entranceFee; |
| 151 | + } |
| 152 | + |
| 153 | + function getPlayer(uint256 index) public view returns (address) { |
| 154 | + return s_players[index]; |
| 155 | + } |
| 156 | + |
| 157 | + function getRecentWinner() public view returns (address) { |
| 158 | + return s_recentWinner; |
| 159 | + } |
| 160 | + |
| 161 | + function getRaffleState() public view returns (RaffleState) { |
| 162 | + return s_raffleState; |
| 163 | + } |
| 164 | + |
| 165 | + function getNumWords() public pure returns (uint256) { |
| 166 | + return NUM_WORDS; |
| 167 | + } |
| 168 | + |
| 169 | + function getInterval() public view returns (uint256) { |
| 170 | + return i_interval; |
| 171 | + } |
| 172 | +} |
0 commit comments