2022-02-13 01:19:17 +00:00
|
|
|
import * as _ from 'lodash'
|
|
|
|
|
2022-01-10 23:52:03 +00:00
|
|
|
import { Bet } from './bet'
|
2022-02-13 01:19:17 +00:00
|
|
|
import { deductFees, getProbability } from './calculate'
|
2022-02-17 23:00:19 +00:00
|
|
|
import { Contract } 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
|
2022-02-17 23:00:19 +00:00
|
|
|
const poolTotal = _.sum(Object.values(pool))
|
2022-01-22 23:59:50 +00:00
|
|
|
console.log('resolved N/A, pool M$', poolTotal)
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-02-13 01:19:17 +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-02-17 23:00:19 +00:00
|
|
|
outcome: string,
|
2022-01-10 22:49:04 +00:00
|
|
|
contract: Contract,
|
|
|
|
bets: Bet[]
|
|
|
|
) => {
|
2022-02-17 23:00:19 +00:00
|
|
|
const winningBets = bets.filter((bet) => bet.outcome === outcome)
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
const poolTotal = _.sum(Object.values(contract.pool))
|
2022-02-13 01:19:17 +00:00
|
|
|
const totalShares = _.sumBy(winningBets, (b) => b.shares)
|
2022-01-22 23:59:50 +00:00
|
|
|
|
2022-02-13 01:19:17 +00:00
|
|
|
const payouts = winningBets.map(({ userId, amount, shares }) => {
|
2022-02-17 23:00:19 +00:00
|
|
|
const winnings = (shares / totalShares) * poolTotal
|
2022-02-13 01:19:17 +00:00
|
|
|
const profit = winnings - amount
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-02-13 01:19:17 +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
|
|
|
|
2022-02-13 01:19:17 +00:00
|
|
|
const profits = _.sumBy(payouts, (po) => Math.max(0, po.profit))
|
|
|
|
const creatorPayout = CREATOR_FEE * profits
|
2022-01-14 17:11:01 +00:00
|
|
|
|
|
|
|
console.log(
|
|
|
|
'resolved',
|
|
|
|
outcome,
|
2022-02-13 01:19:17 +00:00
|
|
|
'pool',
|
2022-02-17 23:00:19 +00:00
|
|
|
poolTotal,
|
2022-02-13 01:19:17 +00:00
|
|
|
'profits',
|
|
|
|
profits,
|
|
|
|
'creator fee',
|
2022-01-14 17:11:01 +00:00
|
|
|
creatorPayout
|
|
|
|
)
|
|
|
|
|
2022-02-13 01:19:17 +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-30 21:51:30 +00:00
|
|
|
export const getMktPayouts = (
|
|
|
|
contract: Contract,
|
|
|
|
bets: Bet[],
|
|
|
|
resolutionProbability?: number
|
|
|
|
) => {
|
|
|
|
const p =
|
|
|
|
resolutionProbability === undefined
|
|
|
|
? getProbability(contract.totalShares)
|
|
|
|
: resolutionProbability
|
|
|
|
|
2022-02-13 01:19:17 +00:00
|
|
|
const weightedShareTotal = _.sumBy(bets, (b) =>
|
|
|
|
b.outcome === 'YES' ? p * b.shares : (1 - p) * b.shares
|
|
|
|
)
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-02-13 01:19:17 +00:00
|
|
|
const pool = contract.pool.YES + contract.pool.NO
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-02-13 01:19:17 +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
|
|
|
|
2022-02-13 01:19:17 +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
|
|
|
|
2022-02-13 01:19:17 +00:00
|
|
|
console.log(
|
|
|
|
'resolved MKT',
|
|
|
|
p,
|
|
|
|
'pool',
|
|
|
|
pool,
|
|
|
|
'profits',
|
|
|
|
profits,
|
|
|
|
'creator fee',
|
|
|
|
creatorPayout
|
|
|
|
)
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-02-13 01:19:17 +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 = (
|
2022-02-17 23:00:19 +00:00
|
|
|
outcome: string,
|
2022-01-22 23:59:50 +00:00
|
|
|
contract: Contract,
|
2022-01-30 21:51:30 +00:00
|
|
|
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':
|
2022-01-30 21:51:30 +00:00
|
|
|
return getMktPayouts(contract, bets, resolutionProbability)
|
2022-01-22 23:59:50 +00:00
|
|
|
case 'CANCEL':
|
|
|
|
return getCancelPayouts(contract, bets)
|
2022-02-17 23:00:19 +00:00
|
|
|
default:
|
|
|
|
// Multi outcome.
|
|
|
|
return getStandardPayouts(outcome, contract, bets)
|
2022-01-22 23:59:50 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-20 07:26:26 +00:00
|
|
|
|
|
|
|
export const getPayoutsMultiOutcome = (
|
|
|
|
resolutions: { [outcome: string]: number },
|
|
|
|
contract: Contract,
|
|
|
|
bets: Bet[]
|
|
|
|
) => {
|
|
|
|
const poolTotal = _.sum(Object.values(contract.pool))
|
|
|
|
const winningBets = bets.filter((bet) => resolutions[bet.outcome])
|
|
|
|
|
|
|
|
const betsByOutcome = _.groupBy(winningBets, (bet) => bet.outcome)
|
|
|
|
const sharesByOutcome = _.mapValues(betsByOutcome, (bets) =>
|
|
|
|
_.sumBy(bets, (bet) => bet.shares)
|
|
|
|
)
|
|
|
|
|
|
|
|
const probTotal = _.sum(Object.values(resolutions))
|
|
|
|
|
|
|
|
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-03-07 21:14:56 +00:00
|
|
|
const payout = deductFees(amount, winnings)
|
2022-02-20 07:26:26 +00:00
|
|
|
return { userId, profit, payout }
|
|
|
|
})
|
|
|
|
|
|
|
|
const profits = _.sumBy(payouts, (po) => po.profit)
|
|
|
|
const creatorPayout = CREATOR_FEE * profits
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
'resolved',
|
|
|
|
resolutions,
|
|
|
|
'pool',
|
|
|
|
poolTotal,
|
|
|
|
'profits',
|
|
|
|
profits,
|
|
|
|
'creator fee',
|
|
|
|
creatorPayout
|
|
|
|
)
|
|
|
|
|
|
|
|
return payouts
|
|
|
|
.map(({ userId, payout }) => ({ userId, payout }))
|
|
|
|
.concat([{ userId: contract.creatorId, payout: creatorPayout }]) // add creator fee
|
|
|
|
}
|
2022-03-02 03:31:48 +00:00
|
|
|
|
|
|
|
export const getLoanPayouts = (bets: Bet[]) => {
|
|
|
|
const betsWithLoans = bets.filter((bet) => bet.loanAmount)
|
|
|
|
const betsByUser = _.groupBy(betsWithLoans, (bet) => bet.userId)
|
|
|
|
const loansByUser = _.mapValues(betsByUser, (bets) =>
|
|
|
|
_.sumBy(bets, (bet) => -(bet.loanAmount ?? 0))
|
|
|
|
)
|
|
|
|
return _.toPairs(loansByUser).map(([userId, payout]) => ({ userId, payout }))
|
|
|
|
}
|