2021-12-14 01:07:28 +00:00
|
|
|
import router from 'next/router'
|
2021-12-16 03:58:28 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
2022-01-05 05:51:26 +00:00
|
|
|
import clsx from 'clsx'
|
|
|
|
import dayjs from 'dayjs'
|
2022-01-06 08:45:30 +00:00
|
|
|
import Textarea from 'react-expanding-textarea'
|
2021-12-14 01:07:28 +00:00
|
|
|
|
2021-12-16 02:31:14 +00:00
|
|
|
import { Spacer } from '../components/layout/spacer'
|
|
|
|
import { useUser } from '../hooks/use-user'
|
2022-01-12 19:01:04 +00:00
|
|
|
import { Contract, contractPath } from '../lib/firebase/contracts'
|
2022-01-05 18:23:58 +00:00
|
|
|
import { createContract } from '../lib/firebase/api-call'
|
2022-01-11 03:41:42 +00:00
|
|
|
import { AmountInput } from '../components/amount-input'
|
2022-01-14 23:39:17 +00:00
|
|
|
import { MINIMUM_ANTE } from '../../common/antes'
|
2022-01-15 23:30:48 +00:00
|
|
|
import { InfoTooltip } from '../components/info-tooltip'
|
|
|
|
import { CREATOR_FEE } from '../../common/fees'
|
2022-01-21 18:33:58 +00:00
|
|
|
import { Page } from '../components/page'
|
|
|
|
import { Title } from '../components/title'
|
2022-01-30 21:51:30 +00:00
|
|
|
import { ProbabilitySelector } from '../components/probability-selector'
|
2022-01-21 18:33:58 +00:00
|
|
|
|
|
|
|
export default function Create() {
|
|
|
|
const [question, setQuestion] = useState('')
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Page>
|
|
|
|
<div className="w-full max-w-2xl mx-auto">
|
|
|
|
<Title text="Create a new prediction market" />
|
|
|
|
|
|
|
|
<div className="bg-gray-100 rounded-lg shadow-md px-6 py-4">
|
|
|
|
<form>
|
|
|
|
<div className="form-control w-full">
|
|
|
|
<label className="label">
|
|
|
|
<span className="mb-1">Question</span>
|
|
|
|
</label>
|
|
|
|
|
|
|
|
<Textarea
|
|
|
|
placeholder="e.g. Will the Democrats win the 2024 US presidential election?"
|
|
|
|
className="input input-bordered resize-none"
|
|
|
|
value={question}
|
|
|
|
onChange={(e) => setQuestion(e.target.value || '')}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
<NewContract question={question} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Page>
|
|
|
|
)
|
|
|
|
}
|
2021-12-14 01:07:28 +00:00
|
|
|
|
2021-12-10 17:54:16 +00:00
|
|
|
// Allow user to create a new contract
|
2022-01-27 18:45:35 +00:00
|
|
|
export function NewContract(props: { question: string; tag?: string }) {
|
|
|
|
const { question, tag } = props
|
2021-12-10 17:54:16 +00:00
|
|
|
const creator = useUser()
|
|
|
|
|
2021-12-16 03:58:28 +00:00
|
|
|
useEffect(() => {
|
2021-12-16 07:00:35 +00:00
|
|
|
if (creator === null) router.push('/')
|
2022-01-05 18:23:58 +00:00
|
|
|
}, [creator])
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-01-14 23:39:17 +00:00
|
|
|
createContract({}).catch() // warm up function
|
2022-01-05 18:23:58 +00:00
|
|
|
}, [])
|
2021-12-16 03:58:28 +00:00
|
|
|
|
2021-12-14 01:07:28 +00:00
|
|
|
const [initialProb, setInitialProb] = useState(50)
|
|
|
|
const [description, setDescription] = useState('')
|
2022-01-05 05:51:26 +00:00
|
|
|
|
2022-01-12 05:29:50 +00:00
|
|
|
const [ante, setAnte] = useState<number | undefined>(undefined)
|
2022-01-14 23:39:17 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (creator) {
|
|
|
|
const initialAnte = creator.balance < 100 ? 10 : 100
|
|
|
|
setAnte(initialAnte)
|
|
|
|
}
|
|
|
|
}, [creator])
|
|
|
|
|
2022-01-11 03:41:42 +00:00
|
|
|
const [anteError, setAnteError] = useState<string | undefined>()
|
2022-01-21 18:33:58 +00:00
|
|
|
// By default, close the market a week from today
|
2022-01-31 05:20:27 +00:00
|
|
|
const weekFromToday = dayjs().add(7, 'day').format('YYYY-MM-DDT23:59')
|
|
|
|
const [closeDate, setCloseDate] = useState<undefined | string>(weekFromToday)
|
2022-01-05 05:51:26 +00:00
|
|
|
|
2021-12-14 01:07:28 +00:00
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
2022-01-03 05:21:25 +00:00
|
|
|
|
2022-01-27 20:31:09 +00:00
|
|
|
const closeTime = closeDate ? dateToMillis(closeDate) : undefined
|
2022-01-05 05:51:26 +00:00
|
|
|
|
2022-01-15 23:30:48 +00:00
|
|
|
const balance = creator?.balance || 0
|
2022-01-03 05:21:25 +00:00
|
|
|
|
|
|
|
const isValid =
|
|
|
|
initialProb > 0 &&
|
|
|
|
initialProb < 100 &&
|
|
|
|
question.length > 0 &&
|
2022-01-14 23:39:17 +00:00
|
|
|
ante !== undefined &&
|
|
|
|
ante >= MINIMUM_ANTE &&
|
2022-01-15 23:30:48 +00:00
|
|
|
ante <= balance &&
|
2022-01-24 22:33:02 +00:00
|
|
|
// closeTime must be in the future
|
2022-01-12 05:40:41 +00:00
|
|
|
closeTime &&
|
|
|
|
closeTime > Date.now()
|
2021-12-14 01:07:28 +00:00
|
|
|
|
|
|
|
async function submit() {
|
2022-01-03 05:21:25 +00:00
|
|
|
// TODO: Tell users why their contract is invalid
|
|
|
|
if (!creator || !isValid) return
|
2021-12-14 01:07:28 +00:00
|
|
|
|
|
|
|
setIsSubmitting(true)
|
|
|
|
|
2022-01-05 05:51:26 +00:00
|
|
|
const result: any = await createContract({
|
2021-12-14 07:30:09 +00:00
|
|
|
question,
|
|
|
|
description,
|
|
|
|
initialProb,
|
2022-01-05 05:51:26 +00:00
|
|
|
ante,
|
2022-01-24 22:33:02 +00:00
|
|
|
closeTime,
|
2022-01-27 18:45:35 +00:00
|
|
|
tags: tag ? [tag] : [],
|
2022-01-05 05:51:26 +00:00
|
|
|
}).then((r) => r.data || {})
|
|
|
|
|
|
|
|
if (result.status !== 'success') {
|
|
|
|
console.log('error creating contract', result)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-12 19:01:04 +00:00
|
|
|
await router.push(contractPath(result.contract as Contract))
|
2022-01-05 05:51:26 +00:00
|
|
|
}
|
|
|
|
|
2022-01-21 18:33:58 +00:00
|
|
|
const descriptionPlaceholder = `e.g. This market resolves to "YES" if, two weeks after closing, the...\n#politics #world`
|
2021-12-10 17:54:16 +00:00
|
|
|
|
2021-12-16 03:58:28 +00:00
|
|
|
if (!creator) return <></>
|
|
|
|
|
2021-12-10 17:54:16 +00:00
|
|
|
return (
|
2022-01-23 00:38:34 +00:00
|
|
|
<div>
|
2022-01-21 18:33:58 +00:00
|
|
|
<Spacer h={4} />
|
|
|
|
|
|
|
|
<div className="form-control">
|
|
|
|
<label className="label">
|
|
|
|
<span className="mb-1">Initial probability</span>
|
|
|
|
</label>
|
2022-01-30 21:51:30 +00:00
|
|
|
|
|
|
|
<ProbabilitySelector
|
|
|
|
probabilityInt={initialProb}
|
|
|
|
setProbabilityInt={setInitialProb}
|
|
|
|
/>
|
2022-01-21 18:33:58 +00:00
|
|
|
</div>
|
2022-01-16 01:02:01 +00:00
|
|
|
|
2022-01-21 18:33:58 +00:00
|
|
|
<Spacer h={4} />
|
|
|
|
|
|
|
|
<div className="form-control items-start mb-1">
|
|
|
|
<label className="label gap-2 mb-1">
|
|
|
|
<span className="mb-1">Description</span>
|
|
|
|
<InfoTooltip text="Optional. Describe how you will resolve this market." />
|
|
|
|
</label>
|
|
|
|
<Textarea
|
|
|
|
className="textarea w-full textarea-bordered"
|
|
|
|
rows={3}
|
|
|
|
placeholder={descriptionPlaceholder}
|
|
|
|
value={description}
|
|
|
|
disabled={isSubmitting}
|
|
|
|
onClick={(e) => e.stopPropagation()}
|
|
|
|
onChange={(e) => setDescription(e.target.value || '')}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-01-16 01:02:01 +00:00
|
|
|
|
2022-01-21 18:33:58 +00:00
|
|
|
<Spacer h={4} />
|
|
|
|
|
|
|
|
<div className="form-control items-start mb-1">
|
|
|
|
<label className="label gap-2 mb-1">
|
2022-01-31 05:20:27 +00:00
|
|
|
<span>Market close</span>
|
|
|
|
<InfoTooltip text="Trading will be halted after this time (local timezone)." />
|
2022-01-21 18:33:58 +00:00
|
|
|
</label>
|
|
|
|
<input
|
2022-01-31 05:20:27 +00:00
|
|
|
type="datetime-local"
|
2022-01-21 18:33:58 +00:00
|
|
|
className="input input-bordered"
|
|
|
|
onClick={(e) => e.stopPropagation()}
|
|
|
|
onChange={(e) => setCloseDate(e.target.value || '')}
|
2022-01-31 05:20:27 +00:00
|
|
|
min={Date.now()}
|
2022-01-21 18:33:58 +00:00
|
|
|
disabled={isSubmitting}
|
|
|
|
value={closeDate}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-01-16 01:02:01 +00:00
|
|
|
|
2022-01-21 18:33:58 +00:00
|
|
|
<Spacer h={4} />
|
2022-01-16 01:02:01 +00:00
|
|
|
|
2022-01-21 18:33:58 +00:00
|
|
|
<div className="form-control items-start mb-1">
|
|
|
|
<label className="label gap-2 mb-1">
|
|
|
|
<span>Market ante</span>
|
|
|
|
<InfoTooltip
|
|
|
|
text={`Subsidize your market to encourage trading. Ante bets are set to match your initial probability.
|
2022-02-03 22:59:59 +00:00
|
|
|
You earn ${CREATOR_FEE * 100}% of the winnings.`}
|
2022-01-21 18:33:58 +00:00
|
|
|
/>
|
|
|
|
</label>
|
|
|
|
<AmountInput
|
|
|
|
amount={ante}
|
|
|
|
minimumAmount={MINIMUM_ANTE}
|
|
|
|
onChange={setAnte}
|
|
|
|
error={anteError}
|
|
|
|
setError={setAnteError}
|
|
|
|
disabled={isSubmitting}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-01-16 01:02:01 +00:00
|
|
|
|
2022-01-21 18:33:58 +00:00
|
|
|
<Spacer h={4} />
|
|
|
|
|
|
|
|
<div className="flex justify-end my-4">
|
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
className={clsx(
|
|
|
|
'btn btn-primary',
|
|
|
|
isSubmitting && 'loading disabled'
|
|
|
|
)}
|
|
|
|
disabled={isSubmitting || !isValid}
|
|
|
|
onClick={(e) => {
|
|
|
|
e.preventDefault()
|
|
|
|
submit()
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{isSubmitting ? 'Creating...' : 'Create market'}
|
|
|
|
</button>
|
2021-12-10 17:54:16 +00:00
|
|
|
</div>
|
2022-01-23 00:38:34 +00:00
|
|
|
</div>
|
2021-12-10 17:54:16 +00:00
|
|
|
)
|
|
|
|
}
|
2022-01-05 05:51:26 +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()
|
|
|
|
}
|