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'
|
2021-12-14 01:07:28 +00:00
|
|
|
|
2021-12-30 20:03:32 +00:00
|
|
|
import { CreatorContractsList } from '../components/contracts-list'
|
2021-12-16 02:31:14 +00:00
|
|
|
import { Spacer } from '../components/layout/spacer'
|
|
|
|
import { Title } from '../components/title'
|
|
|
|
import { useUser } from '../hooks/use-user'
|
2021-12-17 23:16:42 +00:00
|
|
|
import { path } from '../lib/firebase/contracts'
|
2021-12-16 02:31:14 +00:00
|
|
|
import { createContract } from '../lib/service/create-contract'
|
2021-12-20 04:06:30 +00:00
|
|
|
import { Page } from '../components/page'
|
2022-01-03 05:21:25 +00:00
|
|
|
import clsx from 'clsx'
|
|
|
|
import dayjs from 'dayjs'
|
2021-12-14 01:07:28 +00:00
|
|
|
|
2021-12-10 17:54:16 +00:00
|
|
|
// Allow user to create a new contract
|
|
|
|
export default function NewContract() {
|
|
|
|
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('/')
|
2021-12-16 03:58:28 +00:00
|
|
|
})
|
|
|
|
|
2021-12-14 01:07:28 +00:00
|
|
|
const [initialProb, setInitialProb] = useState(50)
|
|
|
|
const [question, setQuestion] = useState('')
|
|
|
|
const [description, setDescription] = useState('')
|
2022-01-03 05:21:25 +00:00
|
|
|
const [closeDate, setCloseDate] = useState('')
|
2021-12-14 01:07:28 +00:00
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
2022-01-03 05:21:25 +00:00
|
|
|
const [collapsed, setCollapsed] = useState(true)
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
const closeTime = dateToMillis(closeDate)
|
|
|
|
// We'd like this to look like "Apr 2, 2022, 23:59:59 PM PT" but timezones are hard with dayjs
|
|
|
|
const formattedCloseTime = new Date(closeTime).toString()
|
|
|
|
|
|
|
|
const isValid =
|
|
|
|
initialProb > 0 &&
|
|
|
|
initialProb < 100 &&
|
|
|
|
question.length > 0 &&
|
|
|
|
// If set, closeTime must be in the future
|
|
|
|
(!closeDate || 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)
|
|
|
|
|
2021-12-14 07:30:09 +00:00
|
|
|
const contract = await createContract(
|
|
|
|
question,
|
|
|
|
description,
|
|
|
|
initialProb,
|
2022-01-03 05:21:25 +00:00
|
|
|
creator,
|
|
|
|
closeTime
|
2021-12-14 07:30:09 +00:00
|
|
|
)
|
2021-12-17 23:16:42 +00:00
|
|
|
await router.push(path(contract))
|
2021-12-10 17:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const descriptionPlaceholder = `e.g. This market will resolve to “Yes” if, by June 2, 2021, 11:59:59 PM ET, Paxlovid (also known under PF-07321332)...`
|
|
|
|
|
2021-12-16 03:58:28 +00:00
|
|
|
if (!creator) return <></>
|
|
|
|
|
2021-12-10 17:54:16 +00:00
|
|
|
return (
|
2021-12-20 04:06:30 +00:00
|
|
|
<Page>
|
2021-12-18 23:40:39 +00:00
|
|
|
<Title text="Create a new prediction market" />
|
|
|
|
|
2021-12-30 18:52:05 +00:00
|
|
|
<div className="w-full bg-white rounded-lg shadow-md px-6 py-4">
|
2021-12-18 23:40:39 +00:00
|
|
|
{/* Create a Tailwind form that takes in all the fields needed for a new contract */}
|
|
|
|
{/* When the form is submitted, create a new contract in the database */}
|
|
|
|
<form>
|
2022-01-03 18:39:44 +00:00
|
|
|
<div className="form-control w-full">
|
|
|
|
<label className="label">
|
|
|
|
<span className="label-text">Prediction</span>
|
|
|
|
</label>
|
|
|
|
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
placeholder="e.g. The FDA will approve Paxlovid before Jun 2nd, 2022"
|
|
|
|
className="input input-bordered"
|
|
|
|
value={question}
|
|
|
|
onChange={(e) => setQuestion(e.target.value || '')}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Spacer h={4} />
|
2022-01-03 05:21:25 +00:00
|
|
|
|
2022-01-03 18:39:44 +00:00
|
|
|
<div className="form-control">
|
|
|
|
<label className="label">
|
|
|
|
<span className="label-text">Initial probability</span>
|
|
|
|
</label>
|
|
|
|
<label className="input-group input-group-md w-fit">
|
2022-01-03 05:21:25 +00:00
|
|
|
<input
|
2022-01-03 18:39:44 +00:00
|
|
|
type="number"
|
|
|
|
value={initialProb}
|
|
|
|
className="input input-bordered input-md"
|
|
|
|
min={1}
|
|
|
|
max={99}
|
|
|
|
onChange={(e) => setInitialProb(parseInt(e.target.value))}
|
2022-01-03 05:21:25 +00:00
|
|
|
/>
|
2022-01-03 18:39:44 +00:00
|
|
|
<span>%</span>
|
|
|
|
</label>
|
|
|
|
</div>
|
2022-01-03 05:21:25 +00:00
|
|
|
|
2022-01-03 18:39:44 +00:00
|
|
|
<Spacer h={4} />
|
|
|
|
|
|
|
|
<div className="form-control">
|
|
|
|
<label className="label">
|
|
|
|
<span className="label-text">Description (optional)</span>
|
|
|
|
</label>
|
|
|
|
<textarea
|
|
|
|
className="textarea w-full h-24 textarea-bordered"
|
|
|
|
placeholder={descriptionPlaceholder}
|
|
|
|
value={description}
|
|
|
|
onClick={(e) => e.stopPropagation()}
|
|
|
|
onChange={(e) => setDescription(e.target.value || '')}
|
|
|
|
/>
|
2021-12-18 23:40:39 +00:00
|
|
|
</div>
|
|
|
|
|
2022-01-03 05:21:25 +00:00
|
|
|
{/* Collapsible "Advanced" section */}
|
|
|
|
<div
|
|
|
|
tabIndex={0}
|
|
|
|
className={clsx(
|
|
|
|
'cursor-pointer relative collapse collapse-arrow',
|
|
|
|
collapsed ? 'collapse-close' : 'collapse-open'
|
|
|
|
)}
|
|
|
|
>
|
2022-01-03 18:39:44 +00:00
|
|
|
<div onClick={() => setCollapsed((collapsed) => !collapsed)}>
|
|
|
|
<div className="mt-4 mr-6 text-sm text-gray-400 text-right">
|
|
|
|
Advanced
|
|
|
|
</div>
|
2022-01-03 05:21:25 +00:00
|
|
|
<div
|
|
|
|
className="collapse-title p-0 absolute w-0 h-0 min-h-0"
|
2022-01-03 18:39:44 +00:00
|
|
|
style={{
|
|
|
|
top: -2,
|
|
|
|
right: -15,
|
|
|
|
color: '#9ca3af' /* gray-400 */,
|
|
|
|
}}
|
2022-01-03 05:21:25 +00:00
|
|
|
/>
|
2022-01-03 18:39:44 +00:00
|
|
|
</div>
|
2022-01-03 05:21:25 +00:00
|
|
|
<div className="collapse-content !p-0 m-0 !bg-transparent">
|
|
|
|
<div className="form-control">
|
|
|
|
<label className="label">
|
|
|
|
<span className="label-text">Close date (optional)</span>
|
|
|
|
</label>
|
|
|
|
<input
|
|
|
|
type="date"
|
|
|
|
className="input input-bordered"
|
|
|
|
onClick={(e) => e.stopPropagation()}
|
|
|
|
onChange={(e) => setCloseDate(e.target.value || '')}
|
|
|
|
min={new Date().toISOString().split('T')[0]}
|
|
|
|
value={closeDate}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<label>
|
|
|
|
{closeDate && (
|
|
|
|
<span className="label-text text-gray-400 ml-1">
|
|
|
|
No new trades will be allowed after {formattedCloseTime}
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</label>
|
|
|
|
</div>
|
2021-12-18 23:40:39 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<Spacer h={4} />
|
|
|
|
|
|
|
|
<div className="flex justify-end my-4">
|
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
className="btn btn-primary"
|
2022-01-03 05:21:25 +00:00
|
|
|
disabled={isSubmitting || !isValid}
|
2021-12-18 23:40:39 +00:00
|
|
|
onClick={(e) => {
|
|
|
|
e.preventDefault()
|
|
|
|
submit()
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Create market
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</form>
|
2021-12-10 17:54:16 +00:00
|
|
|
</div>
|
2021-12-18 23:40:39 +00:00
|
|
|
|
|
|
|
<Spacer h={10} />
|
|
|
|
|
|
|
|
<Title text="Your markets" />
|
|
|
|
|
2021-12-30 20:03:32 +00:00
|
|
|
{creator && <CreatorContractsList creator={creator} />}
|
2021-12-20 04:06:30 +00:00
|
|
|
</Page>
|
2021-12-10 17:54:16 +00:00
|
|
|
)
|
|
|
|
}
|