diff --git a/common/calculate.ts b/common/calculate.ts index 197efad1..61a65f38 100644 --- a/common/calculate.ts +++ b/common/calculate.ts @@ -68,14 +68,18 @@ export function calculateRawShareValue( return currentValue - postSaleValue } -export function calculateMoneyRatio(contract: Contract) { +export function calculateMoneyRatio(contract: Contract, bet: Bet) { const { totalShares, pool } = contract + const { amount } = bet + const p = getProbability(totalShares) - const actual = pool.YES + pool.NO - const expected = p * contract.totalBets.YES + (1 - p) * contract.totalBets.NO + const actual = pool.YES + pool.NO - amount - return expected === 0 ? 0 : actual / expected + const expected = + p * contract.totalBets.YES + (1 - p) * contract.totalBets.NO - amount + + return expected <= 0 ? 0 : actual / expected } export function calculateShareValue(contract: Contract, bet: Bet) { @@ -84,7 +88,7 @@ export function calculateShareValue(contract: Contract, bet: Bet) { bet.shares, bet.outcome ) - const f = calculateMoneyRatio(contract) + const f = calculateMoneyRatio(contract, bet) const myPool = contract.pool[bet.outcome] const adjShareValue = Math.min(Math.min(1, f) * shareValue, myPool) @@ -92,7 +96,7 @@ export function calculateShareValue(contract: Contract, bet: Bet) { } export function calculateSaleAmount(contract: Contract, bet: Bet) { - return (1 - FEES) * calculateShareValue(contract, bet) + return (1 - 2 * FEES) * calculateShareValue(contract, bet) } export function calculatePayout( @@ -133,7 +137,7 @@ export function calculateStandardPayout( totalShares[outcome] - phantomShares[outcome] - totalBets[outcome] const winningsPool = truePool - totalBets[outcome] - return (1 - FEES) * (amount + ((shares - amount) / total) * winningsPool) + return amount + (1 - 2 * FEES) * ((shares - amount) / total) * winningsPool } export function calculatePayoutAfterCorrectBet(contract: Contract, bet: Bet) { @@ -188,9 +192,10 @@ function calculateMktPayout(contract: Contract, bet: Bet) { contract.totalBets.NO) return ( - (1 - FEES) * - (betP * bet.amount + - ((betP * (bet.shares - bet.amount)) / weightedShareTotal) * winningsPool) + betP * bet.amount + + (1 - 2 * FEES) * + ((betP * (bet.shares - bet.amount)) / weightedShareTotal) * + winningsPool ) } diff --git a/common/payouts.ts b/common/payouts.ts index 37efb623..98f1d23e 100644 --- a/common/payouts.ts +++ b/common/payouts.ts @@ -27,7 +27,21 @@ export const getStandardPayouts = ( if (betSum >= truePool) return getCancelPayouts(truePool, winningBets) - const creatorPayout = CREATOR_FEE * truePool + const shareDifferenceSum = sumBy(winningBets, (b) => b.shares - b.amount) + + const winningsPool = truePool - betSum + + const winnerPayouts = winningBets.map((bet) => ({ + userId: bet.userId, + payout: + bet.amount + + (1 - 2 * FEES) * + ((bet.shares - bet.amount) / shareDifferenceSum) * + winningsPool, + })) + + const creatorPayout = 2 * CREATOR_FEE * winningsPool + console.log( 'resolved', outcome, @@ -37,18 +51,6 @@ export const getStandardPayouts = ( creatorPayout ) - const shareDifferenceSum = sumBy(winningBets, (b) => b.shares - b.amount) - - const winningsPool = truePool - betSum - - const winnerPayouts = winningBets.map((bet) => ({ - userId: bet.userId, - payout: - (1 - FEES) * - (bet.amount + - ((bet.shares - bet.amount) / shareDifferenceSum) * winningsPool), - })) - return winnerPayouts.concat([ { userId: contract.creatorId, payout: creatorPayout }, ]) // add creator fee @@ -87,21 +89,22 @@ export const getMktPayouts = ( const yesPayouts = yesBets.map((bet) => ({ userId: bet.userId, payout: - (1 - FEES) * - (p * bet.amount + - ((p * (bet.shares - bet.amount)) / weightedShareTotal) * winningsPool), + p * bet.amount + + (1 - 2 * FEES) * + ((p * (bet.shares - bet.amount)) / weightedShareTotal) * + winningsPool, })) const noPayouts = noBets.map((bet) => ({ userId: bet.userId, payout: - (1 - FEES) * - ((1 - p) * bet.amount + + (1 - p) * bet.amount + + (1 - 2 * FEES) * (((1 - p) * (bet.shares - bet.amount)) / weightedShareTotal) * - winningsPool), + winningsPool, })) - const creatorPayout = CREATOR_FEE * truePool + const creatorPayout = 2 * CREATOR_FEE * winningsPool return [ ...yesPayouts, diff --git a/common/sell-bet.ts b/common/sell-bet.ts index eeeed355..e3080644 100644 --- a/common/sell-bet.ts +++ b/common/sell-bet.ts @@ -36,8 +36,8 @@ export const getSellBetInfo = ( const probBefore = getProbability(contract.totalShares) const probAfter = getProbability(newTotalShares) - const creatorFee = CREATOR_FEE * adjShareValue - const saleAmount = (1 - FEES) * adjShareValue + const creatorFee = 2 * CREATOR_FEE * adjShareValue + const saleAmount = (1 - 2 * FEES) * adjShareValue console.log( 'SELL M$',