From fa817c34a9e24c458504d698bf60ccdcf07001ac Mon Sep 17 00:00:00 2001 From: James Grugett Date: Wed, 2 Mar 2022 14:28:23 -0800 Subject: [PATCH] Fix more places where insufficient balance error was not accounting for loans --- functions/src/place-bet.ts | 10 ++++------ web/components/answers/answer-bet-panel.tsx | 5 ----- web/components/answers/create-answer-panel.tsx | 3 ++- web/components/bet-panel.tsx | 5 ----- 4 files changed, 6 insertions(+), 17 deletions(-) diff --git a/functions/src/place-bet.ts b/functions/src/place-bet.ts index 4adb1779..f473a2e2 100644 --- a/functions/src/place-bet.ts +++ b/functions/src/place-bet.ts @@ -9,7 +9,6 @@ import { getNewMultiBetInfo, } from '../../common/new-bet' import { Bet } from '../../common/bet' -import { getValues } from './utils' export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall( async ( @@ -39,9 +38,6 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall( return { status: 'error', message: 'User not found' } const user = userSnap.data() as User - if (user.balance < amount) - return { status: 'error', message: 'Insufficient balance' } - const contractDoc = firestore.doc(`contracts/${contractId}`) const contractSnap = await transaction.get(contractDoc) if (!contractSnap.exists) @@ -57,6 +53,10 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall( ) const yourBets = yourBetsSnap.docs.map((doc) => doc.data() as Bet) + const loanAmount = getLoanAmount(yourBets, amount) + if (user.balance < amount - loanAmount) + return { status: 'error', message: 'Insufficient balance' } + if (outcomeType === 'FREE_RESPONSE') { const answerSnap = await transaction.get( contractDoc.collection('answers').doc(outcome) @@ -69,8 +69,6 @@ export const placeBet = functions.runWith({ minInstances: 1 }).https.onCall( .collection(`contracts/${contractId}/bets`) .doc() - const loanAmount = getLoanAmount(yourBets, amount) - const { newBet, newPool, newTotalShares, newTotalBets, newBalance } = outcomeType === 'BINARY' ? getNewBinaryBetInfo( diff --git a/web/components/answers/answer-bet-panel.tsx b/web/components/answers/answer-bet-panel.tsx index d45bdc35..bad83813 100644 --- a/web/components/answers/answer-bet-panel.tsx +++ b/web/components/answers/answer-bet-panel.tsx @@ -49,11 +49,6 @@ export function AnswerBetPanel(props: { async function submitBet() { if (!user || !betAmount) return - if (user.balance < betAmount) { - setError('Insufficient balance') - return - } - setError(undefined) setIsSubmitting(true) diff --git a/web/components/answers/create-answer-panel.tsx b/web/components/answers/create-answer-panel.tsx index 3f0f5dee..28596521 100644 --- a/web/components/answers/create-answer-panel.tsx +++ b/web/components/answers/create-answer-panel.tsx @@ -36,6 +36,7 @@ export function CreateAnswerPanel(props: { contract: Contract }) { const submitAnswer = async () => { if (canSubmit) { setIsSubmitting(true) + const result = await createAnswer({ contractId: contract.id, text, @@ -48,7 +49,7 @@ export function CreateAnswerPanel(props: { contract: Contract }) { setText('') setBetAmount(10) setAmountError(undefined) - } + } else setAmountError(result.message) } } diff --git a/web/components/bet-panel.tsx b/web/components/bet-panel.tsx index 89594ae3..ec3e7cfc 100644 --- a/web/components/bet-panel.tsx +++ b/web/components/bet-panel.tsx @@ -78,11 +78,6 @@ export function BetPanel(props: { async function submitBet() { if (!user || !betAmount) return - if (user.balance < betAmount) { - setError('Insufficient balance') - return - } - setError(undefined) setIsSubmitting(true)