import clsx from 'clsx' import _ from 'lodash' import { useState } from 'react' import Textarea from 'react-expanding-textarea' import { Contract } from '../../../common/contract' import { AmountInput } from '../amount-input' import { Col } from '../layout/col' import { createAnswer } from '../../lib/firebase/api-call' import { Row } from '../layout/row' import { formatMoney, formatPercent, formatWithCommas, } from '../../../common/util/format' import { InfoTooltip } from '../info-tooltip' import { useUser } from '../../hooks/use-user' import { getProbabilityAfterBet, calculateShares, calculatePayoutAfterCorrectBet, } from '../../../common/calculate' import { firebaseLogin } from '../../lib/firebase/users' import { Bet } from '../../../common/bet' export function CreateAnswerPanel(props: { contract: Contract }) { const { contract } = props const user = useUser() const [text, setText] = useState('') const [betAmount, setBetAmount] = useState(10) const [amountError, setAmountError] = useState() const [isSubmitting, setIsSubmitting] = useState(false) const canSubmit = text && betAmount && !amountError && !isSubmitting const submitAnswer = async () => { if (canSubmit) { setIsSubmitting(true) const result = await createAnswer({ contractId: contract.id, text, amount: betAmount, }).then((r) => r.data) setIsSubmitting(false) if (result.status === 'success') { setText('') setBetAmount(10) setAmountError(undefined) } } } const resultProb = getProbabilityAfterBet( contract.totalShares, 'new', betAmount ?? 0 ) const shares = calculateShares(contract.totalShares, betAmount ?? 0, 'new') const currentPayout = betAmount ? calculatePayoutAfterCorrectBet(contract, { outcome: 'new', amount: betAmount, shares, } as Bet) : 0 const currentReturn = betAmount ? (currentPayout - betAmount) / betAmount : 0 const currentReturnPercent = (currentReturn * 100).toFixed() + '%' return (
Add your answer