From 89ebf344df507069d7f2b6d0222480a919a19f70 Mon Sep 17 00:00:00 2001 From: James Grugett Date: Fri, 6 May 2022 17:00:45 -0400 Subject: [PATCH] Set min and max for contract --- functions/src/create-contract.ts | 9 +++-- web/pages/create.tsx | 62 +++++++++++++++++++++++++++----- 2 files changed, 59 insertions(+), 12 deletions(-) diff --git a/functions/src/create-contract.ts b/functions/src/create-contract.ts index 79fe63ba..c074d33b 100644 --- a/functions/src/create-contract.ts +++ b/functions/src/create-contract.ts @@ -41,6 +41,8 @@ export const createContract = functions ante: number closeTime: number tags?: string[] + min?: number + max?: number }, context ) => { @@ -50,7 +52,8 @@ export const createContract = functions const creator = await getUser(userId) if (!creator) return { status: 'error', message: 'User not found' } - let { question, description, initialProb, closeTime, tags } = data + let { question, description, initialProb, closeTime, tags, min, max } = + data if (!question || typeof question != 'string') return { status: 'error', message: 'Missing or invalid question field' } @@ -121,8 +124,8 @@ export const createContract = functions closeTime, tags ?? [], 100, - 0, - 100 + min ?? 0, + max ?? 100 ) if (!isFree && ante) await chargeUser(creator.id, ante) diff --git a/web/pages/create.tsx b/web/pages/create.tsx index 68ae3ff3..1def1652 100644 --- a/web/pages/create.tsx +++ b/web/pages/create.tsx @@ -19,6 +19,7 @@ import { Row } from '../components/layout/row' import { MAX_DESCRIPTION_LENGTH, outcomeType } from '../../common/contract' import { formatMoney } from '../../common/util/format' import { useHasCreatedContractToday } from '../hooks/use-has-created-contract-today' +import { removeUndefinedProps } from '../../common/util/object' export default function Create() { const [question, setQuestion] = useState('') @@ -66,6 +67,8 @@ export function NewContract(props: { question: string; tag?: string }) { const [outcomeType, setOutcomeType] = useState('BINARY') const [initialProb, setInitialProb] = useState(50) + const [min, setMin] = useState() + const [max, setMax] = useState() const [description, setDescription] = useState('') const [tagText, setTagText] = useState(tag ?? '') const tags = parseWordsAsTags(tagText) @@ -110,15 +113,19 @@ export function NewContract(props: { question: string; tag?: string }) { setIsSubmitting(true) - const result: any = await createContract({ - question, - outcomeType, - description, - initialProb, - ante, - closeTime, - tags, - }).then((r) => r.data || {}) + const result: any = await createContract( + removeUndefinedProps({ + question, + outcomeType, + description, + initialProb, + ante, + closeTime, + tags, + min, + max, + }) + ).then((r) => r.data || {}) if (result.status !== 'success') { console.log('error creating contract', result) @@ -191,6 +198,43 @@ export function NewContract(props: { question: string; tag?: string }) { )} + {outcomeType === 'NUMERIC' && ( +
+ + + + e.stopPropagation()} + onChange={(e) => + setMin(isNaN(+e.target.value) ? undefined : +e.target.value) + } + min={0} + max={Number.MAX_SAFE_INTEGER} + disabled={isSubmitting} + value={min} + /> + e.stopPropagation()} + onChange={(e) => + setMax(isNaN(+e.target.value) ? undefined : +e.target.value) + } + min={0} + max={Number.MAX_SAFE_INTEGER} + disabled={isSubmitting} + value={max} + /> + +
+ )} +