2022-02-17 23:00:19 +00:00
|
|
|
import * as _ from 'lodash'
|
2022-01-10 23:52:03 +00:00
|
|
|
import { Bet } from './bet'
|
|
|
|
import { Contract } from './contract'
|
|
|
|
import { FEES } from './fees'
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
export function getProbability(totalShares: { [outcome: string]: number }) {
|
|
|
|
// For binary contracts only.
|
|
|
|
return getOutcomeProbability(totalShares, 'YES')
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getOutcomeProbability(
|
|
|
|
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
|
|
|
|
|
|
|
export function getProbabilityAfterBet(
|
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-01-12 19:01:04 +00:00
|
|
|
const shares = calculateShares(totalShares, bet, outcome)
|
|
|
|
|
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-02-17 23:00:19 +00:00
|
|
|
return getOutcomeProbability(newTotalShares, outcome)
|
2022-01-10 21:07:57 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 00:24:00 +00:00
|
|
|
export function getProbabilityAfterSale(
|
|
|
|
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
|
|
|
|
return getOutcomeProbability(newTotalShares, predictionOutcome)
|
|
|
|
}
|
|
|
|
|
2022-01-10 21:07:57 +00:00
|
|
|
export function calculateShares(
|
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
|
|
|
}
|
|
|
|
|
|
|
|
export function calculateRawShareValue(
|
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-01-15 06:11:27 +00:00
|
|
|
export function calculateMoneyRatio(
|
|
|
|
contract: Contract,
|
|
|
|
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-02-17 23:00:19 +00:00
|
|
|
const p = getOutcomeProbability(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) =>
|
|
|
|
getOutcomeProbability(totalShares, outcome) *
|
|
|
|
(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
|
|
|
}
|
|
|
|
|
|
|
|
export function calculateShareValue(contract: Contract, bet: Bet) {
|
2022-02-17 23:00:19 +00:00
|
|
|
const { pool, totalShares } = contract
|
|
|
|
const { shares, outcome } = bet
|
|
|
|
|
|
|
|
const shareValue = calculateRawShareValue(totalShares, shares, outcome)
|
2022-01-15 06:11:27 +00:00
|
|
|
const f = calculateMoneyRatio(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
|
|
|
|
}
|
|
|
|
|
|
|
|
export function calculateSaleAmount(contract: Contract, bet: Bet) {
|
2022-02-14 02:24:03 +00:00
|
|
|
const { amount } = bet
|
|
|
|
const winnings = calculateShareValue(contract, bet)
|
|
|
|
return deductFees(amount, winnings)
|
2022-01-10 21:07:57 +00:00
|
|
|
}
|
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
export function calculatePayout(contract: Contract, bet: Bet, outcome: string) {
|
2022-01-12 19:01:04 +00:00
|
|
|
if (outcome === 'CANCEL') return calculateCancelPayout(contract, bet)
|
2022-01-10 23:52:03 +00:00
|
|
|
if (outcome === 'MKT') return calculateMktPayout(contract, bet)
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-01-12 19:01:04 +00:00
|
|
|
return calculateStandardPayout(contract, bet, outcome)
|
|
|
|
}
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-01-12 19:01:04 +00:00
|
|
|
export function calculateCancelPayout(contract: Contract, 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
|
|
|
}
|
|
|
|
|
|
|
|
export function calculateStandardPayout(
|
|
|
|
contract: Contract,
|
|
|
|
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
|
|
|
|
return amount + (1 - FEES) * Math.max(0, winnings - amount)
|
2022-01-10 21:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function calculatePayoutAfterCorrectBet(contract: Contract, 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-02-17 23:00:19 +00:00
|
|
|
return calculateStandardPayout(newContract, bet, outcome)
|
2022-01-10 21:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function calculateMktPayout(contract: Contract, bet: Bet) {
|
2022-02-17 23:00:19 +00:00
|
|
|
if (contract.outcomeType === 'BINARY')
|
|
|
|
return calculateBinaryMktPayout(contract, bet)
|
|
|
|
|
|
|
|
const { totalShares, pool } = contract
|
|
|
|
|
|
|
|
const totalPool = _.sum(Object.values(pool))
|
|
|
|
const sharesSquareSum = _.sumBy(
|
|
|
|
Object.values(totalShares),
|
|
|
|
(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
|
|
|
|
|
|
|
|
const betP = getOutcomeProbability(totalShares, outcome)
|
|
|
|
const winnings = ((betP * shares) / weightedShareTotal) * totalPool
|
|
|
|
|
|
|
|
return deductFees(amount, winnings)
|
|
|
|
}
|
|
|
|
|
|
|
|
function calculateBinaryMktPayout(contract: Contract, bet: Bet) {
|
|
|
|
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
|
|
|
|
: getProbability(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-02-13 01:19:17 +00:00
|
|
|
return deductFees(amount, winnings)
|
2022-01-10 21:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function resolvedPayout(contract: Contract, bet: Bet) {
|
|
|
|
if (contract.resolution)
|
2022-01-10 23:52:03 +00:00
|
|
|
return calculatePayout(contract, bet, contract.resolution)
|
|
|
|
throw new Error('Contract was not resolved')
|
2022-01-10 21:07:57 +00:00
|
|
|
}
|
|
|
|
|
2022-02-13 01:19:17 +00:00
|
|
|
export const deductFees = (betAmount: number, winnings: number) => {
|
|
|
|
return winnings > betAmount
|
|
|
|
? betAmount + (1 - FEES) * (winnings - betAmount)
|
|
|
|
: winnings
|
|
|
|
}
|