Working tree is now the canonical repo. Off-chain play-money + trustless on-chain (FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution, mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
321 lines
8.6 KiB
TypeScript
321 lines
8.6 KiB
TypeScript
import clsx from 'clsx'
|
|
import { scaleLinear, scaleTime } from 'd3-scale'
|
|
import { first, last, maxBy, minBy } from 'lodash'
|
|
import { useMemo } from 'react'
|
|
|
|
import { Bet } from 'common/bet'
|
|
import { getAnswerProbability, getProbability } from 'common/calculate'
|
|
import { HistoryPoint } from 'common/chart'
|
|
import { ChartPosition } from 'common/chart-position'
|
|
import { BinaryContract, CPMMMultiContract } from 'common/contract'
|
|
import { YES_GRAPH_COLOR } from 'common/envs/constants'
|
|
import { ChartAnnotation } from 'common/supabase/chart-annotations'
|
|
import { buildArray } from 'common/util/array'
|
|
import { SizedContainer } from 'web/components/sized-container'
|
|
import { ChartAnnotations } from '../chart-annotations'
|
|
import { ChartPositionsCarousel } from '../chart-positions'
|
|
import {
|
|
SingleValueHistoryChart,
|
|
SingleValueStackedHistoryChart,
|
|
} from '../generic-charts'
|
|
import {
|
|
formatPct,
|
|
getEndDate,
|
|
getRightmostVisibleDate,
|
|
PointerMode,
|
|
ZoomParams,
|
|
} from '../helpers'
|
|
import { getAnswerColor } from './choice'
|
|
import {
|
|
MultiBinaryChartTooltip,
|
|
SingleContractChartTooltip,
|
|
SingleContractPoint,
|
|
} from './single-value'
|
|
|
|
export const GRAPH_Y_DIVISOR = 20
|
|
|
|
const getVisibleYRange = (params: {
|
|
data: SingleContractPoint[]
|
|
zoomY?: boolean
|
|
zoomParams?: ZoomParams
|
|
}) => {
|
|
const { data, zoomY, zoomParams } = params
|
|
|
|
if (!zoomY || !zoomParams?.xScale) return [0, 1]
|
|
|
|
const [minXDate, maxXDate] = zoomParams?.viewXScale.domain() ?? [null, null]
|
|
const [fullMin, fullMax] = zoomParams.xScale.domain()
|
|
|
|
if (
|
|
minXDate.getTime() === fullMin.getTime() &&
|
|
maxXDate.getTime() === fullMax.getTime()
|
|
) {
|
|
return [0, 1]
|
|
}
|
|
|
|
const minYValue = zoomY
|
|
? Math.max(
|
|
Math.floor((minBy(data, 'y')?.y ?? 0) * GRAPH_Y_DIVISOR) /
|
|
GRAPH_Y_DIVISOR,
|
|
0
|
|
)
|
|
: 0
|
|
const maxYValue = zoomY
|
|
? Math.min(
|
|
Math.ceil((maxBy(data, 'y')?.y ?? 1) * GRAPH_Y_DIVISOR) /
|
|
GRAPH_Y_DIVISOR,
|
|
1
|
|
)
|
|
: 1
|
|
|
|
return [minYValue, maxYValue]
|
|
}
|
|
|
|
export const BinaryContractChart = (props: {
|
|
contract: BinaryContract
|
|
betPoints: SingleContractPoint[]
|
|
width: number
|
|
height: number
|
|
zoomParams?: ZoomParams
|
|
showZoomer?: boolean
|
|
hoveredAnnotation?: number | null
|
|
setHoveredAnnotation?: (id: number | null) => void
|
|
pointerMode?: PointerMode
|
|
chartAnnotations?: ChartAnnotation[]
|
|
chartPositions?: ChartPosition[]
|
|
hoveredChartPosition?: ChartPosition | null
|
|
setHoveredChartPosition?: (position: ChartPosition | null) => void
|
|
graphColor?: string
|
|
noWatermark?: boolean
|
|
zoomY?: boolean
|
|
startTime?: number
|
|
}) => {
|
|
const {
|
|
contract,
|
|
width,
|
|
height,
|
|
zoomParams,
|
|
betPoints,
|
|
showZoomer,
|
|
hoveredAnnotation,
|
|
setHoveredAnnotation,
|
|
pointerMode = 'zoom',
|
|
chartAnnotations,
|
|
chartPositions,
|
|
hoveredChartPosition,
|
|
setHoveredChartPosition,
|
|
graphColor,
|
|
noWatermark,
|
|
zoomY,
|
|
startTime,
|
|
} = props
|
|
|
|
const start =
|
|
startTime ?? Math.min(first(betPoints)?.x ?? Infinity, contract.createdTime)
|
|
const end = getEndDate(contract)
|
|
const endP = getProbability(contract as BinaryContract)
|
|
const stringifiedBetPoints = JSON.stringify(betPoints)
|
|
|
|
const now = useMemo(() => Date.now(), [stringifiedBetPoints, endP])
|
|
const data = useMemo(() => {
|
|
return buildArray(...betPoints, { x: end ?? now, y: endP })
|
|
}, [end, endP, betPoints])
|
|
|
|
const rightmostDate = getRightmostVisibleDate(end, last(betPoints)?.x, now)
|
|
|
|
const xScale = scaleTime([start, rightmostDate], [0, width])
|
|
|
|
const [minYValue, maxYValue] = getVisibleYRange({
|
|
data,
|
|
zoomY,
|
|
zoomParams,
|
|
})
|
|
const yScale = scaleLinear([minYValue, maxYValue], [height, 0])
|
|
|
|
return (
|
|
<SingleValueHistoryChart
|
|
w={width}
|
|
h={height}
|
|
xScale={xScale}
|
|
yScale={yScale}
|
|
rightmostDate={rightmostDate}
|
|
zoomParams={zoomParams}
|
|
showZoomer={showZoomer}
|
|
yKind="percent"
|
|
data={data}
|
|
color={graphColor ?? YES_GRAPH_COLOR}
|
|
Tooltip={(props) => (
|
|
<SingleContractChartTooltip
|
|
ttProps={props}
|
|
xScale={zoomParams?.viewXScale ?? xScale}
|
|
formatY={formatPct}
|
|
/>
|
|
)}
|
|
contractId={contract.id}
|
|
hoveredAnnotation={hoveredAnnotation}
|
|
setHoveredAnnotation={setHoveredAnnotation}
|
|
pointerMode={pointerMode}
|
|
chartAnnotations={chartAnnotations}
|
|
chartPositions={chartPositions}
|
|
hoveredChartPosition={hoveredChartPosition}
|
|
setHoveredChartPosition={setHoveredChartPosition}
|
|
noWatermark={noWatermark}
|
|
/>
|
|
)
|
|
}
|
|
|
|
export function SizedBinaryChart(props: {
|
|
showZoomer?: boolean
|
|
zoomParams?: ZoomParams
|
|
showAnnotations?: boolean
|
|
betPoints: HistoryPoint<Partial<Bet>>[]
|
|
contract: BinaryContract
|
|
className?: string
|
|
size?: 'sm' | 'md'
|
|
color?: string
|
|
hoveredAnnotation?: number | null
|
|
setHoveredAnnotation?: (id: number | null) => void
|
|
pointerMode?: PointerMode
|
|
chartAnnotations?: ChartAnnotation[]
|
|
chartPositions?: ChartPosition[]
|
|
hoveredChartPosition?: ChartPosition | null
|
|
setHoveredChartPosition?: (position: ChartPosition | null) => void
|
|
noWatermark?: boolean
|
|
zoomY?: boolean
|
|
startTime?: number
|
|
}) {
|
|
const {
|
|
showZoomer,
|
|
zoomParams,
|
|
showAnnotations,
|
|
betPoints,
|
|
contract,
|
|
className,
|
|
size = 'md',
|
|
pointerMode,
|
|
setHoveredAnnotation,
|
|
hoveredAnnotation,
|
|
chartAnnotations,
|
|
chartPositions,
|
|
hoveredChartPosition,
|
|
setHoveredChartPosition,
|
|
noWatermark,
|
|
zoomY,
|
|
startTime,
|
|
} = props
|
|
|
|
return (
|
|
<>
|
|
<SizedContainer
|
|
className={clsx(
|
|
showZoomer ? 'mb-12' : '',
|
|
'w-full pb-3 pr-10',
|
|
size == 'sm' ? 'h-[100px]' : 'h-[150px] sm:h-[250px]',
|
|
className
|
|
)}
|
|
>
|
|
{(w, h) => (
|
|
<BinaryContractChart
|
|
width={w}
|
|
height={h}
|
|
betPoints={betPoints}
|
|
showZoomer={showZoomer}
|
|
zoomParams={zoomParams}
|
|
contract={contract}
|
|
hoveredAnnotation={hoveredAnnotation}
|
|
setHoveredAnnotation={setHoveredAnnotation}
|
|
pointerMode={pointerMode}
|
|
chartAnnotations={chartAnnotations}
|
|
chartPositions={chartPositions}
|
|
hoveredChartPosition={hoveredChartPosition}
|
|
setHoveredChartPosition={setHoveredChartPosition}
|
|
noWatermark={noWatermark}
|
|
zoomY={zoomY}
|
|
startTime={startTime}
|
|
/>
|
|
)}
|
|
</SizedContainer>
|
|
{showAnnotations && chartAnnotations?.length ? (
|
|
<ChartAnnotations
|
|
annotations={chartAnnotations}
|
|
hoveredAnnotation={hoveredAnnotation}
|
|
setHoveredAnnotation={setHoveredAnnotation}
|
|
/>
|
|
) : null}
|
|
{chartPositions && chartPositions.length > 0 ? (
|
|
<ChartPositionsCarousel
|
|
positions={chartPositions}
|
|
hoveredPosition={hoveredChartPosition}
|
|
setHoveredPosition={setHoveredChartPosition}
|
|
/>
|
|
) : null}
|
|
</>
|
|
)
|
|
}
|
|
|
|
export const MultiBinaryChart = (props: {
|
|
contract: CPMMMultiContract
|
|
betPoints: SingleContractPoint[]
|
|
width: number
|
|
height: number
|
|
zoomParams?: ZoomParams
|
|
showZoomer?: boolean
|
|
zoomY?: boolean
|
|
}) => {
|
|
const { contract, width, height, zoomParams, betPoints, showZoomer, zoomY } =
|
|
props
|
|
|
|
const start = first(betPoints)?.x ?? contract.createdTime
|
|
const end = getEndDate(contract)
|
|
const mainBinaryMCAnswer = contract.answers[0]
|
|
const endP = getAnswerProbability(
|
|
contract as CPMMMultiContract,
|
|
mainBinaryMCAnswer.id
|
|
)
|
|
|
|
const [bottom, top] = contract.answers.map(getAnswerColor)
|
|
const stringifiedBetPoints = JSON.stringify(betPoints)
|
|
const now = useMemo(() => Date.now(), [stringifiedBetPoints, endP])
|
|
|
|
const data = useMemo(() => {
|
|
return [{ x: start, y: 0.5 }, ...betPoints, { x: end ?? now, y: endP }]
|
|
}, [end, endP, betPoints])
|
|
|
|
const rightmostDate = getRightmostVisibleDate(end, last(betPoints)?.x, now)
|
|
|
|
const xScale = scaleTime([start, rightmostDate], [0, width])
|
|
|
|
const [minYValue, maxYValue] = getVisibleYRange({
|
|
data,
|
|
zoomY,
|
|
zoomParams,
|
|
})
|
|
|
|
const yScale = scaleLinear([minYValue, maxYValue], [height, 0])
|
|
|
|
return (
|
|
<SingleValueStackedHistoryChart
|
|
w={width}
|
|
h={height}
|
|
xScale={xScale}
|
|
yScale={yScale}
|
|
rightmostDate={rightmostDate}
|
|
zoomParams={zoomParams}
|
|
showZoomer={showZoomer}
|
|
data={data}
|
|
topColor={top}
|
|
bottomColor={bottom}
|
|
Tooltip={(props) => (
|
|
<MultiBinaryChartTooltip
|
|
ttProps={props}
|
|
xScale={zoomParams?.viewXScale ?? xScale}
|
|
topColor={top}
|
|
topLabel={contract.answers[1].text}
|
|
bottomColor={bottom}
|
|
bottomLabel={contract.answers[0].text}
|
|
/>
|
|
)}
|
|
/>
|
|
)
|
|
}
|