diff --git a/common/src/calculate-cpmm-arbitrage.ts b/common/src/calculate-cpmm-arbitrage.ts index 6709cf1f6d..bf2383dbc9 100644 --- a/common/src/calculate-cpmm-arbitrage.ts +++ b/common/src/calculate-cpmm-arbitrage.ts @@ -10,6 +10,7 @@ import { computeFills } from './new-bet' import { floatingEqual } from './util/math' import { Fees, getFeesSplit, getTakerFee, noFees, sumAllFees } from './fees' import { addObjects } from './util/object' +import { MAX_CPMM_PROB, MIN_CPMM_PROB } from 'common/contract' const DEBUG = false export type ArbitrageBetArray = ReturnType @@ -18,11 +19,17 @@ export function calculateCpmmMultiArbitrageBet( answerToBuy: Answer, outcome: 'YES' | 'NO', betAmount: number, - limitProb: number | undefined, + initialLimitProb: number | undefined, unfilledBets: LimitBet[], balanceByUserId: { [userId: string]: number }, collectedFees: Fees ) { + const limitProb = + initialLimitProb !== undefined + ? initialLimitProb + : outcome === 'YES' + ? MAX_CPMM_PROB + : MIN_CPMM_PROB const result = outcome === 'YES' ? calculateCpmmMultiArbitrageBetYes( diff --git a/common/src/new-bet.ts b/common/src/new-bet.ts index 62c9a66b3f..50086fa8f3 100644 --- a/common/src/new-bet.ts +++ b/common/src/new-bet.ts @@ -498,7 +498,7 @@ const getNewMultiCpmmBetsInfoSumsToOne = ( answers: Answer[], answersToBuy: Answer[], outcome: 'YES' | 'NO', - betAmount: number, + initialBetAmount: number, limitProb: number | undefined, unfilledBets: LimitBet[], balanceByUserId: { [userId: string]: number }, @@ -512,21 +512,24 @@ const getNewMultiCpmmBetsInfoSumsToOne = ( answers, answersToBuy[0], outcome, - betAmount, + initialBetAmount, limitProb, unfilledBets, balanceByUserId, contract.collectedFees ) + if (newBetResult.takers.length === 0 && !limitProb) { + throw new APIError(400, 'Betting allowed only between 1-99%.') + } newBetResults.push(...([newBetResult] as ArbitrageBetArray)) if (otherBetResults.length > 0) otherBetsResults.push(...(otherBetResults as ArbitrageBetArray)) } else { - // TODO: only accepts YES bets atm + // NOTE: only accepts YES bets atm const multiRes = calculateCpmmMultiArbitrageYesBets( answers, answersToBuy, - betAmount, + initialBetAmount, limitProb, unfilledBets, balanceByUserId, @@ -539,28 +542,33 @@ const getNewMultiCpmmBetsInfoSumsToOne = ( return newBetResults.map((newBetResult, i) => { const { takers, cpmmState, answer: updatedAnswer, totalFees } = newBetResult const probAfter = getCpmmProbability(cpmmState.pool, cpmmState.p) - const amount = sumBy(takers, 'amount') - const shares = sumBy(takers, 'shares') + const takerAmount = sumBy(takers, 'amount') + const takerShares = sumBy(takers, 'shares') const answer = answers.find((a) => a.id === updatedAnswer.id) as Answer - const multiBuyAmount = sumBy( + const multiBuyTakerAmount = sumBy( newBetResults.flatMap((r) => r.takers), 'amount' ) + const betAmount = limitProb + ? initialBetAmount + : isMultiBuy + ? multiBuyTakerAmount + : takerAmount const newBet: CandidateBet = removeUndefinedProps({ + orderAmount: betAmount, + amount: takerAmount, + shares: takerShares, + isFilled: isMultiBuy + ? floatingEqual(multiBuyTakerAmount, betAmount) + : floatingEqual(takerAmount, betAmount), + fills: takers, contractId: contract.id, outcome, - orderAmount: betAmount, limitProb, isCancelled: false, - amount, loanAmount: 0, - shares, answerId: answer.id, - fills: takers, - isFilled: isMultiBuy - ? floatingEqual(multiBuyAmount, betAmount) - : floatingEqual(amount, betAmount), probBefore: answer.prob, probAfter, createdTime: now,