Created
August 1, 2018 13:31
-
-
Save thephucit/cfde23eef5c67987497d8d72198a06f8 to your computer and use it in GitHub Desktop.
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.24; | |
| contract Lottery | |
| { | |
| address public manager; | |
| address[] public players; | |
| constructor() public | |
| { | |
| manager = msg.sender; | |
| } | |
| /* payable: khi goi ham nay thi phai chuyen tien vao truoc */ | |
| function enter() public payable | |
| { | |
| require(msg.value == 0.1 ether); | |
| players.push(msg.sender); | |
| } | |
| /* view neu khong de thi cung tu hieu la view */ | |
| /* view de thong bao tat ca mn khong duoc thay doi data trong smartcontract */ | |
| function random() private view returns (uint) | |
| { | |
| return uint(keccak256(block.difficulty, now, players)); | |
| } | |
| function pickWinner() public onlyManagerCanCall returns (address) | |
| { | |
| uint wIndex = random() % players.length; | |
| players[wIndex].transfer(address(this).balance); | |
| return players[wIndex]; | |
| } | |
| modifier onlyManagerCanCall() | |
| { | |
| require(msg.sender == manager); | |
| _; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment