manifold/common/payouts.ts

119 lines
3.1 KiB
TypeScript
Raw Normal View History

import * as _ from 'lodash'
2022-01-10 23:52:03 +00:00
import { Bet } from './bet'
import { deductFees, 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
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[]
) => {
const [yesBets, noBets] = _.partition(bets, (bet) => bet.outcome === 'YES')
2022-01-10 23:52:03 +00:00
const winningBets = outcome === 'YES' ? yesBets : noBets
2022-01-10 22:49:04 +00:00
const pool = contract.pool.YES + contract.pool.NO
const totalShares = _.sumBy(winningBets, (b) => b.shares)
2022-01-22 23:59:50 +00:00
const payouts = winningBets.map(({ userId, amount, shares }) => {
const winnings = (shares / totalShares) * pool
const profit = winnings - amount
2022-01-10 22:49:04 +00:00
// profit can be negative if using phantom shares
const payout = amount + (1 - FEES) * Math.max(0, profit)
return { userId, profit, payout }
})
2022-01-10 22:49:04 +00:00
const profits = _.sumBy(payouts, (po) => Math.max(0, po.profit))
const creatorPayout = CREATOR_FEE * profits
console.log(
'resolved',
outcome,
'pool',
pool,
'profits',
profits,
'creator fee',
creatorPayout
)
return payouts
.map(({ userId, payout }) => ({ userId, payout }))
.concat([{ userId: contract.creatorId, payout: creatorPayout }]) // add creator fee
2022-01-10 23:52:03 +00:00
}
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
const weightedShareTotal = _.sumBy(bets, (b) =>
b.outcome === 'YES' ? p * b.shares : (1 - p) * b.shares
)
2022-01-10 22:49:04 +00:00
const pool = contract.pool.YES + contract.pool.NO
2022-01-10 22:49:04 +00:00
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 = deductFees(amount, winnings)
return { userId, profit, payout }
})
2022-01-10 22:49:04 +00:00
const profits = _.sumBy(payouts, (po) => Math.max(0, po.profit))
const creatorPayout = CREATOR_FEE * profits
2022-01-10 22:49:04 +00:00
console.log(
'resolved MKT',
p,
'pool',
pool,
'profits',
profits,
'creator fee',
creatorPayout
)
2022-01-10 22:49:04 +00:00
return payouts
.map(({ userId, payout }) => ({ userId, payout }))
.concat([{ userId: contract.creatorId, payout: creatorPayout }]) // add creator fee
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)
}
}