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 1 commit
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
61 changes: 56 additions & 5 deletions web/components/answers/answer-resolve-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ 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'
Expand All @@ -32,7 +35,7 @@ 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'
Expand Down Expand Up @@ -419,12 +422,49 @@ export const IndependentAnswersResolvePanel = (props: {

const isAdmin = useAdmin()
const user = useUser()
const answers = useAnswersCpmm(contract.id) ?? contract.answers
const isAdminorMod = useAdminOrMod()
SirSaltyy marked this conversation as resolved.
Show resolved Hide resolved

const [isSubmitting, setIsSubmitting] = useState(false)
const [error, setError] = useState<string | undefined>(undefined)

const onCancelAll = async () => {
setIsSubmitting(true)
try {
for (const answer of answers) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ask chat gpt to turn this for loop into a Promise.all. This will make it only a bit faster because the server will still resolve each answer sequentially.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't that hit by-IP rate limit?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question! I don't know our exact limits, but I think a hundred requests in a short period would be ok.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this (see recent commit), but I think it has introduced an API error.

APIError: High volume of requests. Please try again later.

It managed to get through about 3-4 N/As each time before it then failed to resolve the rest.

const resolutionProps = removeUndefinedProps({
contractId: contract.id,
outcome: 'CANCEL',
answerId: answer.id,
})

try {
const result = await api(
'market/:contractId/resolve',
resolutionProps as any
SirSaltyy marked this conversation as resolved.
Show resolved Hide resolved
)
console.log('resolved', resolutionProps, 'result:', result)
} catch (e) {
if (e instanceof APIError) {
setError(e.toString())
} else {
console.error(e)
setError('Error resolving question')
}
}
}
console.log('All contracts canceled')
} catch (e) {
console.error(e)
setError('Error canceling all contracts')
}
SirSaltyy marked this conversation as resolved.
Show resolved Hide resolved
setIsSubmitting(false)
}

const { answers, addAnswersMode } = contract
const sortedAnswers = sortBy(
answers,
(a) => (a.resolution ? -a.subsidyPool : -Infinity),
(a) => (addAnswersMode === 'ANYONE' ? -1 * a.prob : a.index)
(a) => a.index
SirSaltyy marked this conversation as resolved.
Show resolved Hide resolved
)

return (
Expand All @@ -435,6 +475,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 +498,7 @@ export const IndependentAnswersResolvePanel = (props: {
</Col>
<ResolutionExplainer independentMulti />
</Col>
{!!error && <div className="text-scarlet-500">{error}</div>}
</GradientContainer>
)
}
Expand All @@ -470,7 +521,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