2022-01-10 23:52:03 +00:00
|
|
|
import { Bet } from './bet'
|
2022-03-15 22:27:51 +00:00
|
|
|
import {
|
|
|
|
calculateCpmmSale,
|
|
|
|
getCpmmProbability,
|
|
|
|
getCpmmOutcomeProbabilityAfterBet,
|
|
|
|
getCpmmProbabilityAfterSale,
|
|
|
|
calculateCpmmSharesAfterFee,
|
|
|
|
} from './calculate-cpmm'
|
|
|
|
import {
|
|
|
|
calculateDpmPayout,
|
|
|
|
calculateDpmPayoutAfterCorrectBet,
|
|
|
|
calculateDpmSaleAmount,
|
|
|
|
calculateDpmShares,
|
|
|
|
getDpmOutcomeProbability,
|
|
|
|
getDpmProbability,
|
|
|
|
getDpmOutcomeProbabilityAfterBet,
|
|
|
|
getDpmProbabilityAfterSale,
|
|
|
|
} from './calculate-dpm'
|
|
|
|
import { calculateFixedPayout } from './calculate-fixed-payouts'
|
|
|
|
import { Binary, Contract, CPMM, DPM, FullContract } from './contract'
|
|
|
|
|
|
|
|
export function getProbability(contract: FullContract<DPM | CPMM, Binary>) {
|
|
|
|
return contract.mechanism === 'cpmm-1'
|
|
|
|
? getCpmmProbability(contract.pool, contract.p)
|
|
|
|
: getDpmProbability(contract.totalShares)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getInitialProbability(
|
|
|
|
contract: FullContract<DPM | CPMM, Binary>
|
2022-02-17 23:00:19 +00:00
|
|
|
) {
|
2022-03-15 22:27:51 +00:00
|
|
|
return (
|
|
|
|
contract.initialProbability ??
|
|
|
|
(contract.mechanism === 'cpmm-1'
|
|
|
|
? getCpmmProbability(contract.pool, contract.p)
|
|
|
|
: getDpmProbability(contract.phantomShares ?? contract.totalShares))
|
|
|
|
)
|
2022-01-10 23:52:03 +00:00
|
|
|
}
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
export function getOutcomeProbability(contract: Contract, outcome: string) {
|
|
|
|
return contract.mechanism === 'cpmm-1'
|
|
|
|
? getCpmmProbability(contract.pool, contract.p)
|
|
|
|
: getDpmOutcomeProbability(contract.totalShares, outcome)
|
2022-01-10 21:07:57 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
export function getOutcomeProbabilityAfterBet(
|
|
|
|
contract: Contract,
|
2022-02-18 00:24:00 +00:00
|
|
|
outcome: string,
|
2022-03-15 22:27:51 +00:00
|
|
|
bet: number
|
2022-02-18 00:24:00 +00:00
|
|
|
) {
|
2022-03-15 22:27:51 +00:00
|
|
|
return contract.mechanism === 'cpmm-1'
|
|
|
|
? getCpmmOutcomeProbabilityAfterBet(
|
|
|
|
contract as FullContract<CPMM, Binary>,
|
|
|
|
outcome,
|
|
|
|
bet
|
|
|
|
)
|
|
|
|
: getDpmOutcomeProbabilityAfterBet(contract.totalShares, outcome, bet)
|
2022-02-18 00:24:00 +00:00
|
|
|
}
|
|
|
|
|
2022-01-10 21:07:57 +00:00
|
|
|
export function calculateShares(
|
2022-03-15 22:27:51 +00:00
|
|
|
contract: Contract,
|
2022-01-10 21:07:57 +00:00
|
|
|
bet: number,
|
2022-02-17 23:00:19 +00:00
|
|
|
betChoice: string
|
2022-01-12 19:01:04 +00:00
|
|
|
) {
|
2022-03-15 22:27:51 +00:00
|
|
|
return contract.mechanism === 'cpmm-1'
|
|
|
|
? calculateCpmmSharesAfterFee(
|
|
|
|
contract as FullContract<CPMM, Binary>,
|
|
|
|
bet,
|
|
|
|
betChoice
|
|
|
|
)
|
|
|
|
: calculateDpmShares(contract.totalShares, bet, betChoice)
|
2022-01-12 19:01:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function calculateSaleAmount(contract: Contract, bet: Bet) {
|
2022-03-15 22:27:51 +00:00
|
|
|
return contract.mechanism === 'cpmm-1' && contract.outcomeType === 'BINARY'
|
2022-03-30 02:30:04 +00:00
|
|
|
? calculateCpmmSale(contract, Math.abs(bet.shares), bet.outcome).saleValue
|
2022-03-15 22:27:51 +00:00
|
|
|
: calculateDpmSaleAmount(contract, bet)
|
2022-01-12 19:01:04 +00:00
|
|
|
}
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
export function calculatePayoutAfterCorrectBet(contract: Contract, bet: Bet) {
|
|
|
|
return contract.mechanism === 'cpmm-1'
|
|
|
|
? bet.shares
|
|
|
|
: calculateDpmPayoutAfterCorrectBet(contract, bet)
|
2022-01-12 19:01:04 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
export function getProbabilityAfterSale(
|
2022-01-12 19:01:04 +00:00
|
|
|
contract: Contract,
|
2022-03-15 22:27:51 +00:00
|
|
|
outcome: string,
|
|
|
|
shares: number
|
2022-01-12 19:01:04 +00:00
|
|
|
) {
|
2022-03-15 22:27:51 +00:00
|
|
|
return contract.mechanism === 'cpmm-1'
|
|
|
|
? getCpmmProbabilityAfterSale(
|
|
|
|
contract as FullContract<CPMM, Binary>,
|
2022-03-30 02:30:04 +00:00
|
|
|
shares,
|
|
|
|
outcome as 'YES' | 'NO'
|
2022-03-15 22:27:51 +00:00
|
|
|
)
|
|
|
|
: getDpmProbabilityAfterSale(contract.totalShares, outcome, shares)
|
2022-01-10 21:07:57 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
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)
|
2022-01-10 21:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function resolvedPayout(contract: Contract, bet: Bet) {
|
2022-03-15 22:27:51 +00:00
|
|
|
const outcome = contract.resolution
|
|
|
|
if (!outcome) throw new Error('Contract not resolved')
|
2022-01-10 21:07:57 +00:00
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
return contract.mechanism === 'cpmm-1' && contract.outcomeType === 'BINARY'
|
|
|
|
? calculateFixedPayout(contract, bet, outcome)
|
|
|
|
: calculateDpmPayout(contract, bet, outcome)
|
2022-02-13 01:19:17 +00:00
|
|
|
}
|