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-01-12 19:01:04 +00:00
|
|
|
export function getProbability(totalShares: { YES: number; NO: number }) {
|
|
|
|
const { YES: y, NO: n } = totalShares
|
|
|
|
return y ** 2 / (y ** 2 + n ** 2)
|
2022-01-10 23:52:03 +00:00
|
|
|
}
|
2022-01-10 21:07:57 +00:00
|
|
|
|
|
|
|
export function getProbabilityAfterBet(
|
2022-01-12 19:01:04 +00:00
|
|
|
totalShares: { YES: number; NO: number },
|
2022-01-10 23:52:03 +00:00
|
|
|
outcome: 'YES' | 'NO',
|
2022-01-10 21:07:57 +00:00
|
|
|
bet: number
|
|
|
|
) {
|
2022-01-12 19:01:04 +00:00
|
|
|
const shares = calculateShares(totalShares, bet, outcome)
|
|
|
|
|
|
|
|
const [YES, NO] =
|
|
|
|
outcome === 'YES'
|
|
|
|
? [totalShares.YES + shares, totalShares.NO]
|
|
|
|
: [totalShares.YES, totalShares.NO + shares]
|
|
|
|
|
2022-01-10 23:52:03 +00:00
|
|
|
return getProbability({ YES, NO })
|
2022-01-10 21:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function calculateShares(
|
2022-01-12 19:01:04 +00:00
|
|
|
totalShares: { YES: number; NO: number },
|
2022-01-10 21:07:57 +00:00
|
|
|
bet: number,
|
2022-01-10 23:52:03 +00:00
|
|
|
betChoice: 'YES' | 'NO'
|
2022-01-10 21:07:57 +00:00
|
|
|
) {
|
2022-01-12 19:01:04 +00:00
|
|
|
const [yesShares, noShares] = [totalShares.YES, totalShares.NO]
|
|
|
|
|
|
|
|
const c = 2 * bet * Math.sqrt(yesShares ** 2 + noShares ** 2)
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-01-10 23:52:03 +00:00
|
|
|
return betChoice === 'YES'
|
2022-01-12 19:01:04 +00:00
|
|
|
? Math.sqrt(bet ** 2 + yesShares ** 2 + c) - yesShares
|
|
|
|
: Math.sqrt(bet ** 2 + noShares ** 2 + c) - noShares
|
|
|
|
}
|
|
|
|
|
|
|
|
export function calculateEstimatedWinnings(
|
|
|
|
totalShares: { YES: number; NO: number },
|
|
|
|
shares: number,
|
|
|
|
betChoice: 'YES' | 'NO'
|
|
|
|
) {
|
|
|
|
const ind = betChoice === 'YES' ? 1 : 0
|
|
|
|
|
|
|
|
const yesShares = totalShares.YES + ind * shares
|
|
|
|
const noShares = totalShares.NO + (1 - ind) * shares
|
|
|
|
|
|
|
|
const estPool = Math.sqrt(yesShares ** 2 + noShares ** 2)
|
|
|
|
const total = ind * yesShares + (1 - ind) * noShares
|
|
|
|
|
|
|
|
return ((1 - FEES) * (shares * estPool)) / total
|
|
|
|
}
|
|
|
|
|
|
|
|
export function calculateRawShareValue(
|
|
|
|
totalShares: { YES: number; NO: number },
|
|
|
|
shares: number,
|
|
|
|
betChoice: 'YES' | 'NO'
|
|
|
|
) {
|
|
|
|
const [yesShares, noShares] = [totalShares.YES, totalShares.NO]
|
|
|
|
const currentValue = Math.sqrt(yesShares ** 2 + noShares ** 2)
|
|
|
|
|
|
|
|
const postSaleValue =
|
|
|
|
betChoice === 'YES'
|
|
|
|
? Math.sqrt(Math.max(0, yesShares - shares) ** 2 + noShares ** 2)
|
|
|
|
: Math.sqrt(yesShares ** 2 + Math.max(0, noShares - shares) ** 2)
|
|
|
|
|
|
|
|
return currentValue - postSaleValue
|
|
|
|
}
|
|
|
|
|
2022-01-15 06:11:27 +00:00
|
|
|
export function calculateMoneyRatio(
|
|
|
|
contract: Contract,
|
|
|
|
bet: Bet,
|
|
|
|
shareValue: number
|
|
|
|
) {
|
2022-01-14 02:52:03 +00:00
|
|
|
const { totalShares, pool } = contract
|
2022-01-14 17:11:01 +00:00
|
|
|
|
2022-01-14 02:52:03 +00:00
|
|
|
const p = getProbability(totalShares)
|
2022-01-12 19:01:04 +00:00
|
|
|
|
2022-01-15 06:11:27 +00:00
|
|
|
const actual = pool.YES + pool.NO - shareValue
|
|
|
|
|
|
|
|
const betAmount =
|
|
|
|
bet.outcome === 'YES' ? p * bet.amount : (1 - p) * bet.amount
|
2022-01-14 17:11:01 +00:00
|
|
|
|
|
|
|
const expected =
|
2022-01-15 06:11:27 +00:00
|
|
|
p * contract.totalBets.YES + (1 - p) * contract.totalBets.NO - 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) {
|
|
|
|
const shareValue = calculateRawShareValue(
|
|
|
|
contract.totalShares,
|
|
|
|
bet.shares,
|
|
|
|
bet.outcome
|
|
|
|
)
|
2022-01-15 06:11:27 +00:00
|
|
|
const f = calculateMoneyRatio(contract, bet, shareValue)
|
2022-01-12 19:01:04 +00:00
|
|
|
|
|
|
|
const myPool = contract.pool[bet.outcome]
|
|
|
|
const adjShareValue = Math.min(Math.min(1, f) * shareValue, myPool)
|
|
|
|
return adjShareValue
|
|
|
|
}
|
|
|
|
|
|
|
|
export function calculateSaleAmount(contract: Contract, bet: Bet) {
|
2022-01-14 17:11:01 +00:00
|
|
|
return (1 - 2 * FEES) * calculateShareValue(contract, bet)
|
2022-01-10 21:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function calculatePayout(
|
|
|
|
contract: Contract,
|
|
|
|
bet: Bet,
|
2022-01-10 23:52:03 +00:00
|
|
|
outcome: 'YES' | 'NO' | 'CANCEL' | 'MKT'
|
2022-01-10 21:07:57 +00:00
|
|
|
) {
|
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) {
|
|
|
|
const totalBets = contract.totalBets.YES + contract.totalBets.NO
|
|
|
|
const pool = contract.pool.YES + contract.pool.NO
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-01-12 19:01:04 +00:00
|
|
|
return (bet.amount / totalBets) * pool
|
|
|
|
}
|
|
|
|
|
|
|
|
export function calculateStandardPayout(
|
|
|
|
contract: Contract,
|
|
|
|
bet: Bet,
|
|
|
|
outcome: 'YES' | 'NO'
|
|
|
|
) {
|
|
|
|
const { amount, outcome: betOutcome, shares } = bet
|
|
|
|
if (betOutcome !== outcome) return 0
|
|
|
|
|
|
|
|
const { totalShares, totalBets, phantomShares } = contract
|
2022-01-10 23:52:03 +00:00
|
|
|
if (totalShares[outcome] === 0) return 0
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-01-12 19:01:04 +00:00
|
|
|
const truePool = contract.pool.YES + contract.pool.NO
|
2022-01-10 21:07:57 +00:00
|
|
|
|
|
|
|
if (totalBets[outcome] >= truePool)
|
2022-01-10 23:52:03 +00:00
|
|
|
return (amount / totalBets[outcome]) * truePool
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-01-12 19:01:04 +00:00
|
|
|
const total =
|
|
|
|
totalShares[outcome] - phantomShares[outcome] - totalBets[outcome]
|
2022-01-10 23:52:03 +00:00
|
|
|
const winningsPool = truePool - totalBets[outcome]
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-01-14 17:11:01 +00:00
|
|
|
return amount + (1 - 2 * FEES) * ((shares - amount) / total) * winningsPool
|
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
|
|
|
|
|
|
|
|
const ind = bet.outcome === 'YES' ? 1 : 0
|
|
|
|
const { shares, amount } = bet
|
|
|
|
|
|
|
|
const newContract = {
|
|
|
|
...contract,
|
|
|
|
totalShares: {
|
|
|
|
YES: totalShares.YES + ind * shares,
|
|
|
|
NO: totalShares.NO + (1 - ind) * shares,
|
|
|
|
},
|
|
|
|
pool: {
|
|
|
|
YES: pool.YES + ind * amount,
|
|
|
|
NO: pool.NO + (1 - ind) * amount,
|
|
|
|
},
|
|
|
|
totalBets: {
|
|
|
|
YES: totalBets.YES + ind * amount,
|
|
|
|
NO: totalBets.NO + (1 - ind) * amount,
|
|
|
|
},
|
|
|
|
}
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-01-12 19:01:04 +00:00
|
|
|
return calculateStandardPayout(newContract, bet, bet.outcome)
|
2022-01-10 21:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function calculateMktPayout(contract: Contract, bet: Bet) {
|
2022-01-12 19:01:04 +00:00
|
|
|
const p = getProbability(contract.totalShares)
|
|
|
|
|
2022-01-10 21:07:57 +00:00
|
|
|
const weightedTotal =
|
2022-01-10 23:52:03 +00:00
|
|
|
p * contract.totalBets.YES + (1 - p) * contract.totalBets.NO
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-01-12 19:01:04 +00:00
|
|
|
const truePool = contract.pool.YES + contract.pool.NO
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-01-10 23:52:03 +00:00
|
|
|
const betP = bet.outcome === 'YES' ? p : 1 - p
|
2022-01-10 21:07:57 +00:00
|
|
|
|
|
|
|
if (weightedTotal >= truePool) {
|
2022-01-10 23:52:03 +00:00
|
|
|
return ((betP * bet.amount) / weightedTotal) * truePool
|
2022-01-10 21:07:57 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 23:52:03 +00:00
|
|
|
const winningsPool = truePool - weightedTotal
|
2022-01-10 21:07:57 +00:00
|
|
|
|
|
|
|
const weightedShareTotal =
|
2022-01-12 19:01:04 +00:00
|
|
|
p *
|
|
|
|
(contract.totalShares.YES -
|
|
|
|
contract.phantomShares.YES -
|
|
|
|
contract.totalBets.YES) +
|
|
|
|
(1 - p) *
|
|
|
|
(contract.totalShares.NO -
|
|
|
|
contract.phantomShares.NO -
|
|
|
|
contract.totalBets.NO)
|
2022-01-10 21:07:57 +00:00
|
|
|
|
|
|
|
return (
|
2022-01-14 17:11:01 +00:00
|
|
|
betP * bet.amount +
|
|
|
|
(1 - 2 * FEES) *
|
|
|
|
((betP * (bet.shares - bet.amount)) / weightedShareTotal) *
|
|
|
|
winningsPool
|
2022-01-10 23:52:03 +00:00
|
|
|
)
|
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-01-12 19:01:04 +00:00
|
|
|
// deprecated use MKT payout
|
2022-01-10 21:07:57 +00:00
|
|
|
export function currentValue(contract: Contract, bet: Bet) {
|
2022-01-10 23:52:03 +00:00
|
|
|
const prob = getProbability(contract.pool)
|
|
|
|
const yesPayout = calculatePayout(contract, bet, 'YES')
|
|
|
|
const noPayout = calculatePayout(contract, bet, 'NO')
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-01-10 23:52:03 +00:00
|
|
|
return prob * yesPayout + (1 - prob) * noPayout
|
2022-01-10 21:07:57 +00:00
|
|
|
}
|