2022-08-05 05:22:45 +00:00
|
|
|
import clsx from 'clsx'
|
|
|
|
import dayjs from 'dayjs'
|
|
|
|
import React, { useEffect, useState } from 'react'
|
|
|
|
import { LinkIcon, SwitchVerticalIcon } from '@heroicons/react/outline'
|
|
|
|
import toast from 'react-hot-toast'
|
|
|
|
|
|
|
|
import { Col } from '../layout/col'
|
|
|
|
import { Row } from '../layout/row'
|
|
|
|
import { Title } from '../title'
|
|
|
|
import { User } from 'common/user'
|
|
|
|
import { Modal } from 'web/components/layout/modal'
|
|
|
|
import { Button } from '../button'
|
|
|
|
import { createChallenge, getChallengeUrl } from 'web/lib/firebase/challenges'
|
2022-08-18 14:15:20 +00:00
|
|
|
import { BinaryContract, MAX_QUESTION_LENGTH } from 'common/contract'
|
2022-08-05 05:22:45 +00:00
|
|
|
import { SiteLink } from 'web/components/site-link'
|
|
|
|
import { formatMoney } from 'common/util/format'
|
|
|
|
import { NoLabel, YesLabel } from '../outcome-label'
|
|
|
|
import { QRCode } from '../qr-code'
|
|
|
|
import { copyToClipboard } from 'web/lib/util/copy'
|
2022-08-05 06:42:35 +00:00
|
|
|
import { AmountInput } from '../amount-input'
|
2022-09-05 23:07:44 +00:00
|
|
|
import { getProbability } from 'common/calculate'
|
2022-08-18 14:15:20 +00:00
|
|
|
import { createMarket } from 'web/lib/firebase/api'
|
|
|
|
import { removeUndefinedProps } from 'common/util/object'
|
2022-08-22 05:37:26 +00:00
|
|
|
import { FIXED_ANTE } from 'common/economy'
|
2022-08-18 14:15:20 +00:00
|
|
|
import Textarea from 'react-expanding-textarea'
|
|
|
|
import { useTextEditor } from 'web/components/editor'
|
|
|
|
import { LoadingIndicator } from 'web/components/loading-indicator'
|
2022-08-05 20:42:02 +00:00
|
|
|
import { track } from 'web/lib/service/analytics'
|
2022-08-05 05:22:45 +00:00
|
|
|
|
|
|
|
type challengeInfo = {
|
|
|
|
amount: number
|
|
|
|
expiresTime: number | null
|
|
|
|
outcome: 'YES' | 'NO' | number
|
|
|
|
acceptorAmount: number
|
2022-08-18 14:15:20 +00:00
|
|
|
question: string
|
2022-08-05 05:22:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function CreateChallengeModal(props: {
|
|
|
|
user: User | null | undefined
|
|
|
|
isOpen: boolean
|
|
|
|
setOpen: (open: boolean) => void
|
2022-08-18 14:15:20 +00:00
|
|
|
contract?: BinaryContract
|
2022-08-05 05:22:45 +00:00
|
|
|
}) {
|
|
|
|
const { user, contract, isOpen, setOpen } = props
|
|
|
|
const [challengeSlug, setChallengeSlug] = useState('')
|
2022-08-18 14:15:20 +00:00
|
|
|
const [loading, setLoading] = useState(false)
|
|
|
|
const { editor } = useTextEditor({ placeholder: '' })
|
2022-08-05 05:22:45 +00:00
|
|
|
|
|
|
|
return (
|
2022-08-05 06:42:35 +00:00
|
|
|
<Modal open={isOpen} setOpen={setOpen}>
|
2022-08-05 05:22:45 +00:00
|
|
|
<Col className="gap-4 rounded-md bg-white px-8 py-6">
|
|
|
|
{/*// add a sign up to challenge button?*/}
|
|
|
|
{user && (
|
|
|
|
<CreateChallengeForm
|
|
|
|
user={user}
|
|
|
|
contract={contract}
|
2022-08-18 14:15:20 +00:00
|
|
|
loading={loading}
|
2022-08-05 05:22:45 +00:00
|
|
|
onCreate={async (newChallenge) => {
|
2022-08-18 14:15:20 +00:00
|
|
|
setLoading(true)
|
|
|
|
try {
|
|
|
|
const challengeContract = contract
|
|
|
|
? contract
|
|
|
|
: await createMarket(
|
|
|
|
removeUndefinedProps({
|
|
|
|
question: newChallenge.question,
|
|
|
|
outcomeType: 'BINARY',
|
|
|
|
initialProb: 50,
|
|
|
|
description: editor?.getJSON(),
|
|
|
|
ante: FIXED_ANTE,
|
|
|
|
closeTime: dayjs().add(30, 'day').valueOf(),
|
|
|
|
})
|
|
|
|
)
|
|
|
|
const challenge = await createChallenge({
|
|
|
|
creator: user,
|
|
|
|
creatorAmount: newChallenge.amount,
|
|
|
|
expiresTime: newChallenge.expiresTime,
|
|
|
|
acceptorAmount: newChallenge.acceptorAmount,
|
|
|
|
outcome: newChallenge.outcome,
|
|
|
|
contract: challengeContract as BinaryContract,
|
2022-08-05 20:42:02 +00:00
|
|
|
})
|
2022-08-18 14:15:20 +00:00
|
|
|
if (challenge) {
|
|
|
|
setChallengeSlug(getChallengeUrl(challenge))
|
|
|
|
track('challenge created', {
|
|
|
|
creator: user.username,
|
|
|
|
amount: newChallenge.amount,
|
|
|
|
contractId: challengeContract.id,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error("couldn't create market/challenge:", e)
|
2022-08-05 20:42:02 +00:00
|
|
|
}
|
2022-08-18 14:15:20 +00:00
|
|
|
setLoading(false)
|
2022-08-05 05:22:45 +00:00
|
|
|
}}
|
|
|
|
challengeSlug={challengeSlug}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Col>
|
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function CreateChallengeForm(props: {
|
|
|
|
user: User
|
|
|
|
onCreate: (m: challengeInfo) => Promise<void>
|
|
|
|
challengeSlug: string
|
2022-08-18 14:15:20 +00:00
|
|
|
loading: boolean
|
|
|
|
contract?: BinaryContract
|
2022-08-05 05:22:45 +00:00
|
|
|
}) {
|
2022-08-18 14:15:20 +00:00
|
|
|
const { user, onCreate, contract, challengeSlug, loading } = props
|
2022-08-05 05:22:45 +00:00
|
|
|
const [isCreating, setIsCreating] = useState(false)
|
|
|
|
const [finishedCreating, setFinishedCreating] = useState(false)
|
|
|
|
const [error, setError] = useState<string>('')
|
2022-09-05 23:07:44 +00:00
|
|
|
const [editingAcceptorAmount, setEditingAcceptorAmount] = useState(false)
|
2022-08-05 05:22:45 +00:00
|
|
|
const defaultExpire = 'week'
|
|
|
|
|
|
|
|
const [challengeInfo, setChallengeInfo] = useState<challengeInfo>({
|
|
|
|
expiresTime: dayjs().add(2, defaultExpire).valueOf(),
|
|
|
|
outcome: 'YES',
|
|
|
|
amount: 100,
|
|
|
|
acceptorAmount: 100,
|
2022-08-18 14:15:20 +00:00
|
|
|
question: contract ? contract.question : '',
|
2022-08-05 05:22:45 +00:00
|
|
|
})
|
|
|
|
useEffect(() => {
|
|
|
|
setError('')
|
|
|
|
}, [challengeInfo])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{!finishedCreating && (
|
|
|
|
<form
|
|
|
|
onSubmit={(e) => {
|
|
|
|
e.preventDefault()
|
|
|
|
if (user.balance < challengeInfo.amount) {
|
2022-08-18 14:15:20 +00:00
|
|
|
setError("You don't have enough mana to create this challenge")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!contract && user.balance < FIXED_ANTE + challengeInfo.amount) {
|
|
|
|
setError(
|
|
|
|
`You don't have enough mana to create this challenge and market. You need ${formatMoney(
|
|
|
|
FIXED_ANTE + challengeInfo.amount
|
|
|
|
)}`
|
|
|
|
)
|
2022-08-05 05:22:45 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
setIsCreating(true)
|
|
|
|
onCreate(challengeInfo).finally(() => setIsCreating(false))
|
|
|
|
setFinishedCreating(true)
|
|
|
|
}}
|
|
|
|
>
|
2022-09-06 13:58:00 +00:00
|
|
|
<Title className="!mt-2 hidden sm:block" text="Challenge bet " />
|
2022-08-05 06:42:35 +00:00
|
|
|
|
|
|
|
<div className="mb-8">
|
|
|
|
Challenge a friend to bet on{' '}
|
2022-08-18 14:15:20 +00:00
|
|
|
{contract ? (
|
|
|
|
<span className="underline">{contract.question}</span>
|
|
|
|
) : (
|
|
|
|
<Textarea
|
|
|
|
placeholder="e.g. Will a Democrat be the next president?"
|
|
|
|
className="input input-bordered mt-1 w-full resize-none"
|
2022-09-05 23:07:44 +00:00
|
|
|
autoFocus={true}
|
2022-08-18 14:15:20 +00:00
|
|
|
maxLength={MAX_QUESTION_LENGTH}
|
|
|
|
value={challengeInfo.question}
|
|
|
|
onChange={(e) =>
|
|
|
|
setChallengeInfo({
|
|
|
|
...challengeInfo,
|
|
|
|
question: e.target.value,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
)}
|
2022-08-05 06:42:35 +00:00
|
|
|
</div>
|
|
|
|
|
2022-09-06 13:58:00 +00:00
|
|
|
<Col className="mt-2 flex-wrap justify-center gap-x-5 sm:gap-y-2">
|
|
|
|
<Col>
|
|
|
|
<div>You'll bet:</div>
|
|
|
|
<Row
|
|
|
|
className={
|
|
|
|
'form-control w-full max-w-xs items-center justify-between gap-4 pr-3'
|
2022-08-05 05:22:45 +00:00
|
|
|
}
|
|
|
|
>
|
2022-08-05 06:42:35 +00:00
|
|
|
<AmountInput
|
2022-09-06 13:58:00 +00:00
|
|
|
amount={challengeInfo.amount || undefined}
|
|
|
|
onChange={(newAmount) =>
|
2022-08-05 06:42:35 +00:00
|
|
|
setChallengeInfo((m: challengeInfo) => {
|
|
|
|
return {
|
|
|
|
...m,
|
2022-09-06 13:58:00 +00:00
|
|
|
amount: newAmount ?? 0,
|
|
|
|
acceptorAmount: editingAcceptorAmount
|
|
|
|
? m.acceptorAmount
|
|
|
|
: newAmount ?? 0,
|
2022-08-05 06:42:35 +00:00
|
|
|
}
|
|
|
|
})
|
2022-09-06 13:58:00 +00:00
|
|
|
}
|
2022-08-05 06:42:35 +00:00
|
|
|
error={undefined}
|
|
|
|
label={'M$'}
|
|
|
|
inputClassName="w-24"
|
|
|
|
/>
|
2022-09-06 13:58:00 +00:00
|
|
|
<span className={''}>on</span>
|
|
|
|
{challengeInfo.outcome === 'YES' ? <YesLabel /> : <NoLabel />}
|
|
|
|
</Row>
|
|
|
|
<Row className={'mt-3 max-w-xs justify-end'}>
|
|
|
|
<Button
|
|
|
|
color={'gray-white'}
|
|
|
|
onClick={() =>
|
|
|
|
setChallengeInfo((m: challengeInfo) => {
|
|
|
|
return {
|
|
|
|
...m,
|
|
|
|
outcome: m.outcome === 'YES' ? 'NO' : 'YES',
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<SwitchVerticalIcon className={'h-6 w-6'} />
|
|
|
|
</Button>
|
|
|
|
</Row>
|
|
|
|
<Row className={'items-center'}>If they bet:</Row>
|
|
|
|
<Row
|
|
|
|
className={'max-w-xs items-center justify-between gap-4 pr-3'}
|
|
|
|
>
|
|
|
|
<div className={'w-32 sm:mr-1'}>
|
|
|
|
<AmountInput
|
|
|
|
amount={challengeInfo.acceptorAmount || undefined}
|
|
|
|
onChange={(newAmount) => {
|
|
|
|
setEditingAcceptorAmount(true)
|
|
|
|
|
|
|
|
setChallengeInfo((m: challengeInfo) => {
|
|
|
|
return {
|
|
|
|
...m,
|
|
|
|
acceptorAmount: newAmount ?? 0,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}}
|
|
|
|
error={undefined}
|
|
|
|
label={'M$'}
|
|
|
|
inputClassName="w-24"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<span>on</span>
|
|
|
|
{challengeInfo.outcome === 'YES' ? <NoLabel /> : <YesLabel />}
|
|
|
|
</Row>
|
|
|
|
</Col>
|
|
|
|
</Col>
|
2022-09-05 23:07:44 +00:00
|
|
|
{contract && (
|
|
|
|
<Button
|
|
|
|
size="2xs"
|
|
|
|
color="gray"
|
|
|
|
onClick={() => {
|
|
|
|
setEditingAcceptorAmount(true)
|
|
|
|
|
|
|
|
const p = getProbability(contract)
|
|
|
|
const prob = challengeInfo.outcome === 'YES' ? p : 1 - p
|
|
|
|
const { amount } = challengeInfo
|
|
|
|
const acceptorAmount = Math.round(amount / prob - amount)
|
|
|
|
setChallengeInfo({ ...challengeInfo, acceptorAmount })
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Use market odds
|
|
|
|
</Button>
|
|
|
|
)}
|
2022-08-05 06:42:35 +00:00
|
|
|
<div className="mt-8">
|
|
|
|
If the challenge is accepted, whoever is right will earn{' '}
|
|
|
|
<span className="font-semibold">
|
|
|
|
{formatMoney(
|
|
|
|
challengeInfo.acceptorAmount + challengeInfo.amount || 0
|
|
|
|
)}
|
|
|
|
</span>{' '}
|
2022-08-18 14:15:20 +00:00
|
|
|
in total.{' '}
|
|
|
|
<span>
|
|
|
|
{!contract && (
|
|
|
|
<span>
|
|
|
|
Because there's no market yet, you'll be charged
|
|
|
|
<span className={'mx-1 font-semibold'}>
|
|
|
|
{formatMoney(FIXED_ANTE)}
|
|
|
|
</span>
|
|
|
|
to create it.
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</span>
|
2022-08-05 06:42:35 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<Row className="mt-8 items-center">
|
2022-08-05 05:22:45 +00:00
|
|
|
<Button
|
|
|
|
type="submit"
|
2022-08-05 06:42:35 +00:00
|
|
|
color={'gradient'}
|
|
|
|
size="xl"
|
2022-08-18 14:15:20 +00:00
|
|
|
disabled={isCreating || challengeInfo.question === ''}
|
|
|
|
className={clsx('whitespace-nowrap drop-shadow-md')}
|
2022-08-05 05:22:45 +00:00
|
|
|
>
|
2022-08-05 06:42:35 +00:00
|
|
|
Create challenge bet
|
2022-08-05 05:22:45 +00:00
|
|
|
</Button>
|
|
|
|
</Row>
|
|
|
|
<Row className={'text-error'}>{error} </Row>
|
|
|
|
</form>
|
|
|
|
)}
|
2022-08-18 14:15:20 +00:00
|
|
|
{loading && (
|
|
|
|
<Col className={'h-56 w-full items-center justify-center'}>
|
|
|
|
<LoadingIndicator />
|
|
|
|
</Col>
|
|
|
|
)}
|
|
|
|
{finishedCreating && !loading && (
|
2022-08-05 05:22:45 +00:00
|
|
|
<>
|
|
|
|
<Title className="!my-0" text="Challenge Created!" />
|
|
|
|
|
|
|
|
<div>Share the challenge using the link.</div>
|
|
|
|
<button
|
|
|
|
onClick={() => {
|
|
|
|
copyToClipboard(challengeSlug)
|
|
|
|
toast('Link copied to clipboard!')
|
|
|
|
}}
|
|
|
|
className={'btn btn-outline mb-4 whitespace-nowrap normal-case'}
|
|
|
|
>
|
|
|
|
<LinkIcon className={'mr-2 h-5 w-5'} />
|
|
|
|
Copy link
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<QRCode url={challengeSlug} className="self-center" />
|
|
|
|
<Row className={'gap-1 text-gray-500'}>
|
|
|
|
See your other
|
|
|
|
<SiteLink className={'underline'} href={'/challenges'}>
|
|
|
|
challenges
|
|
|
|
</SiteLink>
|
|
|
|
</Row>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|