Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

N/A all contracts on independent multi #2676

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 73 additions & 16 deletions web/components/answers/answer-resolve-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ import { Col } from '../layout/col'
import { APIError, api } from 'web/lib/firebase/api'
import { Row } from '../layout/row'
import { ChooseCancelSelector } from '../bet/yes-no-selector'
import { ResolveConfirmationButton } from '../buttons/confirmation-button'
import {
CancelAllConfirmationButton,
ResolveConfirmationButton,
} from '../buttons/confirmation-button'
import { removeUndefinedProps } from 'common/util/object'
import { BETTORS } from 'common/user'
import { Button } from '../buttons/button'
import { useUser } from 'web/hooks/use-user'
import { Answer, OTHER_TOOLTIP_TEXT } from 'common/answer'
import { Answer, OTHER_TOOLTIP_TEXT, sortAnswers } from 'common/answer'
import { getAnswerProbability } from 'common/calculate'
import { useDisplayUserByIdOrAnswer } from 'web/hooks/use-user-supabase'
import {
Expand All @@ -32,11 +35,15 @@ import {
ClosedProb,
OpenProb,
} from './answer-components'
import { useAdmin } from 'web/hooks/use-admin'
import { useAdmin, useAdminOrMod } from 'web/hooks/use-admin'
import { GradientContainer } from '../widgets/gradient-container'
import { AmountInput } from '../widgets/amount-input'
import { getAnswerColor } from '../charts/contract/choice'
import { useAnswersCpmm } from 'web/hooks/use-answers'
import { resolveMarketProps } from 'common/api/market-types'
import { z } from 'zod'

type ResolutionProps = z.infer<typeof resolveMarketProps>

function getAnswerResolveButtonColor(
resolveOption: string | undefined,
Expand Down Expand Up @@ -116,18 +123,21 @@ function AnswersResolveOptions(props: {

const resolutionProps = removeUndefinedProps({
contractId: contract.id,
outcome: resolveOption,
outcome:
resolveOption === 'CANCEL'
? 'CANCEL'
: resolveOption === 'CHOOSE_ONE'
? 'MKT'
: resolveOption,
resolutions:
resolveOption === 'CHOOSE_MULTIPLE' ? resolutions : undefined,
answerId: resolveOption === 'CHOOSE_ONE' ? answerIds[0] : undefined,
})

try {
// NOTE(James): I don't understand why this doesn't work without the cast to any.
const result = await api(
'market/:contractId/resolve',
resolutionProps as any
)
const validatedProps: ResolutionProps =
resolveMarketProps.parse(resolutionProps) // Validate props with Zod schema
const result = await api('market/:contractId/resolve', validatedProps)
console.log('resolved', resolutionProps, 'result:', result)
} catch (e) {
if (e instanceof APIError) {
Expand Down Expand Up @@ -419,13 +429,49 @@ export const IndependentAnswersResolvePanel = (props: {

const isAdmin = useAdmin()
const user = useUser()
const answers = useAnswersCpmm(contract.id) ?? contract.answers
const isAdminOrMod = useAdminOrMod()

const { answers, addAnswersMode } = contract
const sortedAnswers = sortBy(
answers,
(a) => (a.resolution ? -a.subsidyPool : -Infinity),
(a) => (addAnswersMode === 'ANYONE' ? -1 * a.prob : a.index)
)
const [isSubmitting, setIsSubmitting] = useState(false)
const [error, setError] = useState<string | undefined>(undefined)

const onCancelAll = async () => {
setIsSubmitting(true)
setError(undefined)

const cancelPromises = answers.map((answer) => {
const resolutionProps: ResolutionProps = removeUndefinedProps({
contractId: contract.id,
outcome: 'CANCEL',
answerId: answer.id,
})

return api('market/:contractId/resolve', resolutionProps)
.then((result) => {
console.log('resolved', resolutionProps, 'result:', result)
})
.catch((e) => {
if (e instanceof APIError) {
setError(e.toString())
} else {
console.error(e)
setError('Error resolving question')
}
})
})

try {
await Promise.all(cancelPromises)
console.log('All contracts canceled successfully')
} catch (e) {
console.error(e)
setError('Error canceling all contracts')
} finally {
setIsSubmitting(false)
}
}

const sortedAnswers = sortAnswers(contract, answers)

return (
<GradientContainer>
Expand All @@ -435,6 +481,16 @@ export const IndependentAnswersResolvePanel = (props: {
isCreator={user?.id === contract.creatorId}
onClose={onClose}
/>
{isAdminOrMod && (
<Row className="justify-end">
<CancelAllConfirmationButton
onResolve={onCancelAll}
isSubmitting={isSubmitting}
marketTitle={contract.question}
color="red"
/>
</Row>
)}
<Col className="gap-2">
{sortedAnswers.map((answer) => (
<IndependentResolutionAnswerItem
Expand All @@ -448,6 +504,7 @@ export const IndependentAnswersResolvePanel = (props: {
</Col>
<ResolutionExplainer independentMulti />
</Col>
{!!error && <div className="text-scarlet-500">{error}</div>}
</GradientContainer>
)
}
Expand All @@ -470,7 +527,7 @@ function IndependentResolutionAnswerItem(props: {
const addAnswersMode = contract.addAnswersMode ?? 'DISABLED'

return (
<GradientContainer className={' shadow-none'}>
<GradientContainer className={'shadow-none'}>
<Col>
<AnswerBar
color={color}
Expand Down
45 changes: 45 additions & 0 deletions web/components/buttons/confirmation-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,48 @@ export function ResolveConfirmationButton(props: {
</ConfirmationButton>
)
}

export function CancelAllConfirmationButton(props: {
onResolve: () => void
isSubmitting: boolean
openModalButtonClass?: string
marketTitle: string

color: ColorType
disabled?: boolean
}) {
const {
onResolve,
isSubmitting,
openModalButtonClass,
color,
marketTitle,
disabled,
} = props
return (
<ConfirmationButton
openModalBtn={{
className: clsx('border-none self-start', openModalButtonClass),
label: 'Cancel All',
color: color,
disabled: isSubmitting || disabled,
size: 'xl',
}}
cancelBtn={{
label: 'Back',
}}
submitBtn={{
label: `Resolve all to N/A`,
color: color,
isSubmitting,
}}
onSubmit={onResolve}
>
<p>
Are you sure you want to resolve all answers on "{marketTitle}" to N/A?
This can take a while.
<br />
</p>
</ConfirmationButton>
)
}
Loading