76f27d1a93
* Numeric contract type * Create market numeric type * Add numeric graph (coded without testing) * Outline of numeric bet panel * Update bet panel logic * create numeric contracts * remove batching for antes for numeric markets * Remove focus * numeric market range [1, 100] * Zoom graph * Hide bet panels * getNumericBets * Add numeric resolution panel * Use getNumericBets in bet panel calc * Switch bucket count to 100 * Parallelize ante creation * placeBet for numeric markets * halve std of numeric bets * Update resolveMarket with numeric type * Set min and max for contract * lower std for numeric bets * calculateNumericDpmShares: use sorted order * Use min and max to map the input * Fix probability calc * normpdf variance mislabeled * range input * merge * change numeric graph color * fix getNewContract params * bet panel labels * validation * number input * fix bucketing * bucket input, numeric resolution panel * outcome label * merge * numeric bet panel on mobile * Make text underneath filled green answer bar selectable * Default to 'all' feed category when loading page. * fix numeric resolution panel * fix numeric bet panel calculations * display numeric resolution * don't render NumericBetPanel for non numeric markets * numeric bets: store shares, bet amounts across buckets in each bet object * restore your bets for numeric markets * numeric pnl calculations * remove hasUserHitManaLimit * contrain contract type * handle undefined allOutcomeShares * numeric ante bet amount * use correct amount for numeric dpm payouts * change numeric graph/outcome color * numeric constants * hack to show correct numeric payout in calculateDpmPayoutAfterCorrectBet * remove comment * fix ante display in bet list * halve bucket count * cast to NumericContract * fix merge imports * OUTCOME_TYPES * typo * lower bucket count to 200 * store raw numeric value with bet * store raw numeric resolution value * number input max length * create page: min, max to undefined if not numeric market * numeric resolution formatting * expected value for numeric markets * expected value for numeric markets * rearrange lines for readability * move normalpdf to util/math * show bets tab * check if outcomeMode is undefined * remove extraneous auto-merge cruft * hide comment status for numeric markets * import Co-authored-by: mantikoros <sgrugett@gmail.com>
208 lines
5.7 KiB
TypeScript
208 lines
5.7 KiB
TypeScript
import clsx from 'clsx'
|
|
import { getNumericBetsInfo } from 'common/new-bet'
|
|
import { useState } from 'react'
|
|
|
|
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 { placeBet } from '../lib/firebase/api-call'
|
|
import { firebaseLogin, 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'
|
|
|
|
export function NumericBetPanel(props: {
|
|
contract: NumericContract
|
|
className?: string
|
|
}) {
|
|
const { contract, className } = props
|
|
const user = useUser()
|
|
|
|
return (
|
|
<Col className={clsx('rounded-md bg-white px-8 py-6', className)}>
|
|
<div className="mb-6 text-2xl">Place your bet</div>
|
|
|
|
<NumericBuyPanel contract={contract} user={user} />
|
|
|
|
{user === null && (
|
|
<button
|
|
className="btn flex-1 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 up to trade!
|
|
</button>
|
|
)}
|
|
</Col>
|
|
)
|
|
}
|
|
|
|
function NumericBuyPanel(props: {
|
|
contract: NumericContract
|
|
user: User | null | undefined
|
|
onBuySuccess?: () => void
|
|
}) {
|
|
const { contract, user, onBuySuccess } = props
|
|
|
|
const [bucketChoice, setBucketChoice] = useState<string | undefined>(
|
|
undefined
|
|
)
|
|
|
|
const [value, setValue] = useState<number | undefined>(undefined)
|
|
|
|
const [betAmount, setBetAmount] = useState<number | undefined>(undefined)
|
|
|
|
const [valueError, setValueError] = useState<string | undefined>()
|
|
const [error, setError] = useState<string | undefined>()
|
|
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)
|
|
|
|
const result = await placeBet({
|
|
amount: betAmount,
|
|
outcome: bucketChoice,
|
|
value,
|
|
contractId: contract.id,
|
|
}).then((r) => r.data as any)
|
|
|
|
console.log('placed bet. Result:', result)
|
|
|
|
if (result?.status === 'success') {
|
|
setIsSubmitting(false)
|
|
setWasSubmitted(true)
|
|
setBetAmount(undefined)
|
|
if (onBuySuccess) onBuySuccess()
|
|
} else {
|
|
setError(result?.message || 'Error placing bet')
|
|
setIsSubmitting(false)
|
|
}
|
|
}
|
|
|
|
const betDisabled = isSubmitting || !betAmount || !bucketChoice || error
|
|
|
|
const { newBet, newPool, newTotalShares, newTotalBets } = getNumericBetsInfo(
|
|
{ id: 'dummy', balance: 0 } as User, // a little hackish
|
|
value ?? 0,
|
|
bucketChoice ?? 'NaN',
|
|
betAmount ?? 0,
|
|
contract,
|
|
'dummy id'
|
|
)
|
|
|
|
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 (
|
|
<>
|
|
<div className="my-3 text-left text-sm text-gray-500">
|
|
Predicted value
|
|
</div>
|
|
|
|
<BucketInput
|
|
contract={contract}
|
|
isSubmitting={isSubmitting}
|
|
onBucketChange={(v, b) => (setValue(v), setBucketChoice(b))}
|
|
/>
|
|
|
|
<div className="my-3 text-left text-sm text-gray-500">Bet amount</div>
|
|
<BuyAmountInput
|
|
inputClassName="w-full max-w-none"
|
|
amount={betAmount}
|
|
onChange={onBetChange}
|
|
error={error}
|
|
setError={setError}
|
|
disabled={isSubmitting}
|
|
/>
|
|
|
|
<Col className="mt-3 w-full gap-3">
|
|
<Row className="items-center justify-between text-sm">
|
|
<div className="text-gray-500">Probability</div>
|
|
<Row>
|
|
<div>{formatPercent(initialProb)}</div>
|
|
<div className="mx-2">→</div>
|
|
<div>{formatPercent(outcomeProb)}</div>
|
|
</Row>
|
|
</Row>
|
|
|
|
<Row className="items-center justify-between gap-2 text-sm">
|
|
<Row className="flex-nowrap items-center gap-2 whitespace-nowrap text-gray-500">
|
|
<div>
|
|
Estimated
|
|
<br /> payout if correct
|
|
</div>
|
|
</Row>
|
|
<Row className="flex-wrap items-end justify-end gap-2">
|
|
<span className="whitespace-nowrap">
|
|
{formatMoney(currentPayout)}
|
|
</span>
|
|
<span>(+{currentReturnPercent})</span>
|
|
</Row>
|
|
</Row>
|
|
</Col>
|
|
|
|
<Spacer h={8} />
|
|
|
|
{user && (
|
|
<button
|
|
className={clsx(
|
|
'btn flex-1',
|
|
betDisabled ? 'btn-disabled' : 'btn-primary',
|
|
isSubmitting ? 'loading' : ''
|
|
)}
|
|
onClick={betDisabled ? undefined : submitBet}
|
|
>
|
|
{isSubmitting ? 'Submitting...' : 'Submit bet'}
|
|
</button>
|
|
)}
|
|
|
|
{wasSubmitted && <div className="mt-4">Bet submitted!</div>}
|
|
</>
|
|
)
|
|
}
|