check if NaN when updating user balances

This commit is contained in:
mantikoros 2022-03-15 22:05:08 -05:00
parent a150be88e2
commit 3f0002f9e8
4 changed files with 17 additions and 0 deletions

View File

@ -122,6 +122,11 @@ export const createAnswer = functions.runWith({ minInstances: 1 }).https.onCall(
totalBets: newTotalBets, totalBets: newTotalBets,
answers: [...(contract.answers ?? []), answer], answers: [...(contract.answers ?? []), answer],
}) })
if (!isFinite(newBalance)) {
throw new Error('Invalid user balance for ' + user.username)
}
transaction.update(userDoc, { balance: newBalance }) transaction.update(userDoc, { balance: newBalance })
return { status: 'success', answerId, betId: newBetDoc.id, answer } return { status: 'success', answerId, betId: newBetDoc.id, answer }

View File

@ -132,6 +132,10 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall(
}) })
) )
if (!isFinite(newBalance)) {
throw new Error('Invalid user balance for ' + user.username)
}
transaction.update(userDoc, { balance: newBalance }) transaction.update(userDoc, { balance: newBalance })
return { status: 'success', betId: newBetDoc.id } return { status: 'success', betId: newBetDoc.id }

View File

@ -78,6 +78,11 @@ export const redeemShares = async (userId: string, contractId: string) => {
const user = userSnap.data() as User const user = userSnap.data() as User
const newBalance = user.balance + netAmount const newBalance = user.balance + netAmount
if (!isFinite(newBalance)) {
throw new Error('Invalid user balance for ' + user.username)
}
transaction.update(userDoc, { balance: newBalance }) transaction.update(userDoc, { balance: newBalance })
transaction.create(yesDoc, yesBet) transaction.create(yesDoc, yesBet)

View File

@ -67,6 +67,9 @@ export const sellBet = functions.runWith({ minInstances: 1 }).https.onCall(
newBetDoc.id newBetDoc.id
) as any) ) as any)
if (!isFinite(newBalance)) {
throw new Error('Invalid user balance for ' + user.username)
}
transaction.update(userDoc, { balance: newBalance }) transaction.update(userDoc, { balance: newBalance })
transaction.update(betDoc, { isSold: true }) transaction.update(betDoc, { isSold: true })