Skip to content

Instantly share code, notes, and snippets.

@thephucit
Created August 1, 2018 13:31
Show Gist options
  • Select an option

  • Save thephucit/cfde23eef5c67987497d8d72198a06f8 to your computer and use it in GitHub Desktop.

Select an option

Save thephucit/cfde23eef5c67987497d8d72198a06f8 to your computer and use it in GitHub Desktop.
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