Skip to content

Instantly share code, notes, and snippets.

@jackpmorgan
Created March 7, 2022 15:11
Show Gist options
  • Save jackpmorgan/b1bb3578b3ba1a9be779de05e7d840ca to your computer and use it in GitHub Desktop.
Save jackpmorgan/b1bb3578b3ba1a9be779de05e7d840ca to your computer and use it in GitHub Desktop.
The lottery smart contract that chooses a winner a Ownable.Page using Chainlink's VRF.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;
import "@chainlink/contracts/src/v0.8/VRFConsumerBase.sol";
contract Lottery is VRFConsumerBase {
address public owner;
address payable[] public players;
uint public lotteryId;
mapping (uint => address payable) public lotteryHistory;
bytes32 internal keyHash; // identifies which Chainlink oracle to use
uint internal fee; // fee to get random number
uint public randomResult;
constructor()
VRFConsumerBase(
0xb3dCcb4Cf7a26f6cf6B120Cf5A73875B7BBc655B, // VRF coordinator
0x01BE23585060835E02B77ef475b0Cc51aA1e0709 // LINK token address
) {
keyHash = 0x2ed0feb3e7fd2022120aa84fab1945545a9f2ffc9076fd6156fa96eaff4c1311;
fee = 0.1 * 10 ** 18; // 0.1 LINK
owner = msg.sender;
lotteryId = 1;
}
function getRandomNumber() public returns (bytes32 requestId) {
require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK in contract");
return requestRandomness(keyHash, fee);
}
function fulfillRandomness(bytes32 requestId, uint randomness) internal override {
randomResult = randomness;
payWinner();
}
function getWinnerByLottery(uint lottery) public view returns (address payable) {
return lotteryHistory[lottery];
}
function getBalance() public view returns (uint) {
return address(this).balance;
}
function getPlayers() public view returns (address payable[] memory) {
return players;
}
function enter() public payable {
require(msg.value > .01 ether);
// address of player entering lottery
players.push(payable(msg.sender));
}
function pickWinner() public onlyowner {
getRandomNumber();
}
function payWinner() public {
uint index = randomResult % players.length;
players[index].transfer(address(this).balance);
lotteryHistory[lotteryId] = players[index];
lotteryId++;
// reset the state of the contract
players = new address payable[](0);
}
modifier onlyowner() {
require(msg.sender == owner);
_;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment