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-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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-02-13 01:19:17 +00:00
|
|
|
const { totalShares, 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-02-13 01:19:17 +00:00
|
|
|
const pool = contract.pool.YES + contract.pool.NO
|
|
|
|
const total = totalShares[outcome] - phantomShares[outcome]
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-02-13 01:19:17 +00:00
|
|
|
const winnings = (shares / total) * pool
|
|
|
|
// 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
|
|
|
|
|
|
|
|
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-30 21:51:30 +00:00
|
|
|
const p =
|
|
|
|
contract.resolutionProbability !== undefined
|
|
|
|
? contract.resolutionProbability
|
|
|
|
: getProbability(contract.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 =
|
|
|
|
p * (contract.totalShares.YES - contract.phantomShares.YES) +
|
|
|
|
(1 - p) * (contract.totalShares.NO - contract.phantomShares.NO)
|
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-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
|
|
|
}
|
2022-02-13 01:19:17 +00:00
|
|
|
|
|
|
|
export const deductFees = (betAmount: number, winnings: number) => {
|
|
|
|
return winnings > betAmount
|
|
|
|
? betAmount + (1 - FEES) * (winnings - betAmount)
|
|
|
|
: winnings
|
|
|
|
}
|