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
|
|
|
|
2022-05-09 13:04:36 +00:00
|
|
|
import { Spacer } from 'web/components/layout/spacer'
|
|
|
|
import { useUser } from 'web/hooks/use-user'
|
|
|
|
import { Contract, contractPath } from 'web/lib/firebase/contracts'
|
|
|
|
import { createContract } from 'web/lib/firebase/api-call'
|
|
|
|
import { FIXED_ANTE, MINIMUM_ANTE } from 'common/antes'
|
|
|
|
import { InfoTooltip } from 'web/components/info-tooltip'
|
|
|
|
import { Page } from 'web/components/page'
|
|
|
|
import { Title } from 'web/components/title'
|
|
|
|
import { ProbabilitySelector } from 'web/components/probability-selector'
|
|
|
|
import { Row } from 'web/components/layout/row'
|
|
|
|
import { MAX_DESCRIPTION_LENGTH, outcomeType } from 'common/contract'
|
|
|
|
import { formatMoney } from 'common/util/format'
|
|
|
|
import { useHasCreatedContractToday } from 'web/hooks/use-has-created-contract-today'
|
2022-05-12 15:07:10 +00:00
|
|
|
import { removeUndefinedProps } from '../../common/util/object'
|
2022-05-13 23:42:48 +00:00
|
|
|
import { CATEGORIES } from 'common/categories'
|
2022-01-21 18:33:58 +00:00
|
|
|
|
|
|
|
export default function Create() {
|
|
|
|
const [question, setQuestion] = useState('')
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Page>
|
2022-02-11 18:40:22 +00:00
|
|
|
<div className="mx-auto w-full max-w-2xl">
|
2022-01-21 18:33:58 +00:00
|
|
|
<Title text="Create a new prediction market" />
|
|
|
|
|
2022-02-11 18:40:22 +00:00
|
|
|
<div className="rounded-lg bg-gray-100 px-6 py-4 shadow-md">
|
2022-01-21 18:33:58 +00:00
|
|
|
<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"
|
2022-04-19 16:04:07 +00:00
|
|
|
autoFocus
|
2022-01-21 18:33:58 +00:00
|
|
|
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-05-17 04:43:40 +00:00
|
|
|
createContract({}).catch(() => {}) // warm up function
|
2022-01-05 18:23:58 +00:00
|
|
|
}, [])
|
2021-12-16 03:58:28 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
const [outcomeType, setOutcomeType] = useState<outcomeType>('BINARY')
|
2021-12-14 01:07:28 +00:00
|
|
|
const [initialProb, setInitialProb] = useState(50)
|
|
|
|
const [description, setDescription] = useState('')
|
2022-05-12 15:07:10 +00:00
|
|
|
|
|
|
|
const [category, setCategory] = useState<string>('')
|
|
|
|
// const [tagText, setTagText] = useState<string>(tag ?? '')
|
|
|
|
// const tags = parseWordsAsTags(tagText)
|
2022-01-05 05:51:26 +00:00
|
|
|
|
2022-04-09 18:51:22 +00:00
|
|
|
const [ante, setAnte] = useState(FIXED_ANTE)
|
2022-04-28 23:01:50 +00:00
|
|
|
|
|
|
|
const deservesDailyFreeMarket = !useHasCreatedContractToday(creator)
|
|
|
|
|
2022-04-09 18:51:22 +00:00
|
|
|
// useEffect(() => {
|
|
|
|
// if (ante === null && creator) {
|
|
|
|
// const initialAnte = creator.balance < 100 ? MINIMUM_ANTE : 100
|
|
|
|
// setAnte(initialAnte)
|
|
|
|
// }
|
|
|
|
// }, [ante, creator])
|
2022-01-14 23:39:17 +00:00
|
|
|
|
2022-04-09 18:51:22 +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-02-04 23:38:40 +00:00
|
|
|
const closeTime = closeDate ? dayjs(closeDate).valueOf() : 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 &&
|
2022-02-06 01:02:13 +00:00
|
|
|
ante !== null &&
|
2022-01-14 23:39:17 +00:00
|
|
|
ante >= MINIMUM_ANTE &&
|
2022-04-29 13:34:36 +00:00
|
|
|
(ante <= balance || deservesDailyFreeMarket) &&
|
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-05-12 15:07:10 +00:00
|
|
|
const result: any = await createContract(
|
|
|
|
removeUndefinedProps({
|
|
|
|
question,
|
|
|
|
outcomeType,
|
|
|
|
description,
|
|
|
|
initialProb,
|
|
|
|
ante,
|
|
|
|
closeTime,
|
|
|
|
tags: category ? [category] : undefined,
|
|
|
|
})
|
|
|
|
).then((r) => r.data || {})
|
2022-01-05 05:51:26 +00:00
|
|
|
|
|
|
|
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-02-17 23:00:19 +00:00
|
|
|
const descriptionPlaceholder =
|
|
|
|
outcomeType === 'BINARY'
|
|
|
|
? `e.g. This market resolves to "YES" if, two weeks after closing, the...`
|
|
|
|
: `e.g. I will choose the answer according to...`
|
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-02-17 23:00:19 +00:00
|
|
|
<label className="label">
|
|
|
|
<span className="mb-1">Answer type</span>
|
|
|
|
</label>
|
|
|
|
<Row className="form-control gap-2">
|
2022-03-03 09:09:32 +00:00
|
|
|
<label className="label cursor-pointer gap-2">
|
2022-02-17 23:00:19 +00:00
|
|
|
<input
|
|
|
|
className="radio"
|
|
|
|
type="radio"
|
|
|
|
name="opt"
|
|
|
|
checked={outcomeType === 'BINARY'}
|
|
|
|
value="BINARY"
|
|
|
|
onChange={() => setOutcomeType('BINARY')}
|
|
|
|
/>
|
|
|
|
<span className="label-text">Yes / No</span>
|
|
|
|
</label>
|
2022-01-21 18:33:58 +00:00
|
|
|
|
2022-03-03 09:09:32 +00:00
|
|
|
<label className="label cursor-pointer gap-2">
|
2022-02-17 23:00:19 +00:00
|
|
|
<input
|
|
|
|
className="radio"
|
|
|
|
type="radio"
|
|
|
|
name="opt"
|
|
|
|
checked={outcomeType === 'FREE_RESPONSE'}
|
|
|
|
value="FREE_RESPONSE"
|
|
|
|
onChange={() => setOutcomeType('FREE_RESPONSE')}
|
|
|
|
/>
|
|
|
|
<span className="label-text">Free response</span>
|
2022-01-21 18:33:58 +00:00
|
|
|
</label>
|
2022-02-17 23:00:19 +00:00
|
|
|
</Row>
|
|
|
|
<Spacer h={4} />
|
2022-01-30 21:51:30 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
{outcomeType === 'BINARY' && (
|
|
|
|
<div className="form-control">
|
|
|
|
<label className="label">
|
|
|
|
<span className="mb-1">Initial probability</span>
|
|
|
|
</label>
|
|
|
|
|
|
|
|
<ProbabilitySelector
|
|
|
|
probabilityInt={initialProb}
|
|
|
|
setProbabilityInt={setInitialProb}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-01-16 01:02:01 +00:00
|
|
|
|
2022-01-21 18:33:58 +00:00
|
|
|
<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-01-21 18:33:58 +00:00
|
|
|
<span className="mb-1">Description</span>
|
|
|
|
<InfoTooltip text="Optional. Describe how you will resolve this market." />
|
|
|
|
</label>
|
|
|
|
<Textarea
|
2022-03-25 16:27:28 +00:00
|
|
|
className="textarea textarea-bordered w-full resize-none"
|
2022-01-21 18:33:58 +00:00
|
|
|
rows={3}
|
2022-03-09 17:08:57 +00:00
|
|
|
maxLength={MAX_DESCRIPTION_LENGTH}
|
2022-01-21 18:33:58 +00:00
|
|
|
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-05-12 15:07:10 +00:00
|
|
|
<Spacer h={4} />
|
2022-01-21 18:33:58 +00:00
|
|
|
|
2022-02-11 18:40:22 +00:00
|
|
|
<div className="form-control max-w-sm items-start">
|
2022-02-06 23:14:07 +00:00
|
|
|
<label className="label gap-2">
|
2022-05-12 15:07:10 +00:00
|
|
|
<span className="mb-1">Category</span>
|
2022-02-06 23:14:07 +00:00
|
|
|
</label>
|
|
|
|
|
2022-05-12 15:07:10 +00:00
|
|
|
<select
|
|
|
|
className="select select-bordered w-full max-w-xs"
|
2022-05-13 23:42:48 +00:00
|
|
|
value={category}
|
|
|
|
onChange={(e) => setCategory(e.currentTarget.value ?? '')}
|
2022-05-12 15:07:10 +00:00
|
|
|
>
|
2022-05-17 15:56:43 +00:00
|
|
|
<option value={''}>(none)</option>
|
2022-05-13 23:42:48 +00:00
|
|
|
{Object.entries(CATEGORIES).map(([id, name]) => (
|
|
|
|
<option key={id} value={id}>
|
|
|
|
{name}
|
2022-05-12 15:07:10 +00:00
|
|
|
</option>
|
|
|
|
))}
|
|
|
|
</select>
|
|
|
|
</div>
|
2022-02-06 23:14:07 +00:00
|
|
|
|
|
|
|
<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-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-02-11 18:40:22 +00:00
|
|
|
<div className="form-control mb-1 items-start">
|
|
|
|
<label className="label mb-1 gap-2">
|
2022-04-09 18:51:22 +00:00
|
|
|
<span>Cost</span>
|
2022-05-10 13:49:14 +00:00
|
|
|
{!deservesDailyFreeMarket && (
|
|
|
|
<InfoTooltip
|
|
|
|
text={`Cost to create your market. This amount is used to subsidize trading.`}
|
|
|
|
/>
|
|
|
|
)}
|
2022-01-21 18:33:58 +00:00
|
|
|
</label>
|
2022-04-28 23:01:50 +00:00
|
|
|
{deservesDailyFreeMarket ? (
|
|
|
|
<div className="label-text text-primary pl-1">FREE</div>
|
|
|
|
) : (
|
|
|
|
<div className="label-text text-neutral pl-1">
|
|
|
|
{formatMoney(ante)}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{!deservesDailyFreeMarket && ante > balance && (
|
2022-04-09 18:51:22 +00:00
|
|
|
<div className="mb-2 mt-2 mr-auto self-center whitespace-nowrap text-xs font-medium tracking-wide">
|
|
|
|
<span className="mr-2 text-red-500">Insufficient balance</span>
|
|
|
|
<button
|
|
|
|
className="btn btn-xs btn-primary"
|
|
|
|
onClick={() => (window.location.href = '/add-funds')}
|
|
|
|
>
|
|
|
|
Add funds
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{/* <BuyAmountInput
|
2022-02-06 01:10:26 +00:00
|
|
|
amount={ante ?? undefined}
|
2022-01-21 18:33:58 +00:00
|
|
|
minimumAmount={MINIMUM_ANTE}
|
|
|
|
onChange={setAnte}
|
|
|
|
error={anteError}
|
|
|
|
setError={setAnteError}
|
|
|
|
disabled={isSubmitting}
|
2022-03-02 21:50:19 +00:00
|
|
|
contractIdForLoan={undefined}
|
2022-04-09 18:51:22 +00:00
|
|
|
/> */}
|
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} />
|
|
|
|
|
2022-02-11 18:40:22 +00:00
|
|
|
<div className="my-4 flex justify-end">
|
2022-01-21 18:33:58 +00:00
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
className={clsx(
|
2022-04-28 23:01:50 +00:00
|
|
|
'btn btn-primary capitalize',
|
2022-01-21 18:33:58 +00:00
|
|
|
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
|
|
|
)
|
|
|
|
}
|