import clsx from 'clsx' import { useEffect, useRef, useState } from 'react' import { XIcon } from '@heroicons/react/solid' import { Answer } from 'common/answer' import { FreeResponseContract, MultipleChoiceContract } from 'common/contract' import { BuyAmountInput } from '../amount-input' import { Col } from '../layout/col' import { APIError, placeBet } from 'web/lib/firebase/api' import { Row } from '../layout/row' import { Spacer } from '../layout/spacer' import { formatMoney, formatPercent, formatWithCommas, } from 'common/util/format' import { InfoTooltip } from '../info-tooltip' import { useUser } from 'web/hooks/use-user' import { getDpmOutcomeProbability, calculateDpmShares, calculateDpmPayoutAfterCorrectBet, getDpmOutcomeProbabilityAfterBet, } from 'common/calculate-dpm' import { Bet } from 'common/bet' import { track } from 'web/lib/service/analytics' import { BetSignUpPrompt } from '../sign-up-prompt' import { isIOS } from 'web/lib/util/device' import { AlertBox } from '../alert-box' export function AnswerBetPanel(props: { answer: Answer contract: FreeResponseContract | MultipleChoiceContract closePanel: () => void className?: string isModal?: boolean }) { const { answer, contract, closePanel, className, isModal } = props const { id: answerId } = answer const user = useUser() const [betAmount, setBetAmount] = useState(undefined) const [error, setError] = useState() const [isSubmitting, setIsSubmitting] = useState(false) const inputRef = useRef(null) useEffect(() => { if (isIOS()) window.scrollTo(0, window.scrollY + 200) inputRef.current && inputRef.current.focus() }, []) async function submitBet() { if (!user || !betAmount) return setError(undefined) setIsSubmitting(true) placeBet({ amount: betAmount, outcome: answerId, contractId: contract.id, }) .then((r) => { console.log('placed bet. Result:', r) setIsSubmitting(false) setBetAmount(undefined) props.closePanel() }) .catch((e) => { if (e instanceof APIError) { setError(e.toString()) } else { console.error(e) setError('Error placing bet') } setIsSubmitting(false) }) track('bet', { location: 'answer panel', outcomeType: contract.outcomeType, slug: contract.slug, contractId: contract.id, amount: betAmount, outcome: answerId, }) } const betDisabled = isSubmitting || !betAmount || error const initialProb = getDpmOutcomeProbability(contract.totalShares, answer.id) const resultProb = getDpmOutcomeProbabilityAfterBet( contract.totalShares, answerId, betAmount ?? 0 ) const shares = calculateDpmShares( contract.totalShares, betAmount ?? 0, answerId ) const currentPayout = betAmount ? calculateDpmPayoutAfterCorrectBet(contract, { outcome: answerId, amount: betAmount, shares, } as Bet) : 0 const currentReturn = betAmount ? (currentPayout - betAmount) / betAmount : 0 const currentReturnPercent = formatPercent(currentReturn) const bankrollFraction = (betAmount ?? 0) / (user?.balance ?? 1e9) return (
Bet on {isModal ? `"${answer.text}"` : 'this answer'}
{!isModal && ( )}
Amount
{(betAmount ?? 0) > 10 && bankrollFraction >= 0.5 && bankrollFraction <= 1 ? ( ) : ( '' )}
Probability
{formatPercent(initialProb)}
{formatPercent(resultProb)}
Estimated
payout if chosen
{formatMoney(currentPayout)} (+{currentReturnPercent})
{user ? ( ) : ( )} ) }