2022-01-02 19:09:01 +00:00
|
|
|
import clsx from 'clsx'
|
|
|
|
import Link from 'next/link'
|
2022-04-07 21:15:51 +00:00
|
|
|
import { Row } from '../layout/row'
|
2022-05-20 19:25:32 +00:00
|
|
|
import { formatLargeNumber, formatPercent } from 'common/util/format'
|
2022-01-12 19:01:04 +00:00
|
|
|
import {
|
|
|
|
Contract,
|
|
|
|
contractPath,
|
2022-02-17 23:00:19 +00:00
|
|
|
getBinaryProbPercent,
|
2022-05-16 23:15:22 +00:00
|
|
|
getBinaryProb,
|
2022-05-09 13:04:36 +00:00
|
|
|
} from 'web/lib/firebase/contracts'
|
2022-04-07 21:15:51 +00:00
|
|
|
import { Col } from '../layout/col'
|
2022-04-18 23:02:40 +00:00
|
|
|
import {
|
|
|
|
Binary,
|
|
|
|
CPMM,
|
|
|
|
DPM,
|
|
|
|
FreeResponse,
|
|
|
|
FreeResponseContract,
|
|
|
|
FullContract,
|
2022-05-19 17:42:03 +00:00
|
|
|
NumericContract,
|
2022-05-09 13:04:36 +00:00
|
|
|
} from 'common/contract'
|
2022-04-18 23:02:40 +00:00
|
|
|
import {
|
2022-04-19 02:44:31 +00:00
|
|
|
AnswerLabel,
|
2022-04-18 23:02:40 +00:00
|
|
|
BinaryContractOutcomeLabel,
|
|
|
|
FreeResponseOutcomeLabel,
|
2022-05-16 23:15:22 +00:00
|
|
|
OUTCOME_TO_COLOR,
|
2022-04-18 23:02:40 +00:00
|
|
|
} from '../outcome-label'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { getOutcomeProbability, getTopAnswer } from 'common/calculate'
|
2022-05-21 20:26:34 +00:00
|
|
|
import {
|
|
|
|
AbbrContractDetails,
|
|
|
|
AvatarDetails,
|
|
|
|
MiscDetails,
|
|
|
|
} from './contract-details'
|
2022-05-19 17:42:03 +00:00
|
|
|
import { getExpectedValue, getValueFromBucket } from 'common/calculate-dpm'
|
2022-05-21 18:58:22 +00:00
|
|
|
import {
|
|
|
|
ArrowCircleUpIcon,
|
|
|
|
ArrowCircleDownIcon,
|
|
|
|
StarIcon,
|
|
|
|
PaperAirplaneIcon,
|
|
|
|
} from '@heroicons/react/outline'
|
2022-05-21 20:26:34 +00:00
|
|
|
import { PaperAirplaneIcon as SolidPlaneIcon } from '@heroicons/react/solid'
|
2022-05-16 23:15:22 +00:00
|
|
|
|
|
|
|
// Return a number from 0 to 1 for this contract
|
|
|
|
// Resolved contracts are set to 1, for coloring purposes (even if NO)
|
|
|
|
function getProb(contract: Contract) {
|
|
|
|
const { outcomeType, resolution } = contract
|
|
|
|
return resolution
|
|
|
|
? 1
|
|
|
|
: outcomeType === 'BINARY'
|
|
|
|
? getBinaryProb(contract)
|
|
|
|
: outcomeType === 'FREE_RESPONSE'
|
|
|
|
? getOutcomeProbability(contract, getTopAnswer(contract)?.id || '')
|
2022-05-20 18:00:22 +00:00
|
|
|
: outcomeType === 'NUMERIC'
|
|
|
|
? getNumericScale(contract as NumericContract)
|
2022-05-16 23:15:22 +00:00
|
|
|
: 1 // Should not happen
|
|
|
|
}
|
|
|
|
|
2022-05-20 18:00:22 +00:00
|
|
|
function getNumericScale(contract: NumericContract) {
|
|
|
|
const { min, max } = contract
|
|
|
|
const ev = getExpectedValue(contract)
|
|
|
|
return (ev - min) / (max - min)
|
|
|
|
}
|
|
|
|
|
2022-05-16 23:15:22 +00:00
|
|
|
function getColor(contract: Contract) {
|
2022-05-21 20:26:34 +00:00
|
|
|
// TODO: Not sure why eg green-400 doesn't work here; try upgrading Tailwind
|
|
|
|
// TODO: Try injecting a gradient here
|
|
|
|
// return 'primary'
|
2022-05-16 23:15:22 +00:00
|
|
|
const { resolution } = contract
|
|
|
|
if (resolution) {
|
|
|
|
return (
|
|
|
|
// @ts-ignore; TODO: Have better typing for contract.resolution?
|
|
|
|
OUTCOME_TO_COLOR[resolution] ||
|
|
|
|
// If resolved to a FR answer, use 'primary'
|
|
|
|
'primary'
|
|
|
|
)
|
|
|
|
}
|
2022-05-20 18:00:22 +00:00
|
|
|
if (contract.outcomeType === 'NUMERIC') {
|
|
|
|
return 'blue-400'
|
|
|
|
}
|
2022-05-16 23:15:22 +00:00
|
|
|
|
|
|
|
const marketClosed = (contract.closeTime || Infinity) < Date.now()
|
|
|
|
return marketClosed
|
|
|
|
? 'gray-400'
|
|
|
|
: getProb(contract) >= 0.5
|
|
|
|
? 'primary'
|
|
|
|
: 'red-400'
|
|
|
|
}
|
2022-01-02 19:09:01 +00:00
|
|
|
|
2022-01-09 21:21:30 +00:00
|
|
|
export function ContractCard(props: {
|
|
|
|
contract: Contract
|
|
|
|
showHotVolume?: boolean
|
2022-01-17 22:54:00 +00:00
|
|
|
showCloseTime?: boolean
|
2022-01-14 06:55:35 +00:00
|
|
|
className?: string
|
2022-01-09 21:21:30 +00:00
|
|
|
}) {
|
2022-01-17 22:54:00 +00:00
|
|
|
const { contract, showHotVolume, showCloseTime, className } = props
|
2022-05-16 23:15:22 +00:00
|
|
|
const { question, outcomeType } = contract
|
2022-01-02 19:09:01 +00:00
|
|
|
|
2022-05-16 23:15:22 +00:00
|
|
|
const prob = getProb(contract)
|
|
|
|
const color = getColor(contract)
|
|
|
|
const marketClosed = (contract.closeTime || Infinity) < Date.now()
|
|
|
|
const showTopBar = prob >= 0.5 || marketClosed
|
2022-05-12 23:28:21 +00:00
|
|
|
|
2022-01-02 19:09:01 +00:00
|
|
|
return (
|
2022-01-18 22:55:39 +00:00
|
|
|
<div>
|
2022-05-12 23:28:21 +00:00
|
|
|
<Col
|
2022-01-18 22:55:39 +00:00
|
|
|
className={clsx(
|
2022-05-16 23:15:22 +00:00
|
|
|
'relative gap-3 rounded-lg bg-white p-6 pr-7 shadow-md hover:bg-gray-100',
|
2022-01-18 22:55:39 +00:00
|
|
|
className
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<Link href={contractPath(contract)}>
|
|
|
|
<a className="absolute left-0 right-0 top-0 bottom-0" />
|
|
|
|
</Link>
|
2022-02-04 18:30:56 +00:00
|
|
|
|
2022-05-12 23:28:21 +00:00
|
|
|
<Row className={clsx('justify-between gap-4')}>
|
|
|
|
<Col className="gap-3">
|
2022-05-21 20:26:34 +00:00
|
|
|
<AvatarDetails contract={contract} />
|
2022-05-12 23:28:21 +00:00
|
|
|
<p
|
|
|
|
className="break-words font-medium text-indigo-700"
|
|
|
|
style={{ /* For iOS safari */ wordBreak: 'break-word' }}
|
|
|
|
>
|
|
|
|
{question}
|
|
|
|
</p>
|
2022-05-21 20:26:34 +00:00
|
|
|
|
|
|
|
<MiscDetails
|
2022-04-18 23:02:40 +00:00
|
|
|
contract={contract}
|
2022-05-21 20:26:34 +00:00
|
|
|
showHotVolume={showHotVolume}
|
|
|
|
showCloseTime={showCloseTime}
|
2022-04-18 23:02:40 +00:00
|
|
|
/>
|
2022-05-21 20:26:34 +00:00
|
|
|
</Col>
|
2022-05-19 17:42:03 +00:00
|
|
|
|
2022-05-21 20:26:34 +00:00
|
|
|
<Col className="gap-2">
|
|
|
|
{contract.createdTime % 3 == 0 ? (
|
|
|
|
<SolidPlaneIcon
|
|
|
|
className={clsx('mx-auto h-6 w-6', `text-${color}`)}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<PaperAirplaneIcon className="mx-auto h-6 w-6 text-gray-400" />
|
|
|
|
)}
|
2022-05-12 23:28:21 +00:00
|
|
|
|
2022-05-21 20:26:34 +00:00
|
|
|
{outcomeType === 'BINARY' && (
|
|
|
|
<BinaryResolutionOrChance
|
|
|
|
className="items-center"
|
|
|
|
contract={contract}
|
|
|
|
/>
|
|
|
|
)}
|
2022-05-12 23:28:21 +00:00
|
|
|
|
2022-05-21 20:26:34 +00:00
|
|
|
{outcomeType === 'NUMERIC' && (
|
|
|
|
<NumericResolutionOrExpectation
|
|
|
|
className="items-center"
|
|
|
|
contract={contract as NumericContract}
|
|
|
|
/>
|
2022-05-21 18:58:22 +00:00
|
|
|
)}
|
2022-05-21 20:26:34 +00:00
|
|
|
|
|
|
|
{outcomeType === 'FREE_RESPONSE' && (
|
|
|
|
<FreeResponseResolutionOrChance
|
|
|
|
className="self-end text-gray-600"
|
|
|
|
contract={contract as FullContract<DPM, FreeResponse>}
|
|
|
|
truncate="long"
|
|
|
|
/>
|
2022-05-21 18:58:22 +00:00
|
|
|
)}
|
2022-05-21 20:26:34 +00:00
|
|
|
|
|
|
|
{contract.createdTime % 3 == 2 ? (
|
|
|
|
<SolidPlaneIcon
|
|
|
|
className={clsx('mx-auto h-6 w-6 rotate-180', `text-${color}`)}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<PaperAirplaneIcon className="mx-auto h-6 w-6 rotate-180 text-gray-400" />
|
2022-05-21 18:58:22 +00:00
|
|
|
)}
|
2022-05-21 20:26:34 +00:00
|
|
|
</Col>
|
|
|
|
</Row>
|
2022-05-21 18:58:22 +00:00
|
|
|
|
2022-05-16 23:15:22 +00:00
|
|
|
<div
|
|
|
|
className={clsx(
|
|
|
|
'absolute right-0 top-0 w-2 rounded-tr-md',
|
|
|
|
'bg-gray-200'
|
|
|
|
)}
|
|
|
|
style={{ height: `${100 * (1 - prob)}%` }}
|
|
|
|
></div>
|
|
|
|
<div
|
|
|
|
className={clsx(
|
|
|
|
'absolute right-0 bottom-0 w-2 rounded-br-md',
|
|
|
|
`bg-${color}`,
|
|
|
|
// If we're showing the full bar, also round the top
|
|
|
|
prob === 1 ? 'rounded-tr-md' : ''
|
|
|
|
)}
|
|
|
|
style={{ height: `${100 * prob}%` }}
|
|
|
|
></div>
|
2022-05-12 23:28:21 +00:00
|
|
|
</Col>
|
2022-01-14 22:59:14 +00:00
|
|
|
</div>
|
2022-01-02 19:09:01 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-04-18 23:02:40 +00:00
|
|
|
export function BinaryResolutionOrChance(props: {
|
|
|
|
contract: FullContract<DPM | CPMM, Binary>
|
2022-01-02 20:53:42 +00:00
|
|
|
large?: boolean
|
|
|
|
className?: string
|
|
|
|
}) {
|
2022-02-14 21:33:47 +00:00
|
|
|
const { contract, large, className } = props
|
2022-04-18 23:02:40 +00:00
|
|
|
const { resolution } = contract
|
2022-05-16 23:15:22 +00:00
|
|
|
const textColor = `text-${getColor(contract)}`
|
2022-02-14 21:33:47 +00:00
|
|
|
|
2022-01-02 20:53:42 +00:00
|
|
|
return (
|
|
|
|
<Col className={clsx(large ? 'text-4xl' : 'text-3xl', className)}>
|
|
|
|
{resolution ? (
|
|
|
|
<>
|
|
|
|
<div
|
|
|
|
className={clsx('text-gray-500', large ? 'text-xl' : 'text-base')}
|
|
|
|
>
|
|
|
|
Resolved
|
|
|
|
</div>
|
2022-04-18 23:02:40 +00:00
|
|
|
<BinaryContractOutcomeLabel
|
|
|
|
contract={contract}
|
|
|
|
resolution={resolution}
|
|
|
|
/>
|
2022-01-02 20:53:42 +00:00
|
|
|
</>
|
|
|
|
) : (
|
2022-04-18 23:02:40 +00:00
|
|
|
<>
|
2022-05-16 23:15:22 +00:00
|
|
|
<div className={textColor}>{getBinaryProbPercent(contract)}</div>
|
|
|
|
<div className={clsx(textColor, large ? 'text-xl' : 'text-base')}>
|
2022-04-18 23:02:40 +00:00
|
|
|
chance
|
|
|
|
</div>
|
|
|
|
</>
|
2022-01-02 20:53:42 +00:00
|
|
|
)}
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-04-19 02:44:31 +00:00
|
|
|
export function FreeResponseResolutionOrChance(props: {
|
2022-04-18 23:02:40 +00:00
|
|
|
contract: FreeResponseContract
|
|
|
|
truncate: 'short' | 'long' | 'none'
|
2022-04-20 03:31:41 +00:00
|
|
|
className?: string
|
2022-04-18 23:02:40 +00:00
|
|
|
}) {
|
2022-04-20 03:31:41 +00:00
|
|
|
const { contract, truncate, className } = props
|
2022-04-19 02:44:31 +00:00
|
|
|
const { resolution } = contract
|
|
|
|
|
|
|
|
const topAnswer = getTopAnswer(contract)
|
2022-05-16 23:15:22 +00:00
|
|
|
const textColor = `text-${getColor(contract)}`
|
2022-04-19 02:44:31 +00:00
|
|
|
|
2022-04-18 23:02:40 +00:00
|
|
|
return (
|
2022-04-20 03:31:41 +00:00
|
|
|
<Col className={clsx(resolution ? 'text-3xl' : 'text-xl', className)}>
|
2022-04-19 02:44:31 +00:00
|
|
|
{resolution ? (
|
|
|
|
<>
|
|
|
|
<div className={clsx('text-base text-gray-500')}>Resolved</div>
|
|
|
|
<FreeResponseOutcomeLabel
|
|
|
|
contract={contract}
|
|
|
|
resolution={resolution}
|
|
|
|
truncate={truncate}
|
2022-04-20 03:31:41 +00:00
|
|
|
answerClassName="text-xl"
|
2022-04-19 02:44:31 +00:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
topAnswer && (
|
2022-04-20 03:31:41 +00:00
|
|
|
<Row className="items-center gap-6">
|
|
|
|
<AnswerLabel
|
|
|
|
className="!text-gray-600"
|
|
|
|
answer={topAnswer}
|
|
|
|
truncate={truncate}
|
|
|
|
/>
|
2022-05-16 23:15:22 +00:00
|
|
|
<Col className={clsx('text-3xl', textColor)}>
|
2022-04-19 02:44:31 +00:00
|
|
|
<div>
|
|
|
|
{formatPercent(getOutcomeProbability(contract, topAnswer.id))}
|
|
|
|
</div>
|
|
|
|
<div className="text-base">chance</div>
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
)
|
|
|
|
)}
|
2022-04-18 23:02:40 +00:00
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
2022-05-19 17:42:03 +00:00
|
|
|
|
|
|
|
export function NumericResolutionOrExpectation(props: {
|
|
|
|
contract: NumericContract
|
|
|
|
className?: string
|
|
|
|
}) {
|
|
|
|
const { contract, className } = props
|
|
|
|
const { resolution } = contract
|
2022-05-21 20:26:34 +00:00
|
|
|
const textColor = `text-${getColor(contract)}`
|
2022-05-19 17:42:03 +00:00
|
|
|
|
|
|
|
const resolutionValue =
|
|
|
|
contract.resolutionValue ?? getValueFromBucket(resolution ?? '', contract)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Col className={clsx(resolution ? 'text-3xl' : 'text-xl', className)}>
|
|
|
|
{resolution ? (
|
|
|
|
<>
|
|
|
|
<div className={clsx('text-base text-gray-500')}>Resolved</div>
|
|
|
|
<div className="text-blue-400">{resolutionValue}</div>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
2022-05-21 20:26:34 +00:00
|
|
|
<div className={clsx('text-3xl', textColor)}>
|
2022-05-20 19:25:32 +00:00
|
|
|
{formatLargeNumber(getExpectedValue(contract))}
|
2022-05-19 17:42:03 +00:00
|
|
|
</div>
|
2022-05-21 20:26:34 +00:00
|
|
|
<div className={clsx('text-base', textColor)}>expected</div>
|
2022-05-19 17:42:03 +00:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|