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-06-01 02:42:35 +00:00
|
|
|
import { Contract, BinaryContract, FreeResponseContract } from './contract'
|
2022-03-15 22:27:51 +00:00
|
|
|
|
2022-06-01 02:42:35 +00:00
|
|
|
export function getProbability(contract: BinaryContract) {
|
2022-03-15 22:27:51 +00:00
|
|
|
return contract.mechanism === 'cpmm-1'
|
|
|
|
? getCpmmProbability(contract.pool, contract.p)
|
|
|
|
: getDpmProbability(contract.totalShares)
|
|
|
|
}
|
|
|
|
|
2022-06-01 02:42:35 +00:00
|
|
|
export function getInitialProbability(contract: BinaryContract) {
|
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'
|
2022-06-01 02:42:35 +00:00
|
|
|
? getCpmmOutcomeProbabilityAfterBet(contract, outcome, bet)
|
2022-03-15 22:27:51 +00:00
|
|
|
: 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'
|
2022-06-01 02:42:35 +00:00
|
|
|
? calculateCpmmSharesAfterFee(contract, bet, betChoice)
|
2022-03-15 22:27:51 +00:00
|
|
|
: 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'
|
2022-06-01 02:42:35 +00:00
|
|
|
? getCpmmProbabilityAfterSale(contract, 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
|
2022-05-28 05:18:07 +00:00
|
|
|
const totalShares: { [outcome: string]: number } = {}
|
2022-04-03 19:48:53 +00:00
|
|
|
|
|
|
|
for (const bet of yourBets) {
|
2022-05-28 05:18:07 +00:00
|
|
|
const { isSold, sale, amount, loanAmount, isRedemption, shares, outcome } =
|
|
|
|
bet
|
|
|
|
totalShares[outcome] = (totalShares[outcome] ?? 0) + shares
|
|
|
|
|
2022-04-03 19:48:53 +00:00
|
|
|
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
|
|
|
|
2022-06-21 15:14:53 +00:00
|
|
|
const hasShares = Object.values(totalShares).some(
|
|
|
|
(shares) => shares > 0
|
|
|
|
)
|
|
|
|
|
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,
|
2022-05-28 05:18:07 +00:00
|
|
|
totalShares,
|
2022-06-21 15:14:53 +00:00
|
|
|
hasShares,
|
2022-04-03 19:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-28 05:18:07 +00:00
|
|
|
totalShares: {} as { [outcome: string]: number },
|
2022-06-21 15:14:53 +00:00
|
|
|
hasShares: false,
|
2022-04-03 19:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|