manifold/common/payouts.ts

132 lines
3.2 KiB
TypeScript
Raw Normal View History

2022-01-10 23:52:03 +00:00
import { Bet } from './bet'
import { getProbability } from './calculate'
2022-01-10 23:52:03 +00:00
import { Contract } from './contract'
import { CREATOR_FEE, FEES } from './fees'
2022-01-10 22:49:04 +00:00
export const getCancelPayouts = (truePool: number, bets: Bet[]) => {
2022-01-10 23:52:03 +00:00
console.log('resolved N/A, pool M$', truePool)
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,
payout: (bet.amount / betSum) * truePool,
2022-01-10 23:52:03 +00:00
}))
}
2022-01-10 22:49:04 +00:00
export const getStandardPayouts = (
outcome: string,
truePool: number,
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-10 23:52:03 +00:00
if (betSum >= truePool) return getCancelPayouts(truePool, 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-10 23:52:03 +00:00
const winningsPool = truePool - betSum
2022-01-10 22:49:04 +00:00
const winnerPayouts = winningBets.map((bet) => ({
userId: bet.userId,
payout:
bet.amount +
(1 - 2 * FEES) *
((bet.shares - bet.amount) / shareDifferenceSum) *
winningsPool,
2022-01-10 23:52:03 +00:00
}))
2022-01-10 22:49:04 +00:00
const creatorPayout = 2 * CREATOR_FEE * winningsPool
console.log(
'resolved',
outcome,
'pool: M$',
truePool,
'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 = (
truePool: number,
contract: Contract,
bets: Bet[]
) => {
const p = getProbability(contract.totalShares)
2022-01-10 23:52:03 +00:00
console.log('Resolved MKT at p=', p, 'pool: $M', truePool)
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
if (weightedBetTotal >= truePool) {
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) *
truePool,
2022-01-10 23:52:03 +00:00
}))
2022-01-10 22:49:04 +00:00
}
2022-01-10 23:52:03 +00:00
const winningsPool = truePool - 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 +
(1 - 2 * 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 +
(1 - 2 * 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
const creatorPayout = 2 * 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
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)
}