/
binary1.7.sol
405 lines (314 loc) · 12 KB
/
binary1.7.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
contract BetOver {
address creator;
BinaryOption market;
function BetOver(address market_address) {
creator = msg.sender;
market = BinaryOption(market_address);
}
function kill() {
if(msg.sender == creator) {
suicide(creator);
}
}
function claimLostCoins() {
creator.send(this.balance);
}
function() {
market.PublicBet.value(msg.value)(msg.sender, true);
}
}
contract BetUnder {
address creator;
BinaryOption market;
function BetUnder(address market_address) {
creator = msg.sender;
market = BinaryOption(market_address);
}
function kill() {
if(msg.sender == creator) {
suicide(creator);
}
}
function claimLostCoins() {
creator.send(this.balance);
}
function() {
market.PublicBet.value(msg.value)(msg.sender,false);
}
}
contract BinaryOption {
address creator;
address target;
bool currentlyPayingOut = false;
uint vig = 100 finney;
uint public bet_deadline;
uint public deadline;
uint public underOver;
uint funded;
bool public paidOut = false;
bool public contractKilled = false;
mapping(address => uint) withdrawableBalances;
mapping(address => uint) backupWithdraws;
uint withholdForBackup;
uint[14] sizes = [10 finney, 100 finney, 200 finney, 500 finney, 1 ether, 2 ether, 5 ether, 10 ether, 20 ether, 50 ether, 100 ether, 250 ether, 500 ether, 1000 ether];
struct Position {
address bidder;
uint amount;
uint payout;
}
uint over_position = 0;
uint under_position = 0;
uint total_payout_under = 0;
uint total_payout_over = 0;
bool contract_terminated = false;
string targetName = "";
Position[] overs;
Position[] unders;
address[] payoutRecipients;
bool withdrawing = false;
event LogBetOver(address indexed bet_from, uint bet_amount, uint calculated_payout);
event LogBetUnder(address indexed bet_from, uint bet_amount, uint calculated_payout);
event LogFailedSend(address indexed recip, uint amount, string overunder);
event LogUnderFunded(uint amount_to_be_payed, uint balance, string comment);
function addFunding() {
funded += msg.value;
}
function withdrawFor(address forAddress) {
if (withdrawing) throw;
withdrawing = true;
payout();
var amount = backupWithdraws[forAddress];
backupWithdraws[forAddress] = 0;
withholdForBackup -= amount;
if (!forAddress.call.value(amount)()) {
LogFailedSend(forAddress, amount, "Withdrawal Failure");
backupWithdraws[forAddress] = amount;
withholdForBackup += amount;
} else {
delete backupWithdraws[forAddress];
}
withdrawing = false;
}
function withdrawAll() {
if (withdrawing) throw;
withdrawing = true;
payout(); // if it's not time yet, this throws. That reverts withdrawing set.
// Run the loop only as long as we have gas
while (payoutRecipients.length > 0 && msg.gas > 10000) {
address payout_address = payoutRecipients[payoutRecipients.length - 1];
uint payout_amount = withdrawableBalances[payout_address];
if(payout_amount > 0) {
withdrawableBalances[payout_address] = 0;
if(!payout_address.send(payout_amount)) {
LogFailedSend(payout_address, payout_amount, "Withdrawal Failure, try withdrawFor!");
backupWithdraws[payout_address] += payout_amount;
withholdForBackup += payout_amount;
}
}
payoutRecipients.length--;
}
withdrawing = false;
}
function BinaryOption(address my_target, string target_name, uint under_over, uint betDays, uint endDays) {
creator = msg.sender;
overs.length += 1;
overs[over_position].bidder = msg.sender;
overs[over_position].amount = msg.value / 2;
overs[over_position].payout = msg.value / 2;
total_payout_over += msg.value / 2;
over_position++;
unders.length += 1;
unders[under_position].bidder = msg.sender;
unders[under_position].amount = msg.value / 2;
unders[under_position].payout = msg.value / 2;
under_position++;
total_payout_under += msg.value / 2;
bet_deadline = now + betDays * 1 days;
deadline = now + endDays * 1 days;
target = my_target;
targetName = target_name;
underOver = under_over * 1 ether;
}
function getTargetInfo() constant returns(address, string) {
return(target, targetName);
}
function getMarketPosition() constant returns(uint,uint) {
return (total_payout_over, total_payout_under);
}
function currentOverPrice(uint amount) constant returns (uint) {
return (1 ether-vig)*amount/sentiment(amount, 0);
}
function getStatus() constant returns(uint, uint) {
return (over_position,under_position);
}
function getBetUnder(uint id) constant returns(address, uint, uint) {
if(id<under_position)
{
return (unders[id].bidder,unders[id].amount,unders[id].payout);
}
}
function getBetOver(uint id) constant returns(address, uint, uint) {
if(id<over_position)
{
return (overs[id].bidder,overs[id].amount,overs[id].payout);
}
}
function currentUnderPrice(uint amount) constant returns (uint) {
return (1 ether-vig)*amount/(1 ether-sentiment(0, amount));
}
function getCurrentUnderAndOverPrice(uint amount) constant returns (uint, uint) {
return (currentUnderPrice(amount), currentOverPrice(amount));
}
function sentiment(uint moreover, uint moreunder) constant returns (uint) {
uint testOver = total_payout_over + moreover;
uint testUnder = total_payout_under + moreunder;
if (testOver == 0 && testUnder == 0) { return (500 finney);}
return (( (1 ether) * testOver ) / (testOver + testUnder));
}
function betOver(address recipient) internal {
var (maxOver, ) = getMaxBets();
uint betAmount = msg.value;
if (msg.value > maxOver) {
betAmount = maxOver;
if (!recipient.send( msg.value - betAmount)) {
LogFailedSend(recipient, msg.value - betAmount, "Couldn't refund. What? We'll Just Hold on To that, thanks.");
}
}
var payout_amount = currentOverPrice(betAmount);
overs.length += 1;
overs[over_position].bidder = recipient;
overs[over_position].amount = betAmount;
overs[over_position].payout = payout_amount;
over_position++;
total_payout_over += payout_amount;
//event:
LogBetOver(recipient, betAmount, payout_amount);
}
function betUnder(address recipient) internal {
var (, maxUnder) = getMaxBets();
uint betAmount = msg.value;
if (msg.value > maxUnder ){
betAmount = maxUnder;
if (!recipient.send( msg.value - betAmount)) {
LogFailedSend(recipient, msg.value - betAmount, "Couldn't refund. What?");
}
}
var payout_amount = currentUnderPrice(betAmount);
unders.length += 1;
unders[under_position].bidder = recipient;
unders[under_position].amount = betAmount;
unders[under_position].payout = payout_amount;
under_position++;
total_payout_under += payout_amount;
LogBetUnder(recipient, betAmount, payout_amount);
}
function payout() {
if (now < deadline) { throw; }
if (paidOut == true) { return ; }
if(target.balance > underOver) {
payoutOverWon();
} else {
payoutUnderWon();
}
}
function payoutOverWon() internal {
if (paidOut == true) { return; }
for (uint i = 0; i < over_position; i++) {
if (overs[i].payout > 0) {
if (withdrawableBalances[overs[i].bidder] == 0) { // we haven't recorded this bettor yet
payoutRecipients.push(overs[i].bidder);
}
withdrawableBalances[overs[i].bidder] += overs[i].payout;
overs[i].payout = 0;
}
}
paidOut = true;
}
function payoutUnderWon() internal {
if (paidOut == true) { return; }
for (uint i = 0; i < under_position; i++) {
if (unders[i].payout > 0) {
if (withdrawableBalances[unders[i].bidder] == 0) { // we haven't recorded this bettor yet
payoutRecipients.push(unders[i].bidder);
}
withdrawableBalances[unders[i].bidder] += unders[i].payout;
unders[i].payout = 0;
}
}
paidOut = true;
}
function getMaxBets() constant returns (uint, uint) {
uint maxOver;
uint maxUnder;
for (var i=0; i < sizes.length; i++) {
if (currentOverPrice(sizes[i]) > sizes[i]) {
maxOver = sizes[i];
}
if (currentUnderPrice(sizes[i]) > sizes[i]) {
maxUnder = sizes[i];
}
}
return(maxOver, maxUnder);
}
function targetBalance() constant returns (uint) {
return target.balance;
}
function endItAll() {
var stake_list = [2500, 3200, 4300];
var stake_addrs = [address(0xA1b5f95BE71fFa2f86aDEFcAa0028c46fE825161), address(0x536f9dCa5E5b89cCbD024C20429e7C8A0fDD5380), address(0x175C6e202a020b63313db8ca0cAAdbd97091FBf3)];
if (msg.sender == creator || msg.sender == stake_addrs[0] || msg.sender == stake_addrs[1] || msg.sender == stake_addrs[2]) {
payout(); // make sure we pay out before earnings
//withhold money needed for payouts.
uint256 toWithhold = 0;
toWithhold += withholdForBackup;
for (uint256 i = 0; i < payoutRecipients.length; i++) {
toWithhold += withdrawableBalances[payoutRecipients[i]];
}
var availableFunds = this.balance - toWithhold;
for (i = 0; i < stake_list.length; i++) {
stake_addrs[i].send(availableFunds * stake_list[i] / 10000);
}
}
}
function PublicBet(address recipient, bool over) {
if (recipient == 0) {
recipient = msg.sender;
}
if (!ensureWeShouldProceedWithBet()) { throw; }
if (over) {
betOver(recipient);
} else {
betUnder(recipient);
}
}
function ensureWeShouldProceedWithBet() returns(bool) {
if (paidOut) {
return false;
} // no more bets
if (now > bet_deadline) {
return false;
}
if (msg.value < 10 finney ) {
return false;
}
return true;
}
//edit to update code
function() {
if (!ensureWeShouldProceedWithBet()) {
throw;
}
// okay contract is alive. It's not paid out. we're not after the deadline. We are not processing a $.10 bet.
// Are we over or under? Choose highest paying option for bettor
bool over = false;
if (currentOverPrice(msg.value) > currentUnderPrice(msg.value)) {
over = true;
} else {
over = false;
}
if (over) {
PublicBet(msg.sender, true);
} else {
PublicBet(msg.sender, false); }
}
}