2022-03-15 19:42:04 +00:00
|
|
|
import { DatumValue } from '@nivo/core'
|
|
|
|
import { ResponsiveLine } from '@nivo/line'
|
|
|
|
import dayjs from 'dayjs'
|
2022-05-22 08:36:05 +00:00
|
|
|
import { groupBy, sortBy, sumBy } from 'lodash'
|
2022-04-06 16:45:34 +00:00
|
|
|
import { memo } from 'react'
|
2022-03-15 19:42:04 +00:00
|
|
|
|
2022-05-09 13:04:36 +00:00
|
|
|
import { Bet } from 'common/bet'
|
2022-06-01 02:42:35 +00:00
|
|
|
import { FreeResponseContract } from 'common/contract'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { getOutcomeProbability } from 'common/calculate'
|
|
|
|
import { useWindowSize } from 'web/hooks/use-window-size'
|
2022-03-15 19:42:04 +00:00
|
|
|
|
2022-03-16 23:47:12 +00:00
|
|
|
const NUM_LINES = 6
|
|
|
|
|
2022-04-06 16:45:34 +00:00
|
|
|
export const AnswersGraph = memo(function AnswersGraph(props: {
|
2022-06-01 02:42:35 +00:00
|
|
|
contract: FreeResponseContract
|
2022-03-15 22:27:51 +00:00
|
|
|
bets: Bet[]
|
2022-03-24 17:03:08 +00:00
|
|
|
height?: number
|
2022-03-15 22:27:51 +00:00
|
|
|
}) {
|
2022-06-10 17:23:27 +00:00
|
|
|
const { contract, bets, height } = props
|
2022-03-16 23:47:12 +00:00
|
|
|
const { createdTime, resolutionTime, closeTime, answers } = contract
|
2022-03-15 19:42:04 +00:00
|
|
|
|
|
|
|
const { probsByOutcome, sortedOutcomes } = computeProbsByOutcome(
|
|
|
|
bets,
|
2022-03-15 22:27:51 +00:00
|
|
|
contract
|
2022-03-15 19:42:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const isClosed = !!closeTime && Date.now() > closeTime
|
|
|
|
const latestTime = dayjs(
|
|
|
|
resolutionTime && isClosed
|
|
|
|
? Math.min(resolutionTime, closeTime)
|
|
|
|
: isClosed
|
|
|
|
? closeTime
|
|
|
|
: resolutionTime ?? Date.now()
|
|
|
|
)
|
|
|
|
|
|
|
|
const { width } = useWindowSize()
|
|
|
|
|
2022-04-18 23:02:40 +00:00
|
|
|
const isLargeWidth = !width || width > 800
|
|
|
|
const labelLength = isLargeWidth ? 50 : 20
|
2022-03-15 19:42:04 +00:00
|
|
|
|
2022-03-16 23:47:12 +00:00
|
|
|
const endTime =
|
|
|
|
resolutionTime || isClosed
|
|
|
|
? latestTime.valueOf()
|
|
|
|
: // Add a fake datapoint in future so the line continues horizontally
|
|
|
|
// to the right.
|
|
|
|
latestTime.add(1, 'month').valueOf()
|
2022-03-15 19:42:04 +00:00
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const times = sortBy([
|
2022-03-16 23:47:12 +00:00
|
|
|
createdTime,
|
2022-03-15 19:42:04 +00:00
|
|
|
...bets.map((bet) => bet.createdTime),
|
2022-03-16 23:47:12 +00:00
|
|
|
endTime,
|
2022-03-15 19:42:04 +00:00
|
|
|
])
|
|
|
|
const dateTimes = times.map((time) => new Date(time))
|
|
|
|
|
2022-03-16 23:47:12 +00:00
|
|
|
const data = sortedOutcomes.map((outcome) => {
|
|
|
|
const betProbs = probsByOutcome[outcome]
|
|
|
|
// Add extra point for contract start and end.
|
|
|
|
const probs = [0, ...betProbs, betProbs[betProbs.length - 1]]
|
2022-03-15 19:42:04 +00:00
|
|
|
|
|
|
|
const points = probs.map((prob, i) => ({
|
|
|
|
x: dateTimes[i],
|
|
|
|
y: Math.round(prob * 100),
|
|
|
|
}))
|
|
|
|
|
|
|
|
const answer =
|
|
|
|
answers?.find((answer) => answer.id === outcome)?.text ?? 'None'
|
|
|
|
const answerText =
|
|
|
|
answer.slice(0, labelLength) + (answer.length > labelLength ? '...' : '')
|
|
|
|
|
2022-04-18 23:02:40 +00:00
|
|
|
return { id: answerText, data: points }
|
2022-03-15 19:42:04 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
data.reverse()
|
|
|
|
|
|
|
|
const yTickValues = [0, 25, 50, 75, 100]
|
|
|
|
|
2022-04-18 23:02:40 +00:00
|
|
|
const numXTickValues = isLargeWidth ? 5 : 2
|
2022-03-15 19:42:04 +00:00
|
|
|
const hoursAgo = latestTime.subtract(5, 'hours')
|
|
|
|
const startDate = dayjs(contract.createdTime).isBefore(hoursAgo)
|
|
|
|
? new Date(contract.createdTime)
|
|
|
|
: hoursAgo.toDate()
|
|
|
|
|
|
|
|
const lessThanAWeek = dayjs(startDate).add(1, 'week').isAfter(latestTime)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
2022-04-18 23:02:40 +00:00
|
|
|
className="w-full"
|
|
|
|
style={{ height: height ?? (isLargeWidth ? 350 : 250) }}
|
2022-03-15 19:42:04 +00:00
|
|
|
>
|
|
|
|
<ResponsiveLine
|
|
|
|
data={data}
|
2022-03-16 23:47:12 +00:00
|
|
|
yScale={{ min: 0, max: 100, type: 'linear', stacked: true }}
|
2022-03-15 19:42:04 +00:00
|
|
|
yFormat={formatPercent}
|
|
|
|
gridYValues={yTickValues}
|
|
|
|
axisLeft={{
|
|
|
|
tickValues: yTickValues,
|
|
|
|
format: formatPercent,
|
|
|
|
}}
|
|
|
|
xScale={{
|
|
|
|
type: 'time',
|
|
|
|
min: startDate,
|
|
|
|
max: latestTime.toDate(),
|
|
|
|
}}
|
|
|
|
xFormat={(d) => formatTime(+d.valueOf(), lessThanAWeek)}
|
|
|
|
axisBottom={{
|
|
|
|
tickValues: numXTickValues,
|
|
|
|
format: (time) => formatTime(+time, lessThanAWeek),
|
|
|
|
}}
|
2022-03-15 22:20:37 +00:00
|
|
|
colors={{ scheme: 'pastel1' }}
|
2022-03-15 19:42:04 +00:00
|
|
|
pointSize={0}
|
|
|
|
enableSlices="x"
|
|
|
|
enableGridX={!!width && width >= 800}
|
|
|
|
enableArea
|
2022-03-15 22:20:37 +00:00
|
|
|
areaOpacity={1}
|
2022-03-15 19:42:04 +00:00
|
|
|
margin={{ top: 20, right: 28, bottom: 22, left: 40 }}
|
2022-04-18 23:02:40 +00:00
|
|
|
legends={[
|
|
|
|
{
|
|
|
|
anchor: 'top-left',
|
|
|
|
direction: 'column',
|
|
|
|
justify: false,
|
|
|
|
translateX: isLargeWidth ? 5 : 2,
|
|
|
|
translateY: 0,
|
|
|
|
itemsSpacing: 0,
|
|
|
|
itemTextColor: 'black',
|
|
|
|
itemDirection: 'left-to-right',
|
|
|
|
itemWidth: isLargeWidth ? 288 : 138,
|
|
|
|
itemHeight: 20,
|
|
|
|
itemBackground: 'white',
|
|
|
|
itemOpacity: 0.9,
|
|
|
|
symbolSize: 12,
|
|
|
|
effects: [
|
|
|
|
{
|
|
|
|
on: 'hover',
|
|
|
|
style: {
|
|
|
|
itemBackground: 'rgba(255, 255, 255, 1)',
|
|
|
|
itemOpacity: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]}
|
2022-03-15 19:42:04 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)
|
2022-04-06 16:45:34 +00:00
|
|
|
})
|
2022-03-15 19:42:04 +00:00
|
|
|
|
|
|
|
function formatPercent(y: DatumValue) {
|
|
|
|
return `${Math.round(+y.toString())}%`
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatTime(time: number, includeTime: boolean) {
|
|
|
|
const d = dayjs(time)
|
|
|
|
|
|
|
|
if (d.isSame(Date.now(), 'day')) return d.format('ha')
|
|
|
|
|
|
|
|
if (includeTime) return dayjs(time).format('MMM D, ha')
|
|
|
|
|
|
|
|
return dayjs(time).format('MMM D')
|
|
|
|
}
|
|
|
|
|
2022-06-01 02:42:35 +00:00
|
|
|
const computeProbsByOutcome = (bets: Bet[], contract: FreeResponseContract) => {
|
2022-03-22 00:48:48 +00:00
|
|
|
const { totalBets } = contract
|
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const betsByOutcome = groupBy(bets, (bet) => bet.outcome)
|
2022-03-15 19:42:04 +00:00
|
|
|
const outcomes = Object.keys(betsByOutcome).filter((outcome) => {
|
|
|
|
const maxProb = Math.max(
|
|
|
|
...betsByOutcome[outcome].map((bet) => bet.probAfter)
|
|
|
|
)
|
2022-03-22 00:48:48 +00:00
|
|
|
return outcome !== '0' && maxProb > 0.02 && totalBets[outcome] > 0.000000001
|
2022-03-15 19:42:04 +00:00
|
|
|
})
|
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const trackedOutcomes = sortBy(
|
2022-03-15 19:42:04 +00:00
|
|
|
outcomes,
|
2022-03-15 22:27:51 +00:00
|
|
|
(outcome) => -1 * getOutcomeProbability(contract, outcome)
|
2022-03-16 23:47:12 +00:00
|
|
|
).slice(0, NUM_LINES)
|
2022-03-15 19:42:04 +00:00
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const probsByOutcome = Object.fromEntries(
|
2022-03-16 23:47:12 +00:00
|
|
|
trackedOutcomes.map((outcome) => [outcome, [] as number[]])
|
2022-03-15 19:42:04 +00:00
|
|
|
)
|
2022-05-22 08:36:05 +00:00
|
|
|
const sharesByOutcome = Object.fromEntries(
|
2022-03-15 19:42:04 +00:00
|
|
|
Object.keys(betsByOutcome).map((outcome) => [outcome, 0])
|
|
|
|
)
|
|
|
|
|
|
|
|
for (const bet of bets) {
|
|
|
|
const { outcome, shares } = bet
|
|
|
|
sharesByOutcome[outcome] += shares
|
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const sharesSquared = sumBy(
|
2022-03-15 19:42:04 +00:00
|
|
|
Object.values(sharesByOutcome).map((shares) => shares ** 2)
|
|
|
|
)
|
|
|
|
|
|
|
|
for (const outcome of trackedOutcomes) {
|
|
|
|
probsByOutcome[outcome].push(
|
|
|
|
sharesByOutcome[outcome] ** 2 / sharesSquared
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { probsByOutcome, sortedOutcomes: trackedOutcomes }
|
|
|
|
}
|