Skip to content

Commit 176f9da

Browse files
committed
#25 use bignumber for math
1 parent 8e8b046 commit 176f9da

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

app.js

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ let initialState = {
3131
},
3232
}
3333

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+
*/
3539

3640
/*
3741
format of start tx
@@ -117,9 +121,9 @@ function txBetHandler(state, tx, chainInfo) {
117121
let cloned = Object.assign({}, tx);
118122
console.log("bet: ", cloned);
119123
let user = cloned.user;
120-
let amount = cloned.amount;
124+
let amount = new BigNumber(cloned.amount);
121125
// 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();
123127
state.market1.bets.push(cloned);
124128
console.log("bets: ", state.market1.bets);
125129
} else {
@@ -151,9 +155,9 @@ function txChallengeHandler(state, tx, chainInfo) {
151155
let cloned = Object.assign({}, tx);
152156
console.log("challenge tx: ", cloned);
153157
let user = cloned.user;
154-
let amount = cloned.amount;
158+
let amount = new BigNumber(cloned.amount);
155159
// 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();
157161
state.market1.challenge = cloned;
158162
console.log("challenge: ", state.market1.challenge);
159163
} else {
@@ -181,10 +185,10 @@ function txVoteHandler(state, tx, chainInfo) {
181185
let cloned = Object.assign({}, tx);
182186
console.log("vote tx", cloned);
183187
let user = cloned.user;
184-
let amount = cloned.amount;
188+
let amount = new BigNumber(cloned.amount);
185189
let outcome = cloned.outcome;
186190
// 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();
188192
state.market1.voteRecords.push(cloned);
189193
// update votes
190194
//updateVotes(outcome, amount);
@@ -206,7 +210,7 @@ function updateVotes(outcome, amount, state) {
206210
// state.market1.votes[outcomeName] = 0;
207211
// console.log("4");
208212
// }
209-
state.market1.votes[outcomeName] = state.market1.votes[outcomeName] + amount;
213+
state.market1.votes[outcomeName] = new BigNumber(state.market1.votes[outcomeName]).plus(amount).toNumber();
210214
}
211215

212216
function txDistributeHandler(state, tx, chainInfo) {
@@ -228,18 +232,19 @@ function txDistributeHandler(state, tx, chainInfo) {
228232
// give it to the winners proportionally.
229233
let voteRecords = state.market1.voteRecords;
230234
console.log("1");
231-
let votePoolTotal = 0;
235+
let votePoolTotal = new BigNumber(0);
232236
let result = {};
233237

234238
for (let i = 0; i < voteRecords.length; i++) {
235239
let vote = voteRecords[i];
240+
let voteAmount = new BigNumber(vote.amount);
236241
if (typeof result[vote.outcome] === "undefined") {
237-
result[vote.outcome] = vote.amount;
242+
result[vote.outcome] = voteAmount.toNumber();
238243
} else {
239-
result[vote.outcome] += vote.amount;
244+
result[vote.outcome] = new BigNumber(result[vote.outcome]).plus(voteAmount).toNumber();
240245
}
241-
console.log("vote amount", vote.amount);
242-
votePoolTotal += vote.amount;
246+
console.log("vote amount", voteAmount.toNumber());
247+
votePoolTotal = votePoolTotal.plus(voteAmount);
243248
}
244249

245250
console.log("votePoolTotal: ", votePoolTotal);
@@ -249,14 +254,16 @@ function txDistributeHandler(state, tx, chainInfo) {
249254

250255
// determine which outcome win.
251256
// 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
254259
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)) {
256262
highestOutcome = outcome;
257-
highestOutcomeAmount = result[outcome];
263+
highestOutcomeAmount = outcomeAmount;
258264
}
259265
}
266+
console.log("voted outcome: " + highestOutcome + ". tokens staked for voted outcome: " + highestOutcomeAmount.toNumber());
260267

261268
// set the final outcome to the highest voted outcome
262269
finalOutcome = highestOutcome;
@@ -331,7 +338,8 @@ class Blockchain {
331338
* @return false if not successful. return a transfer info object if successful.
332339
*/
333340
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();
335343
return {"success": true};
336344
}
337345
}
@@ -365,9 +373,9 @@ function doPayout(voteOrBet, finalOutcome, bets, state) {
365373
//let finalOutcome = LocalContractStorage.get("finalOutcome");
366374
let bet;
367375
let user;
368-
let amount;
369-
let userOutcome;
370-
let payoutAmount;
376+
let amount; // BigNumber
377+
let userOutcome; // not BigNumber
378+
let payoutAmount; // BigNumber
371379
let betsIdx = 0;
372380
let betPoolTotal = new BigNumber(0);
373381
let winnerPoolTotal = new BigNumber(0);
@@ -387,7 +395,7 @@ function doPayout(voteOrBet, finalOutcome, bets, state) {
387395
bet = bets[betsIdx];
388396
Event.Trigger(keywords.eventKey, "bets[" + betsIdx + "]: " + JSON.stringify(bet));
389397
user = bet.user;
390-
amount = bet.amount;
398+
amount = new BigNumber(bet.amount);
391399
userOutcome = bet.outcome;
392400
betPoolTotal = betPoolTotal.plus(amount);
393401
if (finalOutcome == userOutcome) {

0 commit comments

Comments
 (0)