Show probability change
This commit is contained in:
parent
b1c047430f
commit
94a225615f
|
@ -1,22 +1,27 @@
|
||||||
import { track } from '@amplitude/analytics-browser'
|
import { track } from '@amplitude/analytics-browser'
|
||||||
import clsx from 'clsx'
|
import clsx from 'clsx'
|
||||||
import { CPMMBinaryContract, PseudoNumericContract } from 'common/contract'
|
import { CPMMBinaryContract, PseudoNumericContract } from 'common/contract'
|
||||||
|
import { getBinaryCpmmBetInfo } from 'common/new-bet'
|
||||||
import { APIError } from 'web/lib/firebase/api'
|
import { APIError } from 'web/lib/firebase/api'
|
||||||
import { useState } from 'react'
|
import { useEffect, useState } from 'react'
|
||||||
import { useMutation } from 'react-query'
|
import { useMutation } from 'react-query'
|
||||||
import { placeBet } from 'web/lib/firebase/api'
|
import { placeBet } from 'web/lib/firebase/api'
|
||||||
import { BuyAmountInput } from './amount-input'
|
import { BuyAmountInput } from './amount-input'
|
||||||
import { Button } from './button'
|
import { Button } from './button'
|
||||||
import { Row } from './layout/row'
|
import { Row } from './layout/row'
|
||||||
import { YesNoSelector } from './yes-no-selector'
|
import { YesNoSelector } from './yes-no-selector'
|
||||||
|
import { useUnfilledBets } from 'web/hooks/use-bets'
|
||||||
import { useUser } from 'web/hooks/use-user'
|
import { useUser } from 'web/hooks/use-user'
|
||||||
import { SignUpPrompt } from './sign-up-prompt'
|
import { SignUpPrompt } from './sign-up-prompt'
|
||||||
|
import { getCpmmProbability } from 'common/calculate-cpmm'
|
||||||
|
|
||||||
|
// adapted from bet-panel.ts
|
||||||
export function BetInline(props: {
|
export function BetInline(props: {
|
||||||
contract: CPMMBinaryContract | PseudoNumericContract
|
contract: CPMMBinaryContract | PseudoNumericContract
|
||||||
className?: string
|
className?: string
|
||||||
|
setProbAfter: (probAfter: number) => void
|
||||||
}) {
|
}) {
|
||||||
const { contract, className } = props
|
const { contract, className, setProbAfter: setResult } = props
|
||||||
|
|
||||||
const user = useUser()
|
const user = useUser()
|
||||||
|
|
||||||
|
@ -24,7 +29,19 @@ export function BetInline(props: {
|
||||||
const [amount, setAmount] = useState<number>()
|
const [amount, setAmount] = useState<number>()
|
||||||
const [error, setError] = useState<string>()
|
const [error, setError] = useState<string>()
|
||||||
|
|
||||||
// adapted from bet-panel.ts submitBet()
|
const isPseudoNumeric = contract.outcomeType === 'PSEUDO_NUMERIC'
|
||||||
|
const unfilledBets = useUnfilledBets(contract.id) ?? []
|
||||||
|
|
||||||
|
const { newPool, newP } = getBinaryCpmmBetInfo(
|
||||||
|
outcome ?? 'YES',
|
||||||
|
amount ?? 0,
|
||||||
|
contract,
|
||||||
|
undefined,
|
||||||
|
unfilledBets
|
||||||
|
)
|
||||||
|
const resultProb = getCpmmProbability(newPool, newP)
|
||||||
|
useEffect(() => setResult(resultProb), [resultProb])
|
||||||
|
|
||||||
const submitBet = useMutation(
|
const submitBet = useMutation(
|
||||||
() => placeBet({ outcome, amount, contractId: contract.id }),
|
() => placeBet({ outcome, amount, contractId: contract.id }),
|
||||||
{
|
{
|
||||||
|
@ -54,7 +71,7 @@ export function BetInline(props: {
|
||||||
btnClassName="rounded-none first:rounded-l-2xl last:rounded-r-2xl"
|
btnClassName="rounded-none first:rounded-l-2xl last:rounded-r-2xl"
|
||||||
selected={outcome}
|
selected={outcome}
|
||||||
onSelect={setOutcome}
|
onSelect={setOutcome}
|
||||||
isPseudoNumeric={false}
|
isPseudoNumeric={isPseudoNumeric}
|
||||||
/>
|
/>
|
||||||
<BuyAmountInput
|
<BuyAmountInput
|
||||||
className="-mb-4"
|
className="-mb-4"
|
||||||
|
|
|
@ -185,11 +185,16 @@ export function BinaryResolutionOrChance(props: {
|
||||||
contract: BinaryContract
|
contract: BinaryContract
|
||||||
large?: boolean
|
large?: boolean
|
||||||
className?: string
|
className?: string
|
||||||
|
probAfter?: number // 0 to 1
|
||||||
}) {
|
}) {
|
||||||
const { contract, large, className } = props
|
const { contract, large, className, probAfter } = props
|
||||||
const { resolution } = contract
|
const { resolution } = contract
|
||||||
const textColor = `text-${getColor(contract)}`
|
const textColor = `text-${getColor(contract)}`
|
||||||
|
|
||||||
|
const before = getBinaryProbPercent(contract)
|
||||||
|
const after = probAfter && formatPercent(probAfter)
|
||||||
|
const probChanged = before !== after
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Col className={clsx(large ? 'text-4xl' : 'text-3xl', className)}>
|
<Col className={clsx(large ? 'text-4xl' : 'text-3xl', className)}>
|
||||||
{resolution ? (
|
{resolution ? (
|
||||||
|
@ -206,7 +211,14 @@ export function BinaryResolutionOrChance(props: {
|
||||||
</>
|
</>
|
||||||
) : (
|
) : (
|
||||||
<>
|
<>
|
||||||
<div className={textColor}>{getBinaryProbPercent(contract)}</div>
|
{probAfter && probChanged ? (
|
||||||
|
<div>
|
||||||
|
<span className="text-gray-500 line-through">{before}</span>
|
||||||
|
<span className={textColor}>{after}</span>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className={textColor}>{before}</div>
|
||||||
|
)}
|
||||||
<div className={clsx(textColor, large ? 'text-xl' : 'text-base')}>
|
<div className={clsx(textColor, large ? 'text-xl' : 'text-base')}>
|
||||||
chance
|
chance
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -94,6 +94,8 @@ export function ContractEmbed(props: { contract: Contract; bets: Bet[] }) {
|
||||||
|
|
||||||
const [betPanelOpen, setBetPanelOpen] = useState(false)
|
const [betPanelOpen, setBetPanelOpen] = useState(false)
|
||||||
|
|
||||||
|
const [probAfter, setProbAfter] = useState<number>()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Col className="h-[100vh] w-full bg-white">
|
<Col className="h-[100vh] w-full bg-white">
|
||||||
<div className="relative flex flex-col pt-2">
|
<div className="relative flex flex-col pt-2">
|
||||||
|
@ -119,7 +121,13 @@ export function ContractEmbed(props: { contract: Contract; bets: Bet[] }) {
|
||||||
</Button>
|
</Button>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{isBinary && <BinaryResolutionOrChance contract={contract} />}
|
{isBinary && (
|
||||||
|
<BinaryResolutionOrChance
|
||||||
|
contract={contract}
|
||||||
|
probAfter={probAfter}
|
||||||
|
className="items-center"
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
{isPseudoNumeric && (
|
{isPseudoNumeric && (
|
||||||
<PseudoNumericResolutionOrExpectation contract={contract} />
|
<PseudoNumericResolutionOrExpectation contract={contract} />
|
||||||
|
@ -142,7 +150,7 @@ export function ContractEmbed(props: { contract: Contract; bets: Bet[] }) {
|
||||||
|
|
||||||
{(isBinary || isPseudoNumeric) && betPanelOpen && (
|
{(isBinary || isPseudoNumeric) && betPanelOpen && (
|
||||||
<Row className="mb-2 items-center justify-center gap-4 self-center rounded bg-indigo-200 py-2 px-3">
|
<Row className="mb-2 items-center justify-center gap-4 self-center rounded bg-indigo-200 py-2 px-3">
|
||||||
<BetInline contract={contract as any} />
|
<BetInline contract={contract as any} setProbAfter={setProbAfter}/>
|
||||||
<button onClick={() => setBetPanelOpen(false)}>
|
<button onClick={() => setBetPanelOpen(false)}>
|
||||||
<XIcon className="h-6 w-6" />
|
<XIcon className="h-6 w-6" />
|
||||||
</button>
|
</button>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user