2022-03-04 23:31:04 +00:00
|
|
|
import * as _ from 'lodash'
|
|
|
|
|
|
|
|
import { Bet } from './bet'
|
|
|
|
import { deductDpmFees, getDpmProbability } from './calculate-dpm'
|
|
|
|
import { DPM, FreeResponse, FullContract, Multi } from './contract'
|
2022-03-10 20:55:23 +00:00
|
|
|
import {
|
|
|
|
DPM_CREATOR_FEE,
|
|
|
|
DPM_FEES,
|
|
|
|
DPM_PLATFORM_FEE,
|
|
|
|
Fees,
|
|
|
|
noFees,
|
|
|
|
} from './fees'
|
|
|
|
import { addObjects } from './util/object'
|
2022-03-04 23:31:04 +00:00
|
|
|
|
|
|
|
export const getDpmCancelPayouts = (
|
|
|
|
contract: FullContract<DPM, any>,
|
|
|
|
bets: Bet[]
|
|
|
|
) => {
|
|
|
|
const { pool } = contract
|
|
|
|
const poolTotal = _.sum(Object.values(pool))
|
|
|
|
console.log('resolved N/A, pool M$', poolTotal)
|
|
|
|
|
|
|
|
const betSum = _.sumBy(bets, (b) => b.amount)
|
|
|
|
|
2022-03-10 20:55:23 +00:00
|
|
|
const payouts = bets.map((bet) => ({
|
2022-03-04 23:31:04 +00:00
|
|
|
userId: bet.userId,
|
|
|
|
payout: (bet.amount / betSum) * poolTotal,
|
|
|
|
}))
|
2022-03-10 20:55:23 +00:00
|
|
|
|
|
|
|
return [payouts, contract.collectedFees ?? noFees]
|
2022-03-04 23:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getDpmStandardPayouts = (
|
|
|
|
outcome: string,
|
|
|
|
contract: FullContract<DPM, any>,
|
|
|
|
bets: Bet[]
|
|
|
|
) => {
|
|
|
|
const winningBets = bets.filter((bet) => bet.outcome === outcome)
|
|
|
|
|
|
|
|
const poolTotal = _.sum(Object.values(contract.pool))
|
|
|
|
const totalShares = _.sumBy(winningBets, (b) => b.shares)
|
|
|
|
|
|
|
|
const payouts = winningBets.map(({ userId, amount, shares }) => {
|
|
|
|
const winnings = (shares / totalShares) * poolTotal
|
|
|
|
const profit = winnings - amount
|
|
|
|
|
|
|
|
// profit can be negative if using phantom shares
|
2022-03-10 20:55:23 +00:00
|
|
|
const payout = amount + (1 - DPM_FEES) * Math.max(0, profit)
|
2022-03-04 23:31:04 +00:00
|
|
|
return { userId, profit, payout }
|
|
|
|
})
|
|
|
|
|
|
|
|
const profits = _.sumBy(payouts, (po) => Math.max(0, po.profit))
|
2022-03-10 20:55:23 +00:00
|
|
|
const creatorFee = DPM_CREATOR_FEE * profits
|
|
|
|
const platformFee = DPM_PLATFORM_FEE * profits
|
|
|
|
|
|
|
|
const finalFees: Fees = {
|
|
|
|
creatorFee,
|
|
|
|
platformFee,
|
|
|
|
liquidityFee: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
const fees = addObjects<Fees>(finalFees, contract.collectedFees ?? {})
|
2022-03-04 23:31:04 +00:00
|
|
|
|
|
|
|
console.log(
|
|
|
|
'resolved',
|
|
|
|
outcome,
|
|
|
|
'pool',
|
|
|
|
poolTotal,
|
|
|
|
'profits',
|
|
|
|
profits,
|
|
|
|
'creator fee',
|
2022-03-10 20:55:23 +00:00
|
|
|
creatorFee
|
2022-03-04 23:31:04 +00:00
|
|
|
)
|
|
|
|
|
2022-03-10 20:55:23 +00:00
|
|
|
const totalPayouts = payouts
|
2022-03-04 23:31:04 +00:00
|
|
|
.map(({ userId, payout }) => ({ userId, payout }))
|
2022-03-10 20:55:23 +00:00
|
|
|
.concat([{ userId: contract.creatorId, payout: creatorFee }]) // add creator fee
|
|
|
|
|
|
|
|
return [totalPayouts, fees]
|
2022-03-04 23:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getDpmMktPayouts = (
|
|
|
|
contract: FullContract<DPM, any>,
|
|
|
|
bets: Bet[],
|
|
|
|
resolutionProbability?: number
|
|
|
|
) => {
|
|
|
|
const p =
|
|
|
|
resolutionProbability === undefined
|
|
|
|
? getDpmProbability(contract.totalShares)
|
|
|
|
: resolutionProbability
|
|
|
|
|
|
|
|
const weightedShareTotal = _.sumBy(bets, (b) =>
|
|
|
|
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 }
|
|
|
|
})
|
|
|
|
|
|
|
|
const profits = _.sumBy(payouts, (po) => Math.max(0, po.profit))
|
2022-03-10 20:55:23 +00:00
|
|
|
|
|
|
|
const creatorFee = DPM_CREATOR_FEE * profits
|
|
|
|
const platformFee = DPM_PLATFORM_FEE * profits
|
|
|
|
|
|
|
|
const finalFees: Fees = {
|
|
|
|
creatorFee,
|
|
|
|
platformFee,
|
|
|
|
liquidityFee: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
const fees = addObjects<Fees>(finalFees, contract.collectedFees ?? {})
|
2022-03-04 23:31:04 +00:00
|
|
|
|
|
|
|
console.log(
|
|
|
|
'resolved MKT',
|
|
|
|
p,
|
|
|
|
'pool',
|
|
|
|
pool,
|
|
|
|
'profits',
|
|
|
|
profits,
|
2022-03-10 20:55:23 +00:00
|
|
|
'creator fee'
|
2022-03-04 23:31:04 +00:00
|
|
|
)
|
|
|
|
|
2022-03-10 20:55:23 +00:00
|
|
|
const totalPayouts = payouts
|
2022-03-04 23:31:04 +00:00
|
|
|
.map(({ userId, payout }) => ({ userId, payout }))
|
2022-03-10 20:55:23 +00:00
|
|
|
.concat([{ userId: contract.creatorId, payout: creatorFee }]) // add creator fee
|
|
|
|
|
|
|
|
return [totalPayouts, fees]
|
2022-03-04 23:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getPayoutsMultiOutcome = (
|
|
|
|
resolutions: { [outcome: string]: number },
|
|
|
|
contract: FullContract<DPM, Multi | FreeResponse>,
|
|
|
|
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-10 20:55:23 +00:00
|
|
|
const payout = amount + (1 - DPM_FEES) * Math.max(0, profit)
|
2022-03-04 23:31:04 +00:00
|
|
|
return { userId, profit, payout }
|
|
|
|
})
|
|
|
|
|
|
|
|
const profits = _.sumBy(payouts, (po) => po.profit)
|
2022-03-10 20:55:23 +00:00
|
|
|
|
|
|
|
const creatorFee = DPM_CREATOR_FEE * profits
|
|
|
|
const platformFee = DPM_PLATFORM_FEE * profits
|
|
|
|
|
|
|
|
const finalFees: Fees = {
|
|
|
|
creatorFee,
|
|
|
|
platformFee,
|
|
|
|
liquidityFee: 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
const fees = addObjects<Fees>(finalFees, contract.collectedFees ?? {})
|
2022-03-04 23:31:04 +00:00
|
|
|
|
|
|
|
console.log(
|
|
|
|
'resolved',
|
|
|
|
resolutions,
|
|
|
|
'pool',
|
|
|
|
poolTotal,
|
|
|
|
'profits',
|
|
|
|
profits,
|
|
|
|
'creator fee',
|
2022-03-10 20:55:23 +00:00
|
|
|
creatorFee
|
2022-03-04 23:31:04 +00:00
|
|
|
)
|
|
|
|
|
2022-03-10 20:55:23 +00:00
|
|
|
const totalPayouts = payouts
|
2022-03-04 23:31:04 +00:00
|
|
|
.map(({ userId, payout }) => ({ userId, payout }))
|
2022-03-10 20:55:23 +00:00
|
|
|
.concat([{ userId: contract.creatorId, payout: creatorFee }]) // add creator fee
|
|
|
|
|
|
|
|
return [totalPayouts, fees]
|
2022-03-04 23:31:04 +00:00
|
|
|
}
|