diff --git a/common/new-bet.ts b/common/new-bet.ts index ea0b011d..1f5c0340 100644 --- a/common/new-bet.ts +++ b/common/new-bet.ts @@ -142,6 +142,13 @@ export const computeFills = ( limitProb: number | undefined, unfilledBets: LimitBet[] ) => { + if (isNaN(betAmount)) { + throw new Error('Invalid bet amount: ${betAmount}') + } + if (isNaN(limitProb ?? 0)) { + throw new Error('Invalid limitProb: ${limitProb}') + } + const sortedBets = sortBy( unfilledBets.filter((bet) => bet.outcome !== outcome), (bet) => (outcome === 'YES' ? bet.limitProb : -bet.limitProb), diff --git a/common/pseudo-numeric.ts b/common/pseudo-numeric.ts index 73f9fd01..ca62a80e 100644 --- a/common/pseudo-numeric.ts +++ b/common/pseudo-numeric.ts @@ -37,6 +37,9 @@ export const getPseudoProbability = ( max: number, isLogScale = false ) => { + if (value < min) return 0 + if (value > max) return 1 + if (isLogScale) { return Math.log10(value - min + 1) / Math.log10(max - min + 1) } diff --git a/web/components/bet-panel.tsx b/web/components/bet-panel.tsx index 902b0040..1d9f128c 100644 --- a/web/components/bet-panel.tsx +++ b/web/components/bet-panel.tsx @@ -1,6 +1,6 @@ import clsx from 'clsx' import React, { useEffect, useState } from 'react' -import { partition, sum, sumBy } from 'lodash' +import { clamp, partition, sum, sumBy } from 'lodash' import { useUser } from 'web/hooks/use-user' import { CPMMBinaryContract, PseudoNumericContract } from 'common/contract' @@ -385,20 +385,22 @@ function LimitOrderPanel(props: { (!hasYesLimitBet && !hasNoLimitBet) const yesLimitProb = - lowLimitProb === undefined ? undefined : lowLimitProb / 100 + lowLimitProb === undefined ? undefined : clamp(lowLimitProb, 0.001, 0.999) const noLimitProb = - highLimitProb === undefined ? undefined : highLimitProb / 100 + highLimitProb === undefined ? undefined : clamp(highLimitProb, 0.001, 0.999) + const amount = betAmount ?? 0 const shares = yesLimitProb !== undefined && noLimitProb !== undefined - ? Math.min( - (betAmount ?? 0) / yesLimitProb, - (betAmount ?? 0) / (1 - noLimitProb) - ) - : (betAmount ?? 0) / (yesLimitProb ?? 1 - (noLimitProb ?? 1)) + ? Math.min(amount / yesLimitProb, amount / (1 - noLimitProb)) + : yesLimitProb !== undefined + ? amount / yesLimitProb + : noLimitProb !== undefined + ? amount / (1 - noLimitProb) + : 0 const yesAmount = shares * (yesLimitProb ?? 1) - const noAmount = shares * (1 - (noLimitProb ?? 1)) + const noAmount = shares * (1 - (noLimitProb ?? 0)) const profitIfBothFilled = shares - (yesAmount + noAmount) @@ -490,7 +492,7 @@ function LimitOrderPanel(props: { 'YES', yesAmount, contract, - Math.min(yesLimitProb ?? initialProb, 0.999), + yesLimitProb ?? initialProb, unfilledBets as LimitBet[] ) const yesReturnPercent = formatPercent(yesReturn) @@ -504,7 +506,7 @@ function LimitOrderPanel(props: { 'NO', noAmount, contract, - Math.max(noLimitProb ?? initialProb, 0.01), + noLimitProb ?? initialProb, unfilledBets as LimitBet[] ) const noReturnPercent = formatPercent(noReturn) @@ -536,17 +538,17 @@ function LimitOrderPanel(props: { - {rangeError && ( -