2022-02-20 22:25:58 +00:00
|
|
|
import clsx from 'clsx'
|
2022-09-09 20:58:23 +00:00
|
|
|
import React, { useState } from 'react'
|
2022-02-20 22:25:58 +00:00
|
|
|
import { XIcon } from '@heroicons/react/solid'
|
|
|
|
|
2022-05-09 13:04:36 +00:00
|
|
|
import { Answer } from 'common/answer'
|
2022-07-28 02:40:33 +00:00
|
|
|
import { FreeResponseContract, MultipleChoiceContract } from 'common/contract'
|
2022-03-29 19:56:56 +00:00
|
|
|
import { BuyAmountInput } from '../amount-input'
|
2022-02-20 22:25:58 +00:00
|
|
|
import { Col } from '../layout/col'
|
2022-07-10 22:03:15 +00:00
|
|
|
import { APIError, placeBet } from 'web/lib/firebase/api'
|
2022-02-20 22:25:58 +00:00
|
|
|
import { Row } from '../layout/row'
|
|
|
|
import { Spacer } from '../layout/spacer'
|
|
|
|
import {
|
|
|
|
formatMoney,
|
|
|
|
formatPercent,
|
|
|
|
formatWithCommas,
|
2022-05-09 13:04:36 +00:00
|
|
|
} from 'common/util/format'
|
2022-02-20 22:25:58 +00:00
|
|
|
import { InfoTooltip } from '../info-tooltip'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { useUser } from 'web/hooks/use-user'
|
2022-02-20 22:25:58 +00:00
|
|
|
import {
|
2022-03-15 22:27:51 +00:00
|
|
|
getDpmOutcomeProbability,
|
|
|
|
calculateDpmShares,
|
|
|
|
calculateDpmPayoutAfterCorrectBet,
|
|
|
|
getDpmOutcomeProbabilityAfterBet,
|
2022-05-09 13:04:36 +00:00
|
|
|
} from 'common/calculate-dpm'
|
|
|
|
import { Bet } from 'common/bet'
|
2022-06-15 03:00:36 +00:00
|
|
|
import { track } from 'web/lib/service/analytics'
|
2022-08-28 20:44:22 +00:00
|
|
|
import { BetSignUpPrompt } from '../sign-up-prompt'
|
2022-09-09 06:02:22 +00:00
|
|
|
import { WarningConfirmationButton } from '../warning-confirmation-button'
|
2022-02-20 22:25:58 +00:00
|
|
|
|
|
|
|
export function AnswerBetPanel(props: {
|
|
|
|
answer: Answer
|
2022-07-28 02:40:33 +00:00
|
|
|
contract: FreeResponseContract | MultipleChoiceContract
|
2022-02-20 22:25:58 +00:00
|
|
|
closePanel: () => void
|
2022-03-02 03:31:48 +00:00
|
|
|
className?: string
|
2022-03-17 04:42:24 +00:00
|
|
|
isModal?: boolean
|
2022-02-20 22:25:58 +00:00
|
|
|
}) {
|
2022-03-17 04:42:24 +00:00
|
|
|
const { answer, contract, closePanel, className, isModal } = props
|
2022-02-20 22:25:58 +00:00
|
|
|
const { id: answerId } = answer
|
|
|
|
|
|
|
|
const user = useUser()
|
|
|
|
const [betAmount, setBetAmount] = useState<number | undefined>(undefined)
|
|
|
|
|
|
|
|
const [error, setError] = useState<string | undefined>()
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
|
|
|
|
|
|
|
async function submitBet() {
|
|
|
|
if (!user || !betAmount) return
|
|
|
|
|
|
|
|
setError(undefined)
|
|
|
|
setIsSubmitting(true)
|
|
|
|
|
2022-05-19 22:04:34 +00:00
|
|
|
placeBet({
|
2022-02-20 22:25:58 +00:00
|
|
|
amount: betAmount,
|
|
|
|
outcome: answerId,
|
|
|
|
contractId: contract.id,
|
2022-05-19 22:04:34 +00:00
|
|
|
})
|
|
|
|
.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)
|
|
|
|
})
|
2022-06-15 03:00:36 +00:00
|
|
|
|
|
|
|
track('bet', {
|
|
|
|
location: 'answer panel',
|
|
|
|
outcomeType: contract.outcomeType,
|
|
|
|
slug: contract.slug,
|
|
|
|
contractId: contract.id,
|
|
|
|
amount: betAmount,
|
|
|
|
outcome: answerId,
|
|
|
|
})
|
2022-02-20 22:25:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const betDisabled = isSubmitting || !betAmount || error
|
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
const initialProb = getDpmOutcomeProbability(contract.totalShares, answer.id)
|
2022-02-20 22:25:58 +00:00
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
const resultProb = getDpmOutcomeProbabilityAfterBet(
|
2022-02-20 22:25:58 +00:00
|
|
|
contract.totalShares,
|
|
|
|
answerId,
|
|
|
|
betAmount ?? 0
|
|
|
|
)
|
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
const shares = calculateDpmShares(
|
|
|
|
contract.totalShares,
|
|
|
|
betAmount ?? 0,
|
|
|
|
answerId
|
|
|
|
)
|
2022-02-20 22:25:58 +00:00
|
|
|
|
|
|
|
const currentPayout = betAmount
|
2022-03-15 22:27:51 +00:00
|
|
|
? calculateDpmPayoutAfterCorrectBet(contract, {
|
2022-02-20 22:25:58 +00:00
|
|
|
outcome: answerId,
|
|
|
|
amount: betAmount,
|
|
|
|
shares,
|
|
|
|
} as Bet)
|
|
|
|
: 0
|
|
|
|
|
|
|
|
const currentReturn = betAmount ? (currentPayout - betAmount) / betAmount : 0
|
2022-03-19 03:02:04 +00:00
|
|
|
const currentReturnPercent = formatPercent(currentReturn)
|
2022-02-20 22:25:58 +00:00
|
|
|
|
2022-08-12 16:24:08 +00:00
|
|
|
const bankrollFraction = (betAmount ?? 0) / (user?.balance ?? 1e9)
|
|
|
|
|
2022-09-09 06:02:22 +00:00
|
|
|
const warning =
|
2022-09-09 20:58:23 +00:00
|
|
|
(betAmount ?? 0) >= 100 && bankrollFraction >= 0.5 && bankrollFraction <= 1
|
2022-09-09 06:02:22 +00:00
|
|
|
? `You might not want to spend ${formatPercent(
|
|
|
|
bankrollFraction
|
|
|
|
)} of your balance on a single bet. \n\nCurrent balance: ${formatMoney(
|
|
|
|
user?.balance ?? 0
|
|
|
|
)}`
|
|
|
|
: undefined
|
|
|
|
|
2022-02-20 22:25:58 +00:00
|
|
|
return (
|
2022-03-02 03:31:48 +00:00
|
|
|
<Col className={clsx('px-2 pb-2 pt-4 sm:pt-0', className)}>
|
2022-03-03 09:09:32 +00:00
|
|
|
<Row className="items-center justify-between self-stretch">
|
2022-03-17 04:42:24 +00:00
|
|
|
<div className="text-xl">
|
2022-09-07 19:45:04 +00:00
|
|
|
Buy answer: {isModal ? `"${answer.text}"` : 'this answer'}
|
2022-03-17 04:42:24 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
{!isModal && (
|
2022-10-12 19:27:17 +00:00
|
|
|
<button
|
|
|
|
className="hover:bg-greyscale-2 rounded-full"
|
|
|
|
onClick={closePanel}
|
|
|
|
>
|
2022-03-17 04:42:24 +00:00
|
|
|
<XIcon
|
|
|
|
className="mx-auto h-8 w-8 text-gray-500"
|
|
|
|
aria-hidden="true"
|
|
|
|
/>
|
|
|
|
</button>
|
|
|
|
)}
|
2022-02-20 22:25:58 +00:00
|
|
|
</Row>
|
2022-09-01 14:12:46 +00:00
|
|
|
<Row className="my-3 justify-between text-left text-sm text-gray-500">
|
|
|
|
Amount
|
2022-09-05 21:59:35 +00:00
|
|
|
<span>Balance: {formatMoney(user?.balance ?? 0)}</span>
|
2022-09-01 14:12:46 +00:00
|
|
|
</Row>
|
2022-09-05 22:11:32 +00:00
|
|
|
|
2022-03-29 19:56:56 +00:00
|
|
|
<BuyAmountInput
|
2022-05-02 16:15:00 +00:00
|
|
|
inputClassName="w-full max-w-none"
|
2022-02-20 22:25:58 +00:00
|
|
|
amount={betAmount}
|
|
|
|
onChange={setBetAmount}
|
|
|
|
error={error}
|
|
|
|
setError={setError}
|
|
|
|
disabled={isSubmitting}
|
2022-09-05 22:11:32 +00:00
|
|
|
showSliderOnMobile
|
2022-02-20 22:25:58 +00:00
|
|
|
/>
|
2022-08-12 16:24:08 +00:00
|
|
|
|
2022-03-03 09:09:32 +00:00
|
|
|
<Col className="mt-3 w-full gap-3">
|
|
|
|
<Row className="items-center justify-between text-sm">
|
2022-03-02 03:31:48 +00:00
|
|
|
<div className="text-gray-500">Probability</div>
|
|
|
|
<Row>
|
|
|
|
<div>{formatPercent(initialProb)}</div>
|
|
|
|
<div className="mx-2">→</div>
|
|
|
|
<div>{formatPercent(resultProb)}</div>
|
|
|
|
</Row>
|
|
|
|
</Row>
|
|
|
|
|
2022-03-22 05:18:08 +00:00
|
|
|
<Row className="items-center justify-between gap-2 text-sm">
|
2022-03-03 09:09:32 +00:00
|
|
|
<Row className="flex-nowrap items-center gap-2 whitespace-nowrap text-gray-500">
|
2022-03-22 05:18:08 +00:00
|
|
|
<div>
|
|
|
|
Estimated <br /> payout if chosen
|
|
|
|
</div>
|
2022-03-02 03:31:48 +00:00
|
|
|
<InfoTooltip
|
|
|
|
text={`Current payout for ${formatWithCommas(
|
|
|
|
shares
|
|
|
|
)} / ${formatWithCommas(
|
|
|
|
shares + contract.totalShares[answerId]
|
|
|
|
)} shares`}
|
|
|
|
/>
|
|
|
|
</Row>
|
2022-03-03 09:09:32 +00:00
|
|
|
<Row className="flex-wrap items-end justify-end gap-2">
|
2022-03-02 03:31:48 +00:00
|
|
|
<span className="whitespace-nowrap">
|
|
|
|
{formatMoney(currentPayout)}
|
|
|
|
</span>
|
|
|
|
<span>(+{currentReturnPercent})</span>
|
|
|
|
</Row>
|
|
|
|
</Row>
|
|
|
|
</Col>
|
2022-02-20 22:25:58 +00:00
|
|
|
|
|
|
|
<Spacer h={6} />
|
|
|
|
{user ? (
|
2022-09-09 06:02:22 +00:00
|
|
|
<WarningConfirmationButton
|
2022-09-30 05:41:22 +00:00
|
|
|
size="xl"
|
2022-09-27 04:40:56 +00:00
|
|
|
marketType="freeResponse"
|
2022-09-27 00:28:54 +00:00
|
|
|
amount={betAmount}
|
2022-09-09 06:02:22 +00:00
|
|
|
warning={warning}
|
|
|
|
onSubmit={submitBet}
|
|
|
|
isSubmitting={isSubmitting}
|
|
|
|
disabled={!!betDisabled}
|
2022-09-30 05:41:22 +00:00
|
|
|
color={'indigo'}
|
2022-10-05 00:35:44 +00:00
|
|
|
actionLabel="Buy"
|
2022-09-09 06:02:22 +00:00
|
|
|
/>
|
2022-02-20 22:25:58 +00:00
|
|
|
) : (
|
2022-08-28 20:44:22 +00:00
|
|
|
<BetSignUpPrompt />
|
2022-02-20 22:25:58 +00:00
|
|
|
)}
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|