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-03-29 19:56:56 +00:00
|
|
|
import { BuyAmountInput } from '../components/amount-input'
|
2022-04-09 18:51:22 +00:00
|
|
|
import { FIXED_ANTE, 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-02-06 23:14:07 +00:00
|
|
|
import { parseWordsAsTags } from '../../common/util/parse'
|
|
|
|
import { TagsList } from '../components/tags-list'
|
2022-02-17 23:00:19 +00:00
|
|
|
import { Row } from '../components/layout/row'
|
2022-03-09 17:08:57 +00:00
|
|
|
import { MAX_DESCRIPTION_LENGTH, outcomeType } from '../../common/contract'
|
2022-04-09 18:51:22 +00:00
|
|
|
import { formatMoney } from '../../common/util/format'
|
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"
|
|
|
|
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
|
|
|
|
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-02-06 23:14:07 +00:00
|
|
|
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)
|
|
|
|
// 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-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,
|
2022-02-17 23:00:19 +00:00
|
|
|
outcomeType,
|
2021-12-14 07:30:09 +00:00
|
|
|
description,
|
|
|
|
initialProb,
|
2022-01-05 05:51:26 +00:00
|
|
|
ante,
|
2022-01-24 22:33:02 +00:00
|
|
|
closeTime,
|
2022-02-06 23:14:07 +00:00
|
|
|
tags,
|
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-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-04-04 15:32:04 +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">
|
|
|
|
<span className="mb-1">Tags</span>
|
|
|
|
<InfoTooltip text="Optional. Help categorize your market with related tags." />
|
|
|
|
</label>
|
|
|
|
|
|
|
|
<input
|
|
|
|
placeholder="e.g. Politics, Economics..."
|
|
|
|
className="input input-bordered resize-none"
|
|
|
|
disabled={isSubmitting}
|
|
|
|
value={tagText}
|
|
|
|
onChange={(e) => setTagText(e.target.value || '')}
|
|
|
|
/>
|
2022-04-04 15:32:04 +00:00
|
|
|
</div> */}
|
2022-02-06 23:14:07 +00:00
|
|
|
|
|
|
|
<Spacer h={4} />
|
|
|
|
<TagsList tags={tags} noLink noLabel />
|
|
|
|
<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-01-21 18:33:58 +00:00
|
|
|
<InfoTooltip
|
2022-04-09 18:51:22 +00:00
|
|
|
text={`Cost to create your market. This amount is used to subsidize trading.`}
|
2022-01-21 18:33:58 +00:00
|
|
|
/>
|
|
|
|
</label>
|
2022-04-09 18:51:22 +00:00
|
|
|
|
|
|
|
<div className="label-text text-neutral pl-1">{formatMoney(ante)}</div>
|
|
|
|
|
|
|
|
{ante > balance && (
|
|
|
|
<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(
|
|
|
|
'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
|
|
|
)
|
|
|
|
}
|