manifold/common/calculate-fixed-payouts.ts
Marshall Polaris 7c4ec2a8e3
Refactor contract typing to be more concise and more correct (#355)
* Refactor contract types slightly

* Refactor contract types greatly

* Kill dead binary DPM contract creation code

* Use BinaryContract, DPMContract, etc. type aliases
2022-05-31 19:42:35 -07:00

39 lines
1000 B
TypeScript

import { Bet } from './bet'
import { getProbability } from './calculate'
import { CPMMContract } from './contract'
export function calculateFixedPayout(
contract: CPMMContract,
bet: Bet,
outcome: string
) {
if (outcome === 'CANCEL') return calculateFixedCancelPayout(bet)
if (outcome === 'MKT') return calculateFixedMktPayout(contract, bet)
return calculateStandardFixedPayout(bet, outcome)
}
export function calculateFixedCancelPayout(bet: Bet) {
return bet.amount
}
export function calculateStandardFixedPayout(bet: Bet, outcome: string) {
const { outcome: betOutcome, shares } = bet
if (betOutcome !== outcome) return 0
return shares
}
function calculateFixedMktPayout(contract: CPMMContract, bet: Bet) {
const { resolutionProbability } = contract
const p =
resolutionProbability !== undefined
? resolutionProbability
: getProbability(contract)
const { outcome, shares } = bet
const betP = outcome === 'YES' ? p : 1 - p
return betP * shares
}