bet panel: use new calculations
This commit is contained in:
parent
61f30d4ab9
commit
fdea1d5ba6
|
@ -1,8 +1,8 @@
|
||||||
import clsx from 'clsx'
|
import clsx from 'clsx'
|
||||||
import React, { useEffect, useRef, useState } from 'react'
|
import React, { useEffect, useState } from 'react'
|
||||||
|
|
||||||
import { useUser } from '../hooks/use-user'
|
import { useUser } from '../hooks/use-user'
|
||||||
import { Contract } from '../../common/contract'
|
import { Binary, CPMM, DPM, FullContract } from '../../common/contract'
|
||||||
import { Col } from './layout/col'
|
import { Col } from './layout/col'
|
||||||
import { Row } from './layout/row'
|
import { Row } from './layout/row'
|
||||||
import { Spacer } from './layout/spacer'
|
import { Spacer } from './layout/spacer'
|
||||||
|
@ -13,31 +13,22 @@ import {
|
||||||
formatWithCommas,
|
formatWithCommas,
|
||||||
} from '../../common/util/format'
|
} from '../../common/util/format'
|
||||||
import { Title } from './title'
|
import { Title } from './title'
|
||||||
import {
|
|
||||||
getDpmProbability,
|
|
||||||
calculateDpmShares,
|
|
||||||
getDpmProbabilityAfterBet,
|
|
||||||
calculateDpmPayoutAfterCorrectBet,
|
|
||||||
} from '../../common/calculate-dpm'
|
|
||||||
import { firebaseLogin } from '../lib/firebase/users'
|
import { firebaseLogin } from '../lib/firebase/users'
|
||||||
import { Bet } from '../../common/bet'
|
import { Bet } from '../../common/bet'
|
||||||
import { placeBet } from '../lib/firebase/api-call'
|
import { placeBet } from '../lib/firebase/api-call'
|
||||||
import { AmountInput } from './amount-input'
|
import { AmountInput } from './amount-input'
|
||||||
import { InfoTooltip } from './info-tooltip'
|
import { InfoTooltip } from './info-tooltip'
|
||||||
import { OutcomeLabel } from './outcome-label'
|
import { OutcomeLabel } from './outcome-label'
|
||||||
|
import {
|
||||||
// Focus helper from https://stackoverflow.com/a/54159564/1222351
|
calculatePayoutAfterCorrectBet,
|
||||||
function useFocus(): [React.RefObject<HTMLElement>, () => void] {
|
calculateShares,
|
||||||
const htmlElRef = useRef<HTMLElement>(null)
|
getProbability,
|
||||||
const setFocus = () => {
|
getProbabilityAfterBet,
|
||||||
htmlElRef.current && htmlElRef.current.focus()
|
} from '../../common/calculate'
|
||||||
}
|
import { useFocus } from '../hooks/use-focus'
|
||||||
|
|
||||||
return [htmlElRef, setFocus]
|
|
||||||
}
|
|
||||||
|
|
||||||
export function BetPanel(props: {
|
export function BetPanel(props: {
|
||||||
contract: Contract
|
contract: FullContract<DPM | CPMM, Binary>
|
||||||
className?: string
|
className?: string
|
||||||
title?: string // Set if BetPanel is on a feed modal
|
title?: string // Set if BetPanel is on a feed modal
|
||||||
selected?: 'YES' | 'NO'
|
selected?: 'YES' | 'NO'
|
||||||
|
@ -49,7 +40,6 @@ export function BetPanel(props: {
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
const { contract, className, title, selected, onBetSuccess } = props
|
const { contract, className, title, selected, onBetSuccess } = props
|
||||||
const { totalShares, phantomShares } = contract
|
|
||||||
|
|
||||||
const user = useUser()
|
const user = useUser()
|
||||||
|
|
||||||
|
@ -107,23 +97,19 @@ export function BetPanel(props: {
|
||||||
|
|
||||||
const betDisabled = isSubmitting || !betAmount || error
|
const betDisabled = isSubmitting || !betAmount || error
|
||||||
|
|
||||||
const initialProb = getDpmProbability(contract.totalShares)
|
const initialProb = getProbability(contract)
|
||||||
|
|
||||||
const outcomeProb = getDpmProbabilityAfterBet(
|
const outcomeProb = getProbabilityAfterBet(
|
||||||
contract.totalShares,
|
contract,
|
||||||
betChoice || 'YES',
|
betChoice || 'YES',
|
||||||
betAmount ?? 0
|
betAmount ?? 0
|
||||||
)
|
)
|
||||||
const resultProb = betChoice === 'NO' ? 1 - outcomeProb : outcomeProb
|
const resultProb = betChoice === 'NO' ? 1 - outcomeProb : outcomeProb
|
||||||
|
|
||||||
const shares = calculateDpmShares(
|
const shares = calculateShares(contract, betAmount ?? 0, betChoice || 'YES')
|
||||||
contract.totalShares,
|
|
||||||
betAmount ?? 0,
|
|
||||||
betChoice || 'YES'
|
|
||||||
)
|
|
||||||
|
|
||||||
const currentPayout = betAmount
|
const currentPayout = betAmount
|
||||||
? calculateDpmPayoutAfterCorrectBet(contract, {
|
? calculatePayoutAfterCorrectBet(contract, {
|
||||||
outcome: betChoice,
|
outcome: betChoice,
|
||||||
amount: betAmount,
|
amount: betAmount,
|
||||||
shares,
|
shares,
|
||||||
|
@ -132,11 +118,23 @@ export function BetPanel(props: {
|
||||||
|
|
||||||
const currentReturn = betAmount ? (currentPayout - betAmount) / betAmount : 0
|
const currentReturn = betAmount ? (currentPayout - betAmount) / betAmount : 0
|
||||||
const currentReturnPercent = (currentReturn * 100).toFixed() + '%'
|
const currentReturnPercent = (currentReturn * 100).toFixed() + '%'
|
||||||
|
|
||||||
const panelTitle = title ?? 'Place a trade'
|
const panelTitle = title ?? 'Place a trade'
|
||||||
if (title) {
|
if (title) {
|
||||||
focusAmountInput()
|
focusAmountInput()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const tooltip =
|
||||||
|
contract.mechanism === 'dpm-2'
|
||||||
|
? `Current payout for ${formatWithCommas(shares)} / ${formatWithCommas(
|
||||||
|
shares +
|
||||||
|
contract.totalShares[betChoice ?? 'YES'] -
|
||||||
|
(contract.phantomShares
|
||||||
|
? contract.phantomShares[betChoice ?? 'YES']
|
||||||
|
: 0)
|
||||||
|
)} ${betChoice} shares`
|
||||||
|
: undefined
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Col className={clsx('rounded-md bg-white px-8 py-6', className)}>
|
<Col className={clsx('rounded-md bg-white px-8 py-6', className)}>
|
||||||
<Title
|
<Title
|
||||||
|
@ -177,15 +175,8 @@ export function BetPanel(props: {
|
||||||
<div>
|
<div>
|
||||||
Payout if <OutcomeLabel outcome={betChoice ?? 'YES'} />
|
Payout if <OutcomeLabel outcome={betChoice ?? 'YES'} />
|
||||||
</div>
|
</div>
|
||||||
<InfoTooltip
|
|
||||||
text={`Current payout for ${formatWithCommas(
|
{tooltip && <InfoTooltip text={tooltip} />}
|
||||||
shares
|
|
||||||
)} / ${formatWithCommas(
|
|
||||||
shares +
|
|
||||||
totalShares[betChoice ?? 'YES'] -
|
|
||||||
(phantomShares ? phantomShares[betChoice ?? 'YES'] : 0)
|
|
||||||
)} ${betChoice} shares`}
|
|
||||||
/>
|
|
||||||
</Row>
|
</Row>
|
||||||
<Row className="flex-wrap justify-end items-end gap-2">
|
<Row className="flex-wrap justify-end items-end gap-2">
|
||||||
<span className="whitespace-nowrap">
|
<span className="whitespace-nowrap">
|
||||||
|
|
Loading…
Reference in New Issue
Block a user