-
Notifications
You must be signed in to change notification settings - Fork 0
/
DogeBet_old.sol
359 lines (280 loc) · 10.1 KB
/
DogeBet_old.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
pragma solidity ^0.4.8;
import "./oraclizeAPI_0.4.sol";
import "./strings.sol";
contract DogeBet {
address public owner;
mapping (uint => address) public bets;
// New Bet Created
event NewBet(uint matchID, address betAddress);
function DogeBet() {
owner = msg.sender;
}
/** Create a new bet
@return the address of the betting contract
*/
function createBet(uint _match) {
if(bets[_match] != 0){
throw;
}
// matchID, Id of team
address newBet = new WinLoseTieBet(_match);
// save contractaddress in mapping with ID
bets[_match] = newBet;
}
function kill(){
if (msg.sender == owner) suicide(msg.sender);
}
}
contract WinLoseTieBet is usingOraclize {
using strings for *;
address public creator;
// ufixed public houseEdge = 0.02; // 2 percent to the house
uint public matchID;
string strMatchID;
bytes32 timeQueryID;
bytes32 finishedQueryID;
bytes32 winningQueryID;
bytes32 scoreTeam1QueryID;
bytes32 scoreTeam2QueryID;
// 0 is Tie, 1 is Team 1 wins and 2 is Team 2 wins
mapping (uint => mapping (address => uint)) bids;
mapping (bytes32 => bool) validIds;
address[] betters;
string public api;
uint public startTime;
string public matchTime;
uint public counter;
uint winner;
bool initialized;
string api1;
string api2;
string public scoreTeam1;
string public scoreTeam2;
bool finalScore1;
bool finalScore2;
string public finished;
uint public totalBalance1 = 0; // balance of all bets on "win" teamA
uint public totalBalance2 = 0; // balance of all bets on "win" teamB
uint public totalBalanceTie = 0; // balance of all bets on ""
function WinLoseTieBet(uint _matchID) payable {
// shit doesnt work this way (?) but anyway just testnet yo
creator = msg.sender;
OAR = OraclizeAddrResolverI(0x51efaf4c8b3c9afbd5ab9f4bbc82784ab6ef8faa);
api1 = "json(http://carlfriess.com:3000/api/getmatchdata/";
matchID = _matchID;
strMatchID = uint2str(_matchID);
api = strConcat(api1, strMatchID);
initialized = false;
}
// pay Ether + Fees
function bid(uint _id) payable{
// Revert the call if the bidding
// period is over.
if (!initialized){
// Time UTC <MatchDateTimeUTC>2016-11-19T17:30:00Z</MatchDateTimeUTC>
var str = strConcat(api, ").MatchDateTimeUTC");
timeQueryID = oraclize_query("URL", str);
validIds[timeQueryID] = true;
// Finished?
finished = "false";
str = strConcat(api, ").MatchIsFinished");
finishedQueryID = oraclize_query("URL", str);
validIds[finishedQueryID] = true;
initialized = true;
}else{
require(now <= startTime);
}
// Sanity Check
if(_id < 0 && 2 < _id && msg.value <= 0.0){
throw;
}
bids[_id][msg.sender] += msg.value;
betters.push(msg.sender);
// Bets on tie
if (_id == 0){
totalBalanceTie += msg.value;
// Bets on Team 1
}else if(_id == 1){
totalBalance1 += msg.value;
// Bets on Team 2
}else{
totalBalance2 += msg.value;
}
}
function payoff() internal{
uint totalPayoff;
uint payoutBalance;
uint collectedFees;
address better;
uint amount;
uint winAmount;
uint idx;
if (winner == 0) {
totalPayoff = totalBalance1 + totalBalance2;
payoutBalance = totalPayoff; // * (1 - houseEdge);
collectedFees = totalPayoff; // * houseEdge;
for (idx = 0; idx < betters.length; idx += 1) {
better = betters[idx];
amount = bids[0][better];
// what he gave + the his
winAmount = amount + ((amount / totalBalanceTie) * totalPayoff);
better.transfer(winAmount);
}
} else if (winner == 1){
totalPayoff = totalBalanceTie + totalBalance2;
payoutBalance = totalPayoff; // * (1 - houseEdge);
collectedFees = totalPayoff; // * houseEdge;
for (idx = 0; idx < betters.length; idx += 1) {
better = betters[idx];
amount = bids[1][better];
// what he gave + the his
winAmount = amount + ((amount / totalBalance1) * totalPayoff);
better.transfer(winAmount);
}
} else if (winner == 2) {
totalPayoff = totalBalanceTie + totalBalance2;
payoutBalance = totalPayoff; // * (1 - houseEdge);
collectedFees = totalPayoff; // * houseEdge;
for (idx = 0; idx < betters.length; idx += 1) {
better = betters[idx];
amount = bids[1][better];
// what he gave + the his
winAmount = amount + ((amount / totalBalance1) * totalPayoff);
better.transfer(winAmount);
}
}
}
function __callback(bytes32 id, string result) {
if (!validIds[id]) throw;
if (msg.sender != oraclize_cbAddress()) throw;
if (id == timeQueryID){
matchTime = result;
startTime = parseTime(result);
} else if (id == finishedQueryID){
finished = result;
if(sha3(finished) == sha3("false")){
update();
}
} else if (id == scoreTeam1QueryID){
scoreTeam1 = result;
finalScore1 = true;
} else if (id == scoreTeam2QueryID){
scoreTeam2 = result;
finalScore1 = true;
}
// Game is finished
if (sha3(finished) == sha3("true") && !finalScore1 && !finalScore2) {
// PointsTeam1
var str = strConcat(api, ").MatchResults.1.PointsTeam1");
scoreTeam1QueryID = oraclize_query("URL", str);
validIds[scoreTeam1QueryID] = true;
// PointsTeam2
str = strConcat(api, ").MatchResults.1.PointsTeam2");
scoreTeam2QueryID = oraclize_query("URL", str);
validIds[scoreTeam2QueryID] = true;
}else if(sha3(finished) == sha3("true") && finalScore1 && finalScore2){
// Tie
if (sha3(scoreTeam1) == sha3(scoreTeam2)){
winner = 0;
// Team 1 won
}else if (sha3(scoreTeam1) > sha3(scoreTeam2)){
winner = 1;
// Team 2 won
}else {
winner = 2;
}
// payoff();
}
delete validIds[id];
}
function update() payable{
counter += 1;
// Finished?
var str = strConcat(api, ").MatchIsFinished");
finishedQueryID = oraclize_query(120, "URL", str);
validIds[finishedQueryID] = true;
}
// ------------- Timestamp Stuff -------------
function parseTime(string _time) internal returns (uint){
// uses https://github.com/Arachnid/solidity-stringutils
var slicedTime = _time.toSlice();
// slicedTime: 2016-11-19T17:30:00Z
uint yy = parseInt((slicedTime.split("-".toSlice())).toString());
// year: 2016, slicedTime: 11-19T17:30:00Z
uint mm = parseInt((slicedTime.split("-".toSlice())).toString());
// month: 11, slicedTime: 19T17:30:00Z
uint dd = parseInt((slicedTime.split("T".toSlice())).toString());
// day: 19, slicedTime: 17:30:00Z
uint hh = parseInt((slicedTime.split(":".toSlice())).toString());
// hour: 17, slicedTime: 30:00Z
uint min = parseInt((slicedTime.split(":".toSlice())).toString());
// minutes: 30, slicedTime: 00Z
uint sec = parseInt((slicedTime.split("Z".toSlice())).toString());
// seconds: 00, slicedTime: ""
// convert everything to unix epoch aka "seconds since Jan 01 1970"
// using https://github.com/pipermerriam/ethereum-datetime
uint epoch = toTimestamp(yy, mm, dd, hh, min, sec);
return epoch;
}
uint constant ORIGIN_YEAR = 1970;
uint constant DAY_IN_SECONDS = 86400;
uint constant YEAR_IN_SECONDS = 31536000;
uint constant LEAP_YEAR_IN_SECONDS = 31622400;
uint constant HOUR_IN_SECONDS = 3600;
uint constant MINUTE_IN_SECONDS = 60;
function isLeapYear(uint year) internal constant returns (bool) {
if (year % 4 != 0) {
return false;
}
if (year % 100 != 0) {
return true;
}
if (year % 400 != 0) {
return false;
}
return true;
}
function toTimestamp(uint year, uint month, uint day, uint hour, uint minute, uint second) internal constant returns (uint timestamp) {
uint i;
// Year
for (i = ORIGIN_YEAR; i < year; i++) {
if (isLeapYear(i)) {
timestamp += LEAP_YEAR_IN_SECONDS;
}
else {
timestamp += YEAR_IN_SECONDS;
}
}
// Month
uint[12] memory monthDayCounts;
monthDayCounts[0] = 31;
if (isLeapYear(year)) {
monthDayCounts[1] = 29;
}
else {
monthDayCounts[1] = 28;
}
monthDayCounts[2] = 31;
monthDayCounts[3] = 30;
monthDayCounts[4] = 31;
monthDayCounts[5] = 30;
monthDayCounts[6] = 31;
monthDayCounts[7] = 31;
monthDayCounts[8] = 30;
monthDayCounts[9] = 31;
monthDayCounts[10] = 30;
monthDayCounts[11] = 31;
for (i = 1; i < month; i++) {
timestamp += DAY_IN_SECONDS * monthDayCounts[i - 1];
}
// Day
timestamp += DAY_IN_SECONDS * (day - 1);
// Hour
timestamp += HOUR_IN_SECONDS * (hour);
// Minute
timestamp += MINUTE_IN_SECONDS * (minute);
// Second
timestamp += second;
return timestamp;
}
}