2021-12-10 17:14:05 +00:00
|
|
|
import clsx from 'clsx'
|
2022-03-15 22:27:51 +00:00
|
|
|
import React, { useEffect, useState } from 'react'
|
2022-07-11 15:54:37 +00:00
|
|
|
import { partition, sum, sumBy } from 'lodash'
|
2022-07-10 18:05:44 +00:00
|
|
|
import { SwitchHorizontalIcon } from '@heroicons/react/solid'
|
2021-12-11 00:06:17 +00:00
|
|
|
|
2022-05-09 13:04:36 +00:00
|
|
|
import { useUser } from 'web/hooks/use-user'
|
2022-07-10 18:05:44 +00:00
|
|
|
import { CPMMBinaryContract, PseudoNumericContract } from 'common/contract'
|
2021-12-10 14:56:17 +00:00
|
|
|
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'
|
2022-01-05 18:23:44 +00:00
|
|
|
import {
|
|
|
|
formatMoney,
|
2022-06-17 21:28:12 +00:00
|
|
|
formatMoneyWithDecimals,
|
2022-01-05 18:23:44 +00:00
|
|
|
formatPercent,
|
|
|
|
formatWithCommas,
|
2022-05-09 13:04:36 +00:00
|
|
|
} from 'common/util/format'
|
2022-07-10 18:05:44 +00:00
|
|
|
import { getBinaryCpmmBetInfo } from 'common/new-bet'
|
2022-05-25 16:25:39 +00:00
|
|
|
import { User } from 'web/lib/firebase/users'
|
2022-07-10 18:05:44 +00:00
|
|
|
import { Bet, LimitBet } from 'common/bet'
|
2022-07-10 22:03:15 +00:00
|
|
|
import { APIError, placeBet } from 'web/lib/firebase/api'
|
|
|
|
import { sellShares } from 'web/lib/firebase/api'
|
2022-05-17 03:27:37 +00:00
|
|
|
import { AmountInput, BuyAmountInput } from './amount-input'
|
2022-01-15 21:28:19 +00:00
|
|
|
import { InfoTooltip } from './info-tooltip'
|
2022-07-10 18:05:44 +00:00
|
|
|
import { BinaryOutcomeLabel } from './outcome-label'
|
|
|
|
import { getProbability } from 'common/calculate'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { useFocus } from 'web/hooks/use-focus'
|
|
|
|
import { useUserContractBets } from 'web/hooks/use-user-bets'
|
2022-07-11 15:54:37 +00:00
|
|
|
import { calculateCpmmSale, getCpmmProbability } from 'common/calculate-cpmm'
|
2022-07-10 18:05:44 +00:00
|
|
|
import {
|
|
|
|
getFormattedMappedValue,
|
|
|
|
getPseudoProbability,
|
|
|
|
} from 'common/pseudo-numeric'
|
2022-04-20 14:13:39 +00:00
|
|
|
import { SellRow } from './sell-row'
|
2022-07-10 18:05:44 +00:00
|
|
|
import { useSaveBinaryShares } from './use-save-binary-shares'
|
2022-05-25 16:25:39 +00:00
|
|
|
import { SignUpPrompt } from './sign-up-prompt'
|
2022-06-10 16:35:53 +00:00
|
|
|
import { isIOS } from 'web/lib/util/device'
|
2022-07-10 18:05:44 +00:00
|
|
|
import { ProbabilityInput } from './probability-input'
|
2022-06-15 03:00:36 +00:00
|
|
|
import { track } from 'web/lib/service/analytics'
|
2022-07-10 18:05:44 +00:00
|
|
|
import { removeUndefinedProps } from 'common/util/object'
|
|
|
|
import { useUnfilledBets } from 'web/hooks/use-bets'
|
|
|
|
import { LimitBets } from './limit-bets'
|
|
|
|
import { BucketInput } from './bucket-input'
|
2022-01-26 20:08:03 +00:00
|
|
|
|
|
|
|
export function BetPanel(props: {
|
2022-07-10 18:05:44 +00:00
|
|
|
contract: CPMMBinaryContract | PseudoNumericContract
|
2022-01-26 20:08:03 +00:00
|
|
|
className?: string
|
2022-03-29 19:56:56 +00:00
|
|
|
}) {
|
|
|
|
const { contract, className } = props
|
|
|
|
const user = useUser()
|
|
|
|
const userBets = useUserContractBets(user?.id, contract.id)
|
2022-07-10 18:05:44 +00:00
|
|
|
const unfilledBets = useUnfilledBets(contract.id) ?? []
|
|
|
|
const yourUnfilledBets = unfilledBets.filter((bet) => bet.userId === user?.id)
|
|
|
|
const { sharesOutcome } = useSaveBinaryShares(contract, userBets)
|
|
|
|
|
|
|
|
const [isLimitOrder, setIsLimitOrder] = useState(false)
|
2022-07-12 21:55:00 +00:00
|
|
|
const toggleLimitOrder = () => {
|
|
|
|
setIsLimitOrder(!isLimitOrder)
|
|
|
|
track('toggle limit order')
|
|
|
|
}
|
2022-03-29 19:56:56 +00:00
|
|
|
|
2022-07-13 17:14:58 +00:00
|
|
|
const showLimitOrders =
|
|
|
|
(isLimitOrder && unfilledBets.length > 0) || yourUnfilledBets.length > 0
|
|
|
|
|
2022-03-29 19:56:56 +00:00
|
|
|
return (
|
|
|
|
<Col className={className}>
|
2022-04-20 14:13:39 +00:00
|
|
|
<SellRow
|
|
|
|
contract={contract}
|
|
|
|
user={user}
|
2022-07-10 18:05:44 +00:00
|
|
|
className={'rounded-t-md bg-gray-100 px-4 py-5'}
|
2022-04-20 14:13:39 +00:00
|
|
|
/>
|
2022-03-29 19:56:56 +00:00
|
|
|
<Col
|
|
|
|
className={clsx(
|
2022-07-10 18:05:44 +00:00
|
|
|
'relative rounded-b-md bg-white px-8 py-6',
|
2022-03-29 19:56:56 +00:00
|
|
|
!sharesOutcome && 'rounded-t-md',
|
|
|
|
className
|
|
|
|
)}
|
|
|
|
>
|
2022-07-10 18:05:44 +00:00
|
|
|
<Row className="align-center justify-between">
|
|
|
|
<div className="mb-6 text-2xl">
|
2022-07-12 21:46:03 +00:00
|
|
|
{isLimitOrder ? <>Limit order</> : <>Place your bet</>}
|
2022-07-10 18:05:44 +00:00
|
|
|
</div>
|
|
|
|
<button
|
|
|
|
className="btn btn-ghost btn-sm text-sm normal-case"
|
2022-07-12 21:55:00 +00:00
|
|
|
onClick={toggleLimitOrder}
|
2022-07-10 18:05:44 +00:00
|
|
|
>
|
|
|
|
<SwitchHorizontalIcon className="inline h-6 w-6" />
|
|
|
|
</button>
|
|
|
|
</Row>
|
2022-03-29 19:56:56 +00:00
|
|
|
|
2022-07-10 18:05:44 +00:00
|
|
|
<BuyPanel
|
|
|
|
contract={contract}
|
|
|
|
user={user}
|
|
|
|
isLimitOrder={isLimitOrder}
|
|
|
|
unfilledBets={unfilledBets}
|
|
|
|
/>
|
2022-03-29 19:56:56 +00:00
|
|
|
|
2022-05-25 16:25:39 +00:00
|
|
|
<SignUpPrompt />
|
2022-03-29 19:56:56 +00:00
|
|
|
</Col>
|
2022-07-13 17:14:58 +00:00
|
|
|
{showLimitOrders && (
|
|
|
|
<LimitBets className="mt-4" contract={contract} bets={unfilledBets} />
|
2022-07-10 18:05:44 +00:00
|
|
|
)}
|
2022-03-29 19:56:56 +00:00
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-07-10 18:05:44 +00:00
|
|
|
export function SimpleBetPanel(props: {
|
|
|
|
contract: CPMMBinaryContract | PseudoNumericContract
|
2022-03-29 19:56:56 +00:00
|
|
|
className?: string
|
2022-01-26 20:08:03 +00:00
|
|
|
selected?: 'YES' | 'NO'
|
2022-07-12 22:34:10 +00:00
|
|
|
hasShares?: boolean
|
2022-02-05 18:26:11 +00:00
|
|
|
onBetSuccess?: () => void
|
2022-01-26 20:08:03 +00:00
|
|
|
}) {
|
2022-07-12 22:34:10 +00:00
|
|
|
const { contract, className, selected, hasShares, onBetSuccess } = props
|
2022-03-29 19:56:56 +00:00
|
|
|
|
2021-12-10 17:14:05 +00:00
|
|
|
const user = useUser()
|
2022-07-10 18:05:44 +00:00
|
|
|
const [isLimitOrder, setIsLimitOrder] = useState(false)
|
2022-03-29 19:56:56 +00:00
|
|
|
|
2022-07-10 18:05:44 +00:00
|
|
|
const unfilledBets = useUnfilledBets(contract.id) ?? []
|
|
|
|
const yourUnfilledBets = unfilledBets.filter((bet) => bet.userId === user?.id)
|
2022-07-13 17:14:58 +00:00
|
|
|
const showLimitOrders =
|
|
|
|
(isLimitOrder && unfilledBets.length > 0) || yourUnfilledBets.length > 0
|
2022-03-29 19:56:56 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Col className={className}>
|
2022-07-12 22:34:10 +00:00
|
|
|
<SellRow
|
|
|
|
contract={contract}
|
|
|
|
user={user}
|
|
|
|
className={'rounded-t-md bg-gray-100 px-4 py-5'}
|
|
|
|
/>
|
|
|
|
<Col
|
|
|
|
className={clsx(
|
|
|
|
!hasShares && 'rounded-t-md',
|
|
|
|
'rounded-b-md bg-white px-8 py-6'
|
|
|
|
)}
|
|
|
|
>
|
2022-07-10 18:05:44 +00:00
|
|
|
<Row className="justify-between">
|
2022-07-11 03:07:42 +00:00
|
|
|
<div className="mb-6 text-2xl">
|
2022-07-12 21:46:03 +00:00
|
|
|
{isLimitOrder ? <>Limit order</> : <>Place your bet</>}
|
2022-07-11 03:07:42 +00:00
|
|
|
</div>
|
2022-03-29 19:56:56 +00:00
|
|
|
|
2022-07-10 18:05:44 +00:00
|
|
|
<button
|
|
|
|
className="btn btn-ghost btn-sm text-sm normal-case"
|
|
|
|
onClick={() => setIsLimitOrder(!isLimitOrder)}
|
|
|
|
>
|
|
|
|
<SwitchHorizontalIcon className="inline h-6 w-6" />
|
|
|
|
</button>
|
|
|
|
</Row>
|
2022-03-29 19:56:56 +00:00
|
|
|
|
2022-07-10 18:05:44 +00:00
|
|
|
<BuyPanel
|
|
|
|
contract={contract}
|
|
|
|
user={user}
|
|
|
|
unfilledBets={unfilledBets}
|
|
|
|
selected={selected}
|
|
|
|
onBuySuccess={onBetSuccess}
|
|
|
|
isLimitOrder={isLimitOrder}
|
2022-03-29 19:56:56 +00:00
|
|
|
/>
|
|
|
|
|
2022-05-25 16:25:39 +00:00
|
|
|
<SignUpPrompt />
|
2022-03-29 19:56:56 +00:00
|
|
|
</Col>
|
2022-07-10 18:05:44 +00:00
|
|
|
|
2022-07-13 17:14:58 +00:00
|
|
|
{showLimitOrders && (
|
|
|
|
<LimitBets className="mt-4" contract={contract} bets={unfilledBets} />
|
2022-07-10 18:05:44 +00:00
|
|
|
)}
|
2022-03-29 19:56:56 +00:00
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function BuyPanel(props: {
|
2022-07-10 18:05:44 +00:00
|
|
|
contract: CPMMBinaryContract | PseudoNumericContract
|
2022-03-29 19:56:56 +00:00
|
|
|
user: User | null | undefined
|
2022-07-10 18:05:44 +00:00
|
|
|
unfilledBets: Bet[]
|
|
|
|
isLimitOrder?: boolean
|
2022-03-29 19:56:56 +00:00
|
|
|
selected?: 'YES' | 'NO'
|
|
|
|
onBuySuccess?: () => void
|
|
|
|
}) {
|
2022-07-10 18:05:44 +00:00
|
|
|
const { contract, user, unfilledBets, isLimitOrder, selected, onBuySuccess } =
|
|
|
|
props
|
|
|
|
|
|
|
|
const initialProb = getProbability(contract)
|
2022-07-02 19:37:59 +00:00
|
|
|
const isPseudoNumeric = contract.outcomeType === 'PSEUDO_NUMERIC'
|
2021-12-10 17:14:05 +00:00
|
|
|
|
2022-01-26 20:08:03 +00:00
|
|
|
const [betChoice, setBetChoice] = useState<'YES' | 'NO' | undefined>(selected)
|
2021-12-10 15:51:48 +00:00
|
|
|
const [betAmount, setBetAmount] = useState<number | undefined>(undefined)
|
2022-07-10 18:05:44 +00:00
|
|
|
const [limitProb, setLimitProb] = useState<number | undefined>(
|
|
|
|
Math.round(100 * initialProb)
|
|
|
|
)
|
2021-12-15 00:08:55 +00:00
|
|
|
const [error, setError] = useState<string | undefined>()
|
2021-12-10 17:14:05 +00:00
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
|
|
|
const [wasSubmitted, setWasSubmitted] = useState(false)
|
|
|
|
|
2022-03-29 19:56:56 +00:00
|
|
|
const [inputRef, focusAmountInput] = useFocus()
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-06-10 16:35:53 +00:00
|
|
|
if (selected) {
|
|
|
|
if (isIOS()) window.scrollTo(0, window.scrollY + 200)
|
|
|
|
focusAmountInput()
|
|
|
|
}
|
2022-03-29 19:56:56 +00:00
|
|
|
}, [selected, focusAmountInput])
|
|
|
|
|
2021-12-11 04:09:32 +00:00
|
|
|
function onBetChoice(choice: 'YES' | 'NO') {
|
|
|
|
setBetChoice(choice)
|
|
|
|
setWasSubmitted(false)
|
2022-01-26 20:08:03 +00:00
|
|
|
focusAmountInput()
|
2021-12-11 04:09:32 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 03:41:42 +00:00
|
|
|
function onBetChange(newAmount: number | undefined) {
|
2021-12-15 00:08:55 +00:00
|
|
|
setWasSubmitted(false)
|
2022-01-11 03:41:42 +00:00
|
|
|
setBetAmount(newAmount)
|
2022-01-26 20:08:03 +00:00
|
|
|
if (!betChoice) {
|
|
|
|
setBetChoice('YES')
|
|
|
|
}
|
2021-12-10 16:04:59 +00:00
|
|
|
}
|
|
|
|
|
2021-12-10 17:14:05 +00:00
|
|
|
async function submitBet() {
|
|
|
|
if (!user || !betAmount) return
|
2022-07-10 18:05:44 +00:00
|
|
|
if (isLimitOrder && limitProb === undefined) return
|
|
|
|
|
|
|
|
const limitProbScaled =
|
|
|
|
isLimitOrder && limitProb !== undefined ? limitProb / 100 : undefined
|
2021-12-10 17:14:05 +00:00
|
|
|
|
2021-12-15 00:08:55 +00:00
|
|
|
setError(undefined)
|
2021-12-10 17:14:05 +00:00
|
|
|
setIsSubmitting(true)
|
|
|
|
|
2022-07-10 18:05:44 +00:00
|
|
|
placeBet(
|
|
|
|
removeUndefinedProps({
|
|
|
|
amount: betAmount,
|
|
|
|
outcome: betChoice,
|
|
|
|
contractId: contract.id,
|
|
|
|
limitProb: limitProbScaled,
|
|
|
|
})
|
|
|
|
)
|
2022-05-19 22:04:34 +00:00
|
|
|
.then((r) => {
|
|
|
|
console.log('placed bet. Result:', r)
|
|
|
|
setIsSubmitting(false)
|
|
|
|
setWasSubmitted(true)
|
|
|
|
setBetAmount(undefined)
|
|
|
|
if (onBuySuccess) onBuySuccess()
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
if (e instanceof APIError) {
|
|
|
|
setError(e.toString())
|
|
|
|
} else {
|
|
|
|
console.error(e)
|
|
|
|
setError('Error placing bet')
|
|
|
|
}
|
|
|
|
setIsSubmitting(false)
|
|
|
|
})
|
2022-06-15 03:00:36 +00:00
|
|
|
|
|
|
|
track('bet', {
|
|
|
|
location: 'bet panel',
|
|
|
|
outcomeType: contract.outcomeType,
|
|
|
|
slug: contract.slug,
|
|
|
|
contractId: contract.id,
|
|
|
|
amount: betAmount,
|
|
|
|
outcome: betChoice,
|
2022-07-12 21:55:00 +00:00
|
|
|
isLimitOrder,
|
|
|
|
limitProb: limitProbScaled,
|
2022-06-15 03:00:36 +00:00
|
|
|
})
|
2021-12-10 17:14:05 +00:00
|
|
|
}
|
|
|
|
|
2021-12-15 00:08:55 +00:00
|
|
|
const betDisabled = isSubmitting || !betAmount || error
|
2021-12-10 17:14:05 +00:00
|
|
|
|
2022-07-10 18:05:44 +00:00
|
|
|
const limitProbFrac = (limitProb ?? 0) / 100
|
2022-01-12 19:01:04 +00:00
|
|
|
|
2022-07-10 18:05:44 +00:00
|
|
|
const { newPool, newP, newBet } = getBinaryCpmmBetInfo(
|
|
|
|
betChoice ?? 'YES',
|
|
|
|
betAmount ?? 0,
|
2022-03-15 22:27:51 +00:00
|
|
|
contract,
|
2022-07-10 18:05:44 +00:00
|
|
|
isLimitOrder ? limitProbFrac : undefined,
|
|
|
|
unfilledBets as LimitBet[]
|
2021-12-15 22:57:40 +00:00
|
|
|
)
|
2021-12-11 03:47:46 +00:00
|
|
|
|
2022-07-10 18:05:44 +00:00
|
|
|
const resultProb = getCpmmProbability(newPool, newP)
|
|
|
|
const remainingMatched = isLimitOrder
|
|
|
|
? ((newBet.orderAmount ?? 0) - newBet.amount) /
|
|
|
|
(betChoice === 'YES' ? limitProbFrac : 1 - limitProbFrac)
|
2021-12-11 03:47:46 +00:00
|
|
|
: 0
|
2022-07-10 18:05:44 +00:00
|
|
|
const currentPayout = newBet.shares + remainingMatched
|
2022-01-12 19:01:04 +00:00
|
|
|
|
2022-01-15 21:28:19 +00:00
|
|
|
const currentReturn = betAmount ? (currentPayout - betAmount) / betAmount : 0
|
2022-03-19 03:02:04 +00:00
|
|
|
const currentReturnPercent = formatPercent(currentReturn)
|
2022-03-15 22:27:51 +00:00
|
|
|
|
2022-07-11 15:54:37 +00:00
|
|
|
const totalFees = sum(Object.values(newBet.fees))
|
2022-07-02 19:37:59 +00:00
|
|
|
|
|
|
|
const format = getFormattedMappedValue(contract)
|
|
|
|
|
2021-12-10 14:56:17 +00:00
|
|
|
return (
|
2022-03-29 19:56:56 +00:00
|
|
|
<>
|
2021-12-10 14:56:17 +00:00
|
|
|
<YesNoSelector
|
2022-01-27 06:38:22 +00:00
|
|
|
className="mb-4"
|
2022-05-11 21:35:50 +00:00
|
|
|
btnClassName="flex-1"
|
2021-12-10 14:56:17 +00:00
|
|
|
selected={betChoice}
|
2021-12-13 18:32:40 +00:00
|
|
|
onSelect={(choice) => onBetChoice(choice)}
|
2022-07-02 19:37:59 +00:00
|
|
|
isPseudoNumeric={isPseudoNumeric}
|
2021-12-10 14:56:17 +00:00
|
|
|
/>
|
2022-03-29 19:56:56 +00:00
|
|
|
<div className="my-3 text-left text-sm text-gray-500">Amount</div>
|
|
|
|
<BuyAmountInput
|
2022-05-02 16:15:00 +00:00
|
|
|
inputClassName="w-full max-w-none"
|
2022-01-11 03:41:42 +00:00
|
|
|
amount={betAmount}
|
|
|
|
onChange={onBetChange}
|
|
|
|
error={error}
|
|
|
|
setError={setError}
|
|
|
|
disabled={isSubmitting}
|
2022-01-26 20:08:03 +00:00
|
|
|
inputRef={inputRef}
|
2022-01-11 03:41:42 +00:00
|
|
|
/>
|
2022-07-10 18:05:44 +00:00
|
|
|
{isLimitOrder && (
|
|
|
|
<>
|
|
|
|
<Row className="my-3 items-center gap-2 text-left text-sm text-gray-500">
|
|
|
|
Limit {isPseudoNumeric ? 'value' : 'probability'}
|
|
|
|
<InfoTooltip
|
|
|
|
text={`Bet ${betChoice === 'NO' ? 'down' : 'up'} to this ${
|
|
|
|
isPseudoNumeric ? 'value' : 'probability'
|
|
|
|
} and wait to match other bets.`}
|
|
|
|
/>
|
|
|
|
</Row>
|
|
|
|
{isPseudoNumeric ? (
|
|
|
|
<BucketInput
|
|
|
|
contract={contract}
|
|
|
|
onBucketChange={(value) =>
|
|
|
|
setLimitProb(
|
|
|
|
value === undefined
|
|
|
|
? undefined
|
|
|
|
: 100 *
|
|
|
|
getPseudoProbability(
|
|
|
|
value,
|
|
|
|
contract.min,
|
|
|
|
contract.max,
|
|
|
|
contract.isLogScale
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
isSubmitting={isSubmitting}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<ProbabilityInput
|
|
|
|
inputClassName="w-full max-w-none"
|
|
|
|
prob={limitProb}
|
|
|
|
onChange={setLimitProb}
|
|
|
|
disabled={isSubmitting}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
2022-03-03 09:09:32 +00:00
|
|
|
<Col className="mt-3 w-full gap-3">
|
2022-07-10 18:05:44 +00:00
|
|
|
{!isLimitOrder && (
|
|
|
|
<Row className="items-center justify-between text-sm">
|
|
|
|
<div className="text-gray-500">
|
|
|
|
{isPseudoNumeric ? 'Estimated value' : 'Probability'}
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
{format(initialProb)}
|
|
|
|
<span className="mx-2">→</span>
|
|
|
|
{format(resultProb)}
|
|
|
|
</div>
|
|
|
|
</Row>
|
|
|
|
)}
|
2021-12-10 17:14:05 +00:00
|
|
|
|
2022-03-22 05:18:08 +00:00
|
|
|
<Row className="items-center justify-between gap-2 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-02 03:31:48 +00:00
|
|
|
<div>
|
2022-07-10 18:05:44 +00:00
|
|
|
{isPseudoNumeric ? (
|
2022-07-02 19:37:59 +00:00
|
|
|
'Max payout'
|
2022-03-22 05:18:08 +00:00
|
|
|
) : (
|
|
|
|
<>
|
2022-04-18 23:02:40 +00:00
|
|
|
Payout if <BinaryOutcomeLabel outcome={betChoice ?? 'YES'} />
|
2022-03-22 05:18:08 +00:00
|
|
|
</>
|
|
|
|
)}
|
2022-03-02 03:31:48 +00:00
|
|
|
</div>
|
2022-07-10 18:05:44 +00:00
|
|
|
<InfoTooltip
|
2022-07-11 15:54:37 +00:00
|
|
|
text={`Includes ${formatMoneyWithDecimals(totalFees)} in fees`}
|
2022-07-10 18:05:44 +00:00
|
|
|
/>
|
2022-01-26 20:08:03 +00:00
|
|
|
</Row>
|
2022-05-15 21:10:26 +00:00
|
|
|
<div>
|
|
|
|
<span className="mr-2 whitespace-nowrap">
|
2022-03-02 03:31:48 +00:00
|
|
|
{formatMoney(currentPayout)}
|
|
|
|
</span>
|
2022-05-15 21:10:26 +00:00
|
|
|
(+{currentReturnPercent})
|
|
|
|
</div>
|
2022-03-02 03:31:48 +00:00
|
|
|
</Row>
|
|
|
|
</Col>
|
2021-12-11 03:47:46 +00:00
|
|
|
|
2022-03-02 03:31:48 +00:00
|
|
|
<Spacer h={8} />
|
2021-12-11 03:47:46 +00:00
|
|
|
|
2022-02-21 04:37:53 +00:00
|
|
|
{user && (
|
2021-12-18 07:09:11 +00:00
|
|
|
<button
|
|
|
|
className={clsx(
|
2022-03-02 03:31:48 +00:00
|
|
|
'btn flex-1',
|
2021-12-18 07:09:11 +00:00
|
|
|
betDisabled
|
|
|
|
? 'btn-disabled'
|
|
|
|
: betChoice === 'YES'
|
|
|
|
? 'btn-primary'
|
2022-02-11 18:40:22 +00:00
|
|
|
: 'border-none bg-red-400 hover:bg-red-500',
|
2021-12-18 07:09:11 +00:00
|
|
|
isSubmitting ? 'loading' : ''
|
|
|
|
)}
|
|
|
|
onClick={betDisabled ? undefined : submitBet}
|
|
|
|
>
|
2022-04-09 21:13:36 +00:00
|
|
|
{isSubmitting ? 'Submitting...' : 'Submit bet'}
|
2021-12-18 07:09:11 +00:00
|
|
|
</button>
|
|
|
|
)}
|
2021-12-11 03:47:46 +00:00
|
|
|
|
2022-07-12 21:46:03 +00:00
|
|
|
{wasSubmitted && (
|
|
|
|
<div className="mt-4">{isLimitOrder ? 'Order' : 'Bet'} submitted!</div>
|
|
|
|
)}
|
2022-03-29 19:56:56 +00:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-04-20 14:13:39 +00:00
|
|
|
export function SellPanel(props: {
|
2022-07-02 19:37:59 +00:00
|
|
|
contract: CPMMBinaryContract | PseudoNumericContract
|
2022-03-29 19:56:56 +00:00
|
|
|
userBets: Bet[]
|
|
|
|
shares: number
|
|
|
|
sharesOutcome: 'YES' | 'NO'
|
|
|
|
user: User
|
|
|
|
onSellSuccess?: () => void
|
|
|
|
}) {
|
|
|
|
const { contract, shares, sharesOutcome, userBets, user, onSellSuccess } =
|
|
|
|
props
|
|
|
|
|
|
|
|
const [amount, setAmount] = useState<number | undefined>(shares)
|
|
|
|
const [error, setError] = useState<string | undefined>()
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
|
|
|
const [wasSubmitted, setWasSubmitted] = useState(false)
|
|
|
|
|
2022-07-10 18:05:44 +00:00
|
|
|
const unfilledBets = useUnfilledBets(contract.id) ?? []
|
|
|
|
|
2022-03-29 19:56:56 +00:00
|
|
|
const betDisabled = isSubmitting || !amount || error
|
|
|
|
|
2022-07-10 18:05:44 +00:00
|
|
|
// Sell all shares if remaining shares would be < 1
|
|
|
|
const sellQuantity = amount === Math.floor(shares) ? shares : amount
|
|
|
|
|
2022-03-29 19:56:56 +00:00
|
|
|
async function submitSell() {
|
|
|
|
if (!user || !amount) return
|
|
|
|
|
|
|
|
setError(undefined)
|
|
|
|
setIsSubmitting(true)
|
|
|
|
|
2022-06-07 20:54:58 +00:00
|
|
|
await sellShares({
|
2022-07-10 18:05:44 +00:00
|
|
|
shares: sellQuantity,
|
2022-03-29 19:56:56 +00:00
|
|
|
outcome: sharesOutcome,
|
|
|
|
contractId: contract.id,
|
2022-06-07 20:54:58 +00:00
|
|
|
})
|
|
|
|
.then((r) => {
|
|
|
|
console.log('Sold shares. Result:', r)
|
|
|
|
setIsSubmitting(false)
|
|
|
|
setWasSubmitted(true)
|
|
|
|
setAmount(undefined)
|
|
|
|
if (onSellSuccess) onSellSuccess()
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
if (e instanceof APIError) {
|
|
|
|
setError(e.toString())
|
|
|
|
} else {
|
|
|
|
console.error(e)
|
|
|
|
setError('Error selling')
|
|
|
|
}
|
|
|
|
setIsSubmitting(false)
|
|
|
|
})
|
2022-06-15 03:00:36 +00:00
|
|
|
|
|
|
|
track('sell shares', {
|
|
|
|
outcomeType: contract.outcomeType,
|
|
|
|
slug: contract.slug,
|
|
|
|
contractId: contract.id,
|
2022-07-10 18:05:44 +00:00
|
|
|
shares: sellQuantity,
|
2022-06-15 03:00:36 +00:00
|
|
|
outcome: sharesOutcome,
|
|
|
|
})
|
2022-03-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const initialProb = getProbability(contract)
|
2022-07-10 18:05:44 +00:00
|
|
|
const { cpmmState, saleValue } = calculateCpmmSale(
|
2022-03-30 02:30:04 +00:00
|
|
|
contract,
|
2022-07-10 18:05:44 +00:00
|
|
|
sellQuantity ?? 0,
|
|
|
|
sharesOutcome,
|
|
|
|
unfilledBets
|
2022-03-30 02:30:04 +00:00
|
|
|
)
|
2022-07-10 18:05:44 +00:00
|
|
|
const resultProb = getCpmmProbability(cpmmState.pool, cpmmState.p)
|
2022-03-29 19:56:56 +00:00
|
|
|
|
2022-05-17 03:27:37 +00:00
|
|
|
const openUserBets = userBets.filter((bet) => !bet.isSold && !bet.sale)
|
2022-05-22 08:36:05 +00:00
|
|
|
const [yesBets, noBets] = partition(
|
2022-05-17 03:27:37 +00:00
|
|
|
openUserBets,
|
|
|
|
(bet) => bet.outcome === 'YES'
|
|
|
|
)
|
|
|
|
const [yesShares, noShares] = [
|
2022-05-22 08:36:05 +00:00
|
|
|
sumBy(yesBets, (bet) => bet.shares),
|
|
|
|
sumBy(noBets, (bet) => bet.shares),
|
2022-05-17 03:27:37 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
const ownedShares = Math.round(yesShares) || Math.round(noShares)
|
|
|
|
|
|
|
|
const onAmountChange = (amount: number | undefined) => {
|
|
|
|
setAmount(amount)
|
|
|
|
|
|
|
|
// Check for errors.
|
|
|
|
if (amount !== undefined) {
|
|
|
|
if (amount > ownedShares) {
|
|
|
|
setError(`Maximum ${formatWithCommas(Math.floor(ownedShares))} shares`)
|
|
|
|
} else {
|
|
|
|
setError(undefined)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-02 19:37:59 +00:00
|
|
|
const { outcomeType } = contract
|
|
|
|
const isPseudoNumeric = outcomeType === 'PSEUDO_NUMERIC'
|
|
|
|
const format = getFormattedMappedValue(contract)
|
|
|
|
|
2022-03-29 19:56:56 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-05-17 03:27:37 +00:00
|
|
|
<AmountInput
|
2022-05-04 15:47:45 +00:00
|
|
|
amount={
|
|
|
|
amount
|
|
|
|
? Math.round(amount) === 0
|
|
|
|
? 0
|
|
|
|
: Math.floor(amount)
|
|
|
|
: undefined
|
|
|
|
}
|
2022-05-17 03:27:37 +00:00
|
|
|
onChange={onAmountChange}
|
|
|
|
label="Qty"
|
2022-03-29 19:56:56 +00:00
|
|
|
error={error}
|
|
|
|
disabled={isSubmitting}
|
2022-05-17 03:27:37 +00:00
|
|
|
inputClassName="w-full"
|
2022-03-29 19:56:56 +00:00
|
|
|
/>
|
|
|
|
|
2022-05-17 03:27:37 +00:00
|
|
|
<Col className="mt-3 w-full gap-3 text-sm">
|
|
|
|
<Row className="items-center justify-between gap-2 text-gray-500">
|
|
|
|
Sale proceeds
|
|
|
|
<span className="text-neutral">{formatMoney(saleValue)}</span>
|
|
|
|
</Row>
|
|
|
|
<Row className="items-center justify-between">
|
2022-07-02 19:37:59 +00:00
|
|
|
<div className="text-gray-500">
|
|
|
|
{isPseudoNumeric ? 'Estimated value' : 'Probability'}
|
|
|
|
</div>
|
2022-05-15 21:10:26 +00:00
|
|
|
<div>
|
2022-07-02 19:37:59 +00:00
|
|
|
{format(initialProb)}
|
2022-05-15 21:10:26 +00:00
|
|
|
<span className="mx-2">→</span>
|
2022-07-02 19:37:59 +00:00
|
|
|
{format(resultProb)}
|
2022-05-15 21:10:26 +00:00
|
|
|
</div>
|
2022-03-29 19:56:56 +00:00
|
|
|
</Row>
|
|
|
|
</Col>
|
|
|
|
|
|
|
|
<Spacer h={8} />
|
|
|
|
|
|
|
|
<button
|
|
|
|
className={clsx(
|
|
|
|
'btn flex-1',
|
|
|
|
betDisabled
|
|
|
|
? 'btn-disabled'
|
|
|
|
: sharesOutcome === 'YES'
|
|
|
|
? 'btn-primary'
|
|
|
|
: 'border-none bg-red-400 hover:bg-red-500',
|
|
|
|
isSubmitting ? 'loading' : ''
|
|
|
|
)}
|
|
|
|
onClick={betDisabled ? undefined : submitSell}
|
|
|
|
>
|
|
|
|
{isSubmitting ? 'Submitting...' : 'Submit sell'}
|
|
|
|
</button>
|
|
|
|
|
|
|
|
{wasSubmitted && <div className="mt-4">Sell submitted!</div>}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|