diff --git a/common/new-bet.ts b/common/new-bet.ts index c000b764..13b008b0 100644 --- a/common/new-bet.ts +++ b/common/new-bet.ts @@ -262,7 +262,7 @@ export const getBinaryBetStats = ( const totalFees = sum(Object.values(newBet.fees)) - return { currentPayout, currentReturn, totalFees } + return { currentPayout, currentReturn, totalFees, newBet } } export const getBinaryRangeBetInfo = ( diff --git a/web/components/bet-panel.tsx b/web/components/bet-panel.tsx index a48eaa65..e90e4ec7 100644 --- a/web/components/bet-panel.tsx +++ b/web/components/bet-panel.tsx @@ -428,35 +428,58 @@ function RangeOrderPanel(props: { highLimitProb !== undefined && lowLimitProb >= highLimitProb + const betDisabled = isSubmitting || !betAmount || rangeError || error + + const hasYesLimitBet = lowLimitProb !== undefined && !!betAmount + const hasNoLimitBet = highLimitProb !== undefined && !!betAmount + const hasTwoBets = hasYesLimitBet && hasNoLimitBet + + const yesLimitProb = (lowLimitProb ?? initialProb * 100) / 100 + const noLimitProb = (highLimitProb ?? initialProb * 100) / 100 + + const shares = Math.min( + (betAmount ?? 0) / yesLimitProb, + (betAmount ?? 0) / (1 - noLimitProb) + ) + const yesAmount = shares * yesLimitProb + const noAmount = shares * (1 - noLimitProb) + + const profitIfBothFilled = shares - (yesAmount + noAmount) + function onBetChange(newAmount: number | undefined) { setWasSubmitted(false) setBetAmount(newAmount) } async function submitBet() { - if (!user || !betAmount || rangeError) return - - const limitProbScaled = - lowLimitProb !== undefined ? lowLimitProb / 100 : undefined + if (!user || betDisabled) return setError(undefined) setIsSubmitting(true) - placeBet( - removeUndefinedProps({ - amount: betAmount, - outcome: betChoice, - contractId: contract.id, - limitProb: limitProbScaled, - }) - ) - .then((r) => { - console.log('placed bet. Result:', r) - setIsSubmitting(false) - setWasSubmitted(true) - setBetAmount(undefined) - if (onBuySuccess) onBuySuccess() - }) + const betsPromise = hasTwoBets + ? Promise.all([ + placeBet({ + betChoice: 'YES', + amount: yesAmount, + limitProb: yesLimitProb, + contractId: contract.id, + }), + placeBet({ + betChoice: 'NO', + amount: noAmount, + limitProb: noLimitProb, + contractId: contract.id, + }), + ]) + : placeBet({ + betChoice: hasYesLimitBet ? 'YES' : 'NO', + amount: betAmount, + contractId: contract.id, + limitProb: hasYesLimitBet ? yesLimitProb : noLimitProb, + }) + + betsPromise .catch((e) => { if (e instanceof APIError) { setError(e.toString()) @@ -466,54 +489,75 @@ function RangeOrderPanel(props: { } setIsSubmitting(false) }) + .then((r) => { + console.log('placed bet. Result:', r) + setIsSubmitting(false) + setWasSubmitted(true) + setBetAmount(undefined) + if (onBuySuccess) onBuySuccess() + }) - track('bet', { - location: 'bet panel', - outcomeType: contract.outcomeType, - slug: contract.slug, - contractId: contract.id, - amount: betAmount, - outcome: betChoice, - isLimitOrder: true, - limitProb: limitProbScaled, - }) + if (hasYesLimitBet) { + track('bet', { + location: 'bet panel', + outcomeType: contract.outcomeType, + slug: contract.slug, + contractId: contract.id, + amount: yesAmount, + outcome: 'YES', + limitProb: yesLimitProb, + isLimitOrder: true, + isRangeOrder: hasTwoBets, + }) + } + if (hasNoLimitBet) { + track('bet', { + location: 'bet panel', + outcomeType: contract.outcomeType, + slug: contract.slug, + contractId: contract.id, + amount: noAmount, + outcome: 'NO', + limitProb: noLimitProb, + isLimitOrder: true, + isRangeOrder: hasTwoBets, + }) + } } - const betDisabled = isSubmitting || !betAmount || rangeError || error - - const lowProbFrac = (lowLimitProb ?? initialProb * 100) / 100 const { - currentPayout: lowPayout, - currentReturn: lowReturn, - totalFees: lowFees, + currentPayout: yesPayout, + currentReturn: yesReturn, + totalFees: yesFees, + newBet: yesBet, } = getBinaryBetStats( 'YES', - betAmount ?? 0, + yesAmount, contract, - lowProbFrac, + yesLimitProb, unfilledBets as LimitBet[] ) - const lowReturnPercent = formatPercent(lowReturn) + const yesReturnPercent = formatPercent(yesReturn) - const highProbFrac = (highLimitProb ?? initialProb * 100) / 100 const { - currentPayout: highPayout, - currentReturn: highReturn, - totalFees: highFees, + currentPayout: noPayout, + currentReturn: noReturn, + totalFees: noFees, + newBet: noBet, } = getBinaryBetStats( 'NO', - betAmount ?? 0, + noAmount, contract, - highProbFrac, + noLimitProb, unfilledBets as LimitBet[] ) - const highReturnPercent = formatPercent(highReturn) + const noReturnPercent = formatPercent(noReturn) return ( <>
Bet only when the {isPseudoNumeric ? 'value' : 'probability'} reaches - low or high limit. + Low or High limit.
@@ -541,7 +585,7 @@ function RangeOrderPanel(props: { {rangeError && (
- Low limit must be less than high limit + Low limit must be less than High limit
)} @@ -558,7 +602,50 @@ function RangeOrderPanel(props: { /> - {lowLimitProb !== undefined && ( + {hasYesLimitBet && ( + +
+ {isPseudoNumeric ? ( + 'Bought now' + ) : ( + <> + bought now + + )} +
+
+ {formatMoney(yesBet.amount)}/ + {formatMoney(yesBet.orderAmount ?? 0)} +
+
+ )} + {hasNoLimitBet && ( + +
+ {isPseudoNumeric ? ( + 'Bought now' + ) : ( + <> + bought now + + )} +
+
+ {formatMoney(noBet.amount)}/{formatMoney(noBet.orderAmount ?? 0)} +
+
+ )} + {hasTwoBets && ( + +
+ Profit if both orders filled +
+
+ {formatMoney(profitIfBothFilled)} +
+
+ )} + {hasYesLimitBet && !hasTwoBets && (
@@ -571,18 +658,18 @@ function RangeOrderPanel(props: { )}
- {formatMoney(lowPayout)} + {formatMoney(yesPayout)} - (+{lowReturnPercent}) + (+{yesReturnPercent})
)} - {highLimitProb !== undefined && ( + {hasNoLimitBet && !hasTwoBets && (
@@ -595,22 +682,20 @@ function RangeOrderPanel(props: { )}
- {formatMoney(highPayout)} + {formatMoney(noPayout)} - (+{highReturnPercent}) + (+{noReturnPercent})
)} - {(lowLimitProb !== undefined || highLimitProb !== undefined) && ( - - )} + {(hasYesLimitBet || hasNoLimitBet) && } {user && ( )} diff --git a/web/components/bucket-input.tsx b/web/components/bucket-input.tsx index 195032dc..85a872b9 100644 --- a/web/components/bucket-input.tsx +++ b/web/components/bucket-input.tsx @@ -39,7 +39,7 @@ export function BucketInput(props: { error={undefined} disabled={isSubmitting} numberString={numberString} - label="Value" + label="" /> ) }