create log scale market, validation

This commit is contained in:
mantikoros 2022-06-29 16:20:49 -05:00
parent 301d9d8538
commit 1afe62296e
3 changed files with 50 additions and 6 deletions

View File

@ -28,7 +28,8 @@ export function getNewContract(
// used for numeric markets
bucketCount: number,
min: number,
max: number
max: number,
isLogScale: boolean
) {
const tags = parseTags(
`${question} ${description} ${extraTags.map((tag) => `#${tag}`).join(' ')}`
@ -39,7 +40,7 @@ export function getNewContract(
outcomeType === 'BINARY'
? getBinaryCpmmProps(initialProb, ante) // getBinaryDpmProps(initialProb, ante)
: outcomeType === 'PSEUDO_NUMERIC'
? getPseudoNumericCpmmProps(initialProb, ante, min, max, false)
? getPseudoNumericCpmmProps(initialProb, ante, min, max, isLogScale)
: outcomeType === 'NUMERIC'
? getNumericProps(ante, bucketCount, min, max)
: getFreeAnswerProps(ante)

View File

@ -49,21 +49,25 @@ const numericSchema = z.object({
min: z.number(),
max: z.number(),
initialValue: z.number(),
isLogScale: z.boolean().optional(),
})
export const createmarket = newEndpoint(['POST'], async (req, auth) => {
const { question, description, tags, closeTime, outcomeType, groupId } =
validate(bodySchema, req.body)
let min, max, initialProb
let min, max, initialProb, isLogScale
if (outcomeType === 'PSEUDO_NUMERIC' || outcomeType === 'NUMERIC') {
let initialValue
;({ min, max, initialValue } = validate(numericSchema, req.body))
;({ min, max, initialValue, isLogScale } = validate(
numericSchema,
req.body
))
if (max - min <= 0.01 || initialValue < min || initialValue > max)
throw new APIError(400, 'Invalid range.')
initialProb = (initialValue - min) / (max - min) * 100
initialProb = ((initialValue - min) / (max - min)) * 100
}
if (outcomeType === 'BINARY') {
;({ initialProb } = validate(binarySchema, req.body))
@ -127,7 +131,8 @@ export const createmarket = newEndpoint(['POST'], async (req, auth) => {
tags ?? [],
NUMERIC_BUCKET_COUNT,
min ?? 0,
max ?? 0
max ?? 0,
isLogScale ?? false
)
if (ante) await chargeUser(user.id, ante, true)

View File

@ -76,9 +76,12 @@ export function NewContract(props: { question: string; groupId?: string }) {
const [outcomeType, setOutcomeType] = useState<outcomeType>('BINARY')
const [initialProb] = useState(50)
const [minString, setMinString] = useState('')
const [maxString, setMaxString] = useState('')
const [isLogScale, setIsLogScale] = useState(false)
const [initialValueString, setInitialValueString] = useState('')
const [description, setDescription] = useState('')
// const [tagText, setTagText] = useState<string>(tag ?? '')
// const tags = parseWordsAsTags(tagText)
@ -174,6 +177,7 @@ export function NewContract(props: { question: string; groupId?: string }) {
min,
max,
initialValue,
isLogScale,
groupId: selectedGroup?.id,
tags: category ? [category] : undefined,
})
@ -260,12 +264,36 @@ export function NewContract(props: { question: string; groupId?: string }) {
placeholder="MAX"
onClick={(e) => e.stopPropagation()}
onChange={(e) => setMaxString(e.target.value)}
onBlur={() => {
if (min === undefined || max === undefined) return
const lengthDiff = Math.abs(
maxString.length - minString.length
)
if (lengthDiff > 2) {
setIsLogScale(true)
}
}}
min={Number.MIN_SAFE_INTEGER}
max={Number.MAX_SAFE_INTEGER}
disabled={isSubmitting}
value={maxString}
/>
</Row>
<Row className="mt-1 ml-2 mb-2 items-center">
<span className="mr-2 text-sm">Log scale</span>{' '}
<input
type="checkbox"
checked={isLogScale}
onChange={() => setIsLogScale(!isLogScale)}
/>
</Row>
{min !== undefined && max !== undefined && min >= max && (
<div className="mt-2 mb-2 text-sm text-red-500">
The maximum value must be greater than the minimum.
</div>
)}
</div>
<div className="form-control mb-2 items-start">
<label className="label gap-2">
@ -285,6 +313,16 @@ export function NewContract(props: { question: string; groupId?: string }) {
value={initialValueString ?? ''}
/>
</Row>
{initialValue !== undefined &&
min !== undefined &&
max !== undefined &&
min < max &&
(initialValue < min || initialValue > max) && (
<div className="mt-2 mb-2 text-sm text-red-500">
Initial value must be in between {min} and {max}.{' '}
</div>
)}
</div>
</>
)}