@@ -31,7 +31,11 @@ let initialState = {
31
31
} ,
32
32
}
33
33
34
- //
34
+ /*
35
+ calculation:
36
+ in general, we try to use bignumber to do calculation.
37
+ however, we will still store the final result in state object as number. because bignumber object does not store in state well.
38
+ */
35
39
36
40
/*
37
41
format of start tx
@@ -117,9 +121,9 @@ function txBetHandler(state, tx, chainInfo) {
117
121
let cloned = Object . assign ( { } , tx ) ;
118
122
console . log ( "bet: " , cloned ) ;
119
123
let user = cloned . user ;
120
- let amount = cloned . amount ;
124
+ let amount = new BigNumber ( cloned . amount ) ;
121
125
// lock up their staking tokens
122
- state . balances [ user ] = state . balances [ user ] - amount ;
126
+ state . balances [ user ] = new BigNumber ( state . balances [ user ] ) . minus ( amount ) . toNumber ( ) ;
123
127
state . market1 . bets . push ( cloned ) ;
124
128
console . log ( "bets: " , state . market1 . bets ) ;
125
129
} else {
@@ -151,9 +155,9 @@ function txChallengeHandler(state, tx, chainInfo) {
151
155
let cloned = Object . assign ( { } , tx ) ;
152
156
console . log ( "challenge tx: " , cloned ) ;
153
157
let user = cloned . user ;
154
- let amount = cloned . amount ;
158
+ let amount = new BigNumber ( cloned . amount ) ;
155
159
// lock up their staking tokens
156
- state . balances [ user ] = state . balances [ user ] - amount ;
160
+ state . balances [ user ] = new BigNumber ( state . balances [ user ] ) . minus ( amount ) . toNumber ( ) ;
157
161
state . market1 . challenge = cloned ;
158
162
console . log ( "challenge: " , state . market1 . challenge ) ;
159
163
} else {
@@ -181,10 +185,10 @@ function txVoteHandler(state, tx, chainInfo) {
181
185
let cloned = Object . assign ( { } , tx ) ;
182
186
console . log ( "vote tx" , cloned ) ;
183
187
let user = cloned . user ;
184
- let amount = cloned . amount ;
188
+ let amount = new BigNumber ( cloned . amount ) ;
185
189
let outcome = cloned . outcome ;
186
190
// lock up their staking tokens
187
- state . balances [ user ] = state . balances [ user ] - amount ;
191
+ state . balances [ user ] = new BigNumber ( state . balances [ user ] ) . minus ( amount ) . toNumber ( ) ;
188
192
state . market1 . voteRecords . push ( cloned ) ;
189
193
// update votes
190
194
//updateVotes(outcome, amount);
@@ -206,7 +210,7 @@ function updateVotes(outcome, amount, state) {
206
210
// state.market1.votes[outcomeName] = 0;
207
211
// console.log("4");
208
212
// }
209
- state . market1 . votes [ outcomeName ] = state . market1 . votes [ outcomeName ] + amount ;
213
+ state . market1 . votes [ outcomeName ] = new BigNumber ( state . market1 . votes [ outcomeName ] ) . plus ( amount ) . toNumber ( ) ;
210
214
}
211
215
212
216
function txDistributeHandler ( state , tx , chainInfo ) {
@@ -228,18 +232,19 @@ function txDistributeHandler(state, tx, chainInfo) {
228
232
// give it to the winners proportionally.
229
233
let voteRecords = state . market1 . voteRecords ;
230
234
console . log ( "1" ) ;
231
- let votePoolTotal = 0 ;
235
+ let votePoolTotal = new BigNumber ( 0 ) ;
232
236
let result = { } ;
233
237
234
238
for ( let i = 0 ; i < voteRecords . length ; i ++ ) {
235
239
let vote = voteRecords [ i ] ;
240
+ let voteAmount = new BigNumber ( vote . amount ) ;
236
241
if ( typeof result [ vote . outcome ] === "undefined" ) {
237
- result [ vote . outcome ] = vote . amount ;
242
+ result [ vote . outcome ] = voteAmount . toNumber ( ) ;
238
243
} else {
239
- result [ vote . outcome ] += vote . amount ;
244
+ result [ vote . outcome ] = new BigNumber ( result [ vote . outcome ] ) . plus ( voteAmount ) . toNumber ( ) ;
240
245
}
241
- console . log ( "vote amount" , vote . amount ) ;
242
- votePoolTotal += vote . amount ;
246
+ console . log ( "vote amount" , voteAmount . toNumber ( ) ) ;
247
+ votePoolTotal = votePoolTotal . plus ( voteAmount ) ;
243
248
}
244
249
245
250
console . log ( "votePoolTotal: " , votePoolTotal ) ;
@@ -249,14 +254,16 @@ function txDistributeHandler(state, tx, chainInfo) {
249
254
250
255
// determine which outcome win.
251
256
// loop through to find the highest voted amount
252
- let highestOutcome ;
253
- let highestOutcomeAmount = - 1 ;
257
+ let highestOutcome ; // the outcome with highest votes.
258
+ let highestOutcomeAmount = new BigNumber ( - 1 ) ; // the staked token amount of highest-voted outcome
254
259
for ( let outcome in result ) {
255
- if ( result [ outcome ] > highestOutcomeAmount ) {
260
+ let outcomeAmount = new BigNumber ( result [ outcome ] ) ; // the amount of votes (amount of tokens) for the this outcome
261
+ if ( outcomeAmount . gt ( highestOutcomeAmount ) ) {
256
262
highestOutcome = outcome ;
257
- highestOutcomeAmount = result [ outcome ] ;
263
+ highestOutcomeAmount = outcomeAmount ;
258
264
}
259
265
}
266
+ console . log ( "voted outcome: " + highestOutcome + ". tokens staked for voted outcome: " + highestOutcomeAmount . toNumber ( ) ) ;
260
267
261
268
// set the final outcome to the highest voted outcome
262
269
finalOutcome = highestOutcome ;
@@ -331,7 +338,8 @@ class Blockchain {
331
338
* @return false if not successful. return a transfer info object if successful.
332
339
*/
333
340
static transfer ( user , amount , state ) {
334
- state . balances [ user ] += amount . toNumber ( ) ;
341
+ let bnAmount = new BigNumber ( amount ) ;
342
+ state . balances [ user ] = new BigNumber ( state . balances [ user ] ) . plus ( bnAmount ) . toNumber ( ) ;
335
343
return { "success" : true } ;
336
344
}
337
345
}
@@ -365,9 +373,9 @@ function doPayout(voteOrBet, finalOutcome, bets, state) {
365
373
//let finalOutcome = LocalContractStorage.get("finalOutcome");
366
374
let bet ;
367
375
let user ;
368
- let amount ;
369
- let userOutcome ;
370
- let payoutAmount ;
376
+ let amount ; // BigNumber
377
+ let userOutcome ; // not BigNumber
378
+ let payoutAmount ; // BigNumber
371
379
let betsIdx = 0 ;
372
380
let betPoolTotal = new BigNumber ( 0 ) ;
373
381
let winnerPoolTotal = new BigNumber ( 0 ) ;
@@ -387,7 +395,7 @@ function doPayout(voteOrBet, finalOutcome, bets, state) {
387
395
bet = bets [ betsIdx ] ;
388
396
Event . Trigger ( keywords . eventKey , "bets[" + betsIdx + "]: " + JSON . stringify ( bet ) ) ;
389
397
user = bet . user ;
390
- amount = bet . amount ;
398
+ amount = new BigNumber ( bet . amount ) ;
391
399
userOutcome = bet . outcome ;
392
400
betPoolTotal = betPoolTotal . plus ( amount ) ;
393
401
if ( finalOutcome == userOutcome ) {
0 commit comments