2022-05-22 08:36:05 +00:00
|
|
|
import { maxBy } from 'lodash'
|
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'
|
2022-04-20 21:36:41 +00:00
|
|
|
import {
|
|
|
|
Binary,
|
|
|
|
Contract,
|
|
|
|
CPMM,
|
|
|
|
DPM,
|
|
|
|
FreeResponseContract,
|
|
|
|
FullContract,
|
|
|
|
} from './contract'
|
2022-03-15 22:27:51 +00:00
|
|
|
|
|
|
|
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-04-18 22:15:25 +00:00
|
|
|
if (contract.initialProbability) return contract.initialProbability
|
|
|
|
|
|
|
|
if (contract.mechanism === 'dpm-2' || (contract as any).totalShares)
|
|
|
|
// use totalShares to calculate prob for ported contracts
|
|
|
|
return getDpmProbability(
|
|
|
|
(contract as any).phantomShares ?? (contract as any).totalShares
|
|
|
|
)
|
|
|
|
|
|
|
|
return getCpmmProbability(contract.pool, contract.p)
|
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
|
|
|
}
|
2022-04-03 19:48:53 +00:00
|
|
|
|
|
|
|
export function getContractBetMetrics(contract: Contract, yourBets: Bet[]) {
|
|
|
|
const { resolution } = contract
|
|
|
|
|
2022-04-14 03:28:29 +00:00
|
|
|
let currentInvested = 0
|
|
|
|
let totalInvested = 0
|
2022-04-03 19:48:53 +00:00
|
|
|
let payout = 0
|
|
|
|
let loan = 0
|
2022-04-14 01:42:47 +00:00
|
|
|
let saleValue = 0
|
2022-04-03 19:48:53 +00:00
|
|
|
let redeemed = 0
|
|
|
|
|
|
|
|
for (const bet of yourBets) {
|
|
|
|
const { isSold, sale, amount, loanAmount, isRedemption } = bet
|
|
|
|
if (isSold) {
|
2022-04-14 03:28:29 +00:00
|
|
|
totalInvested += amount
|
2022-04-03 19:48:53 +00:00
|
|
|
} else if (sale) {
|
2022-04-14 01:42:47 +00:00
|
|
|
saleValue += sale.amount
|
2022-04-03 19:48:53 +00:00
|
|
|
} else {
|
|
|
|
if (isRedemption) {
|
|
|
|
redeemed += -1 * amount
|
|
|
|
} else if (amount > 0) {
|
2022-04-14 03:28:29 +00:00
|
|
|
totalInvested += amount
|
2022-04-13 22:25:32 +00:00
|
|
|
} else {
|
2022-04-14 01:42:47 +00:00
|
|
|
saleValue -= amount
|
2022-04-03 19:48:53 +00:00
|
|
|
}
|
|
|
|
|
2022-04-14 03:28:29 +00:00
|
|
|
currentInvested += amount
|
2022-04-03 19:48:53 +00:00
|
|
|
loan += loanAmount ?? 0
|
|
|
|
payout += resolution
|
|
|
|
? calculatePayout(contract, bet, resolution)
|
|
|
|
: calculatePayout(contract, bet, 'MKT')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-14 03:28:29 +00:00
|
|
|
const netPayout = payout - loan
|
|
|
|
const profit = payout + saleValue + redeemed - totalInvested
|
|
|
|
const profitPercent = (profit / totalInvested) * 100
|
2022-04-03 19:48:53 +00:00
|
|
|
|
|
|
|
return {
|
2022-04-14 03:28:29 +00:00
|
|
|
invested: Math.max(0, currentInvested),
|
2022-04-03 19:48:53 +00:00
|
|
|
payout,
|
2022-04-14 03:28:29 +00:00
|
|
|
netPayout,
|
2022-04-03 19:48:53 +00:00
|
|
|
profit,
|
|
|
|
profitPercent,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getContractBetNullMetrics() {
|
|
|
|
return {
|
|
|
|
invested: 0,
|
|
|
|
payout: 0,
|
2022-04-14 03:28:29 +00:00
|
|
|
netPayout: 0,
|
2022-04-03 19:48:53 +00:00
|
|
|
profit: 0,
|
|
|
|
profitPercent: 0,
|
|
|
|
}
|
|
|
|
}
|
2022-04-20 21:36:41 +00:00
|
|
|
|
|
|
|
export function getTopAnswer(contract: FreeResponseContract) {
|
|
|
|
const { answers } = contract
|
2022-05-22 08:36:05 +00:00
|
|
|
const top = maxBy(
|
2022-04-21 17:43:54 +00:00
|
|
|
answers?.map((answer) => ({
|
2022-04-20 21:36:41 +00:00
|
|
|
answer,
|
|
|
|
prob: getOutcomeProbability(contract, answer.id),
|
|
|
|
})),
|
|
|
|
({ prob }) => prob
|
|
|
|
)
|
|
|
|
return top?.answer
|
|
|
|
}
|