From 2937c0d8919c113f5d6935a678dfe5221330ad55 Mon Sep 17 00:00:00 2001 From: James Grugett Date: Fri, 18 Mar 2022 14:59:06 -0500 Subject: [PATCH] Fix free response mkt payout to default to last probabilities if resolutions not specified --- common/calculate-dpm.ts | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/common/calculate-dpm.ts b/common/calculate-dpm.ts index a4730d14..976d1b57 100644 --- a/common/calculate-dpm.ts +++ b/common/calculate-dpm.ts @@ -218,23 +218,30 @@ function calculateMktDpmPayout(contract: FullContract, bet: Bet) { FreeResponse > - if (!resolutions) - throw new Error( - 'resolutions required for MKT payout of free response market' + let probs: { [outcome: string]: number } + + if (resolutions) { + const probTotal = _.sum(Object.values(resolutions)) + probs = _.mapValues( + totalShares, + (_, outcome) => (resolutions[outcome] ?? 0) / probTotal ) + } else { + const squareSum = _.sum( + Object.values(totalShares).map((shares) => shares ** 2) + ) + probs = _.mapValues(totalShares, (shares) => shares ** 2 / squareSum) + } - const totalPool = _.sum(Object.values(pool)) - const probTotal = _.sum(Object.values(resolutions)) - - const weightedShareTotal = _.sumBy(Object.keys(resolutions), (outcome) => { - const prob = resolutions[outcome] / probTotal - return prob * totalShares[outcome] + const weightedShareTotal = _.sumBy(Object.keys(totalShares), (outcome) => { + return probs[outcome] * totalShares[outcome] }) const { outcome, amount, shares } = bet - const betP = (resolutions[outcome] ?? 0) / probTotal - const winnings = ((betP * shares) / weightedShareTotal) * totalPool + const totalPool = _.sum(Object.values(pool)) + const poolFrac = (probs[outcome] * shares) / weightedShareTotal + const winnings = poolFrac * totalPool return deductDpmFees(amount, winnings) }