From 1e3a7747ee8fc4374dee3be6e3e9afaacf80f61b Mon Sep 17 00:00:00 2001 From: James Grugett Date: Fri, 6 May 2022 14:41:29 -0400 Subject: [PATCH] Update bet panel logic --- web/components/numeric-bet-panel.tsx | 90 +++++++++++----------------- 1 file changed, 34 insertions(+), 56 deletions(-) diff --git a/web/components/numeric-bet-panel.tsx b/web/components/numeric-bet-panel.tsx index 74d11148..0911b6c8 100644 --- a/web/components/numeric-bet-panel.tsx +++ b/web/components/numeric-bet-panel.tsx @@ -5,19 +5,15 @@ import { getOutcomeProbabilityAfterBet, calculateShares, calculatePayoutAfterCorrectBet, + getOutcomeProbability, } from '../../common/calculate' import { NumericContract } from '../../common/contract' -import { - formatPercent, - formatWithCommas, - formatMoney, -} from '../../common/util/format' +import { formatPercent, formatMoney } from '../../common/util/format' import { useFocus } from '../hooks/use-focus' import { useUser } from '../hooks/use-user' import { placeBet } from '../lib/firebase/api-call' import { firebaseLogin, User } from '../lib/firebase/users' import { BucketAmountInput, BuyAmountInput } from './amount-input' -import { InfoTooltip } from './info-tooltip' import { Col } from './layout/col' import { Row } from './layout/row' import { Spacer } from './layout/spacer' @@ -69,13 +65,18 @@ function NumericBuyPanel(props: { focusAmountInput() }, [focusAmountInput]) + function onBucketChange(newBucket: string | undefined) { + setWasSubmitted(false) + setBucketChoice(newBucket) + } + function onBetChange(newAmount: number | undefined) { setWasSubmitted(false) setBetAmount(newAmount) } async function submitBet() { - if (!user || !betAmount) return + if (!user || !betAmount || !bucketChoice) return setError(undefined) setIsSubmitting(true) @@ -99,43 +100,32 @@ function NumericBuyPanel(props: { } } - const betDisabled = isSubmitting || !betAmount || error + const betDisabled = isSubmitting || !betAmount || !bucketChoice || error - const initialProb = 0 - const outcomeProb = getOutcomeProbabilityAfterBet( - contract, - bucketChoice || 'YES', - betAmount ?? 0 - ) - const resultProb = bucketChoice === 'NO' ? 1 - outcomeProb : outcomeProb - - const shares = calculateShares( - contract, - betAmount ?? 0, - bucketChoice || 'YES' - ) - - const currentPayout = betAmount - ? calculatePayoutAfterCorrectBet(contract, { - outcome: bucketChoice, - amount: betAmount, - shares, - } as Bet) + const initialProb = bucketChoice + ? getOutcomeProbability(contract, bucketChoice) + : 0 + const outcomeProb = bucketChoice + ? getOutcomeProbabilityAfterBet(contract, bucketChoice, betAmount ?? 0) : 0 - const currentReturn = betAmount ? (currentPayout - betAmount) / betAmount : 0 + const shares = bucketChoice + ? calculateShares(contract, betAmount ?? 0, bucketChoice) + : 0 + + const currentPayout = + betAmount && bucketChoice + ? calculatePayoutAfterCorrectBet(contract, { + outcome: bucketChoice, + amount: betAmount, + shares, + } as Bet) + : 0 + + const currentReturn = + betAmount && bucketChoice ? (currentPayout - betAmount) / betAmount : 0 const currentReturnPercent = formatPercent(currentReturn) - const dpmTooltip = - contract.mechanism === 'dpm-2' - ? `Current payout for ${formatWithCommas(shares)} / ${formatWithCommas( - shares + - contract.totalShares[bucketChoice ?? 'YES'] - - (contract.phantomShares - ? contract.phantomShares[bucketChoice ?? 'YES'] - : 0) - )} ${bucketChoice ?? 'YES'} shares` - : undefined return ( <>
Numeric value
@@ -145,7 +135,7 @@ function NumericBuyPanel(props: { min={min} max={max} inputClassName="w-full max-w-none" - onChange={(bucket) => setBucketChoice(bucket ? `${bucket}` : undefined)} + onChange={(bucket) => onBucketChange(bucket ? `${bucket}` : undefined)} error={error} setError={setError} disabled={isSubmitting} @@ -169,24 +159,16 @@ function NumericBuyPanel(props: {
{formatPercent(initialProb)}
-
{formatPercent(resultProb)}
+
{formatPercent(outcomeProb)}
- {contract.mechanism === 'dpm-2' ? ( - <> - Estimated -
payout if correct - - ) : ( - <>Payout if correct - )} + Estimated +
payout if correct
- - {dpmTooltip && }
@@ -203,11 +185,7 @@ function NumericBuyPanel(props: {