import clsx from 'clsx' import { useState } from 'react' import { getNumericBetsInfo } from 'common/new-bet' import { Bet } from 'common/bet' import { calculatePayoutAfterCorrectBet, getOutcomeProbability, } from 'common/calculate' import { NumericContract } from 'common/contract' import { formatPercent, formatMoney } from 'common/util/format' import { useUser } from '../hooks/use-user' import { APIError, placeBet } from '../lib/firebase/api-call' import { User } from '../lib/firebase/users' import { BuyAmountInput } from './amount-input' import { BucketInput } from './bucket-input' import { Col } from './layout/col' import { Row } from './layout/row' import { Spacer } from './layout/spacer' import { SignUpPrompt } from './sign-up-prompt' export function NumericBetPanel(props: { contract: NumericContract className?: string }) { const { contract, className } = props const user = useUser() return (
Place your bet
) } function NumericBuyPanel(props: { contract: NumericContract user: User | null | undefined onBuySuccess?: () => void }) { const { contract, user, onBuySuccess } = props const [bucketChoice, setBucketChoice] = useState( undefined ) const [value, setValue] = useState(undefined) const [betAmount, setBetAmount] = useState(undefined) const [error, setError] = useState() const [isSubmitting, setIsSubmitting] = useState(false) const [wasSubmitted, setWasSubmitted] = useState(false) function onBetChange(newAmount: number | undefined) { setWasSubmitted(false) setBetAmount(newAmount) } async function submitBet() { if ( !user || !betAmount || bucketChoice === undefined || value === undefined ) return setError(undefined) setIsSubmitting(true) await placeBet({ amount: betAmount, outcome: bucketChoice, value, contractId: contract.id, }) .then((r) => { console.log('placed bet. Result:', r) setIsSubmitting(false) setWasSubmitted(true) setBetAmount(undefined) if (onBuySuccess) onBuySuccess() }) .catch((e) => { if (e instanceof APIError) { setError(e.toString()) } else { console.error(e) setError('Error placing bet') } setIsSubmitting(false) }) } const betDisabled = isSubmitting || !betAmount || !bucketChoice || error const { newBet, newPool, newTotalShares, newTotalBets } = getNumericBetsInfo( value ?? 0, bucketChoice ?? 'NaN', betAmount ?? 0, contract ) const { probAfter: outcomeProb, shares } = newBet const initialProb = bucketChoice ? getOutcomeProbability(contract, bucketChoice) : 0 const currentPayout = betAmount && bucketChoice ? calculatePayoutAfterCorrectBet( { ...contract, pool: newPool, totalShares: newTotalShares, totalBets: newTotalBets, }, { outcome: bucketChoice, amount: betAmount, shares, } as Bet ) : 0 const currentReturn = betAmount && bucketChoice ? (currentPayout - betAmount) / betAmount : 0 const currentReturnPercent = formatPercent(currentReturn) return ( <>
Predicted value
(setValue(v), setBucketChoice(b))} />
Bet amount
Probability
{formatPercent(initialProb)}
{formatPercent(outcomeProb)}
Estimated
payout if correct
{formatMoney(currentPayout)} (+{currentReturnPercent})
{user && ( )} {wasSubmitted &&
Bet submitted!
} ) }