2022-09-05 23:09:01 +00:00
|
|
|
import clsx from 'clsx'
|
2022-10-06 01:02:24 +00:00
|
|
|
import { sortBy } from 'lodash'
|
2022-10-11 05:32:55 +00:00
|
|
|
import { filterDefined } from 'common/util/array'
|
|
|
|
import { ContractMetrics } from 'common/calculate-metrics'
|
|
|
|
import { CPMMBinaryContract, CPMMContract } from 'common/contract'
|
|
|
|
import { formatPercent } from 'common/util/format'
|
2022-09-07 06:18:11 +00:00
|
|
|
import { Col } from '../layout/col'
|
2022-09-12 05:39:04 +00:00
|
|
|
import { LoadingIndicator } from '../loading-indicator'
|
2022-10-06 01:02:24 +00:00
|
|
|
import { ContractCardProbChange } from './contract-card'
|
2022-10-13 14:18:16 +00:00
|
|
|
import { formatNumericProbability } from 'common/pseudo-numeric'
|
2022-09-05 23:09:01 +00:00
|
|
|
|
2022-10-11 05:32:55 +00:00
|
|
|
export function ProfitChangeTable(props: {
|
|
|
|
contracts: CPMMBinaryContract[]
|
|
|
|
metrics: ContractMetrics[]
|
|
|
|
}) {
|
|
|
|
const { contracts, metrics } = props
|
|
|
|
|
|
|
|
const contractProfit = metrics.map(
|
|
|
|
(m) => [m.contractId, m.from?.day.profit ?? 0] as const
|
|
|
|
)
|
|
|
|
|
|
|
|
const positiveProfit = sortBy(
|
|
|
|
contractProfit.filter(([, profit]) => profit > 0),
|
|
|
|
([, profit]) => profit
|
|
|
|
).reverse()
|
|
|
|
const positive = filterDefined(
|
|
|
|
positiveProfit.map(([contractId]) =>
|
|
|
|
contracts.find((c) => c.id === contractId)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
const negativeProfit = sortBy(
|
|
|
|
contractProfit.filter(([, profit]) => profit < 0),
|
|
|
|
([, profit]) => profit
|
|
|
|
)
|
|
|
|
const negative = filterDefined(
|
|
|
|
negativeProfit.map(([contractId]) =>
|
|
|
|
contracts.find((c) => c.id === contractId)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if (positive.length === 0 && negative.length === 0)
|
|
|
|
return <div className="px-4 text-gray-500">None</div>
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Col className="mb-4 w-full gap-4 rounded-lg md:flex-row">
|
|
|
|
<Col className="flex-1">
|
|
|
|
{positive.map((contract) => (
|
|
|
|
<ContractCardProbChange
|
|
|
|
key={contract.id}
|
|
|
|
contract={contract}
|
|
|
|
showPosition
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</Col>
|
|
|
|
<Col className="flex-1">
|
|
|
|
{negative.map((contract) => (
|
|
|
|
<ContractCardProbChange
|
|
|
|
key={contract.id}
|
|
|
|
contract={contract}
|
|
|
|
showPosition
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</Col>
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-09-12 05:39:04 +00:00
|
|
|
export function ProbChangeTable(props: {
|
2022-09-22 21:57:48 +00:00
|
|
|
changes: CPMMContract[] | undefined
|
2022-09-16 21:12:24 +00:00
|
|
|
full?: boolean
|
2022-09-12 05:39:04 +00:00
|
|
|
}) {
|
2022-09-16 21:12:24 +00:00
|
|
|
const { changes, full } = props
|
2022-09-12 05:39:04 +00:00
|
|
|
|
|
|
|
if (!changes) return <LoadingIndicator />
|
2022-09-05 23:09:01 +00:00
|
|
|
|
2022-09-29 16:42:16 +00:00
|
|
|
const descendingChanges = sortBy(changes, (c) => c.probChanges.day).reverse()
|
|
|
|
const ascendingChanges = sortBy(changes, (c) => c.probChanges.day)
|
2022-09-05 23:09:01 +00:00
|
|
|
|
2022-09-18 00:47:04 +00:00
|
|
|
const threshold = 0.01
|
2022-09-29 16:42:16 +00:00
|
|
|
const positiveAboveThreshold = descendingChanges.filter(
|
2022-09-18 00:47:04 +00:00
|
|
|
(c) => c.probChanges.day > threshold
|
2022-09-12 05:39:04 +00:00
|
|
|
)
|
2022-09-29 16:42:16 +00:00
|
|
|
const negativeAboveThreshold = ascendingChanges.filter(
|
2022-09-18 00:47:04 +00:00
|
|
|
(c) => c.probChanges.day < threshold
|
|
|
|
)
|
|
|
|
const maxRows = Math.min(
|
|
|
|
positiveAboveThreshold.length,
|
|
|
|
negativeAboveThreshold.length
|
|
|
|
)
|
|
|
|
const rows = full ? maxRows : Math.min(3, maxRows)
|
2022-09-05 23:09:01 +00:00
|
|
|
|
2022-09-18 00:47:04 +00:00
|
|
|
const filteredPositiveChanges = positiveAboveThreshold.slice(0, rows)
|
|
|
|
const filteredNegativeChanges = negativeAboveThreshold.slice(0, rows)
|
2022-09-05 23:09:01 +00:00
|
|
|
|
2022-09-12 05:39:04 +00:00
|
|
|
if (rows === 0) return <div className="px-4 text-gray-500">None</div>
|
2022-09-05 23:09:01 +00:00
|
|
|
|
|
|
|
return (
|
2022-10-06 01:02:24 +00:00
|
|
|
<Col className="mb-4 w-full gap-4 rounded-lg md:flex-row">
|
2022-10-06 19:01:00 +00:00
|
|
|
<Col className="flex-1">
|
2022-09-12 05:39:04 +00:00
|
|
|
{filteredPositiveChanges.map((contract) => (
|
2022-10-11 05:32:55 +00:00
|
|
|
<ContractCardProbChange
|
|
|
|
key={contract.id}
|
|
|
|
contract={contract}
|
|
|
|
showPosition
|
|
|
|
/>
|
2022-09-12 05:39:04 +00:00
|
|
|
))}
|
|
|
|
</Col>
|
2022-10-06 19:01:00 +00:00
|
|
|
<Col className="flex-1">
|
2022-09-12 05:39:04 +00:00
|
|
|
{filteredNegativeChanges.map((contract) => (
|
2022-10-11 05:32:55 +00:00
|
|
|
<ContractCardProbChange
|
|
|
|
key={contract.id}
|
|
|
|
contract={contract}
|
|
|
|
showPosition
|
|
|
|
/>
|
2022-09-12 05:39:04 +00:00
|
|
|
))}
|
2022-09-07 06:18:11 +00:00
|
|
|
</Col>
|
2022-09-08 06:36:34 +00:00
|
|
|
</Col>
|
2022-09-05 23:09:01 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-10-13 14:18:16 +00:00
|
|
|
export function ProbOrNumericChange(props: {
|
2022-09-05 23:09:01 +00:00
|
|
|
contract: CPMMContract
|
|
|
|
className?: string
|
|
|
|
}) {
|
|
|
|
const { contract, className } = props
|
|
|
|
const {
|
2022-09-17 22:58:08 +00:00
|
|
|
prob,
|
2022-09-05 23:09:01 +00:00
|
|
|
probChanges: { day: change },
|
|
|
|
} = contract
|
2022-10-13 14:18:16 +00:00
|
|
|
const number =
|
|
|
|
contract.outcomeType === 'PSEUDO_NUMERIC'
|
|
|
|
? formatNumericProbability(prob, contract)
|
|
|
|
: null
|
2022-09-19 21:24:36 +00:00
|
|
|
|
2022-10-12 06:58:20 +00:00
|
|
|
const color = change >= 0 ? 'text-teal-500' : 'text-red-400'
|
2022-09-19 21:24:36 +00:00
|
|
|
|
2022-09-17 22:58:08 +00:00
|
|
|
return (
|
|
|
|
<Col className={clsx('flex flex-col items-end', className)}>
|
2022-09-19 21:24:36 +00:00
|
|
|
<div className="mb-0.5 mr-0.5 text-2xl">
|
2022-10-13 14:18:16 +00:00
|
|
|
{number ? number : formatPercent(Math.round(100 * prob) / 100)}
|
2022-09-19 21:24:36 +00:00
|
|
|
</div>
|
|
|
|
<div className={clsx('text-base', color)}>
|
|
|
|
{(change > 0 ? '+' : '') + (change * 100).toFixed(0) + '%'}
|
|
|
|
</div>
|
2022-09-17 22:58:08 +00:00
|
|
|
</Col>
|
|
|
|
)
|
2022-09-05 23:09:01 +00:00
|
|
|
}
|