import clsx from 'clsx' import _ from 'lodash' import { useEffect, useRef, useState } from 'react' import { XIcon } from '@heroicons/react/solid' import { Answer } from '../../../common/answer' import { DPM, FreeResponse, FullContract } from '../../../common/contract' import { BuyAmountInput } from '../amount-input' import { Col } from '../layout/col' import { placeBet } from '../../lib/firebase/api-call' 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 '../../hooks/use-user' import { getDpmOutcomeProbability, calculateDpmShares, calculateDpmPayoutAfterCorrectBet, getDpmOutcomeProbabilityAfterBet, } from '../../../common/calculate-dpm' import { firebaseLogin } from '../../lib/firebase/users' import { Bet } from '../../../common/bet' import { useUserContractBets } from '../../hooks/use-user-bets' export function AnswerBetPanel(props: { answer: Answer contract: FullContract closePanel: () => void className?: string isModal?: boolean }) { const { answer, contract, closePanel, className, isModal } = props const { id: answerId } = answer const user = useUser() const userBets = useUserContractBets(user?.id, contract.id) const [betAmount, setBetAmount] = useState(undefined) const [error, setError] = useState() const [isSubmitting, setIsSubmitting] = useState(false) const inputRef = useRef(null) useEffect(() => { inputRef.current && inputRef.current.focus() }, []) async function submitBet() { if (!user || !betAmount) return setError(undefined) setIsSubmitting(true) const result = await placeBet({ amount: betAmount, outcome: answerId, contractId: contract.id, }).then((r) => r.data as any) console.log('placed bet. Result:', result) if (result?.status === 'success') { setIsSubmitting(false) setBetAmount(undefined) } else { setError(result?.error || 'Error placing bet') setIsSubmitting(false) } } 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) return (
Buy {isModal ? `"${answer.text}"` : 'this answer'}
{!isModal && ( )}
Amount
Probability
{formatPercent(initialProb)}
{formatPercent(resultProb)}
Estimated
payout if chosen
{formatMoney(currentPayout)} (+{currentReturnPercent})
{user ? ( ) : ( )} ) }