2022-05-22 08:36:05 +00:00
|
|
|
import { sum, groupBy, sumBy, mapValues } from 'lodash'
|
2022-03-15 22:27:51 +00:00
|
|
|
|
2022-05-19 17:42:03 +00:00
|
|
|
import { Bet, NumericBet } from './bet'
|
2022-03-15 22:27:51 +00:00
|
|
|
import { deductDpmFees, getDpmProbability } from './calculate-dpm'
|
2022-07-28 02:40:33 +00:00
|
|
|
import {
|
|
|
|
DPMContract,
|
|
|
|
FreeResponseContract,
|
|
|
|
MultipleChoiceContract,
|
|
|
|
} from './contract'
|
2022-05-27 21:02:02 +00:00
|
|
|
import { DPM_CREATOR_FEE, DPM_FEES, DPM_PLATFORM_FEE } from './fees'
|
2022-03-15 22:27:51 +00:00
|
|
|
import { addObjects } from './util/object'
|
|
|
|
|
2022-06-01 02:42:35 +00:00
|
|
|
export const getDpmCancelPayouts = (contract: DPMContract, bets: Bet[]) => {
|
2022-03-15 22:27:51 +00:00
|
|
|
const { pool } = contract
|
2022-05-22 08:36:05 +00:00
|
|
|
const poolTotal = sum(Object.values(pool))
|
2022-03-15 22:27:51 +00:00
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const betSum = sumBy(bets, (b) => b.amount)
|
2022-03-15 22:27:51 +00:00
|
|
|
|
|
|
|
const payouts = bets.map((bet) => ({
|
|
|
|
userId: bet.userId,
|
|
|
|
payout: (bet.amount / betSum) * poolTotal,
|
|
|
|
}))
|
|
|
|
|
2022-05-09 21:04:40 +00:00
|
|
|
return {
|
|
|
|
payouts,
|
|
|
|
creatorPayout: 0,
|
|
|
|
liquidityPayouts: [],
|
2022-05-27 21:02:02 +00:00
|
|
|
collectedFees: contract.collectedFees,
|
2022-05-09 21:04:40 +00:00
|
|
|
}
|
2022-03-15 22:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getDpmStandardPayouts = (
|
|
|
|
outcome: string,
|
2022-06-01 02:42:35 +00:00
|
|
|
contract: DPMContract,
|
2022-03-15 22:27:51 +00:00
|
|
|
bets: Bet[]
|
|
|
|
) => {
|
|
|
|
const winningBets = bets.filter((bet) => bet.outcome === outcome)
|
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const poolTotal = sum(Object.values(contract.pool))
|
|
|
|
const totalShares = sumBy(winningBets, (b) => b.shares)
|
2022-03-15 22:27:51 +00:00
|
|
|
|
|
|
|
const payouts = winningBets.map(({ userId, amount, shares }) => {
|
|
|
|
const winnings = (shares / totalShares) * poolTotal
|
|
|
|
const profit = winnings - amount
|
|
|
|
|
|
|
|
// profit can be negative if using phantom shares
|
|
|
|
const payout = amount + (1 - DPM_FEES) * Math.max(0, profit)
|
|
|
|
return { userId, profit, payout }
|
|
|
|
})
|
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const profits = sumBy(payouts, (po) => Math.max(0, po.profit))
|
2022-03-15 22:27:51 +00:00
|
|
|
const creatorFee = DPM_CREATOR_FEE * profits
|
|
|
|
const platformFee = DPM_PLATFORM_FEE * profits
|
2022-05-27 21:02:02 +00:00
|
|
|
const collectedFees = addObjects(contract.collectedFees, {
|
2022-03-15 22:27:51 +00:00
|
|
|
creatorFee,
|
|
|
|
platformFee,
|
|
|
|
liquidityFee: 0,
|
2022-05-27 21:02:02 +00:00
|
|
|
})
|
2022-03-15 22:27:51 +00:00
|
|
|
|
2022-05-19 17:42:03 +00:00
|
|
|
return {
|
|
|
|
payouts: payouts.map(({ userId, payout }) => ({ userId, payout })),
|
|
|
|
creatorPayout: creatorFee,
|
|
|
|
liquidityPayouts: [],
|
|
|
|
collectedFees,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getNumericDpmPayouts = (
|
|
|
|
outcome: string,
|
2022-06-01 02:42:35 +00:00
|
|
|
contract: DPMContract,
|
2022-05-19 17:42:03 +00:00
|
|
|
bets: NumericBet[]
|
|
|
|
) => {
|
2022-05-22 08:36:05 +00:00
|
|
|
const totalShares = sumBy(bets, (bet) => bet.allOutcomeShares[outcome] ?? 0)
|
2022-05-19 17:42:03 +00:00
|
|
|
const winningBets = bets.filter((bet) => !!bet.allOutcomeShares[outcome])
|
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const poolTotal = sum(Object.values(contract.pool))
|
2022-05-19 17:42:03 +00:00
|
|
|
|
|
|
|
const payouts = winningBets.map(
|
|
|
|
({ userId, allBetAmounts, allOutcomeShares }) => {
|
|
|
|
const shares = allOutcomeShares[outcome] ?? 0
|
|
|
|
const winnings = (shares / totalShares) * poolTotal
|
|
|
|
|
|
|
|
const amount = allBetAmounts[outcome] ?? 0
|
|
|
|
const profit = winnings - amount
|
|
|
|
|
|
|
|
// profit can be negative if using phantom shares
|
|
|
|
const payout = amount + (1 - DPM_FEES) * Math.max(0, profit)
|
|
|
|
return { userId, profit, payout }
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const profits = sumBy(payouts, (po) => Math.max(0, po.profit))
|
2022-05-19 17:42:03 +00:00
|
|
|
const creatorFee = DPM_CREATOR_FEE * profits
|
|
|
|
const platformFee = DPM_PLATFORM_FEE * profits
|
2022-05-27 21:02:02 +00:00
|
|
|
const collectedFees = addObjects(contract.collectedFees, {
|
2022-05-19 17:42:03 +00:00
|
|
|
creatorFee,
|
|
|
|
platformFee,
|
|
|
|
liquidityFee: 0,
|
2022-05-27 21:02:02 +00:00
|
|
|
})
|
2022-05-19 17:42:03 +00:00
|
|
|
|
2022-05-09 21:04:40 +00:00
|
|
|
return {
|
|
|
|
payouts: payouts.map(({ userId, payout }) => ({ userId, payout })),
|
|
|
|
creatorPayout: creatorFee,
|
|
|
|
liquidityPayouts: [],
|
|
|
|
collectedFees,
|
|
|
|
}
|
2022-03-15 22:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getDpmMktPayouts = (
|
2022-06-01 02:42:35 +00:00
|
|
|
contract: DPMContract,
|
2022-03-15 22:27:51 +00:00
|
|
|
bets: Bet[],
|
|
|
|
resolutionProbability?: number
|
|
|
|
) => {
|
|
|
|
const p =
|
|
|
|
resolutionProbability === undefined
|
|
|
|
? getDpmProbability(contract.totalShares)
|
|
|
|
: resolutionProbability
|
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const weightedShareTotal = sumBy(bets, (b) =>
|
2022-03-15 22:27:51 +00:00
|
|
|
b.outcome === 'YES' ? p * b.shares : (1 - p) * b.shares
|
|
|
|
)
|
|
|
|
|
|
|
|
const pool = contract.pool.YES + contract.pool.NO
|
|
|
|
|
|
|
|
const payouts = bets.map(({ userId, outcome, amount, shares }) => {
|
|
|
|
const betP = outcome === 'YES' ? p : 1 - p
|
|
|
|
const winnings = ((betP * shares) / weightedShareTotal) * pool
|
|
|
|
const profit = winnings - amount
|
|
|
|
const payout = deductDpmFees(amount, winnings)
|
|
|
|
return { userId, profit, payout }
|
|
|
|
})
|
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const profits = sumBy(payouts, (po) => Math.max(0, po.profit))
|
2022-03-15 22:27:51 +00:00
|
|
|
|
|
|
|
const creatorFee = DPM_CREATOR_FEE * profits
|
|
|
|
const platformFee = DPM_PLATFORM_FEE * profits
|
2022-05-27 21:02:02 +00:00
|
|
|
const collectedFees = addObjects(contract.collectedFees, {
|
2022-03-15 22:27:51 +00:00
|
|
|
creatorFee,
|
|
|
|
platformFee,
|
|
|
|
liquidityFee: 0,
|
2022-05-27 21:02:02 +00:00
|
|
|
})
|
2022-03-15 22:27:51 +00:00
|
|
|
|
2022-05-09 21:04:40 +00:00
|
|
|
return {
|
|
|
|
payouts: payouts.map(({ userId, payout }) => ({ userId, payout })),
|
|
|
|
creatorPayout: creatorFee,
|
|
|
|
liquidityPayouts: [],
|
|
|
|
collectedFees,
|
|
|
|
}
|
2022-03-15 22:27:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getPayoutsMultiOutcome = (
|
|
|
|
resolutions: { [outcome: string]: number },
|
2022-07-28 02:40:33 +00:00
|
|
|
contract: FreeResponseContract | MultipleChoiceContract,
|
2022-03-15 22:27:51 +00:00
|
|
|
bets: Bet[]
|
|
|
|
) => {
|
2022-05-22 08:36:05 +00:00
|
|
|
const poolTotal = sum(Object.values(contract.pool))
|
2022-03-15 22:27:51 +00:00
|
|
|
const winningBets = bets.filter((bet) => resolutions[bet.outcome])
|
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const betsByOutcome = groupBy(winningBets, (bet) => bet.outcome)
|
|
|
|
const sharesByOutcome = mapValues(betsByOutcome, (bets) =>
|
|
|
|
sumBy(bets, (bet) => bet.shares)
|
2022-03-15 22:27:51 +00:00
|
|
|
)
|
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const probTotal = sum(Object.values(resolutions))
|
2022-03-15 22:27:51 +00:00
|
|
|
|
|
|
|
const payouts = winningBets.map(({ userId, outcome, amount, shares }) => {
|
|
|
|
const prob = resolutions[outcome] / probTotal
|
|
|
|
const winnings = (shares / sharesByOutcome[outcome]) * prob * poolTotal
|
|
|
|
const profit = winnings - amount
|
|
|
|
|
2022-09-26 19:57:21 +00:00
|
|
|
const payout = amount + (1 - DPM_FEES) * profit
|
2022-03-15 22:27:51 +00:00
|
|
|
return { userId, profit, payout }
|
|
|
|
})
|
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const profits = sumBy(payouts, (po) => po.profit)
|
2022-03-15 22:27:51 +00:00
|
|
|
|
|
|
|
const creatorFee = DPM_CREATOR_FEE * profits
|
|
|
|
const platformFee = DPM_PLATFORM_FEE * profits
|
2022-05-27 21:02:02 +00:00
|
|
|
const collectedFees = addObjects(contract.collectedFees, {
|
2022-03-15 22:27:51 +00:00
|
|
|
creatorFee,
|
|
|
|
platformFee,
|
|
|
|
liquidityFee: 0,
|
2022-05-27 21:02:02 +00:00
|
|
|
})
|
2022-03-15 22:27:51 +00:00
|
|
|
|
2022-05-09 21:04:40 +00:00
|
|
|
return {
|
|
|
|
payouts: payouts.map(({ userId, payout }) => ({ userId, payout })),
|
|
|
|
creatorPayout: creatorFee,
|
|
|
|
liquidityPayouts: [],
|
|
|
|
collectedFees,
|
|
|
|
}
|
2022-03-15 22:27:51 +00:00
|
|
|
}
|