import clsx from 'clsx' import { useState, useEffect } from 'react' import { Bet } from '../../common/bet' import { getOutcomeProbabilityAfterBet, calculateShares, calculatePayoutAfterCorrectBet, } from '../../common/calculate' import { NumericContract } from '../../common/contract' import { formatPercent, formatWithCommas, 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' export function NumericBetPanel(props: { contract: NumericContract className?: string }) { const { contract, className } = props const user = useUser() return (
Place your bet
{user === null && ( )} ) } function NumericBuyPanel(props: { contract: NumericContract user: User | null | undefined onBuySuccess?: () => void }) { const { contract, user, onBuySuccess } = props const { bucketCount, min, max } = contract const [bucketChoice, setBucketChoice] = useState( undefined ) const [betAmount, setBetAmount] = useState(undefined) const [error, setError] = useState() const [isSubmitting, setIsSubmitting] = useState(false) const [wasSubmitted, setWasSubmitted] = useState(false) const [inputRef, focusAmountInput] = useFocus() useEffect(() => { focusAmountInput() }, [focusAmountInput]) function onBetChange(newAmount: number | undefined) { setWasSubmitted(false) setBetAmount(newAmount) } async function submitBet() { if (!user || !betAmount) return setError(undefined) setIsSubmitting(true) const result = await placeBet({ amount: betAmount, outcome: bucketChoice, contractId: contract.id, }).then((r) => r.data as any) console.log('placed bet. Result:', result) if (result?.status === 'success') { setIsSubmitting(false) setWasSubmitted(true) setBetAmount(undefined) if (onBuySuccess) onBuySuccess() } else { setError(result?.message || 'Error placing bet') setIsSubmitting(false) } } const betDisabled = isSubmitting || !betAmount || 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) : 0 const currentReturn = betAmount ? (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
setBucketChoice(bucket ? `${bucket}` : undefined)} error={error} setError={setError} disabled={isSubmitting} inputRef={inputRef} />
Amount
Probability
{formatPercent(initialProb)}
{formatPercent(resultProb)}
{contract.mechanism === 'dpm-2' ? ( <> Estimated
payout if correct ) : ( <>Payout if correct )}
{dpmTooltip && }
{formatMoney(currentPayout)} (+{currentReturnPercent})
{user && ( )} {wasSubmitted &&
Bet submitted!
} ) }