2022-05-19 17:42:03 +00:00
|
|
|
import clsx from 'clsx'
|
2022-07-12 19:36:10 +00:00
|
|
|
import React, { useState } from 'react'
|
2022-05-19 17:42:03 +00:00
|
|
|
|
|
|
|
import { Col } from './layout/col'
|
|
|
|
import { User } from 'web/lib/firebase/users'
|
|
|
|
import { NumberCancelSelector } from './yes-no-selector'
|
|
|
|
import { Spacer } from './layout/spacer'
|
|
|
|
import { ResolveConfirmationButton } from './confirmation-button'
|
2022-07-02 19:37:59 +00:00
|
|
|
import { NumericContract, PseudoNumericContract } from 'common/contract'
|
2022-07-10 22:03:15 +00:00
|
|
|
import { APIError, resolveMarket } from 'web/lib/firebase/api'
|
2022-05-19 17:42:03 +00:00
|
|
|
import { BucketInput } from './bucket-input'
|
2022-07-02 19:37:59 +00:00
|
|
|
import { getPseudoProbability } from 'common/pseudo-numeric'
|
2022-09-15 15:12:56 +00:00
|
|
|
import { BETTOR, BETTORS, PAST_BETS } from 'common/user'
|
2022-05-19 17:42:03 +00:00
|
|
|
|
|
|
|
export function NumericResolutionPanel(props: {
|
2022-09-15 03:28:40 +00:00
|
|
|
isAdmin: boolean
|
|
|
|
isCreator: boolean
|
2022-05-19 17:42:03 +00:00
|
|
|
creator: User
|
2022-07-02 19:37:59 +00:00
|
|
|
contract: NumericContract | PseudoNumericContract
|
2022-05-19 17:42:03 +00:00
|
|
|
className?: string
|
|
|
|
}) {
|
2022-09-15 03:28:40 +00:00
|
|
|
const { contract, className, isAdmin, isCreator } = props
|
2022-07-02 19:37:59 +00:00
|
|
|
const { min, max, outcomeType } = contract
|
2022-05-19 17:42:03 +00:00
|
|
|
|
|
|
|
const [outcomeMode, setOutcomeMode] = useState<
|
|
|
|
'NUMBER' | 'CANCEL' | undefined
|
|
|
|
>()
|
|
|
|
const [outcome, setOutcome] = useState<string | undefined>()
|
|
|
|
const [value, setValue] = useState<number | undefined>()
|
|
|
|
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
|
|
|
const [error, setError] = useState<string | undefined>(undefined)
|
|
|
|
|
|
|
|
const resolve = async () => {
|
2022-07-02 19:37:59 +00:00
|
|
|
const finalOutcome =
|
|
|
|
outcomeMode === 'CANCEL'
|
|
|
|
? 'CANCEL'
|
|
|
|
: outcomeType === 'PSEUDO_NUMERIC'
|
|
|
|
? 'MKT'
|
|
|
|
: 'NUMBER'
|
2022-05-19 17:42:03 +00:00
|
|
|
if (outcomeMode === undefined || finalOutcome === undefined) return
|
|
|
|
|
|
|
|
setIsSubmitting(true)
|
|
|
|
|
2022-07-02 19:37:59 +00:00
|
|
|
const boundedValue = Math.max(Math.min(max, value ?? 0), min)
|
|
|
|
|
|
|
|
const probabilityInt =
|
|
|
|
100 *
|
|
|
|
getPseudoProbability(
|
|
|
|
boundedValue,
|
|
|
|
min,
|
|
|
|
max,
|
|
|
|
outcomeType === 'PSEUDO_NUMERIC' && contract.isLogScale
|
|
|
|
)
|
|
|
|
|
2022-06-29 23:47:06 +00:00
|
|
|
try {
|
|
|
|
const result = await resolveMarket({
|
|
|
|
outcome: finalOutcome,
|
|
|
|
value,
|
2022-07-02 19:37:59 +00:00
|
|
|
probabilityInt,
|
2022-06-29 23:47:06 +00:00
|
|
|
contractId: contract.id,
|
|
|
|
})
|
|
|
|
console.log('resolved', outcome, 'result:', result)
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof APIError) {
|
|
|
|
setError(e.toString())
|
|
|
|
} else {
|
|
|
|
console.error(e)
|
|
|
|
setError('Error resolving market')
|
|
|
|
}
|
2022-05-19 17:42:03 +00:00
|
|
|
}
|
2022-06-29 23:47:06 +00:00
|
|
|
|
2022-05-19 17:42:03 +00:00
|
|
|
setIsSubmitting(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
const submitButtonClass =
|
|
|
|
outcomeMode === 'CANCEL'
|
|
|
|
? 'bg-yellow-400 hover:bg-yellow-500'
|
|
|
|
: outcome !== undefined
|
|
|
|
? 'btn-primary'
|
|
|
|
: 'btn-disabled'
|
|
|
|
|
|
|
|
return (
|
2022-09-15 03:28:40 +00:00
|
|
|
<Col
|
|
|
|
className={clsx(
|
|
|
|
'relative w-full rounded-md bg-white px-8 py-6',
|
|
|
|
className
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{isAdmin && !isCreator && (
|
|
|
|
<span className="absolute right-4 top-4 rounded bg-red-200 p-1 text-xs text-red-600">
|
|
|
|
ADMIN
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
<div className="whitespace-nowrap text-2xl">Resolve market</div>
|
2022-05-19 17:42:03 +00:00
|
|
|
|
2022-09-15 03:28:40 +00:00
|
|
|
<div className="my-3 text-sm text-gray-500">Outcome</div>
|
2022-05-19 17:42:03 +00:00
|
|
|
|
|
|
|
<Spacer h={4} />
|
|
|
|
|
|
|
|
<NumberCancelSelector selected={outcomeMode} onSelect={setOutcomeMode} />
|
|
|
|
|
|
|
|
<Spacer h={4} />
|
|
|
|
|
|
|
|
{outcomeMode === 'NUMBER' && (
|
|
|
|
<BucketInput
|
2022-07-10 18:05:44 +00:00
|
|
|
contract={contract}
|
2022-05-19 17:42:03 +00:00
|
|
|
isSubmitting={isSubmitting}
|
|
|
|
onBucketChange={(v, o) => (setValue(v), setOutcome(o))}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<div>
|
|
|
|
{outcome === 'CANCEL' ? (
|
2022-09-15 15:12:56 +00:00
|
|
|
<>
|
|
|
|
All {PAST_BETS} will be returned. Unique {BETTOR} bonuses will be
|
|
|
|
withdrawn from your account
|
|
|
|
</>
|
2022-05-19 17:42:03 +00:00
|
|
|
) : (
|
2022-09-15 15:12:56 +00:00
|
|
|
<>Resolving this market will immediately pay out {BETTORS}.</>
|
2022-05-19 17:42:03 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Spacer h={4} />
|
|
|
|
|
|
|
|
{!!error && <div className="text-red-500">{error}</div>}
|
|
|
|
|
|
|
|
<ResolveConfirmationButton
|
|
|
|
onResolve={resolve}
|
|
|
|
isSubmitting={isSubmitting}
|
|
|
|
openModalButtonClass={clsx('w-full mt-2', submitButtonClass)}
|
|
|
|
submitButtonClass={submitButtonClass}
|
|
|
|
/>
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|