2021-12-14 00:00:02 +00:00
|
|
|
import clsx from 'clsx'
|
2022-01-05 18:23:58 +00:00
|
|
|
import React, { useEffect, useState } from 'react'
|
2021-12-14 00:00:02 +00:00
|
|
|
|
|
|
|
import { Contract } from '../lib/firebase/contracts'
|
|
|
|
import { Col } from './layout/col'
|
|
|
|
import { Title } from './title'
|
|
|
|
import { User } from '../lib/firebase/users'
|
|
|
|
import { YesNoCancelSelector } from './yes-no-selector'
|
|
|
|
import { Spacer } from './layout/spacer'
|
2022-02-17 23:00:19 +00:00
|
|
|
import { ResolveConfirmationButton } from './confirmation-button'
|
2022-01-05 18:23:58 +00:00
|
|
|
import { resolveMarket } from '../lib/firebase/api-call'
|
2022-01-30 21:51:30 +00:00
|
|
|
import { ProbabilitySelector } from './probability-selector'
|
|
|
|
import { getProbability } from '../../common/calculate'
|
2022-02-03 23:07:30 +00:00
|
|
|
import { CREATOR_FEE } from '../../common/fees'
|
2021-12-14 07:02:50 +00:00
|
|
|
|
2021-12-14 00:00:02 +00:00
|
|
|
export function ResolutionPanel(props: {
|
|
|
|
creator: User
|
|
|
|
contract: Contract
|
|
|
|
className?: string
|
|
|
|
}) {
|
2022-01-05 18:23:58 +00:00
|
|
|
useEffect(() => {
|
|
|
|
// warm up cloud function
|
2022-02-17 23:00:19 +00:00
|
|
|
resolveMarket({} as any).catch()
|
2022-01-05 18:23:58 +00:00
|
|
|
}, [])
|
|
|
|
|
2021-12-15 18:44:34 +00:00
|
|
|
const { contract, className } = props
|
2021-12-14 00:00:02 +00:00
|
|
|
|
2022-01-02 06:27:58 +00:00
|
|
|
const [outcome, setOutcome] = useState<
|
|
|
|
'YES' | 'NO' | 'MKT' | 'CANCEL' | undefined
|
|
|
|
>()
|
2021-12-14 00:00:02 +00:00
|
|
|
|
2022-01-30 21:51:30 +00:00
|
|
|
const [prob, setProb] = useState(getProbability(contract.totalShares) * 100)
|
|
|
|
|
2021-12-15 18:44:34 +00:00
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
|
|
|
const [error, setError] = useState<string | undefined>(undefined)
|
|
|
|
|
2021-12-14 07:02:50 +00:00
|
|
|
const resolve = async () => {
|
2022-02-17 23:00:19 +00:00
|
|
|
if (!outcome) return
|
|
|
|
|
2021-12-15 18:44:34 +00:00
|
|
|
setIsSubmitting(true)
|
|
|
|
|
2021-12-18 23:40:39 +00:00
|
|
|
const result = await resolveMarket({
|
|
|
|
outcome,
|
|
|
|
contractId: contract.id,
|
2022-01-30 21:51:30 +00:00
|
|
|
probabilityInt: prob,
|
2022-02-20 07:26:26 +00:00
|
|
|
}).then((r) => r.data)
|
2021-12-15 18:44:34 +00:00
|
|
|
|
|
|
|
console.log('resolved', outcome, 'result:', result)
|
|
|
|
|
|
|
|
if (result?.status !== 'success') {
|
2022-02-20 07:26:26 +00:00
|
|
|
setError(result?.message || 'Error resolving market')
|
2021-12-15 18:44:34 +00:00
|
|
|
}
|
|
|
|
setIsSubmitting(false)
|
2021-12-14 00:28:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const submitButtonClass =
|
|
|
|
outcome === 'YES'
|
|
|
|
? 'btn-primary'
|
|
|
|
: outcome === 'NO'
|
2021-12-18 23:40:39 +00:00
|
|
|
? 'bg-red-400 hover:bg-red-500'
|
|
|
|
: outcome === 'CANCEL'
|
|
|
|
? 'bg-yellow-400 hover:bg-yellow-500'
|
2022-01-02 06:27:58 +00:00
|
|
|
: outcome === 'MKT'
|
|
|
|
? 'bg-blue-400 hover:bg-blue-500'
|
2021-12-18 23:40:39 +00:00
|
|
|
: 'btn-disabled'
|
2021-12-14 00:00:02 +00:00
|
|
|
|
|
|
|
return (
|
2022-02-11 18:40:22 +00:00
|
|
|
<Col className={clsx('rounded-md bg-white px-8 py-6', className)}>
|
2022-02-17 23:47:23 +00:00
|
|
|
<Title className="mt-0 whitespace-nowrap" text="Resolve market" />
|
2021-12-14 00:00:02 +00:00
|
|
|
|
2022-02-17 23:47:23 +00:00
|
|
|
<div className="mb-2 text-sm text-gray-500">Outcome</div>
|
2021-12-15 18:44:34 +00:00
|
|
|
|
2021-12-14 00:00:02 +00:00
|
|
|
<YesNoCancelSelector
|
2021-12-15 09:11:27 +00:00
|
|
|
className="mx-auto my-2"
|
2021-12-14 00:00:02 +00:00
|
|
|
selected={outcome}
|
|
|
|
onSelect={setOutcome}
|
2021-12-15 18:44:34 +00:00
|
|
|
btnClassName={isSubmitting ? 'btn-disabled' : ''}
|
2021-12-14 00:00:02 +00:00
|
|
|
/>
|
|
|
|
|
2022-02-17 23:47:23 +00:00
|
|
|
<Spacer h={4} />
|
2021-12-14 00:00:02 +00:00
|
|
|
|
2021-12-15 09:11:27 +00:00
|
|
|
<div>
|
2021-12-14 00:00:02 +00:00
|
|
|
{outcome === 'YES' ? (
|
|
|
|
<>
|
2022-02-13 23:41:08 +00:00
|
|
|
Winnings will be paid out to YES bettors.
|
|
|
|
<br />
|
|
|
|
<br />
|
|
|
|
You earn {CREATOR_FEE * 100}% of trader profits.
|
2021-12-14 00:00:02 +00:00
|
|
|
</>
|
|
|
|
) : outcome === 'NO' ? (
|
2022-02-03 23:07:30 +00:00
|
|
|
<>
|
2022-02-13 23:41:08 +00:00
|
|
|
Winnings will be paid out to NO bettors.
|
|
|
|
<br />
|
|
|
|
<br />
|
|
|
|
You earn {CREATOR_FEE * 100}% of trader profits.
|
2022-02-03 23:07:30 +00:00
|
|
|
</>
|
2021-12-14 00:00:02 +00:00
|
|
|
) : outcome === 'CANCEL' ? (
|
2021-12-30 21:55:50 +00:00
|
|
|
<>The pool will be returned to traders with no fees.</>
|
2022-01-02 06:27:58 +00:00
|
|
|
) : outcome === 'MKT' ? (
|
2022-02-17 23:47:23 +00:00
|
|
|
<Col className="gap-6">
|
|
|
|
<div>Traders will be paid out at the probability you specify:</div>
|
2022-02-13 23:41:08 +00:00
|
|
|
<ProbabilitySelector
|
|
|
|
probabilityInt={Math.round(prob)}
|
|
|
|
setProbabilityInt={setProb}
|
|
|
|
/>
|
2022-02-17 23:47:23 +00:00
|
|
|
<div>You earn {CREATOR_FEE * 100}% of trader profits.</div>
|
|
|
|
</Col>
|
2021-12-14 00:00:02 +00:00
|
|
|
) : (
|
2021-12-30 21:55:50 +00:00
|
|
|
<>Resolving this market will immediately pay out traders.</>
|
2021-12-14 00:00:02 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
|
2022-02-17 23:47:23 +00:00
|
|
|
<Spacer h={4} />
|
2021-12-14 00:00:02 +00:00
|
|
|
|
2021-12-18 23:40:39 +00:00
|
|
|
{!!error && <div className="text-red-500">{error}</div>}
|
2021-12-15 18:44:34 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
<ResolveConfirmationButton
|
|
|
|
onResolve={resolve}
|
|
|
|
isSubmitting={isSubmitting}
|
|
|
|
openModelButtonClass={clsx('w-full mt-2', submitButtonClass)}
|
|
|
|
submitButtonClass={submitButtonClass}
|
|
|
|
/>
|
2021-12-14 00:00:02 +00:00
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|