-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrandomFail.sol
59 lines (48 loc) · 1.76 KB
/
randomFail.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
pragma solidity ^0.4.21;
contract RandomFail {
address private host;
address[] public players;
uint constant public TICKET_PRICE = 0.01 ether;
function RandomFail() public {
host = msg.sender;
}
modifier onlyHost() {
require(msg.sender == host);
_;
}
function () public payable {
require(msg.value == 0);
}
function purchaseTicket() public payable returns (address[]) {
require(msg.value > TICKET_PRICE);
uint256 refund = msg.value - TICKET_PRICE;
if (refund > 0) {
msg.sender.transfer(refund);
}
players.push(msg.sender);
return players;
}
function determineWinnerTime_NO() public onlyHost returns (address winner) {
uint random = uint(keccak256(now));
uint winnerIndex = random % players.length;
winner = players[winnerIndex];
players.length = 0;
winner.transfer(address(this).balance);
}
function determineWinnerHash_NO() public onlyHost returns (address winner) {
bytes32 previousBlockHash = block.blockhash(block.number-1);
uint random = uint(keccak256(previousBlockHash));
uint winnerIndex = random % players.length;
winner = players[winnerIndex];
players.length = 0;
winner.transfer(address(this).balance);
}
function determineWinnerSeedHash_NO(uint randomSeed) public onlyHost returns (address winner) {
bytes32 previousBlockHash = block.blockhash(block.number-1);
uint random = uint(keccak256(randomSeed + uint(previousBlockHash)));
uint winnerIndex = random % players.length;
winner = players[winnerIndex];
players.length = 0;
winner.transfer(address(this).balance);
}
}