Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save atisheksingh/e18d072168b1eeac8b3fc4b984bacc8a to your computer and use it in GitHub Desktop.
Save atisheksingh/e18d072168b1eeac8b3fc4b984bacc8a to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.12+commit.f00d7308.js&optimize=false&runs=200&gist=
@ksarovar
Copy link

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;

import "@chainlink/contracts/src/v0.8/VRFConsumerBase.sol";

contract Ownable_Page_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(
        0x2Ca8E0C643bDe4C2E08ab1fA0da3401AdAD7734D, // VRF coordinator Goreli
        0x326C977E6efc84E512bB9C30f76E30c160eD06FB  // LINK token address Goreli
    ) {
        keyHash = 0x79d3d8832d904592c0bf9818b621522c988bb8b0c05cdc3b15aea1b6e8db0c15; //Goreli keyHash
        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 > 1 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);
  _;
}

}

//attached goreli vrf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment