2022-02-20 22:25:58 +00:00
|
|
|
import clsx from 'clsx'
|
|
|
|
import _ from 'lodash'
|
|
|
|
import { useState } from 'react'
|
|
|
|
import Textarea from 'react-expanding-textarea'
|
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
import { DPM, FreeResponse, FullContract } 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'
|
|
|
|
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 {
|
2022-03-15 22:27:51 +00:00
|
|
|
calculateDpmShares,
|
|
|
|
calculateDpmPayoutAfterCorrectBet,
|
|
|
|
getDpmOutcomeProbabilityAfterBet,
|
|
|
|
} from '../../../common/calculate-dpm'
|
2022-02-20 22:25:58 +00:00
|
|
|
import { firebaseLogin } from '../../lib/firebase/users'
|
|
|
|
import { Bet } from '../../../common/bet'
|
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
export function CreateAnswerPanel(props: {
|
|
|
|
contract: FullContract<DPM, FreeResponse>
|
|
|
|
}) {
|
2022-02-20 22:25:58 +00:00
|
|
|
const { contract } = props
|
|
|
|
const user = useUser()
|
|
|
|
const [text, setText] = useState('')
|
|
|
|
const [betAmount, setBetAmount] = useState<number | undefined>(10)
|
|
|
|
const [amountError, setAmountError] = useState<string | undefined>()
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
|
|
|
|
|
|
|
const canSubmit = text && betAmount && !amountError && !isSubmitting
|
|
|
|
|
|
|
|
const submitAnswer = async () => {
|
|
|
|
if (canSubmit) {
|
|
|
|
setIsSubmitting(true)
|
2022-03-02 22:28:23 +00:00
|
|
|
|
2022-02-20 22:25:58 +00:00
|
|
|
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)
|
2022-03-02 22:28:23 +00:00
|
|
|
} else setAmountError(result.message)
|
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,
|
|
|
|
'new',
|
|
|
|
betAmount ?? 0
|
|
|
|
)
|
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
const shares = calculateDpmShares(contract.totalShares, betAmount ?? 0, 'new')
|
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: 'new',
|
|
|
|
amount: betAmount,
|
|
|
|
shares,
|
|
|
|
} as Bet)
|
|
|
|
: 0
|
|
|
|
|
|
|
|
const currentReturn = betAmount ? (currentPayout - betAmount) / betAmount : 0
|
|
|
|
const currentReturnPercent = (currentReturn * 100).toFixed() + '%'
|
|
|
|
|
|
|
|
return (
|
2022-03-03 09:09:32 +00:00
|
|
|
<Col className="gap-4 rounded bg-gray-50 p-4">
|
2022-02-20 22:25:58 +00:00
|
|
|
<Col className="flex-1 gap-2">
|
|
|
|
<div className="mb-1">Add your answer</div>
|
|
|
|
<Textarea
|
|
|
|
value={text}
|
|
|
|
onChange={(e) => setText(e.target.value)}
|
2022-03-25 16:27:28 +00:00
|
|
|
className="textarea textarea-bordered w-full resize-none"
|
2022-02-20 22:25:58 +00:00
|
|
|
placeholder="Type your answer..."
|
|
|
|
rows={1}
|
|
|
|
maxLength={10000}
|
|
|
|
/>
|
|
|
|
<div />
|
|
|
|
<Col
|
|
|
|
className={clsx(
|
2022-03-03 09:09:32 +00:00
|
|
|
'gap-4 sm:flex-row sm:items-end',
|
2022-02-20 22:25:58 +00:00
|
|
|
text ? 'justify-between' : 'self-end'
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{text && (
|
|
|
|
<>
|
2022-03-03 09:09:32 +00:00
|
|
|
<Col className="mt-1 gap-2">
|
2022-04-09 21:13:36 +00:00
|
|
|
<div className="text-sm text-gray-500">Bet amount</div>
|
2022-03-29 19:56:56 +00:00
|
|
|
<BuyAmountInput
|
2022-02-20 22:25:58 +00:00
|
|
|
amount={betAmount}
|
|
|
|
onChange={setBetAmount}
|
|
|
|
error={amountError}
|
|
|
|
setError={setAmountError}
|
2022-02-20 23:02:00 +00:00
|
|
|
minimumAmount={1}
|
2022-02-20 22:25:58 +00:00
|
|
|
disabled={isSubmitting}
|
2022-03-02 21:50:19 +00:00
|
|
|
contractIdForLoan={contract.id}
|
2022-02-20 22:25:58 +00:00
|
|
|
/>
|
|
|
|
</Col>
|
2022-03-02 03:31:48 +00:00
|
|
|
<Col className="gap-3">
|
2022-03-03 09:09:32 +00:00
|
|
|
<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(0)}</div>
|
|
|
|
<div className="mx-2">→</div>
|
|
|
|
<div>{formatPercent(resultProb)}</div>
|
|
|
|
</Row>
|
2022-02-20 22:25:58 +00:00
|
|
|
</Row>
|
2022-03-02 03:31:48 +00:00
|
|
|
|
2022-03-25 16:27:28 +00:00
|
|
|
<Row className="items-center justify-between gap-4 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)} 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>
|
2022-02-20 22:25:58 +00:00
|
|
|
</Row>
|
|
|
|
</Col>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
{user ? (
|
|
|
|
<button
|
|
|
|
className={clsx(
|
2022-03-02 03:31:48 +00:00
|
|
|
'btn mt-2',
|
2022-02-20 22:25:58 +00:00
|
|
|
canSubmit ? 'btn-outline' : 'btn-disabled',
|
|
|
|
isSubmitting && 'loading'
|
|
|
|
)}
|
|
|
|
disabled={!canSubmit}
|
|
|
|
onClick={submitAnswer}
|
|
|
|
>
|
2022-02-20 23:02:00 +00:00
|
|
|
Submit answer & buy
|
2022-02-20 22:25:58 +00:00
|
|
|
</button>
|
|
|
|
) : (
|
|
|
|
text && (
|
|
|
|
<button
|
|
|
|
className="btn self-end whitespace-nowrap border-none bg-gradient-to-r from-teal-500 to-green-500 px-10 text-lg font-medium normal-case hover:from-teal-600 hover:to-green-600"
|
|
|
|
onClick={firebaseLogin}
|
|
|
|
>
|
|
|
|
Sign in
|
|
|
|
</button>
|
|
|
|
)
|
|
|
|
)}
|
|
|
|
</Col>
|
|
|
|
</Col>
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|