getCpmmInvested: fix NaN issue

This commit is contained in:
mantikoros 2022-09-15 18:32:30 -05:00
parent e0634cea6d
commit 5a1cc4c19d

View File

@ -142,17 +142,20 @@ function getCpmmInvested(yourBets: Bet[]) {
const { outcome, shares, amount } = bet const { outcome, shares, amount } = bet
if (floatingEqual(shares, 0)) continue if (floatingEqual(shares, 0)) continue
const spent = totalSpent[outcome] ?? 0
const position = totalShares[outcome] ?? 0
if (amount > 0) { if (amount > 0) {
totalShares[outcome] = (totalShares[outcome] ?? 0) + shares totalShares[outcome] = position + shares
totalSpent[outcome] = (totalSpent[outcome] ?? 0) + amount totalSpent[outcome] = spent + amount
} else if (amount < 0) { } else if (amount < 0) {
const averagePrice = totalSpent[outcome] / totalShares[outcome] const averagePrice = position === 0 ? 0 : spent / position
totalShares[outcome] = totalShares[outcome] + shares totalShares[outcome] = position + shares
totalSpent[outcome] = totalSpent[outcome] + averagePrice * shares totalSpent[outcome] = spent + averagePrice * shares
} }
} }
return sum(Object.values(totalSpent)) return sum([0, ...Object.values(totalSpent)])
} }
function getDpmInvested(yourBets: Bet[]) { function getDpmInvested(yourBets: Bet[]) {