manifold/common/calculate.ts

112 lines
3.3 KiB
TypeScript
Raw Normal View History

2022-03-03 05:01:39 +00:00
import { Bet } from './bet'
import {
calculateCpmmSale,
2022-03-03 05:01:39 +00:00
getCpmmProbability,
2022-03-03 17:06:24 +00:00
getCpmmOutcomeProbabilityAfterBet,
2022-03-03 05:32:33 +00:00
getCpmmProbabilityAfterSale,
2022-03-07 23:49:55 +00:00
calculateCpmmSharesAfterFee,
2022-03-03 05:01:39 +00:00
} from './calculate-cpmm'
import {
calculateDpmPayout,
2022-03-03 05:01:39 +00:00
calculateDpmPayoutAfterCorrectBet,
calculateDpmSaleAmount,
2022-03-03 05:01:39 +00:00
calculateDpmShares,
getDpmOutcomeProbability,
getDpmProbability,
2022-03-03 17:06:24 +00:00
getDpmOutcomeProbabilityAfterBet,
2022-03-03 05:32:33 +00:00
getDpmProbabilityAfterSale,
2022-03-03 05:01:39 +00:00
} from './calculate-dpm'
import { calculateFixedPayout } from './calculate-fixed-payouts'
2022-03-03 05:01:39 +00:00
import { Binary, Contract, CPMM, DPM, FullContract } from './contract'
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>
) {
2022-03-10 21:58:44 +00:00
return (
contract.initialProbability ??
(contract.mechanism === 'cpmm-1'
? getCpmmProbability(contract.pool)
: getDpmProbability(contract.phantomShares ?? contract.totalShares))
)
2022-03-03 05:32:33 +00:00
}
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)
}
2022-03-03 17:06:24 +00:00
export function getOutcomeProbabilityAfterBet(
2022-03-03 05:01:39 +00:00
contract: Contract,
outcome: string,
bet: number
) {
return contract.mechanism === 'cpmm-1'
2022-03-03 17:06:24 +00:00
? getCpmmOutcomeProbabilityAfterBet(
2022-03-03 05:01:39 +00:00
contract as FullContract<CPMM, Binary>,
outcome,
bet
)
2022-03-03 17:06:24 +00:00
: getDpmOutcomeProbabilityAfterBet(contract.totalShares, outcome, bet)
2022-03-03 05:01:39 +00:00
}
export function calculateShares(
contract: Contract,
bet: number,
betChoice: string
) {
return contract.mechanism === 'cpmm-1'
2022-03-07 23:49:55 +00:00
? calculateCpmmSharesAfterFee(
contract as FullContract<CPMM, Binary>,
bet,
betChoice
)
2022-03-03 05:01:39 +00:00
: calculateDpmShares(contract.totalShares, bet, betChoice)
}
export function calculateSaleAmount(contract: Contract, bet: Bet) {
return contract.mechanism === 'cpmm-1' && contract.outcomeType === 'BINARY'
2022-03-10 21:05:52 +00:00
? calculateCpmmSale(contract, bet).saleValue
: calculateDpmSaleAmount(contract, bet)
}
2022-03-03 05:01:39 +00:00
export function calculatePayoutAfterCorrectBet(contract: Contract, bet: Bet) {
return contract.mechanism === 'cpmm-1'
? bet.shares
2022-03-03 05:01:39 +00:00
: 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)
}
export function calculatePayout(contract: Contract, bet: Bet, outcome: string) {
return contract.mechanism === 'cpmm-1' && contract.outcomeType === 'BINARY'
? calculateFixedPayout(contract, bet, outcome)
: calculateDpmPayout(contract, bet, outcome)
}
export function resolvedPayout(contract: Contract, bet: Bet) {
const outcome = contract.resolution
if (!outcome) throw new Error('Contract not resolved')
return contract.mechanism === 'cpmm-1' && contract.outcomeType === 'BINARY'
? calculateFixedPayout(contract, bet, outcome)
: calculateDpmPayout(contract, bet, outcome)
2022-03-03 05:01:39 +00:00
}