manifold/common/calculate.ts

87 lines
2.4 KiB
TypeScript
Raw Normal View History

2022-03-03 05:01:39 +00:00
import { Bet } from './bet'
import {
calculateCpmmShares,
getCpmmProbability,
getCpmmProbabilityAfterBet,
2022-03-03 05:32:33 +00:00
getCpmmProbabilityAfterSale,
2022-03-03 05:01:39 +00:00
} from './calculate-cpmm'
import {
calculateDpmPayoutAfterCorrectBet,
calculateDpmShares,
getDpmOutcomeProbability,
getDpmProbability,
getDpmProbabilityAfterBet,
2022-03-03 05:32:33 +00:00
getDpmProbabilityAfterSale,
2022-03-03 05:01:39 +00:00
} from './calculate-dpm'
import { Binary, Contract, CPMM, DPM, FullContract } from './contract'
import { FEES } from './fees'
export function getProbability(contract: FullContract<DPM | CPMM, Binary>) {
return contract.mechanism === 'cpmm-1'
? getCpmmProbability(contract.pool)
: getDpmProbability(contract.totalShares)
}
2022-03-03 05:32:33 +00:00
export function getInitialProbability(
contract: FullContract<DPM | CPMM, Binary>
) {
return contract.mechanism === 'cpmm-1'
? getCpmmProbability(contract.liquidity[contract.creatorId])
: getDpmProbability(contract.phantomShares ?? contract.totalShares)
}
2022-03-03 05:01:39 +00:00
export function getOutcomeProbability(contract: Contract, outcome: string) {
return contract.mechanism === 'cpmm-1'
? getCpmmProbability(contract.pool)
: getDpmOutcomeProbability(contract.totalShares, outcome)
}
export function getProbabilityAfterBet(
contract: Contract,
outcome: string,
bet: number
) {
return contract.mechanism === 'cpmm-1'
? getCpmmProbabilityAfterBet(
contract as FullContract<CPMM, Binary>,
outcome,
bet
)
: getDpmProbabilityAfterBet(contract.totalShares, outcome, bet)
}
export function calculateShares(
contract: Contract,
bet: number,
betChoice: string
) {
return contract.mechanism === 'cpmm-1'
? calculateCpmmShares(contract.pool, contract.k, bet, betChoice)
: calculateDpmShares(contract.totalShares, bet, betChoice)
}
export function calculatePayoutAfterCorrectBet(contract: Contract, bet: Bet) {
return contract.mechanism === 'cpmm-1'
? deductFees(bet.amount, bet.shares)
: calculateDpmPayoutAfterCorrectBet(contract, bet)
}
2022-03-03 05:32:33 +00:00
export function getProbabilityAfterSale(
contract: Contract,
outcome: string,
shares: number
) {
return contract.mechanism === 'cpmm-1'
? getCpmmProbabilityAfterSale(
contract as FullContract<CPMM, Binary>,
{ shares, outcome } as Bet
)
: getDpmProbabilityAfterSale(contract.totalShares, outcome, shares)
}
2022-03-03 05:01:39 +00:00
export const deductFees = (betAmount: number, winnings: number) => {
return winnings > betAmount
? betAmount + (1 - FEES) * (winnings - betAmount)
: winnings
}