2022-02-20 22:25:58 +00:00
|
|
|
import clsx from 'clsx'
|
2022-05-22 08:36:05 +00:00
|
|
|
import { sum, mapValues } from 'lodash'
|
2022-02-20 22:25:58 +00:00
|
|
|
import { useState } from 'react'
|
|
|
|
|
2022-05-09 13:04:36 +00:00
|
|
|
import { DPM, FreeResponse, FullContract } from 'common/contract'
|
2022-02-20 22:25:58 +00:00
|
|
|
import { Col } from '../layout/col'
|
2022-05-19 22:04:34 +00:00
|
|
|
import { resolveMarket } from 'web/lib/firebase/fn-call'
|
2022-02-20 22:25:58 +00:00
|
|
|
import { Row } from '../layout/row'
|
|
|
|
import { ChooseCancelSelector } from '../yes-no-selector'
|
|
|
|
import { ResolveConfirmationButton } from '../confirmation-button'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { removeUndefinedProps } from 'common/util/object'
|
2022-02-20 22:25:58 +00:00
|
|
|
|
|
|
|
export function AnswerResolvePanel(props: {
|
2022-03-15 22:27:51 +00:00
|
|
|
contract: FullContract<DPM, FreeResponse>
|
2022-02-20 22:25:58 +00:00
|
|
|
resolveOption: 'CHOOSE' | 'CHOOSE_MULTIPLE' | 'CANCEL' | undefined
|
|
|
|
setResolveOption: (
|
|
|
|
option: 'CHOOSE' | 'CHOOSE_MULTIPLE' | 'CANCEL' | undefined
|
|
|
|
) => void
|
|
|
|
chosenAnswers: { [answerId: string]: number }
|
|
|
|
}) {
|
|
|
|
const { contract, resolveOption, setResolveOption, chosenAnswers } = props
|
|
|
|
const answers = Object.keys(chosenAnswers)
|
|
|
|
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
|
|
|
const [error, setError] = useState<string | undefined>(undefined)
|
|
|
|
|
|
|
|
const onResolve = async () => {
|
|
|
|
if (resolveOption === 'CHOOSE' && answers.length !== 1) return
|
|
|
|
if (resolveOption === 'CHOOSE_MULTIPLE' && answers.length < 2) return
|
|
|
|
|
|
|
|
setIsSubmitting(true)
|
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const totalProb = sum(Object.values(chosenAnswers))
|
|
|
|
const normalizedProbs = mapValues(
|
2022-02-20 22:25:58 +00:00
|
|
|
chosenAnswers,
|
|
|
|
(prob) => (100 * prob) / totalProb
|
|
|
|
)
|
|
|
|
|
|
|
|
const resolutionProps = removeUndefinedProps({
|
|
|
|
outcome:
|
|
|
|
resolveOption === 'CHOOSE'
|
|
|
|
? answers[0]
|
|
|
|
: resolveOption === 'CHOOSE_MULTIPLE'
|
|
|
|
? 'MKT'
|
|
|
|
: 'CANCEL',
|
|
|
|
resolutions:
|
|
|
|
resolveOption === 'CHOOSE_MULTIPLE' ? normalizedProbs : undefined,
|
|
|
|
contractId: contract.id,
|
|
|
|
})
|
|
|
|
|
|
|
|
const result = await resolveMarket(resolutionProps).then((r) => r.data)
|
|
|
|
|
|
|
|
console.log('resolved', resolutionProps, 'result:', result)
|
|
|
|
|
|
|
|
if (result?.status !== 'success') {
|
|
|
|
setError(result?.message || 'Error resolving market')
|
|
|
|
}
|
|
|
|
setResolveOption(undefined)
|
|
|
|
setIsSubmitting(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
const resolutionButtonClass =
|
|
|
|
resolveOption === 'CANCEL'
|
|
|
|
? 'bg-yellow-400 hover:bg-yellow-500'
|
|
|
|
: resolveOption === 'CHOOSE' && answers.length
|
|
|
|
? 'btn-primary'
|
|
|
|
: resolveOption === 'CHOOSE_MULTIPLE' &&
|
|
|
|
answers.length > 1 &&
|
|
|
|
answers.every((answer) => chosenAnswers[answer] > 0)
|
|
|
|
? 'bg-blue-400 hover:bg-blue-500'
|
|
|
|
: 'btn-disabled'
|
|
|
|
|
|
|
|
return (
|
2022-04-18 23:02:40 +00:00
|
|
|
<Col className="gap-4 rounded">
|
2022-02-20 22:25:58 +00:00
|
|
|
<div>Resolve your market</div>
|
2022-03-03 09:09:32 +00:00
|
|
|
<Col className="gap-4 sm:flex-row sm:items-center">
|
2022-02-20 22:25:58 +00:00
|
|
|
<ChooseCancelSelector
|
|
|
|
className="sm:!flex-row sm:items-center"
|
|
|
|
selected={resolveOption}
|
|
|
|
onSelect={setResolveOption}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<Row
|
|
|
|
className={clsx(
|
|
|
|
'flex-1 items-center',
|
|
|
|
resolveOption ? 'justify-between' : 'justify-end'
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{resolveOption && (
|
|
|
|
<button
|
|
|
|
className="btn btn-ghost"
|
|
|
|
onClick={() => {
|
|
|
|
setResolveOption(undefined)
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Clear
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
<ResolveConfirmationButton
|
|
|
|
onResolve={onResolve}
|
|
|
|
isSubmitting={isSubmitting}
|
2022-03-31 08:38:57 +00:00
|
|
|
openModalButtonClass={resolutionButtonClass}
|
2022-02-20 22:25:58 +00:00
|
|
|
submitButtonClass={resolutionButtonClass}
|
|
|
|
/>
|
|
|
|
</Row>
|
|
|
|
</Col>
|
|
|
|
|
|
|
|
{!!error && <div className="text-red-500">{error}</div>}
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|