forked from cloudzombie/Etherflip
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontract.sol
308 lines (214 loc) · 6.82 KB
/
contract.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
contract Random{
event Stage0BetsOpen();
event Stage1BetsOpen();
event Stage1BetsClosed();
event Stage1BetsDecided();
event readyForNewPlayers();
enum Stages {
betsOpen,
reveal,
reset
}
//this is the current stage.
Stages public stage = Stages.betsOpen;
modifier atStage(Stages _stage) {
if (stage != _stage) throw;
_
}
function nextStage() internal {
stage = Stages(uint(stage) + 1);
}
function sendToFinishStage() internal {
stage = Stages(uint(stage) + 2);
}
//executed at init - sets the owner of the contract to creator
function Random() { owner = msg.sender; }
//owner of the type - address
address public owner;
//block number at init
uint blockNumberAtInit;
uint public rewardValue;
//win amount
uint public amount;
//seedA
uint256 internal seedA;
//seedAHash
bytes32 public seedAHash;
//seedB
uint public seedB;
//seedBHash
bytes32 public seedBStage1Hash;
//seedBStage2Hash
bytes32 public seedBStage2Hash;
//seedC
uint256 public seedC;
//result
uint256 public dieResult;
//high die result (49+)
bool public high;
//win state
bool public win;
//msg.sender
address public msgSender;
//turns input data into a 100-sided 'die' by dividing by ceil(2 ^ 256 / 100)
uint256 public FACTOR = 1157920892373161954235709850086879078532699846656405640394575840079131296399;
//betters
//create an array of struct Funder
Funder[] public funders;
struct Funder {
address addr;
uint amount;
uint Number;
uint rewardValue;
}
uint arrayLength;
uint playerNumber;
uint theAmount;
uint theReward;
address theAddress;
//initialSeed
bool initialSeedSet = false;
address seedInitAddress;
bool acceptNoMoreBets;
//generate random number
function rand ( uint seedBUserParam ) atStage(Stages.betsOpen) returns (uint256){
//user address
msgSender = msg.sender;
uint256 betValue = msg.value;
rewardValue = (betValue)+(betValue*90/100);
if(initialSeedSet){
uint seedBLate = seedBUserParam;
funders.push( Funder({addr: msgSender, amount: betValue, Number: seedBLate, rewardValue: rewardValue}));
}
if(!initialSeedSet){
seedB = seedBUserParam;
seedInitAddress = msg.sender;
funders.push( Funder({addr: seedInitAddress, amount: betValue, Number: seedBUserParam, rewardValue: rewardValue}));
initialSeedSet = true;
//get hash of parent block for seedA
uint256 lastBlockNumberA = block.number - 1;
uint256 lastBlockHashValA = uint256(block.blockhash(lastBlockNumberA));
seedA = lastBlockHashValA;
//provably fair - as can be checked after result
seedAHash = sha3(seedA);
//ensure stage 2 seedB equals stage 1 seedB
seedBStage1Hash = sha3(seedB);
blockNumberAtInit = block.number;
}
nextStage();
Stage0BetsOpen();
}
//generate random number
function reveal( uint256 seedBUserParam2 ) atStage(Stages.reveal) returns (uint256){
Stage1BetsOpen();
if (block.number >= blockNumberAtInit+2){
Stage1BetsClosed();
acceptNoMoreBets = true;
//get hash of parent block for seedC
uint256 lastBlockNumberC = block.number - 1;
uint256 lastBlockHashValC = uint256(block.blockhash(lastBlockNumberC));
seedC = lastBlockHashValC;
//result
dieResult = (uint256(seedA + seedB + seedC) / FACTOR) +1;
//high result boolean
if(dieResult > 49){
//payWinners
arrayLength = funders.length;
uint i;
for (i = 0; i < arrayLength; i++) {
playerNumber = funders[i].Number;
theReward = funders[i].rewardValue;
theAddress = funders[i].addr;
if(playerNumber > 49){
theAddress.send(theReward);
}
}
delete funders;
Stage1BetsDecided();
nextStage();
}
//low result boolean
if(dieResult < 50){
//payWinners
arrayLength = funders.length;
i;
for (i = 0; i < arrayLength; i++) {
playerNumber = funders[i].Number;
theReward = funders[i].rewardValue;
theAddress = funders[i].addr;
if(playerNumber < 50){
theAddress.send(theReward);
}
}
delete funders;
Stage1BetsDecided();
nextStage();
}
}
}
function resetStage() atStage(Stages.reset) {
blockNumberAtInit;
//if ( block.number > blockNumberAtDecide + 2){
initialSeedSet = false;
readyForNewPlayers();
stage = Stages(uint(0));
//}
}
//getters
//to do - remove dangerous ones
function getEntrants(uint getNumberAtPosition) constant returns (uint entrantInfo){
return funders[getNumberAtPosition].Number + funders[getNumberAtPosition].amount;
}
function getStage() returns (Stages)
{
return stage;
}
//todo remove - important
function getSeedA() returns (uint256)
{
if (msg.sender == owner) return seedA;
}
function getSeedAHash() returns (bytes32)
{
return seedAHash;
}
function getSeedB() returns (uint256)
{
return seedB;
}
function getSeedBStage1Hash() returns (bytes32)
{
return seedBStage1Hash;
}
function getSeedBStage2Hash() returns (bytes32)
{
return seedBStage2Hash;
}
function getSeedC() returns (uint256)
{
if (msg.sender == owner) return seedC;
}
function getDieResult() returns (uint256)
{
return dieResult;
}
function ownerResetGame()
{
if (msg.sender == owner){
//refundBets
arrayLength = funders.length;
uint i;
for (i = 0; i < arrayLength; i++) {
theAmount = funders[i].amount;
theAddress = funders[i].addr;
if(playerNumber < 50){
theAddress.send(theAmount);
}
}
delete funders;
stage = Stages(uint(0));
}
}
//recover total contract balance to owner of contract (creator)
function kill() { if (msg.sender == owner) suicide(owner); }
}