Skip to content

Instantly share code, notes, and snippets.

@Scofield-Idehen
Created January 2, 2022 07:18
Show Gist options
  • Save Scofield-Idehen/4922bfe35bb319a8ac8587a9f01e236c to your computer and use it in GitHub Desktop.
Save Scofield-Idehen/4922bfe35bb319a8ac8587a9f01e236c 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.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Lottery{
//manager is in charge of the contract
address public manager;
//new player in the contract using array[] to unlimit number
address[] public players;
function lottery() public {
manager = msg.sender;
}
//to call the enter function we add them to players
function enter() public payable{
//each player is compelled to add a certain ETH to join
require(msg.value > .01 ether);
players.push(msg.sender);
}
//creates a random hash that will become our winner
function random() private view returns(uint){
return uint (keccak256(abi.encode(block.timestamp, players)));
}
function pickWinner() public restricted{
//only the manager can pickWinner
//require(msg.sender == manager);
//creates index that is gotten from func random % play.len
uint index = random() % players.length;
//pays the winner picked randomely(not fully random)
payable (players[index]).transfer(address(this).balance);
//empies the old lottery and starts new one
players = new address[](0);
}
modifier restricted(){
require(msg.sender == manager);
_;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment