Use min and max to map the input

This commit is contained in:
James Grugett 2022-05-06 17:26:32 -04:00
parent 83c89ad940
commit b7b146a35a
4 changed files with 27 additions and 16 deletions

View File

@ -63,6 +63,13 @@ export function getNumericBets(
return bets return bets
} }
export const getMappedBucket = (value: number, contract: NumericContract) => {
const { bucketCount, min, max } = contract
const cappedValue = Math.min(Math.max(min, value), max)
const mapped = Math.floor((bucketCount * (cappedValue - min)) / (max - min))
return `${mapped}`
}
export function getDpmOutcomeProbabilityAfterBet( export function getDpmOutcomeProbabilityAfterBet(
totalShares: { totalShares: {
[outcome: string]: number [outcome: string]: number

View File

@ -141,7 +141,6 @@ export function BuyAmountInput(props: {
export function BucketAmountInput(props: { export function BucketAmountInput(props: {
bucket: number | undefined bucket: number | undefined
bucketCount: number
min: number min: number
max: number max: number
onChange: (newBucket: number | undefined) => void onChange: (newBucket: number | undefined) => void
@ -155,7 +154,6 @@ export function BucketAmountInput(props: {
}) { }) {
const { const {
bucket, bucket,
bucketCount,
min, min,
max, max,
onChange, onChange,
@ -172,8 +170,8 @@ export function BucketAmountInput(props: {
// Check for errors. // Check for errors.
if (bucket !== undefined) { if (bucket !== undefined) {
if (bucket < 0 || bucket >= bucketCount) { if (bucket < min || bucket > max) {
setError('Enter a number between 0 and ' + (bucketCount - 1)) setError(`Enter a number between ${min} and ${max}`)
} else { } else {
setError(undefined) setError(undefined)
} }

View File

@ -7,7 +7,7 @@ import {
calculatePayoutAfterCorrectBet, calculatePayoutAfterCorrectBet,
getOutcomeProbability, getOutcomeProbability,
} from '../../common/calculate' } from '../../common/calculate'
import { getNumericBets } from '../../common/calculate-dpm' import { getMappedBucket, getNumericBets } from '../../common/calculate-dpm'
import { NumericContract } from '../../common/contract' import { NumericContract } from '../../common/contract'
import { formatPercent, formatMoney } from '../../common/util/format' import { formatPercent, formatMoney } from '../../common/util/format'
import { useUser } from '../hooks/use-user' import { useUser } from '../hooks/use-user'
@ -49,20 +49,19 @@ function NumericBuyPanel(props: {
onBuySuccess?: () => void onBuySuccess?: () => void
}) { }) {
const { contract, user, onBuySuccess } = props const { contract, user, onBuySuccess } = props
const { bucketCount, min, max } = contract const { min, max } = contract
const [bucketChoice, setBucketChoice] = useState<string | undefined>( const [bucket, setBucketChoice] = useState<number | undefined>(undefined)
undefined const bucketChoice = bucket === undefined ? undefined : `${bucket}`
)
const [betAmount, setBetAmount] = useState<number | undefined>(undefined) const [betAmount, setBetAmount] = useState<number | undefined>(undefined)
const [valueError, setValueError] = useState<string | undefined>() const [valueError, setValueError] = useState<string | undefined>()
const [error, setError] = useState<string | undefined>() const [error, setError] = useState<string | undefined>()
const [isSubmitting, setIsSubmitting] = useState(false) const [isSubmitting, setIsSubmitting] = useState(false)
const [wasSubmitted, setWasSubmitted] = useState(false) const [wasSubmitted, setWasSubmitted] = useState(false)
function onBucketChange(newBucket: string | undefined) { function onBucketChange(newBucket: number | undefined) {
setWasSubmitted(false)
setBucketChoice(newBucket) setBucketChoice(newBucket)
setWasSubmitted(false)
} }
function onBetChange(newAmount: number | undefined) { function onBetChange(newAmount: number | undefined) {
@ -71,7 +70,9 @@ function NumericBuyPanel(props: {
} }
async function submitBet() { async function submitBet() {
if (!user || !betAmount || !bucketChoice) return if (!user || !betAmount || bucket === undefined) return
const bucketChoice = getMappedBucket(bucket, contract)
setError(undefined) setError(undefined)
setIsSubmitting(true) setIsSubmitting(true)
@ -132,11 +133,10 @@ function NumericBuyPanel(props: {
<div className="my-3 text-left text-sm text-gray-500">Numeric value</div> <div className="my-3 text-left text-sm text-gray-500">Numeric value</div>
<BucketAmountInput <BucketAmountInput
bucket={bucketChoice ? +bucketChoice : undefined} bucket={bucketChoice ? +bucketChoice : undefined}
bucketCount={bucketCount}
min={min} min={min}
max={max} max={max}
inputClassName="w-full max-w-none" inputClassName="w-full max-w-none"
onChange={(bucket) => onBucketChange(bucket ? `${bucket}` : undefined)} onChange={onBucketChange}
error={valueError} error={valueError}
setError={setValueError} setError={setValueError}
disabled={isSubmitting} disabled={isSubmitting}

View File

@ -19,6 +19,7 @@ import {
} from '../../common/contract' } from '../../common/contract'
import { formatMoney } from '../../common/util/format' import { formatMoney } from '../../common/util/format'
import { BucketAmountInput } from './amount-input' import { BucketAmountInput } from './amount-input'
import { getMappedBucket } from '../../common/calculate-dpm'
export function ResolutionPanel(props: { export function ResolutionPanel(props: {
creator: User creator: User
@ -161,10 +162,16 @@ export function NumericResolutionPanel(props: {
const resolve = async () => { const resolve = async () => {
if (!outcome) return if (!outcome) return
let outcomeChoice = outcome
if (outcome !== 'CANCEL') {
const bucket = getMappedBucket(+outcome, contract)
outcomeChoice = `${bucket}`
}
setIsSubmitting(true) setIsSubmitting(true)
const result = await resolveMarket({ const result = await resolveMarket({
outcome: outcome, outcome: outcomeChoice,
contractId: contract.id, contractId: contract.id,
}).then((r) => r.data) }).then((r) => r.data)
@ -201,7 +208,6 @@ export function NumericResolutionPanel(props: {
<> <>
<BucketAmountInput <BucketAmountInput
bucket={outcome && outcome !== 'CANCEL' ? +outcome : undefined} bucket={outcome && outcome !== 'CANCEL' ? +outcome : undefined}
bucketCount={bucketCount}
min={min} min={min}
max={max} max={max}
inputClassName="w-full max-w-none" inputClassName="w-full max-w-none"