From 61f30d4ab933c4ac6a490096b93ba3a6fdf2dfc8 Mon Sep 17 00:00:00 2001 From: mantikoros Date: Thu, 3 Mar 2022 00:01:39 -0500 Subject: [PATCH] mechanism-agnostic calculations --- common/calculate.ts | 63 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 common/calculate.ts diff --git a/common/calculate.ts b/common/calculate.ts new file mode 100644 index 00000000..b10906b2 --- /dev/null +++ b/common/calculate.ts @@ -0,0 +1,63 @@ +import { Bet } from './bet' +import { + calculateCpmmShares, + getCpmmProbability, + getCpmmProbabilityAfterBet, +} from './calculate-cpmm' +import { + calculateDpmPayoutAfterCorrectBet, + calculateDpmShares, + getDpmOutcomeProbability, + getDpmProbability, + getDpmProbabilityAfterBet, +} from './calculate-dpm' +import { Binary, Contract, CPMM, DPM, FullContract } from './contract' +import { FEES } from './fees' + +export function getProbability(contract: FullContract) { + return contract.mechanism === 'cpmm-1' + ? getCpmmProbability(contract.pool) + : getDpmProbability(contract.totalShares) +} + +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, + 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) +} + +export const deductFees = (betAmount: number, winnings: number) => { + return winnings > betAmount + ? betAmount + (1 - FEES) * (winnings - betAmount) + : winnings +}