Created
September 5, 2018 13:39
-
-
Save willianmano/eac9b24ff9df04cce88465f485a8fdbc to your computer and use it in GitHub Desktop.
A Lottery Solidity Smart Contract
This file contains hidden or 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
pragma solidity ^0.4.21; | |
contract Lottery { | |
address public manager; | |
address[] public players; | |
constructor() public { | |
manager = msg.sender; | |
} | |
function enter() public payable { | |
require(msg.value > .01 ether); | |
players.push(msg.sender); | |
} | |
function random() private view returns (uint) { | |
return uint(keccak256(abi.encodePacked(block.difficulty, now, players))); | |
} | |
function pickWinner() public restricted { | |
uint index = random() % players.length; | |
players[index].transfer(address(this).balance); | |
players = new address[](0); | |
} | |
function getPlayers() public view returns (address[]) { | |
return players; | |
} | |
modifier restricted() { | |
require(msg.sender == manager); | |
_; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: The called function should be payable if you send value and the value you send should be less than your current balance.
Debug the transaction to get more information?