diff --git a/functions/src/create-answer.ts b/functions/src/create-answer.ts index 0df8c0df..17d085f5 100644 --- a/functions/src/create-answer.ts +++ b/functions/src/create-answer.ts @@ -122,6 +122,11 @@ export const createAnswer = functions.runWith({ minInstances: 1 }).https.onCall( totalBets: newTotalBets, answers: [...(contract.answers ?? []), answer], }) + + if (!isFinite(newBalance)) { + throw new Error('Invalid user balance for ' + user.username) + } + transaction.update(userDoc, { balance: newBalance }) return { status: 'success', answerId, betId: newBetDoc.id, answer } diff --git a/functions/src/place-bet.ts b/functions/src/place-bet.ts index 6971136b..1a79287d 100644 --- a/functions/src/place-bet.ts +++ b/functions/src/place-bet.ts @@ -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 }) return { status: 'success', betId: newBetDoc.id } diff --git a/functions/src/redeem-shares.ts b/functions/src/redeem-shares.ts index 55fb2d14..08d87a8b 100644 --- a/functions/src/redeem-shares.ts +++ b/functions/src/redeem-shares.ts @@ -78,6 +78,11 @@ export const redeemShares = async (userId: string, contractId: string) => { const user = userSnap.data() as User const newBalance = user.balance + netAmount + + if (!isFinite(newBalance)) { + throw new Error('Invalid user balance for ' + user.username) + } + transaction.update(userDoc, { balance: newBalance }) transaction.create(yesDoc, yesBet) diff --git a/functions/src/sell-bet.ts b/functions/src/sell-bet.ts index 14c15dd7..a5bb2af9 100644 --- a/functions/src/sell-bet.ts +++ b/functions/src/sell-bet.ts @@ -67,6 +67,9 @@ export const sellBet = functions.runWith({ minInstances: 1 }).https.onCall( newBetDoc.id ) as any) + if (!isFinite(newBalance)) { + throw new Error('Invalid user balance for ' + user.username) + } transaction.update(userDoc, { balance: newBalance }) transaction.update(betDoc, { isSold: true })