2021-12-11 00:40:23 +00:00
|
|
|
import { getFunctions, httpsCallable } from 'firebase/functions'
|
2021-12-10 17:14:05 +00:00
|
|
|
import clsx from 'clsx'
|
2021-12-10 14:56:17 +00:00
|
|
|
import React, { useState } from 'react'
|
2021-12-11 00:06:17 +00:00
|
|
|
|
2021-12-10 17:14:05 +00:00
|
|
|
import { useUser } from '../hooks/use-user'
|
2021-12-10 14:56:17 +00:00
|
|
|
import { Contract } from '../lib/firebase/contracts'
|
|
|
|
import { Col } from './layout/col'
|
2021-12-10 15:51:48 +00:00
|
|
|
import { Row } from './layout/row'
|
2021-12-10 14:56:17 +00:00
|
|
|
import { Spacer } from './layout/spacer'
|
|
|
|
import { YesNoSelector } from './yes-no-selector'
|
2021-12-11 03:47:46 +00:00
|
|
|
import { formatMoney } from '../lib/util/format'
|
2021-12-10 14:56:17 +00:00
|
|
|
|
|
|
|
export function BetPanel(props: { contract: Contract; className?: string }) {
|
|
|
|
const { contract, className } = props
|
|
|
|
|
2021-12-10 17:14:05 +00:00
|
|
|
const user = useUser()
|
|
|
|
|
2021-12-10 14:56:17 +00:00
|
|
|
const [betChoice, setBetChoice] = useState<'YES' | 'NO'>('YES')
|
2021-12-10 15:51:48 +00:00
|
|
|
const [betAmount, setBetAmount] = useState<number | undefined>(undefined)
|
2021-12-10 14:56:17 +00:00
|
|
|
|
2021-12-10 17:14:05 +00:00
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
|
|
|
const [wasSubmitted, setWasSubmitted] = useState(false)
|
|
|
|
|
2021-12-11 04:09:32 +00:00
|
|
|
function onBetChoice(choice: 'YES' | 'NO') {
|
|
|
|
setBetChoice(choice)
|
|
|
|
setWasSubmitted(false)
|
|
|
|
}
|
|
|
|
|
2021-12-10 16:04:59 +00:00
|
|
|
function onBetChange(str: string) {
|
|
|
|
const amount = parseInt(str)
|
|
|
|
setBetAmount(isNaN(amount) ? undefined : amount)
|
2021-12-11 03:47:46 +00:00
|
|
|
|
|
|
|
setWasSubmitted(false)
|
2021-12-10 16:04:59 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 17:14:05 +00:00
|
|
|
async function submitBet() {
|
|
|
|
if (!user || !betAmount) return
|
|
|
|
|
|
|
|
setIsSubmitting(true)
|
|
|
|
|
2021-12-11 00:06:17 +00:00
|
|
|
const result = await placeBet({
|
|
|
|
amount: betAmount,
|
|
|
|
outcome: betChoice,
|
2021-12-11 00:40:23 +00:00
|
|
|
contractId: contract.id,
|
2021-12-11 00:06:17 +00:00
|
|
|
})
|
|
|
|
console.log('placed bet. Result:', result)
|
2021-12-10 17:14:05 +00:00
|
|
|
|
|
|
|
setIsSubmitting(false)
|
|
|
|
setWasSubmitted(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
const betDisabled = isSubmitting || wasSubmitted
|
|
|
|
|
2021-12-11 03:47:46 +00:00
|
|
|
const initialProb = getProbability(contract.pot, betChoice)
|
|
|
|
const resultProb = getProbability(contract.pot, betChoice, betAmount)
|
|
|
|
const dpmWeight = getDpmWeight(contract.pot, betAmount ?? 0, betChoice)
|
|
|
|
|
|
|
|
const estimatedWinnings = Math.floor((betAmount ?? 0) + dpmWeight)
|
|
|
|
const estimatedReturn = betAmount
|
|
|
|
? (estimatedWinnings - betAmount) / betAmount
|
|
|
|
: 0
|
|
|
|
const estimatedReturnPercent = (estimatedReturn * 100).toFixed() + '%'
|
|
|
|
|
2021-12-10 14:56:17 +00:00
|
|
|
return (
|
2021-12-10 23:43:22 +00:00
|
|
|
<Col
|
2021-12-11 03:35:21 +00:00
|
|
|
className={clsx(
|
2021-12-12 22:14:52 +00:00
|
|
|
'bg-gray-200 shadow-xl px-8 py-6 rounded-md w-full md:w-auto',
|
2021-12-11 03:35:21 +00:00
|
|
|
className
|
|
|
|
)}
|
2021-12-10 23:43:22 +00:00
|
|
|
>
|
2021-12-10 14:56:17 +00:00
|
|
|
<div className="p-2 font-medium">Pick outcome</div>
|
|
|
|
<YesNoSelector
|
|
|
|
className="p-2"
|
|
|
|
selected={betChoice}
|
2021-12-11 04:09:32 +00:00
|
|
|
onSelect={choice => onBetChoice(choice)}
|
2021-12-10 14:56:17 +00:00
|
|
|
/>
|
|
|
|
|
|
|
|
<Spacer h={4} />
|
|
|
|
|
2021-12-10 15:51:48 +00:00
|
|
|
<div className="p-2 font-medium">Bet amount</div>
|
2021-12-11 03:47:46 +00:00
|
|
|
<Row className="p-2 items-center relative">
|
|
|
|
<div className="absolute inset-y-0 left-2 pl-3 flex items-center pointer-events-none">
|
|
|
|
<span className="text-gray-500 sm:text-sm">M$</span>
|
|
|
|
</div>
|
2021-12-10 14:56:17 +00:00
|
|
|
<input
|
2021-12-11 03:47:46 +00:00
|
|
|
className="input input-bordered input-md pl-10 block"
|
|
|
|
style={{ maxWidth: 100 }}
|
2021-12-10 14:56:17 +00:00
|
|
|
type="text"
|
2021-12-10 15:51:48 +00:00
|
|
|
placeholder="0"
|
2021-12-10 16:04:59 +00:00
|
|
|
value={betAmount ?? ''}
|
|
|
|
onChange={(e) => onBetChange(e.target.value)}
|
2021-12-10 14:56:17 +00:00
|
|
|
/>
|
2021-12-10 15:51:48 +00:00
|
|
|
</Row>
|
2021-12-10 14:56:17 +00:00
|
|
|
|
2021-12-11 03:47:46 +00:00
|
|
|
<Spacer h={4} />
|
2021-12-10 15:51:48 +00:00
|
|
|
|
2021-12-11 03:47:46 +00:00
|
|
|
<div className="p-2 font-medium">Implied probability</div>
|
|
|
|
<Row>
|
2021-12-12 22:37:59 +00:00
|
|
|
<div className="px-2 font-sans">
|
2021-12-12 22:51:44 +00:00
|
|
|
{Math.round(initialProb * 100) + '%'}
|
2021-12-11 03:47:46 +00:00
|
|
|
</div>
|
|
|
|
<div>→</div>
|
2021-12-12 22:37:59 +00:00
|
|
|
<div className="px-2 font-sans">
|
2021-12-12 22:51:44 +00:00
|
|
|
{Math.round(resultProb * 100) + '%'}
|
2021-12-11 03:47:46 +00:00
|
|
|
</div>
|
|
|
|
</Row>
|
2021-12-10 17:14:05 +00:00
|
|
|
|
2021-12-11 03:47:46 +00:00
|
|
|
<Spacer h={2} />
|
|
|
|
|
|
|
|
<div className="p-2 font-medium">Estimated winnings</div>
|
2021-12-12 22:37:59 +00:00
|
|
|
<div className="px-2 font-sans">
|
2021-12-11 03:47:46 +00:00
|
|
|
{formatMoney(estimatedWinnings)} (+{estimatedReturnPercent})
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Spacer h={6} />
|
|
|
|
|
|
|
|
<button
|
|
|
|
className={clsx(
|
|
|
|
'btn',
|
|
|
|
betDisabled
|
|
|
|
? 'btn-disabled'
|
|
|
|
: betChoice === 'YES'
|
2021-12-12 22:51:44 +00:00
|
|
|
? 'btn-primary'
|
2021-12-11 03:47:46 +00:00
|
|
|
: 'bg-red-400 hover:bg-red-500 focus:ring-red-400'
|
|
|
|
)}
|
|
|
|
onClick={betDisabled ? undefined : submitBet}
|
|
|
|
>
|
|
|
|
Place bet
|
|
|
|
</button>
|
|
|
|
|
2021-12-11 04:09:32 +00:00
|
|
|
{wasSubmitted && <div className="mt-4">Bet submitted!</div>}
|
2021-12-10 14:56:17 +00:00
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
2021-12-11 00:06:17 +00:00
|
|
|
|
|
|
|
const functions = getFunctions()
|
|
|
|
export const placeBet = httpsCallable(functions, 'placeBet')
|
2021-12-11 03:47:46 +00:00
|
|
|
|
|
|
|
const getProbability = (
|
|
|
|
pot: { YES: number; NO: number },
|
|
|
|
outcome: 'YES' | 'NO',
|
|
|
|
bet = 0
|
|
|
|
) => {
|
|
|
|
const [yesPot, noPot] = [
|
|
|
|
pot.YES + (outcome === 'YES' ? bet : 0),
|
|
|
|
pot.NO + (outcome === 'NO' ? bet : 0),
|
|
|
|
]
|
|
|
|
const numerator = Math.pow(yesPot, 2)
|
|
|
|
const denominator = Math.pow(yesPot, 2) + Math.pow(noPot, 2)
|
|
|
|
return numerator / denominator
|
|
|
|
}
|
|
|
|
|
|
|
|
const getDpmWeight = (
|
|
|
|
pot: { YES: number; NO: number },
|
|
|
|
bet: number,
|
|
|
|
betChoice: 'YES' | 'NO'
|
|
|
|
) => {
|
|
|
|
const [yesPot, noPot] = [pot.YES, pot.NO]
|
|
|
|
|
|
|
|
return betChoice === 'YES'
|
|
|
|
? (bet * Math.pow(noPot, 2)) / (Math.pow(yesPot, 2) + bet * yesPot)
|
|
|
|
: (bet * Math.pow(yesPot, 2)) / (Math.pow(noPot, 2) + bet * noPot)
|
|
|
|
}
|