2021-12-21 06:29:32 +00:00
|
|
|
import clsx from 'clsx'
|
2022-02-01 02:58:41 +00:00
|
|
|
import dayjs from 'dayjs'
|
2021-12-21 06:29:32 +00:00
|
|
|
import Link from 'next/link'
|
|
|
|
import { useState } from 'react'
|
2022-02-01 03:33:43 +00:00
|
|
|
import Textarea from 'react-expanding-textarea'
|
2022-03-15 22:27:51 +00:00
|
|
|
|
2022-05-09 13:04:36 +00:00
|
|
|
import { getProbability } from 'common/calculate'
|
|
|
|
import { Binary, CPMM, DPM, FullContract } from 'common/contract'
|
|
|
|
import { parseWordsAsTags } from 'common/util/parse'
|
|
|
|
import { BuyAmountInput } from 'web/components/amount-input'
|
|
|
|
import { InfoTooltip } from 'web/components/info-tooltip'
|
|
|
|
import { Col } from 'web/components/layout/col'
|
|
|
|
import { Row } from 'web/components/layout/row'
|
|
|
|
import { Spacer } from 'web/components/layout/spacer'
|
|
|
|
import { Linkify } from 'web/components/linkify'
|
|
|
|
import { Page } from 'web/components/page'
|
|
|
|
import { Title } from 'web/components/title'
|
|
|
|
import { useUser } from 'web/hooks/use-user'
|
|
|
|
import { createContract } from 'web/lib/firebase/api-call'
|
|
|
|
import { contractPath } from 'web/lib/firebase/contracts'
|
2022-01-05 06:07:36 +00:00
|
|
|
|
2021-12-21 06:29:32 +00:00
|
|
|
type Prediction = {
|
|
|
|
question: string
|
|
|
|
description: string
|
|
|
|
initialProb: number
|
|
|
|
createdUrl?: string
|
|
|
|
}
|
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
function toPrediction(contract: FullContract<DPM | CPMM, Binary>): Prediction {
|
|
|
|
const startProb = getProbability(contract)
|
2021-12-21 06:29:32 +00:00
|
|
|
return {
|
|
|
|
question: contract.question,
|
|
|
|
description: contract.description,
|
|
|
|
initialProb: startProb * 100,
|
2022-01-12 19:01:04 +00:00
|
|
|
createdUrl: contractPath(contract),
|
2021-12-21 06:29:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function PredictionRow(props: { prediction: Prediction }) {
|
|
|
|
const { prediction } = props
|
|
|
|
return (
|
2022-02-11 18:40:22 +00:00
|
|
|
<Row className="justify-between gap-4 p-4 hover:bg-gray-300">
|
2021-12-21 06:29:32 +00:00
|
|
|
<Col className="justify-between">
|
2022-02-11 18:40:22 +00:00
|
|
|
<div className="mb-2 font-medium text-indigo-700">
|
2021-12-21 06:29:32 +00:00
|
|
|
<Linkify text={prediction.question} />
|
|
|
|
</div>
|
2022-02-11 18:40:22 +00:00
|
|
|
<div className="text-sm text-gray-500">{prediction.description}</div>
|
2021-12-21 06:29:32 +00:00
|
|
|
</Col>
|
|
|
|
{/* Initial probability */}
|
|
|
|
<div className="ml-auto">
|
|
|
|
<div className="text-3xl">
|
|
|
|
<div className="text-primary">
|
|
|
|
{prediction.initialProb.toFixed(0)}%
|
|
|
|
<div className="text-lg">chance</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{/* Current probability; hidden for now */}
|
|
|
|
{/* <div>
|
|
|
|
<div className="text-3xl">
|
|
|
|
<div className="text-primary">
|
|
|
|
{prediction.initialProb}%<div className="text-lg">chance</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div> */}
|
|
|
|
</Row>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function PredictionList(props: { predictions: Prediction[] }) {
|
|
|
|
const { predictions } = props
|
|
|
|
return (
|
2022-02-11 18:40:22 +00:00
|
|
|
<Col className="divide-y divide-gray-300 rounded-md border border-gray-300">
|
2021-12-21 06:29:32 +00:00
|
|
|
{predictions.map((prediction) =>
|
|
|
|
prediction.createdUrl ? (
|
|
|
|
<Link href={prediction.createdUrl}>
|
|
|
|
<a>
|
|
|
|
<PredictionRow
|
|
|
|
key={prediction.question}
|
|
|
|
prediction={prediction}
|
|
|
|
/>
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
) : (
|
|
|
|
<PredictionRow key={prediction.question} prediction={prediction} />
|
|
|
|
)
|
|
|
|
)}
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const TEST_VALUE = `1. Biden approval rating (as per 538) is greater than 50%: 80%
|
|
|
|
2. Court packing is clearly going to happen (new justices don't have to be appointed by end of year): 5%
|
|
|
|
3. Yang is New York mayor: 80%
|
|
|
|
4. Newsom recalled as CA governor: 5%
|
|
|
|
5. At least $250 million in damage from BLM protests this year: 30%
|
|
|
|
6. Significant capital gains tax hike (above 30% for highest bracket): 20%`
|
|
|
|
|
|
|
|
export default function MakePredictions() {
|
|
|
|
const user = useUser()
|
|
|
|
const [predictionsString, setPredictionsString] = useState('')
|
|
|
|
const [description, setDescription] = useState('')
|
2022-02-01 03:13:37 +00:00
|
|
|
const [tags, setTags] = useState('')
|
2021-12-21 06:29:32 +00:00
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
2022-03-15 22:27:51 +00:00
|
|
|
const [createdContracts, setCreatedContracts] = useState<
|
|
|
|
FullContract<DPM | CPMM, Binary>[]
|
|
|
|
>([])
|
2021-12-21 06:29:32 +00:00
|
|
|
|
2022-02-01 02:58:41 +00:00
|
|
|
const [ante, setAnte] = useState<number | undefined>(100)
|
|
|
|
const [anteError, setAnteError] = useState<string | undefined>()
|
|
|
|
// By default, close the market a week from today
|
|
|
|
const weekFromToday = dayjs().add(7, 'day').format('YYYY-MM-DDT23:59')
|
|
|
|
const [closeDate, setCloseDate] = useState<undefined | string>(weekFromToday)
|
|
|
|
|
|
|
|
const closeTime = closeDate ? dayjs(closeDate).valueOf() : undefined
|
|
|
|
|
2021-12-21 06:29:32 +00:00
|
|
|
const bulkPlaceholder = `e.g.
|
|
|
|
${TEST_VALUE}
|
|
|
|
...
|
|
|
|
`
|
|
|
|
|
|
|
|
const predictions: Prediction[] = []
|
|
|
|
|
|
|
|
// Parse bulkContracts, then run createContract for each
|
|
|
|
const lines = predictionsString ? predictionsString.split('\n') : []
|
|
|
|
for (const line of lines) {
|
|
|
|
// Parse line with regex
|
|
|
|
const matches = line.match(/^(.*):\s*(\d+)%\s*$/) || ['', '', '']
|
|
|
|
const [_, question, prob] = matches
|
|
|
|
|
|
|
|
if (!question || !prob) {
|
|
|
|
console.error('Invalid prediction: ', line)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
predictions.push({
|
|
|
|
question,
|
|
|
|
description,
|
|
|
|
initialProb: parseInt(prob),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createContracts() {
|
|
|
|
if (!user) {
|
|
|
|
// TODO: Convey error with snackbar/toast
|
|
|
|
console.error('You need to be signed in!')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
setIsSubmitting(true)
|
|
|
|
for (const prediction of predictions) {
|
2022-05-20 21:58:14 +00:00
|
|
|
const contract = (await createContract({
|
2022-01-05 06:07:36 +00:00
|
|
|
question: prediction.question,
|
|
|
|
description: prediction.description,
|
|
|
|
initialProb: prediction.initialProb,
|
2022-02-01 02:58:41 +00:00
|
|
|
ante,
|
|
|
|
closeTime,
|
2022-02-01 03:13:37 +00:00
|
|
|
tags: parseWordsAsTags(tags),
|
2022-05-20 21:58:14 +00:00
|
|
|
})) as FullContract<DPM | CPMM, Binary>
|
2022-01-05 06:07:36 +00:00
|
|
|
|
2021-12-21 06:29:32 +00:00
|
|
|
setCreatedContracts((prev) => [...prev, contract])
|
|
|
|
}
|
|
|
|
setPredictionsString('')
|
|
|
|
setIsSubmitting(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Page>
|
|
|
|
<Title text="Make Predictions" />
|
2022-02-11 18:40:22 +00:00
|
|
|
<div className="w-full rounded-lg bg-gray-100 px-6 py-4 shadow-xl">
|
2021-12-21 06:29:32 +00:00
|
|
|
<form>
|
|
|
|
<div className="form-control">
|
|
|
|
<label className="label">
|
|
|
|
<span className="label-text">Prediction</span>
|
2022-02-11 18:40:22 +00:00
|
|
|
<div className="ml-1 text-sm text-gray-500">
|
2021-12-21 06:29:32 +00:00
|
|
|
One prediction per line, each formatted like "The sun will rise
|
|
|
|
tomorrow: 99%"
|
|
|
|
</div>
|
|
|
|
</label>
|
|
|
|
|
|
|
|
<textarea
|
2022-02-11 18:40:22 +00:00
|
|
|
className="textarea textarea-bordered h-60"
|
2021-12-21 06:29:32 +00:00
|
|
|
placeholder={bulkPlaceholder}
|
|
|
|
value={predictionsString}
|
|
|
|
onChange={(e) => setPredictionsString(e.target.value || '')}
|
|
|
|
></textarea>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
|
|
|
|
<Spacer h={4} />
|
|
|
|
|
|
|
|
<div className="form-control w-full">
|
|
|
|
<label className="label">
|
2022-02-01 02:58:41 +00:00
|
|
|
<span className="label-text">Description</span>
|
2021-12-21 06:29:32 +00:00
|
|
|
</label>
|
|
|
|
|
2022-02-01 03:33:43 +00:00
|
|
|
<Textarea
|
2022-02-01 03:13:37 +00:00
|
|
|
placeholder="e.g. This market is part of the ACX predictions for 2022..."
|
2022-03-25 16:27:28 +00:00
|
|
|
className="input resize-none"
|
2021-12-21 06:29:32 +00:00
|
|
|
value={description}
|
|
|
|
onChange={(e) => setDescription(e.target.value || '')}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2022-02-01 03:13:37 +00:00
|
|
|
<div className="form-control w-full">
|
|
|
|
<label className="label">
|
|
|
|
<span className="label-text">Tags</span>
|
|
|
|
</label>
|
|
|
|
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
placeholder="e.g. ACX2021 World"
|
|
|
|
className="input"
|
|
|
|
value={tags}
|
|
|
|
onChange={(e) => setTags(e.target.value || '')}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2022-02-11 18:40:22 +00:00
|
|
|
<div className="form-control mb-1 items-start">
|
|
|
|
<label className="label mb-1 gap-2">
|
2022-02-01 02:58:41 +00:00
|
|
|
<span>Market close</span>
|
|
|
|
<InfoTooltip text="Trading will be halted after this time (local timezone)." />
|
|
|
|
</label>
|
|
|
|
<input
|
|
|
|
type="datetime-local"
|
|
|
|
className="input input-bordered"
|
|
|
|
onClick={(e) => e.stopPropagation()}
|
|
|
|
onChange={(e) => setCloseDate(e.target.value || '')}
|
|
|
|
min={Date.now()}
|
|
|
|
disabled={isSubmitting}
|
|
|
|
value={closeDate}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Spacer h={4} />
|
|
|
|
|
2022-02-11 18:40:22 +00:00
|
|
|
<div className="form-control mb-1 items-start">
|
|
|
|
<label className="label mb-1 gap-2">
|
2022-02-01 02:58:41 +00:00
|
|
|
<span>Market ante</span>
|
|
|
|
<InfoTooltip
|
2022-05-19 22:04:34 +00:00
|
|
|
text={`Subsidize your market to encourage trading. Ante bets are set to match your initial probability.
|
2022-02-01 02:58:41 +00:00
|
|
|
You earn ${0.01 * 100}% of trading volume.`}
|
|
|
|
/>
|
|
|
|
</label>
|
2022-03-29 19:56:56 +00:00
|
|
|
<BuyAmountInput
|
2022-02-01 02:58:41 +00:00
|
|
|
amount={ante}
|
|
|
|
minimumAmount={10}
|
|
|
|
onChange={setAnte}
|
|
|
|
error={anteError}
|
|
|
|
setError={setAnteError}
|
|
|
|
disabled={isSubmitting}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2021-12-21 06:29:32 +00:00
|
|
|
{predictions.length > 0 && (
|
|
|
|
<div>
|
|
|
|
<Spacer h={4} />
|
|
|
|
<label className="label">
|
|
|
|
<span className="label-text">Preview</span>
|
|
|
|
</label>
|
|
|
|
<PredictionList predictions={predictions} />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<Spacer h={4} />
|
|
|
|
|
2022-02-11 18:40:22 +00:00
|
|
|
<div className="my-4 flex justify-end">
|
2021-12-21 06:29:32 +00:00
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
className={clsx('btn btn-primary', {
|
|
|
|
loading: isSubmitting,
|
|
|
|
})}
|
|
|
|
disabled={predictions.length === 0 || isSubmitting}
|
|
|
|
onClick={(e) => {
|
|
|
|
e.preventDefault()
|
|
|
|
createContracts()
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Create all
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{createdContracts.length > 0 && (
|
|
|
|
<>
|
|
|
|
<Spacer h={16} />
|
|
|
|
<Title text="Created Predictions" />
|
2022-02-11 18:40:22 +00:00
|
|
|
<div className="w-full rounded-lg bg-gray-100 px-6 py-4 shadow-xl">
|
2021-12-21 06:29:32 +00:00
|
|
|
<PredictionList predictions={createdContracts.map(toPrediction)} />
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Page>
|
|
|
|
)
|
|
|
|
}
|
2022-02-01 02:58:41 +00:00
|
|
|
|
|
|
|
// Given a date string like '2022-04-02',
|
|
|
|
// return the time just before midnight on that date (in the user's local time), as millis since epoch
|
|
|
|
function dateToMillis(date: string) {
|
|
|
|
return dayjs(date)
|
|
|
|
.set('hour', 23)
|
|
|
|
.set('minute', 59)
|
|
|
|
.set('second', 59)
|
|
|
|
.valueOf()
|
|
|
|
}
|