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-03-03 03:30:07 +00:00
|
|
|
import { deductDpmFees, getDpmProbability } from './calculate-dpm'
|
2022-03-02 21:12:27 +00:00
|
|
|
import {
|
|
|
|
Binary,
|
|
|
|
Contract,
|
|
|
|
CPMM,
|
|
|
|
DPM,
|
|
|
|
FreeResponse,
|
|
|
|
FullContract,
|
|
|
|
Multi,
|
|
|
|
} 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-03-02 21:12:27 +00:00
|
|
|
export const getDpmCancelPayouts = (
|
|
|
|
contract: FullContract<DPM, any>,
|
|
|
|
bets: Bet[]
|
|
|
|
) => {
|
2022-01-22 23:59:50 +00:00
|
|
|
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
|
|
|
|
2022-03-02 21:12:27 +00:00
|
|
|
export const getFixedCancelPayouts = (bets: Bet[]) => {
|
|
|
|
return bets.map((bet) => ({
|
|
|
|
userId: bet.userId,
|
|
|
|
payout: bet.amount,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getStandardFixedPayouts = (
|
|
|
|
outcome: string,
|
|
|
|
contract: FullContract<CPMM, Binary>,
|
|
|
|
bets: Bet[]
|
|
|
|
) => {
|
|
|
|
const winningBets = bets.filter((bet) => bet.outcome === outcome)
|
|
|
|
|
|
|
|
const payouts = winningBets.map(({ userId, amount, shares }) => {
|
|
|
|
const winnings = shares
|
|
|
|
const profit = winnings - amount
|
|
|
|
|
|
|
|
const payout = amount + (1 - FEES) * profit
|
|
|
|
return { userId, profit, payout }
|
|
|
|
})
|
|
|
|
|
|
|
|
const profits = _.sumBy(payouts, (po) => Math.max(0, po.profit))
|
|
|
|
const creatorPayout = CREATOR_FEE * profits
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
'resolved',
|
|
|
|
outcome,
|
|
|
|
'pool',
|
|
|
|
contract.pool,
|
|
|
|
'profits',
|
|
|
|
profits,
|
|
|
|
'creator fee',
|
|
|
|
creatorPayout
|
|
|
|
)
|
|
|
|
|
|
|
|
return payouts
|
|
|
|
.map(({ userId, payout }) => ({ userId, payout }))
|
|
|
|
.concat([{ userId: contract.creatorId, payout: creatorPayout }]) // add creator fee
|
|
|
|
.concat(getLiquidityPoolPayouts(contract, outcome))
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getLiquidityPoolPayouts = (
|
|
|
|
contract: FullContract<CPMM, Binary>,
|
|
|
|
outcome: string
|
|
|
|
) => {
|
|
|
|
const { creatorId, pool } = contract
|
|
|
|
return [{ userId: creatorId, payout: pool[outcome] }]
|
|
|
|
}
|
|
|
|
|
2022-01-10 22:49:04 +00:00
|
|
|
export const getStandardPayouts = (
|
2022-02-17 23:00:19 +00:00
|
|
|
outcome: string,
|
2022-03-02 21:12:27 +00:00
|
|
|
contract: FullContract<DPM, any>,
|
2022-01-10 22:49:04 +00:00
|
|
|
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 = (
|
2022-03-02 21:12:27 +00:00
|
|
|
contract: FullContract<DPM, any>,
|
2022-01-30 21:51:30 +00:00
|
|
|
bets: Bet[],
|
|
|
|
resolutionProbability?: number
|
|
|
|
) => {
|
|
|
|
const p =
|
|
|
|
resolutionProbability === undefined
|
2022-03-03 03:30:07 +00:00
|
|
|
? getDpmProbability(contract.totalShares)
|
2022-01-30 21:51:30 +00:00
|
|
|
: 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
|
2022-03-03 03:30:07 +00:00
|
|
|
const payout = deductDpmFees(amount, winnings)
|
2022-02-13 01:19:17 +00:00
|
|
|
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-03-02 21:12:27 +00:00
|
|
|
export const getMktFixedPayouts = (
|
|
|
|
contract: FullContract<CPMM, Binary>,
|
|
|
|
bets: Bet[],
|
|
|
|
resolutionProbability?: number
|
|
|
|
) => {
|
|
|
|
const p =
|
|
|
|
resolutionProbability === undefined
|
2022-03-03 03:30:07 +00:00
|
|
|
? getDpmProbability(contract.pool)
|
2022-03-02 21:12:27 +00:00
|
|
|
: resolutionProbability
|
|
|
|
|
|
|
|
const payouts = bets.map(({ userId, outcome, amount, shares }) => {
|
|
|
|
const betP = outcome === 'YES' ? p : 1 - p
|
|
|
|
const winnings = betP * shares
|
|
|
|
const profit = winnings - amount
|
2022-03-03 03:30:07 +00:00
|
|
|
const payout = deductDpmFees(amount, winnings)
|
2022-03-02 21:12:27 +00:00
|
|
|
return { userId, profit, payout }
|
|
|
|
})
|
|
|
|
|
|
|
|
const profits = _.sumBy(payouts, (po) => Math.max(0, po.profit))
|
|
|
|
const creatorPayout = CREATOR_FEE * profits
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
'resolved MKT',
|
|
|
|
p,
|
|
|
|
'pool',
|
|
|
|
contract.pool,
|
|
|
|
'profits',
|
|
|
|
profits,
|
|
|
|
'creator fee',
|
|
|
|
creatorPayout
|
|
|
|
)
|
|
|
|
|
|
|
|
return payouts
|
|
|
|
.map(({ userId, payout }) => ({ userId, payout }))
|
|
|
|
.concat([{ userId: contract.creatorId, payout: creatorPayout }]) // add creator fee
|
|
|
|
.concat(getLiquidityPoolProbPayouts(contract, p))
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getLiquidityPoolProbPayouts = (
|
|
|
|
contract: FullContract<CPMM, Binary>,
|
|
|
|
p: number
|
|
|
|
) => {
|
|
|
|
const { creatorId, pool } = contract
|
|
|
|
const payout = p * pool.YES + (1 - p) * pool.NO
|
|
|
|
return [{ userId: creatorId, payout }]
|
|
|
|
}
|
|
|
|
|
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
|
|
|
) => {
|
2022-03-02 21:12:27 +00:00
|
|
|
if (contract.mechanism === 'cpmm-1') {
|
|
|
|
switch (outcome) {
|
|
|
|
case 'YES':
|
|
|
|
case 'NO':
|
|
|
|
return getStandardFixedPayouts(outcome, contract as any, bets)
|
|
|
|
case 'MKT':
|
|
|
|
return getMktFixedPayouts(contract as any, bets, resolutionProbability)
|
|
|
|
case 'CANCEL':
|
|
|
|
return getFixedCancelPayouts(bets)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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':
|
2022-03-02 21:12:27 +00:00
|
|
|
return getDpmCancelPayouts(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 },
|
2022-03-02 21:12:27 +00:00
|
|
|
contract: FullContract<DPM, Multi | FreeResponse>,
|
2022-02-20 07:26:26 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
const payout = amount + (1 - FEES) * Math.max(0, profit)
|
|
|
|
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 }))
|
|
|
|
}
|