manifold/common/payouts.ts

157 lines
3.8 KiB
TypeScript
Raw Permalink Normal View History

2022-01-10 23:52:03 +00:00
import { Bet } from './bet'
import { getProbability } from './calculate'
2022-01-22 23:59:50 +00:00
import { Contract, outcome } from './contract'
2022-01-10 23:52:03 +00:00
import { CREATOR_FEE, FEES } from './fees'
2022-01-10 22:49:04 +00:00
2022-01-22 23:59:50 +00:00
export const getCancelPayouts = (contract: Contract, bets: Bet[]) => {
const { pool } = contract
const poolTotal = pool.YES + pool.NO
console.log('resolved N/A, pool M$', poolTotal)
2022-01-10 22:49:04 +00:00
2022-01-10 23:52:03 +00:00
const betSum = sumBy(bets, (b) => b.amount)
2022-01-10 22:49:04 +00:00
return bets.map((bet) => ({
userId: bet.userId,
2022-01-22 23:59:50 +00:00
payout: (bet.amount / betSum) * poolTotal,
2022-01-10 23:52:03 +00:00
}))
}
2022-01-10 22:49:04 +00:00
export const getStandardPayouts = (
2022-01-22 23:59:50 +00:00
outcome: 'YES' | 'NO',
2022-01-10 22:49:04 +00:00
contract: Contract,
bets: Bet[]
) => {
2022-01-10 23:52:03 +00:00
const [yesBets, noBets] = partition(bets, (bet) => bet.outcome === 'YES')
const winningBets = outcome === 'YES' ? yesBets : noBets
2022-01-10 22:49:04 +00:00
2022-01-10 23:52:03 +00:00
const betSum = sumBy(winningBets, (b) => b.amount)
2022-01-10 22:49:04 +00:00
2022-01-22 23:59:50 +00:00
const poolTotal = contract.pool.YES + contract.pool.NO
if (betSum >= poolTotal) return getCancelPayouts(contract, winningBets)
2022-01-10 22:49:04 +00:00
2022-01-10 23:52:03 +00:00
const shareDifferenceSum = sumBy(winningBets, (b) => b.shares - b.amount)
2022-01-10 22:49:04 +00:00
2022-01-22 23:59:50 +00:00
const winningsPool = poolTotal - betSum
2022-01-10 22:49:04 +00:00
const winnerPayouts = winningBets.map((bet) => ({
userId: bet.userId,
payout:
bet.amount +
2022-02-03 22:59:59 +00:00
(1 - FEES) *
((bet.shares - bet.amount) / shareDifferenceSum) *
winningsPool,
2022-01-10 23:52:03 +00:00
}))
2022-01-10 22:49:04 +00:00
2022-02-03 22:59:59 +00:00
const creatorPayout = CREATOR_FEE * winningsPool
console.log(
'resolved',
outcome,
'pool: M$',
2022-01-22 23:59:50 +00:00
poolTotal,
'creator fee: M$',
creatorPayout
)
2022-01-10 22:49:04 +00:00
return winnerPayouts.concat([
{ userId: contract.creatorId, payout: creatorPayout },
2022-01-10 23:52:03 +00:00
]) // add creator fee
}
2022-01-10 22:49:04 +00:00
export const getMktPayouts = (
contract: Contract,
bets: Bet[],
resolutionProbability?: number
) => {
const p =
resolutionProbability === undefined
? getProbability(contract.totalShares)
: resolutionProbability
2022-01-22 23:59:50 +00:00
const poolTotal = contract.pool.YES + contract.pool.NO
console.log('Resolved MKT at p=', p, 'pool: $M', poolTotal)
2022-01-10 22:49:04 +00:00
2022-01-10 23:52:03 +00:00
const [yesBets, noBets] = partition(bets, (bet) => bet.outcome === 'YES')
2022-01-10 22:49:04 +00:00
const weightedBetTotal =
p * sumBy(yesBets, (b) => b.amount) +
2022-01-10 23:52:03 +00:00
(1 - p) * sumBy(noBets, (b) => b.amount)
2022-01-10 22:49:04 +00:00
2022-01-22 23:59:50 +00:00
if (weightedBetTotal >= poolTotal) {
2022-01-10 22:49:04 +00:00
return bets.map((bet) => ({
userId: bet.userId,
payout:
2022-01-10 23:52:03 +00:00
(((bet.outcome === 'YES' ? p : 1 - p) * bet.amount) /
2022-01-10 22:49:04 +00:00
weightedBetTotal) *
2022-01-22 23:59:50 +00:00
poolTotal,
2022-01-10 23:52:03 +00:00
}))
2022-01-10 22:49:04 +00:00
}
2022-01-22 23:59:50 +00:00
const winningsPool = poolTotal - weightedBetTotal
2022-01-10 22:49:04 +00:00
const weightedShareTotal =
p * sumBy(yesBets, (b) => b.shares - b.amount) +
2022-01-10 23:52:03 +00:00
(1 - p) * sumBy(noBets, (b) => b.shares - b.amount)
2022-01-10 22:49:04 +00:00
const yesPayouts = yesBets.map((bet) => ({
userId: bet.userId,
payout:
p * bet.amount +
2022-02-03 22:59:59 +00:00
(1 - FEES) *
((p * (bet.shares - bet.amount)) / weightedShareTotal) *
winningsPool,
2022-01-10 23:52:03 +00:00
}))
2022-01-10 22:49:04 +00:00
const noPayouts = noBets.map((bet) => ({
userId: bet.userId,
payout:
(1 - p) * bet.amount +
2022-02-03 22:59:59 +00:00
(1 - FEES) *
2022-01-10 22:49:04 +00:00
(((1 - p) * (bet.shares - bet.amount)) / weightedShareTotal) *
winningsPool,
2022-01-10 23:52:03 +00:00
}))
2022-01-10 22:49:04 +00:00
2022-02-03 22:59:59 +00:00
const creatorPayout = CREATOR_FEE * winningsPool
2022-01-10 22:49:04 +00:00
return [
...yesPayouts,
...noPayouts,
{ userId: contract.creatorId, payout: creatorPayout },
2022-01-10 23:52:03 +00:00
]
}
2022-01-10 22:49:04 +00:00
2022-01-22 23:59:50 +00:00
export const getPayouts = (
outcome: outcome,
contract: Contract,
bets: Bet[],
resolutionProbability?: number
2022-01-22 23:59:50 +00:00
) => {
switch (outcome) {
case 'YES':
case 'NO':
return getStandardPayouts(outcome, contract, bets)
case 'MKT':
return getMktPayouts(contract, bets, resolutionProbability)
2022-01-22 23:59:50 +00:00
case 'CANCEL':
return getCancelPayouts(contract, bets)
}
}
2022-01-10 22:49:04 +00:00
const partition = <T>(array: T[], f: (t: T) => boolean) => {
2022-01-10 23:52:03 +00:00
const yes = []
const no = []
2022-01-10 22:49:04 +00:00
for (let t of array) {
2022-01-10 23:52:03 +00:00
if (f(t)) yes.push(t)
else no.push(t)
2022-01-10 22:49:04 +00:00
}
2022-01-10 23:52:03 +00:00
return [yes, no] as [T[], T[]]
}
2022-01-10 22:49:04 +00:00
const sumBy = <T>(array: T[], f: (t: T) => number) => {
2022-01-10 23:52:03 +00:00
const values = array.map(f)
return values.reduce((prev, cur) => prev + cur, 0)
}