From 42533c296a93c43039785ca81c27df951cf62c0f Mon Sep 17 00:00:00 2001 From: James Grugett Date: Tue, 1 Mar 2022 16:26:37 -0800 Subject: [PATCH] Loan frontend: show your loan amount in bet panel, answer bet panel --- web/components/amount-input.tsx | 55 ++++++++++++++---- web/components/answers/answer-bet-panel.tsx | 58 ++++++++++--------- .../answers/create-answer-panel.tsx | 42 ++++++++------ web/components/bet-panel.tsx | 53 +++++++++-------- web/hooks/use-user-bets.ts | 20 ++++++- web/lib/firebase/bets.ts | 15 +++++ web/pages/[username]/[contractSlug].tsx | 2 +- web/pages/create.tsx | 1 + web/pages/make-predictions.tsx | 1 + 9 files changed, 166 insertions(+), 81 deletions(-) diff --git a/web/components/amount-input.tsx b/web/components/amount-input.tsx index dcc29465..1187953d 100644 --- a/web/components/amount-input.tsx +++ b/web/components/amount-input.tsx @@ -1,15 +1,20 @@ import clsx from 'clsx' +import _ from 'lodash' import { useUser } from '../hooks/use-user' import { formatMoney } from '../../common/util/format' -import { AddFundsButton } from './add-funds-button' import { Col } from './layout/col' import { Row } from './layout/row' +import { useUserContractBets } from '../hooks/use-user-bets' +import { MAX_LOAN_PER_CONTRACT } from '../../common/bet' +import { InfoTooltip } from './info-tooltip' +import { Spacer } from './layout/spacer' export function AmountInput(props: { amount: number | undefined onChange: (newAmount: number | undefined) => void error: string | undefined setError: (error: string | undefined) => void + contractId: string | undefined minimumAmount?: number disabled?: boolean className?: string @@ -22,6 +27,7 @@ export function AmountInput(props: { onChange, error, setError, + contractId, disabled, className, inputClassName, @@ -31,10 +37,23 @@ export function AmountInput(props: { const user = useUser() + const userBets = useUserContractBets(user?.id, contractId) ?? [] + const prevLoanAmount = _.sumBy(userBets, (bet) => bet.loanAmount ?? 0) + + const loanAmount = Math.min( + amount ?? 0, + MAX_LOAN_PER_CONTRACT - prevLoanAmount + ) + const onAmountChange = (str: string) => { + if (str.includes('-')) { + onChange(undefined) + return + } const amount = parseInt(str.replace(/[^\d]/, '')) if (str && isNaN(amount)) return + if (amount >= 10 ** 9) return onChange(str ? amount : undefined) @@ -47,7 +66,8 @@ export function AmountInput(props: { } } - const remainingBalance = Math.max(0, (user?.balance ?? 0) - (amount ?? 0)) + const amountNetLoan = (amount ?? 0) - loanAmount + const remainingBalance = Math.max(0, (user?.balance ?? 0) - amountNetLoan) return ( @@ -68,19 +88,34 @@ export function AmountInput(props: { onChange={(e) => onAmountChange(e.target.value)} /> + + + {error && ( -
+
{error}
)} {user && ( - -
- Remaining balance -
- -
{formatMoney(Math.floor(remainingBalance))}
- {user.balance !== 1000 && } + + {contractId && ( + + + Loan amount{' '} + + + {formatMoney(loanAmount)}{' '} + + )} + + Remaining balance{' '} + + {formatMoney(remainingBalance)} + )} diff --git a/web/components/answers/answer-bet-panel.tsx b/web/components/answers/answer-bet-panel.tsx index 33a0593f..4cd67369 100644 --- a/web/components/answers/answer-bet-panel.tsx +++ b/web/components/answers/answer-bet-panel.tsx @@ -114,40 +114,44 @@ export function AnswerBetPanel(props: { setError={setError} disabled={isSubmitting} inputRef={inputRef} + contractId={contract.id} /> + + +
Probability
+ +
{formatPercent(initialProb)}
+
+
{formatPercent(resultProb)}
+
+
- - -
Implied probability
- -
{formatPercent(initialProb)}
-
-
{formatPercent(resultProb)}
-
- - - - - Payout if chosen - - -
- {formatMoney(currentPayout)} -   (+{currentReturnPercent}) -
+ + +
Payout if chosen
+ +
+ + + {formatMoney(currentPayout)} + + (+{currentReturnPercent}) + +
+ {user ? (
diff --git a/web/pages/make-predictions.tsx b/web/pages/make-predictions.tsx index 3f37f535..37872c39 100644 --- a/web/pages/make-predictions.tsx +++ b/web/pages/make-predictions.tsx @@ -245,6 +245,7 @@ ${TEST_VALUE} error={anteError} setError={setAnteError} disabled={isSubmitting} + contractId={undefined} />