Last active
August 7, 2021 22:36
-
-
Save devonartis/118ae46614741b02ff13d84bc9e33be6 to your computer and use it in GitHub Desktop.
Learning Solidity Contracts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.7.0 <0.9.0; | |
contract Lottery { | |
address public manager; | |
address[] public players; | |
constructor() { | |
manager = msg.sender; | |
} | |
function enter() public payable { | |
require(msg.value > .01 ether); | |
players.push(msg.sender); | |
} | |
function pickWinner() public payable restricted { | |
//require(msg.sender == manager); | |
uint index = random() % players.length; | |
payable(players[index]).transfer(address(this).balance); | |
// Reset the array | |
players = new address[](0); | |
} | |
function random() private view returns (uint) { | |
return uint(keccak256(abi.encodePacked(block.difficulty,block.timestamp, players))); | |
} | |
modifier restricted() { | |
require(msg.sender == manager); | |
_; | |
} | |
function getPlayers() public view returns (address[] memory) { | |
return players; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment