-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
885 lines (765 loc) · 27.9 KB
/
app.js
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
let lotion = require('lotion')
let BigNumber = require('bignumber.js'); // https://github.com/MikeMcl/bignumber.js/
let secp = require('secp256k1');
let { sha256, addressHash } = require('./common.js');
let getSigHash = require('./sigHash.js');
let stateUtil = require('./stateUtil.js');
const STARTING_SEQ = 0;
// market state template.
// every market will have a state like this.
let marketTemplate = {
id: "", // [string] market id
phaseTime: {}, // the start and end time of each phase (time in blockheight)
bets: [],
oracles: [], // list of approved oracles when market is created
oracleOutcome: -1, // for hackathon, we only have one oracle, so we makes it simple.
// just one single result. outcome is specified by 1, 2, ....
// 0 indicate no outcome yet
//oracleOutcomes: [], // when oracle pushes the result, it is stored here.
challenge: undefined, // [Object] challenge object stating the challenge info
voteRecords: [], // list of all vote
// votes: {
// outcome1: 0,
// outcome2: 0,
// outcome3: 0,
// }, // aggregated result of vote
payoutRatio: 1.5,
storage: {},
}
let initMarket = Object.assign({}, marketTemplate);
let initialState = {
market: {
market1: initMarket,
}, // store the various market. each market will have a market state object inside this.
balances: {
'5wvwWgKP3Qfw1akQoXWg4NtKmzx5v4dTj': 10000,
'9x2yu6AzwWphm3j6h9pTaJh63h5ioG8RL': 10000,
't6bUAUxNh8xoZzt6YZdQn27J4DSiR2oH': 10000,
},
// store the current sequence of the user.
seq: {
}
}
/*
calculation:
in general, we try to use bignumber to do calculation.
however, we will still store the final result in state object as number. because bignumber object does not store in state well.
*/
/*
format of start tx
{
type: "start",
"startInfo": {
"question": "Who will win FIFA 2018?",
"outcomes": [
"england",
"italy",
"brazil",
"germany"
],
"oracle": ["9x2yu6AzwWphm3j6h9pTaJh63h5ioG8RL","5wvwWgKP3Qfw1akQoXWg4NtKmzx5v4dTj"], // addresses of approved oracles
// meta data about oracle. eg. description
"oracleMeta": "http://data.com/oracleinfo",
"phaseTime":{
"marketStart":9,"marketEnd":3609,
"oracleStart":3610,"oracleEnd":7210,
"challengeStart":7211,"challengeEnd":10811,
"voteStart":10812,"voteEnd":14412,
"distributeStart":14413,"distributeEnd":18013
},
},
}
format of bets
{
type: "bet",
user: "alice", // should be an address
amount: 100, // may allow multi token later
outcome: 1, // user bets on outcome 1,2,3, or...
}
format of challenge
{
type: "challenge",
user: "alice", // should be an address
amount: 100,
}
format of vote
{
type: "vote",
user: "alice", // should be an address
amount: 1000,
outcome: 1,
}
format of distribute
{type: "distribute"}
*/
let app = lotion({
initialState: initialState,
devMode: true
})
app.use(txVerifySigHandler);
app.use(txHandler1);
app.use(txHandler2);
app.use(txStartHandler);
app.use(txBetHandler);
app.use(txOracleHandler);
app.use(txChallengeHandler);
app.use(txVoteHandler);
app.use(txDistributeHandler);
app.use(txSendHandler);
app.listen(3000).then(function(appInfo) {
console.log(appInfo);
})
/**
* doesn't do anything other than verify signature.
* @param {*} state
* @param {*} tx
* @param {*} chainInfo
*/
function txVerifySigHandler(state, tx, chainInfo) {
if (tx.type === "verifySig") {
let cloned = Object.assign({}, tx);
console.log("verifySig tx: ", JSON.stringify(cloned));
let verifyResult = verifySig(state, cloned);
if (!verifyResult.verified) {
console.log('invalid signature!');
throw Error("invalid signature!");
}
console.log("signature verified!");
}
}
function txHandler1(state, tx, chainInfo) {
console.log("tx: ", JSON.stringify(tx));
//console.log(chainInfo);
checkTxCommon(tx); // this is always called, so we check it here.
}
function txHandler2(state, tx, chainInfo) {
console.log("block height: ", chainInfo.height);
}
function txStartHandler(state, tx, chainInfo) {
if (tx.type === "start") {
// starting the market.
// calculate the phase time320c2bfd1b17fdbc9dd876dd2535f562112f2cdf
let cloned = Object.assign({}, tx);
console.log("start: ", JSON.stringify(cloned));
let marketId = cloned.marketId;
let startInfo = cloned.startInfo;
checkStartInfo(startInfo);
// check marketId does not exist yet
if (typeof (state.market[marketId]) === 'undefined') {
// create a new market
createNewMarket(state, marketId);
let blockHeight = chainInfo.height;
var t1 = 1; // ??? todo: delete
let phaseTime = calcPhaseTime(blockHeight);
marketSelector(state, marketId).phaseTime = phaseTime;
console.log("phaseTime: ", marketSelector(state, marketId).phaseTime);
// record statInfo
marketSelector(state, marketId).startInfo = startInfo;
// if phaseTime is provided in the startInfo, override with the one in startInfo.
if (typeof startInfo.phaseTime !== "undefined") {
marketSelector(state, marketId).phaseTime = startInfo.phaseTime;
}
} else {
console.log("marketId " + marketId + " already exists. new market is not created.");
}
}
}
function createNewMarket(state, marketId) {
let initMarket = Object.assign({}, marketTemplate);
initMarket.id = marketId;
state.market[marketId] = initMarket;
}
function txBetHandler(state, tx, chainInfo) {
if (tx.type === "bet") {
// ignore request if it is outside of a particular phase timeframe
if (isInPhase(chainInfo.height, "market", state)) {
let cloned = Object.assign({}, tx);
console.log("bet: ", JSON.stringify(cloned));
let verifyResult = verifySig(state, cloned);
if (!verifyResult.verified) {
console.log('invalid signature!');
throw Error('invalid signature!');
}
cleanSignature(cloned);
console.log("signature verified!");
let marketId = cloned.marketId;
let user = cloned.user;
let amount = new BigNumber(cloned.amount);
// lock up their staking tokens
state.balances[user] = new BigNumber(state.balances[user]).minus(amount).toNumber();
marketSelector(state, marketId).bets.push(cloned);
console.log("bets: ", marketSelector(state, marketId).bets);
} else {
console.log("wrong phase. bet can only be done in market phase.");
}
}
}
function txOracleHandler(state, tx, chainInfo) {
if (tx.type === "oracle") {
// ignore request if it is outside of a particular phase timeframe
if (isInPhase(chainInfo.height, "oracle", state)) {
console.log("should check oracle identity, but that checking is skipped for hackathon.");
let cloned = Object.assign({}, tx);
console.log("oracle tx: ", JSON.stringify(cloned));
let verifyResult = verifySig(state, cloned);
if (!verifyResult.verified) {
console.log('invalid signature!');
throw Error('invalid signature!');
}
cleanSignature(cloned);
let marketId = cloned.marketId;
// check if oracle is from approved oracle list.
let approvedOracles = marketSelector(state, marketId).startInfo.oracle;
if (!approvedOracles.includes(verifyResult.address)) {
let msg = "only oracle from approved list can submit oracle result";
console.log(msg);
throw Error(msg);
}
marketSelector(state, marketId).oracleOutcome = cloned.outcome;
console.log("oracleOutcome: ", marketSelector(state, marketId).oracleOutcome);
} else {
console.log("wrong phase. oracle call can only be done in oracle phase.");
}
}
}
function txChallengeHandler(state, tx, chainInfo) {
if (tx.type === "challenge") {
// ignore request if it is outside of a particular phase timeframe
if (isInPhase(chainInfo.height, "challenge", state)) {
console.log("skip challenger verification and balance verification for hackathon.");
let cloned = Object.assign({}, tx);
console.log("challenge tx: ", JSON.stringify(cloned));
let verifyResult = verifySig(state, cloned);
if (!verifyResult.verified) {
console.log('invalid signature!');
throw Error('invalid signature!');
}
cleanSignature(cloned);
let marketId = cloned.marketId;
let user = cloned.user;
let amount = new BigNumber(cloned.amount);
// lock up their staking tokens
state.balances[user] = new BigNumber(state.balances[user]).minus(amount).toNumber();
marketSelector(state, marketId).challenge = cloned;
console.log("challenge: ", marketSelector(state, marketId).challenge);
} else {
console.log("wrong phase. challenge call can only be done in challenge phase.");
}
}
}
/**
* check if challenge has been issued
* (if the state contains challenge info)
*/
function challenged(state, marketId) {
let obj = marketSelector(state, marketId).challenge;
// check if challenge object is still empty
return (typeof obj !== 'undefined');
}
function txVoteHandler(state, tx, chainInfo) {
if (tx.type === "vote") {
// ignore request if it is outside of a particular phase timeframe
if (isInPhase(chainInfo.height, "vote", state)) {
console.log("skip voter verification and balance verification for hackathon");
let cloned = Object.assign({}, tx);
console.log("vote tx", JSON.stringify(cloned));
let verifyResult = verifySig(state, cloned);
if (!verifyResult.verified) {
console.log('invalid signature!');
throw Error('invalid signature!');
}
cleanSignature(cloned);
let marketId = cloned.marketId;
let user = cloned.user;
let amount = new BigNumber(cloned.amount);
let outcome = cloned.outcome;
if (challenged(state, marketId)) {
// lock up their staking tokens
state.balances[user] = new BigNumber(state.balances[user]).minus(amount).toNumber();
marketSelector(state, marketId).voteRecords.push(cloned);
// update votes
//updateVotes(outcome, amount);
console.log("vote records: ", marketSelector(state, marketId).voteRecords);
} else {
console.log("do not accept vote if there is no challenge");
}
} else {
console.log("wrong phase. vote call can only be done in vote phase.");
}
}
}
function updateVotes(outcome, amount, state) {
let outcomeName = 'outcome' + outcome;
// if (! (outcome in marketSelector(state, marketId).votes) ) {
// console.log("3");
// marketSelector(state, marketId).votes[outcomeName] = 0;
// console.log("4");
// }
marketSelector(state, marketId).votes[outcomeName] = new BigNumber(marketSelector(state, marketId).votes[outcomeName]).plus(amount).toNumber();
}
function txDistributeHandler(state, tx, chainInfo) {
if (tx.type === "distribute") {
// ignore request if it is outside of a particular phase timeframe
if (isInPhase(chainInfo.height, "distribute", state)) {
let cloned = Object.assign({}, tx);
console.log("distribute tx", JSON.stringify(cloned));
let verifyResult = verifySig(state, cloned);
if (!verifyResult.verified) {
console.log('invalid signature!');
throw Error('invalid signature!');
}
cleanSignature(cloned);
let marketId = cloned.marketId;
let localContractStorage = new LocalContractStorage(state, marketId);
// check that distribute hasn't been completed already
let checkValue = localContractStorage.getValue("payouts");
if (typeof checkValue !== "undefined") {
let msg = "distribute cannot be invoked again";
console.log(msg);
throw Error(msg);
}
// do final calculation and distribute the tokens accordingly.
// finaloutcome is assumed to be oracleoutcome, unless there is a vote.
let finalOutcome = marketSelector(state, marketId).oracleOutcome;
// if challenge is there
if (challenged(state, marketId)) {
// get voting pool result
// distribute to the winner of the voter who vote for it.
console.log("challenge was requested");
// sum up voting pool.
// give it to the winners proportionally.
let voteRecords = marketSelector(state, marketId).voteRecords;
let votePoolTotal = new BigNumber(0);
let result = {};
for (let i = 0; i < voteRecords.length; i++) {
let vote = voteRecords[i];
let voteAmount = new BigNumber(vote.amount);
if (typeof result[vote.outcome] === "undefined") {
result[vote.outcome] = voteAmount.toNumber();
} else {
result[vote.outcome] = new BigNumber(result[vote.outcome]).plus(voteAmount).toNumber();
}
console.log("vote amount", voteAmount.toNumber());
votePoolTotal = votePoolTotal.plus(voteAmount);
}
console.log("votePoolTotal: ", votePoolTotal);
//votePoolTotal = votePoolTotal + marketSelector(state, marketId).challenge.amount;
console.log("vote result", result);
localContractStorage.setValue("voteResult", result);
// determine which outcome win.
// loop through to find the highest voted amount
let highestOutcome; // the outcome with highest votes.
let highestOutcomeAmount = new BigNumber(-1); // the staked token amount of highest-voted outcome
for (let outcome in result) {
let outcomeAmount = new BigNumber(result[outcome]); // the amount of votes (amount of tokens) for the this outcome
if (outcomeAmount.gt(highestOutcomeAmount)) {
highestOutcome = outcome;
highestOutcomeAmount = outcomeAmount;
}
}
console.log("voted outcome: " + highestOutcome + ". tokens staked for voted outcome: " + highestOutcomeAmount.toNumber());
// set the final outcome to the highest voted outcome
finalOutcome = highestOutcome;
console.log("final voted outcome: ", finalOutcome);
localContractStorage.setValue("votedOutcome", finalOutcome);
console.log("distributed vote pool");
doPayout("vote", finalOutcome, voteRecords, state, marketId);
}
// distribute the original bet pool to the people
// let betPoolTotal = 0;
let bets = marketSelector(state, marketId).bets;
// for (let j = 0; j < bets.length; j++) {
// console.log("6");
// let bet = bets[j];
// betPoolTotal += bet.amount;
// }
doPayout("bet", finalOutcome, bets, state, marketId);
// if (challenged)
// update the final outcome according to voting results
// distributed the staked coins (in challenge and vote phase) according to votign results.
// distribute coins according to result
// condition to end phase: when everything in the phase is executed
console.log("distribute done");
} else {
console.log("wrong phase. distribute call can only be done in distribute phase.");
}
}
}
/**
* send token (balances) from a to b
* @param {*} state
* @param {*} tx
* @param {*} chainInfo
*/
function txSendHandler(state, tx, chainInfo) {
if (tx.type === "send") {
let cloned = Object.assign({}, tx);
console.log("send tx: ", JSON.stringify(cloned));
let from = cloned.from;
let amount = new BigNumber(from.amount);
let verifyResult = verifySig(state, cloned);
if (!verifyResult.verified) {
console.log('invalid signature!');
throw Error('invalid signature!');
}
console.log("signature verified!");
let fromAddr = verifyResult.address;
let toAddr = cloned.to.address;
send(state, fromAddr, toAddr, amount);
}
}
/**
*
* @param {*} from
* @param {*} to
* @param {BigNumber} amount
*/
function send(state, from, to, amount) {
console.log("send " + amount + " from " + from + " to " + to);
let fromBalance = new BigNumber(state.balances[from]);
let newFromBalance = fromBalance.minus(amount);
if (newFromBalance < 0) {
let msg = "not enough balance to send " + amount + " from " + from;
console.log(msg);
throw Error(msg);
}
state.balances[from] = newFromBalance.toNumber();
let toBalance = new BigNumber(state.balances[to]);
state.balances[to] = toBalance.plus(amount).toNumber();
}
/**
* verify that seq in state matches seq in tx.
* @param {*} state
* @param {string} user
*/
function verifySeq(state, user, sequence) {
let seq = stateUtil.getSeqForUser(state, user);
return (typeof sequence !== "undefined")
&& ((typeof seq === "undefined" && sequence === STARTING_SEQ) || (sequence === seq));
}
/**
* verify is signature is correct.
*
* @param {*} tx
* @return {verified: [boolean], address: [string]}
* verified is true if signature is verified. and if user is provided, addr is same as user.
* address is the address derived form the public key provided in tx.from.pubkey.
*/
function verifySig(state, tx) {
let verified = false;
let addrSame = true;
let seqVerified = false;
let cloned = Object.assign({}, tx);
console.log("verifySig tx: ", JSON.stringify(cloned));
let from = cloned.from;
let pubkey = from.pubkey;
let signature = from.signature;
let fromSequence = from.sequence;
let sigHash = getSigHash(tx);
let fromAddr = addressHash(pubkey);
console.log("pubkey: ", pubkey);
console.log("signature: ", signature);
console.log("sigHash: ", sigHash);
console.log("from addr: ", fromAddr);
console.log("from sequence: ", fromSequence);
let user = cloned.user;
if (typeof cloned.user !== "undefined") {
addrSame = (fromAddr == user);
}
// verify signature
if (!secp.verify(sigHash, signature, pubkey)) {
console.log('invalid signature! *** ');
} else {
console.log("signature verified! ***");
verified = true;
seqVerified = verifySeq(state, fromAddr, fromSequence);
if (seqVerified) {
let seq = stateUtil.getSeqForUser(state, fromAddr);
// if sequence is not yet recorded, set sequence to the new value
// else, increment seq so other people cannot replay the tx.
if (typeof seq === "undefined") {
stateUtil.setSeqForUser(state, fromAddr, STARTING_SEQ + 1);
} else {
stateUtil.setSeqForUser(state, fromAddr, seq + 1); // seq = seq + 1; won't work
//state.seq[fromAddr] = seq + 1; // seq = seq + 1; won't work
}
}
}
return {"verified": (verified && addrSame && seqVerified), "address": fromAddr};
}
/**
* handle a generic key-value storage space in the state.
* the storage space is local to the marketId.
* every market has its own storage space.
*/
class LocalContractStorage {
constructor(state, marketId) {
this.state = state;
this.marketId = marketId;
}
setValue(key, value) {
// sets to the state.
marketSelector(this.state, this.marketId).storage[key] = value;
}
getValue(key) {
return marketSelector(this.state, this.marketId).storage[key];
}
}
class Event {
// it is just log
static Trigger(keyword, string) {
console.log("[" + keyword + "] " + string);
}
}
class Blockchain {
constructor(state) {
this.state = state;
}
/**
* transfer [amount] of tokens to [user]
*
* @param {*} user
* @param {*} amount
* @return false if not successful. return a transfer info object if successful.
*/
static transfer(user, amount, state) {
let bnAmount = new BigNumber(amount);
state.balances[user] = new BigNumber(state.balances[user]).plus(bnAmount).toNumber();
return {"success": true};
}
}
/**
* both vote and bet payout distribution logic are mostly the same. we generalized them together.
* @param {string} voteOrBet
*/
function doPayout(voteOrBet, finalOutcome, bets, state, marketId) {
let localContractStorage = new LocalContractStorage(state, marketId);
let keywords = {}; // keywords to multiplex between vote and bet
if (voteOrBet === "vote") {
keywords = {
//betsInfo: "votes",
distributionInfo: "voteDistribution",
payoutsInfo: "votePayouts",
eventKey: "votePayout",
}
} else {
keywords = {
//betsInfo: "bets",
distributionInfo: "distribution",
payoutsInfo: "payouts",
eventKey: "betPayout",
}
}
//let bets = localContractStorage.get(keywords.betsInfo);
//let finalOutcome = localContractStorage.get("finalOutcome");
let bet;
let user;
let amount; // BigNumber
let userOutcome; // not BigNumber
let payoutAmount; // BigNumber
let betsIdx = 0;
let betPoolTotal = new BigNumber(0);
let winnerPoolTotal = new BigNumber(0);
let payouts = [];
let distribution = {
betPoolTotal: 0,
winnerPoolTotal: 0,
}
if (bets) {
Event.Trigger(keywords.eventKey, "bets: " + JSON.stringify(bets));
// calculate bet pool total and winner pool total amount.
// so we can calculate the payout based on ratio
// of user bet in the winning pool
for (betsIdx = 0; betsIdx < bets.length; betsIdx++) {
bet = bets[betsIdx];
Event.Trigger(keywords.eventKey, "bets[" + betsIdx + "]: " + JSON.stringify(bet));
user = bet.user;
amount = new BigNumber(bet.amount);
userOutcome = bet.outcome;
betPoolTotal = betPoolTotal.plus(amount);
if (finalOutcome == userOutcome) {
// winner pool
winnerPoolTotal = winnerPoolTotal.plus(amount);
}
}
Event.Trigger(keywords.eventKey, "betPoolTotal: " + betPoolTotal + ", winnerPoolTotal: " + winnerPoolTotal);
console.log("betPoolTotal: " + betPoolTotal + ", winnerPoolTotal: " + winnerPoolTotal);
distribution.betPoolTotal = betPoolTotal;
distribution.winnerPoolTotal = winnerPoolTotal;
localContractStorage.setValue(keywords.distributionInfo, distribution);
for (betsIdx = 0; betsIdx < bets.length; betsIdx++) {
bet = bets[betsIdx];
Event.Trigger(keywords.eventKey, "bets[" + betsIdx + "]: " + JSON.stringify(bet));
user = bet.user;
amount = new BigNumber(bet.amount);
userOutcome = bet.outcome;
console.log("bet: ", bet);
// payout is user's bet in proportion to the entire pool.
// payout = ( userAmount / winnerPoolTotal ) * betPoolTotal
// todo: we might want to floor it to prevent multiple rounding to exceed the total payout
payoutAmount = amount.times(betPoolTotal).dividedBy(winnerPoolTotal);
Event.Trigger(keywords.eventKey, "payoutAmount: " + payoutAmount);
console.log("payoutAmount: ", payoutAmount.toNumber());
if (finalOutcome == userOutcome) {
Event.Trigger(keywords.eventKey, "transfer " + payoutAmount + " to " + user);
console.log("transfer " + payoutAmount + " to " + user);
var payoutElem = {
user: user,
betAmount: amount,
payoutAmount: payoutAmount,
}
payouts.push(payoutElem);
var result = Blockchain.transfer(user, payoutAmount, state);
if (!result) {
Event.Trigger(keywords.eventKey, "transfer failed: " + payoutAmount + " to " + user);
console.log("transfer failed");
throw Error("transfer failed.");
} else {
Event.Trigger(keywords.eventKey, "transfer result: " + JSON.stringify(result));
}
}
}
localContractStorage.setValue(keywords.payoutsInfo, payouts);
Event.Trigger(keywords.eventKey, "payouts: " + JSON.stringify(payouts));
}
}
/**
* remove from and to from the tx.
* from contains public key and signature, which are buffer.
* saving buffer into state may cause issue. the state only seems to handle primitive type.
* so we have to clean it up.
* @param {*} tx
*/
function cleanSignature(tx) {
delete tx.from;
delete tx.to;
}
/**
* return the market part of the state referred by the marketId
* @param {Object} state
* @param {string} marketId
*/
function marketSelector(state, marketId) {
return state.market[marketId];
}
/**
* return the start and end time for all phase.
*/
function calcPhaseTime(startingBlockHeight) {
// measured in number of blocks
const MARKET_DURATION = 3600;
const ORACLE_DURATION = 3600;
const CHALLENGE_DURATION = 3600;
const VOTE_DURATION = 3600;
const DISTRIBUTE_DURATION = 3600;
console.log("typeof ", typeof(startingBlockHeight))
let time = {};
time.marketStart = startingBlockHeight,
time.marketEnd = time.marketStart + MARKET_DURATION;
time.oracleStart = time.marketEnd + 1;
time.oracleEnd = time.oracleStart + ORACLE_DURATION;
time.challengeStart = time.oracleEnd + 1;
time.challengeEnd = time.challengeStart + CHALLENGE_DURATION;
time.voteStart = time.challengeEnd + 1;
time.voteEnd = time.voteStart + VOTE_DURATION;
time.distributeStart = time.voteEnd + 1;
time.distributeEnd = time.distributeStart + DISTRIBUTE_DURATION;
return time;
}
/**
* check if this blockheight is within a particular phase.
* @return [boolean]
*/
function isInPhase(blockHeight, phase, state) {
// ***for demo and testing, don't check for timeframe of the phase ***
return true;
//
let result = false;
switch (phase) {
case "market":
result = blockHeight >= marketSelector(state, marketId).phaseTime.marketStart && blockHeight <= marketSelector(state, marketId).phaseTime.marketEnd;
break;
case "oracle":
result = blockHeight >= marketSelector(state, marketId).phaseTime.oracleStart && blockHeight <= marketSelector(state, marketId).phaseTime.oracleEnd;
break;
case "challenge":
result = blockHeight >= marketSelector(state, marketId).phaseTime.challengeStart && blockHeight <= marketSelector(state, marketId).phaseTime.challengeEnd;
break;
case "vote":
result = blockHeight >= marketSelector(state, marketId).phaseTime.voteStart && blockHeight <= marketSelector(state, marketId).phaseTime.voteEnd;
break;
case "distribute":
result = blockHeight >= marketSelector(state, marketId).phaseTime.distributeStart && blockHeight <= marketSelector(state, marketId).phaseTime.distributeEnd;
break;
default:
result = false;
break;
}
return result;
}
const MAX_TYPE_LENGTH = 30;
const MAX_USER_LENGTH = 64;
const SIG_SIZE = 0; // todo
const PUBKEY_SIZE = 0; // todo
/**
* some common tx checking
* throw error if not passing
* 1. type string length <= 30
* 2. user string length <= 64
* todo:
* 3. from.signature size
* 4. from.pubkey size
* 5.
*/
function checkTxCommon(tx) {
if (tx.type.length > MAX_TYPE_LENGTH) { throw Error("type too long"); }
if (typeof tx.user !== "undefined" && tx.user.length > MAX_USER_LENGTH) { throw Error("user too long"); }
}
const MAX_START_INFO_LENGTH = 1024;
/**
* total size of startInfo < 1024
* oracle is defined
* oracle is an array
* question is provided
* outcomes is provided
*
* throw error if not passing
*/
function checkStartInfo(startInfo) {
let sStartInfo = JSON.stringify(startInfo);
if (sStartInfo > MAX_START_INFO_LENGTH) { throw Error("startInfo too large"); }
if (typeof startInfo.oracle === "undefined") { throw Error("startInfo.oracle is required"); }
if (!Array.isArray(startInfo.oracle)) { throw Error("startInfo.oracle must be an array"); }
if (!startInfo.question) { throw Error("startInfo.question is required"); }
if (!startInfo.outcomes) { throw Error("startInfo.outcomes is required"); }
}
/*
prediction market
** market phase **
1. open new market
2. allow bet
3. someone bets (record them)
4. market closes (by time)
** oracle phase **
condition to enter phase: time
10. get data from oracle
11. determine the result and record it
condition to end phase: time
** challenge phase **
condition to enter phase: time, someone triggers it
20. someone stake their coin and challenges
condition to end phase: time
** vote phase **
condition to enter phase: challenge phase completed
30. anyone can stake their coin and vote
** distribute phase **
condition to enter phase: time
40. determine the final outcome
if (challenged)
update the final outcome according to voting results
distributed the staked coins (in challenge and vote phase) according to votign results.
distribute coins according to result
condition to end phase: when everything in the phase is executed
** other info **
time is not actual time. it is block height
*/