2022-02-17 23:00:19 +00:00
|
|
|
import * as _ from 'lodash'
|
2022-01-10 23:52:03 +00:00
|
|
|
import { Bet } from './bet'
|
2022-03-02 21:12:27 +00:00
|
|
|
import { Binary, DPM, FullContract } from './contract'
|
2022-03-10 20:55:23 +00:00
|
|
|
import { DPM_FEES } from './fees'
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
export function getDpmProbability(totalShares: { [outcome: string]: number }) {
|
2022-02-17 23:00:19 +00:00
|
|
|
// For binary contracts only.
|
2022-03-03 03:30:07 +00:00
|
|
|
return getDpmOutcomeProbability(totalShares, 'YES')
|
2022-02-17 23:00:19 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
export function getDpmOutcomeProbability(
|
2022-02-17 23:00:19 +00:00
|
|
|
totalShares: {
|
|
|
|
[outcome: string]: number
|
|
|
|
},
|
|
|
|
outcome: string
|
|
|
|
) {
|
|
|
|
const squareSum = _.sumBy(Object.values(totalShares), (shares) => shares ** 2)
|
|
|
|
const shares = totalShares[outcome] ?? 0
|
|
|
|
return shares ** 2 / squareSum
|
2022-01-10 23:52:03 +00:00
|
|
|
}
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-03-03 17:06:24 +00:00
|
|
|
export function getDpmOutcomeProbabilityAfterBet(
|
2022-02-17 23:00:19 +00:00
|
|
|
totalShares: {
|
|
|
|
[outcome: string]: number
|
|
|
|
},
|
|
|
|
outcome: string,
|
2022-01-10 21:07:57 +00:00
|
|
|
bet: number
|
|
|
|
) {
|
2022-03-03 03:30:07 +00:00
|
|
|
const shares = calculateDpmShares(totalShares, bet, outcome)
|
2022-01-12 19:01:04 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
const prevShares = totalShares[outcome] ?? 0
|
|
|
|
const newTotalShares = { ...totalShares, [outcome]: prevShares + shares }
|
2022-01-12 19:01:04 +00:00
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
return getDpmOutcomeProbability(newTotalShares, outcome)
|
2022-01-10 21:07:57 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
export function getDpmProbabilityAfterSale(
|
2022-02-18 00:24:00 +00:00
|
|
|
totalShares: {
|
|
|
|
[outcome: string]: number
|
|
|
|
},
|
|
|
|
outcome: string,
|
|
|
|
shares: number
|
|
|
|
) {
|
|
|
|
const prevShares = totalShares[outcome] ?? 0
|
|
|
|
const newTotalShares = { ...totalShares, [outcome]: prevShares - shares }
|
|
|
|
|
|
|
|
const predictionOutcome = outcome === 'NO' ? 'YES' : outcome
|
2022-03-03 03:30:07 +00:00
|
|
|
return getDpmOutcomeProbability(newTotalShares, predictionOutcome)
|
2022-02-18 00:24:00 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
export function calculateDpmShares(
|
2022-02-17 23:00:19 +00:00
|
|
|
totalShares: {
|
|
|
|
[outcome: string]: number
|
|
|
|
},
|
2022-01-10 21:07:57 +00:00
|
|
|
bet: number,
|
2022-02-17 23:00:19 +00:00
|
|
|
betChoice: string
|
2022-01-12 19:01:04 +00:00
|
|
|
) {
|
2022-02-17 23:00:19 +00:00
|
|
|
const squareSum = _.sumBy(Object.values(totalShares), (shares) => shares ** 2)
|
|
|
|
const shares = totalShares[betChoice] ?? 0
|
2022-01-12 19:01:04 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
const c = 2 * bet * Math.sqrt(squareSum)
|
2022-01-12 19:01:04 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
return Math.sqrt(bet ** 2 + shares ** 2 + c) - shares
|
2022-01-12 19:01:04 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
export function calculateDpmRawShareValue(
|
2022-02-17 23:00:19 +00:00
|
|
|
totalShares: {
|
|
|
|
[outcome: string]: number
|
|
|
|
},
|
2022-01-12 19:01:04 +00:00
|
|
|
shares: number,
|
2022-02-17 23:00:19 +00:00
|
|
|
betChoice: string
|
2022-01-12 19:01:04 +00:00
|
|
|
) {
|
2022-02-17 23:00:19 +00:00
|
|
|
const currentValue = Math.sqrt(
|
|
|
|
_.sumBy(Object.values(totalShares), (shares) => shares ** 2)
|
|
|
|
)
|
2022-01-12 19:01:04 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
const postSaleValue = Math.sqrt(
|
|
|
|
_.sumBy(Object.keys(totalShares), (outcome) =>
|
|
|
|
outcome === betChoice
|
|
|
|
? Math.max(0, totalShares[outcome] - shares) ** 2
|
|
|
|
: totalShares[outcome] ** 2
|
|
|
|
)
|
|
|
|
)
|
2022-01-12 19:01:04 +00:00
|
|
|
|
|
|
|
return currentValue - postSaleValue
|
|
|
|
}
|
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
export function calculateDpmMoneyRatio(
|
2022-03-02 21:12:27 +00:00
|
|
|
contract: FullContract<DPM, any>,
|
2022-01-15 06:11:27 +00:00
|
|
|
bet: Bet,
|
|
|
|
shareValue: number
|
|
|
|
) {
|
2022-02-17 23:00:19 +00:00
|
|
|
const { totalShares, totalBets, pool } = contract
|
|
|
|
const { outcome, amount } = bet
|
2022-01-14 17:11:01 +00:00
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
const p = getDpmOutcomeProbability(totalShares, outcome)
|
2022-01-12 19:01:04 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
const actual = _.sum(Object.values(pool)) - shareValue
|
2022-01-15 06:11:27 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
const betAmount = p * amount
|
2022-01-14 17:11:01 +00:00
|
|
|
|
|
|
|
const expected =
|
2022-02-17 23:00:19 +00:00
|
|
|
_.sumBy(
|
|
|
|
Object.keys(totalBets),
|
|
|
|
(outcome) =>
|
2022-03-03 03:30:07 +00:00
|
|
|
getDpmOutcomeProbability(totalShares, outcome) *
|
2022-02-17 23:00:19 +00:00
|
|
|
(totalBets as { [outcome: string]: number })[outcome]
|
|
|
|
) - betAmount
|
2022-01-13 05:43:07 +00:00
|
|
|
|
2022-01-14 17:35:32 +00:00
|
|
|
if (actual <= 0 || expected <= 0) return 0
|
|
|
|
|
|
|
|
return actual / expected
|
2022-01-12 19:01:04 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
export function calculateDpmShareValue(
|
2022-03-02 21:12:27 +00:00
|
|
|
contract: FullContract<DPM, any>,
|
|
|
|
bet: Bet
|
|
|
|
) {
|
2022-02-17 23:00:19 +00:00
|
|
|
const { pool, totalShares } = contract
|
|
|
|
const { shares, outcome } = bet
|
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
const shareValue = calculateDpmRawShareValue(totalShares, shares, outcome)
|
|
|
|
const f = calculateDpmMoneyRatio(contract, bet, shareValue)
|
2022-01-12 19:01:04 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
const myPool = pool[outcome]
|
2022-01-12 19:01:04 +00:00
|
|
|
const adjShareValue = Math.min(Math.min(1, f) * shareValue, myPool)
|
|
|
|
return adjShareValue
|
|
|
|
}
|
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
export function calculateDpmSaleAmount(
|
2022-03-02 21:12:27 +00:00
|
|
|
contract: FullContract<DPM, any>,
|
|
|
|
bet: Bet
|
|
|
|
) {
|
2022-02-14 02:24:03 +00:00
|
|
|
const { amount } = bet
|
2022-03-03 03:30:07 +00:00
|
|
|
const winnings = calculateDpmShareValue(contract, bet)
|
|
|
|
return deductDpmFees(amount, winnings)
|
2022-01-10 21:07:57 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
export function calculateDpmPayout(
|
2022-03-02 21:12:27 +00:00
|
|
|
contract: FullContract<DPM, any>,
|
|
|
|
bet: Bet,
|
|
|
|
outcome: string
|
|
|
|
) {
|
2022-03-03 03:30:07 +00:00
|
|
|
if (outcome === 'CANCEL') return calculateDpmCancelPayout(contract, bet)
|
|
|
|
if (outcome === 'MKT') return calculateMktDpmPayout(contract, bet)
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
return calculateStandardDpmPayout(contract, bet, outcome)
|
2022-01-12 19:01:04 +00:00
|
|
|
}
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
export function calculateDpmCancelPayout(
|
2022-03-02 21:12:27 +00:00
|
|
|
contract: FullContract<DPM, any>,
|
|
|
|
bet: Bet
|
|
|
|
) {
|
2022-02-17 23:00:19 +00:00
|
|
|
const { totalBets, pool } = contract
|
|
|
|
const betTotal = _.sum(Object.values(totalBets))
|
|
|
|
const poolTotal = _.sum(Object.values(pool))
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
return (bet.amount / betTotal) * poolTotal
|
2022-01-12 19:01:04 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
export function calculateStandardDpmPayout(
|
2022-03-02 21:12:27 +00:00
|
|
|
contract: FullContract<DPM, any>,
|
2022-01-12 19:01:04 +00:00
|
|
|
bet: Bet,
|
2022-02-17 23:00:19 +00:00
|
|
|
outcome: string
|
2022-01-12 19:01:04 +00:00
|
|
|
) {
|
|
|
|
const { amount, outcome: betOutcome, shares } = bet
|
|
|
|
if (betOutcome !== outcome) return 0
|
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
const { totalShares, phantomShares, pool } = contract
|
|
|
|
if (!totalShares[outcome]) return 0
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
const poolTotal = _.sum(Object.values(pool))
|
|
|
|
|
|
|
|
const total =
|
|
|
|
totalShares[outcome] - (phantomShares ? phantomShares[outcome] : 0)
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
const winnings = (shares / total) * poolTotal
|
2022-02-13 01:19:17 +00:00
|
|
|
// profit can be negative if using phantom shares
|
2022-03-10 20:55:23 +00:00
|
|
|
return amount + (1 - DPM_FEES) * Math.max(0, winnings - amount)
|
2022-01-10 21:07:57 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
export function calculateDpmPayoutAfterCorrectBet(
|
2022-03-02 21:12:27 +00:00
|
|
|
contract: FullContract<DPM, any>,
|
|
|
|
bet: Bet
|
|
|
|
) {
|
2022-01-12 19:01:04 +00:00
|
|
|
const { totalShares, pool, totalBets } = contract
|
2022-02-17 23:00:19 +00:00
|
|
|
const { shares, amount, outcome } = bet
|
2022-01-12 19:01:04 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
const prevShares = totalShares[outcome] ?? 0
|
|
|
|
const prevPool = pool[outcome] ?? 0
|
|
|
|
const prevTotalBet = totalBets[outcome] ?? 0
|
2022-01-12 19:01:04 +00:00
|
|
|
|
|
|
|
const newContract = {
|
|
|
|
...contract,
|
|
|
|
totalShares: {
|
2022-02-17 23:00:19 +00:00
|
|
|
...totalShares,
|
|
|
|
[outcome]: prevShares + shares,
|
2022-01-12 19:01:04 +00:00
|
|
|
},
|
|
|
|
pool: {
|
2022-02-17 23:00:19 +00:00
|
|
|
...pool,
|
|
|
|
[outcome]: prevPool + amount,
|
2022-01-12 19:01:04 +00:00
|
|
|
},
|
|
|
|
totalBets: {
|
2022-02-17 23:00:19 +00:00
|
|
|
...totalBets,
|
|
|
|
[outcome]: prevTotalBet + amount,
|
2022-01-12 19:01:04 +00:00
|
|
|
},
|
|
|
|
}
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
return calculateStandardDpmPayout(newContract, bet, outcome)
|
2022-01-10 21:07:57 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
function calculateMktDpmPayout(contract: FullContract<DPM, any>, bet: Bet) {
|
2022-02-17 23:00:19 +00:00
|
|
|
if (contract.outcomeType === 'BINARY')
|
2022-03-03 03:30:07 +00:00
|
|
|
return calculateBinaryMktDpmPayout(contract, bet)
|
2022-02-17 23:00:19 +00:00
|
|
|
|
|
|
|
const { totalShares, pool } = contract
|
|
|
|
|
|
|
|
const totalPool = _.sum(Object.values(pool))
|
|
|
|
const sharesSquareSum = _.sumBy(
|
2022-03-02 21:12:27 +00:00
|
|
|
Object.values(totalShares) as number[],
|
2022-02-17 23:00:19 +00:00
|
|
|
(shares) => shares ** 2
|
|
|
|
)
|
|
|
|
|
|
|
|
const weightedShareTotal = _.sumBy(Object.keys(totalShares), (outcome) => {
|
|
|
|
// Avoid O(n^2) by reusing sharesSquareSum for prob.
|
|
|
|
const shares = totalShares[outcome]
|
|
|
|
const prob = shares ** 2 / sharesSquareSum
|
|
|
|
return prob * shares
|
|
|
|
})
|
|
|
|
|
|
|
|
const { outcome, amount, shares } = bet
|
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
const betP = getDpmOutcomeProbability(totalShares, outcome)
|
2022-02-17 23:00:19 +00:00
|
|
|
const winnings = ((betP * shares) / weightedShareTotal) * totalPool
|
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
return deductDpmFees(amount, winnings)
|
2022-02-17 23:00:19 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
function calculateBinaryMktDpmPayout(
|
2022-03-02 21:12:27 +00:00
|
|
|
contract: FullContract<DPM, Binary>,
|
|
|
|
bet: Bet
|
|
|
|
) {
|
2022-02-17 23:00:19 +00:00
|
|
|
const { resolutionProbability, totalShares, phantomShares } = contract
|
2022-01-30 21:51:30 +00:00
|
|
|
const p =
|
2022-02-17 23:00:19 +00:00
|
|
|
resolutionProbability !== undefined
|
|
|
|
? resolutionProbability
|
2022-03-03 03:30:07 +00:00
|
|
|
: getDpmProbability(totalShares)
|
2022-01-12 19:01:04 +00:00
|
|
|
|
2022-02-13 01:19:17 +00:00
|
|
|
const pool = contract.pool.YES + contract.pool.NO
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-02-13 01:19:17 +00:00
|
|
|
const weightedShareTotal =
|
2022-02-17 23:00:19 +00:00
|
|
|
p * (totalShares.YES - (phantomShares?.YES ?? 0)) +
|
|
|
|
(1 - p) * (totalShares.NO - (phantomShares?.NO ?? 0))
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-02-13 01:19:17 +00:00
|
|
|
const { outcome, amount, shares } = bet
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-02-13 01:19:17 +00:00
|
|
|
const betP = outcome === 'YES' ? p : 1 - p
|
|
|
|
const winnings = ((betP * shares) / weightedShareTotal) * pool
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
return deductDpmFees(amount, winnings)
|
2022-01-10 21:07:57 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
export function resolvedDpmPayout(contract: FullContract<DPM, any>, bet: Bet) {
|
2022-01-10 21:07:57 +00:00
|
|
|
if (contract.resolution)
|
2022-03-03 03:30:07 +00:00
|
|
|
return calculateDpmPayout(contract, bet, contract.resolution)
|
2022-01-10 23:52:03 +00:00
|
|
|
throw new Error('Contract was not resolved')
|
2022-01-10 21:07:57 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 03:30:07 +00:00
|
|
|
export const deductDpmFees = (betAmount: number, winnings: number) => {
|
2022-02-13 01:19:17 +00:00
|
|
|
return winnings > betAmount
|
2022-03-10 20:55:23 +00:00
|
|
|
? betAmount + (1 - DPM_FEES) * (winnings - betAmount)
|
2022-02-13 01:19:17 +00:00
|
|
|
: winnings
|
|
|
|
}
|
2022-03-07 17:29:58 +00:00
|
|
|
|
|
|
|
export const calcDpmInitialPool = (
|
|
|
|
initialProbInt: number,
|
|
|
|
ante: number,
|
|
|
|
phantomAnte: number
|
|
|
|
) => {
|
|
|
|
const p = initialProbInt / 100.0
|
|
|
|
const totalAnte = phantomAnte + ante
|
|
|
|
|
|
|
|
const sharesYes = Math.sqrt(p * totalAnte ** 2)
|
|
|
|
const sharesNo = Math.sqrt(totalAnte ** 2 - sharesYes ** 2)
|
|
|
|
|
|
|
|
const poolYes = p * ante
|
|
|
|
const poolNo = (1 - p) * ante
|
|
|
|
|
|
|
|
const phantomYes = Math.sqrt(p) * phantomAnte
|
|
|
|
const phantomNo = Math.sqrt(1 - p) * phantomAnte
|
|
|
|
|
|
|
|
return { sharesYes, sharesNo, poolYes, poolNo, phantomYes, phantomNo }
|
|
|
|
}
|