2021-12-15 22:57:40 +00:00
|
|
|
import { Bet } from '../firebase/bets'
|
|
|
|
import { Contract } from '../firebase/contracts'
|
|
|
|
|
2021-12-15 23:27:02 +00:00
|
|
|
const fees = 0.02
|
|
|
|
|
2021-12-15 22:57:40 +00:00
|
|
|
export function getProbability(pot: { YES: number; NO: number }) {
|
|
|
|
const [yesPot, noPot] = [pot.YES, pot.NO]
|
|
|
|
const numerator = Math.pow(yesPot, 2)
|
|
|
|
const denominator = Math.pow(yesPot, 2) + Math.pow(noPot, 2)
|
|
|
|
return numerator / denominator
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getProbabilityAfterBet(
|
|
|
|
pot: { YES: number; NO: number },
|
|
|
|
outcome: 'YES' | 'NO',
|
|
|
|
bet: number
|
|
|
|
) {
|
|
|
|
const [YES, NO] = [
|
|
|
|
pot.YES + (outcome === 'YES' ? bet : 0),
|
|
|
|
pot.NO + (outcome === 'NO' ? bet : 0),
|
|
|
|
]
|
|
|
|
return getProbability({ YES, NO })
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getDpmWeight(
|
|
|
|
pot: { YES: number; NO: number },
|
|
|
|
bet: number,
|
|
|
|
betChoice: 'YES' | 'NO'
|
|
|
|
) {
|
|
|
|
const [yesPot, noPot] = [pot.YES, pot.NO]
|
|
|
|
|
|
|
|
return betChoice === 'YES'
|
|
|
|
? (bet * Math.pow(noPot, 2)) / (Math.pow(yesPot, 2) + bet * yesPot)
|
|
|
|
: (bet * Math.pow(yesPot, 2)) / (Math.pow(noPot, 2) + bet * noPot)
|
|
|
|
}
|
|
|
|
|
2021-12-16 02:27:09 +00:00
|
|
|
export function calculatePayout(
|
2021-12-15 22:57:40 +00:00
|
|
|
contract: Contract,
|
|
|
|
bet: Bet,
|
|
|
|
outcome: 'YES' | 'NO' | 'CANCEL'
|
|
|
|
) {
|
|
|
|
const { amount, outcome: betOutcome, dpmWeight } = bet
|
|
|
|
|
|
|
|
if (outcome === 'CANCEL') return amount
|
2021-12-15 23:27:02 +00:00
|
|
|
if (betOutcome !== outcome) return 0
|
|
|
|
|
|
|
|
let { dpmWeights, pot, seedAmounts } = contract
|
|
|
|
|
|
|
|
// Fake data if not set.
|
|
|
|
if (!dpmWeights) dpmWeights = { YES: 100, NO: 100 }
|
2021-12-15 22:57:40 +00:00
|
|
|
|
|
|
|
// Fake data if not set.
|
|
|
|
if (!pot) pot = { YES: 100, NO: 100 }
|
|
|
|
|
2021-12-15 23:27:02 +00:00
|
|
|
const otherOutcome = outcome === 'YES' ? 'NO' : 'YES'
|
|
|
|
const potSize = pot[otherOutcome] - seedAmounts[otherOutcome]
|
|
|
|
|
|
|
|
return (1 - fees) * (dpmWeight / dpmWeights[outcome]) * potSize + amount
|
2021-12-15 22:57:40 +00:00
|
|
|
}
|
2021-12-16 02:27:09 +00:00
|
|
|
export function resolvedPayout(contract: Contract, bet: Bet) {
|
|
|
|
if (contract.resolution)
|
|
|
|
return calculatePayout(contract, bet, contract.resolution)
|
|
|
|
throw new Error('Contract was not resolved')
|
|
|
|
}
|
2021-12-15 22:57:40 +00:00
|
|
|
|
|
|
|
export function currentValue(contract: Contract, bet: Bet) {
|
|
|
|
const prob = getProbability(contract.pot)
|
2021-12-16 02:27:09 +00:00
|
|
|
const yesPayout = calculatePayout(contract, bet, 'YES')
|
|
|
|
const noPayout = calculatePayout(contract, bet, 'NO')
|
2021-12-15 22:57:40 +00:00
|
|
|
|
2021-12-16 02:27:09 +00:00
|
|
|
return prob * yesPayout + (1 - prob) * noPayout
|
2021-12-15 22:57:40 +00:00
|
|
|
}
|