2022-09-18 22:57:50 +00:00
|
|
|
import { maxBy, partition, sortBy, sum, sumBy } from 'lodash'
|
2022-07-10 18:05:44 +00:00
|
|
|
import { Bet, LimitBet } 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-07-02 19:37:59 +00:00
|
|
|
import {
|
|
|
|
Contract,
|
|
|
|
BinaryContract,
|
|
|
|
FreeResponseContract,
|
|
|
|
PseudoNumericContract,
|
2022-07-28 02:40:33 +00:00
|
|
|
MultipleChoiceContract,
|
2022-07-02 19:37:59 +00:00
|
|
|
} from './contract'
|
2022-07-10 18:05:44 +00:00
|
|
|
import { floatingEqual } from './util/math'
|
2022-07-02 19:37:59 +00:00
|
|
|
|
|
|
|
export function getProbability(
|
|
|
|
contract: BinaryContract | PseudoNumericContract
|
|
|
|
) {
|
2022-03-15 22:27:51 +00:00
|
|
|
return contract.mechanism === 'cpmm-1'
|
|
|
|
? getCpmmProbability(contract.pool, contract.p)
|
|
|
|
: getDpmProbability(contract.totalShares)
|
|
|
|
}
|
|
|
|
|
2022-07-02 19:37:59 +00:00
|
|
|
export function getInitialProbability(
|
|
|
|
contract: BinaryContract | PseudoNumericContract
|
|
|
|
) {
|
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
|
|
|
}
|
|
|
|
|
2022-07-10 18:05:44 +00:00
|
|
|
export function calculateSaleAmount(
|
|
|
|
contract: Contract,
|
|
|
|
bet: Bet,
|
|
|
|
unfilledBets: LimitBet[]
|
|
|
|
) {
|
2022-07-02 19:37:59 +00:00
|
|
|
return contract.mechanism === 'cpmm-1' &&
|
|
|
|
(contract.outcomeType === 'BINARY' ||
|
|
|
|
contract.outcomeType === 'PSEUDO_NUMERIC')
|
2022-07-10 18:05:44 +00:00
|
|
|
? calculateCpmmSale(
|
|
|
|
contract,
|
|
|
|
Math.abs(bet.shares),
|
|
|
|
bet.outcome as 'YES' | 'NO',
|
|
|
|
unfilledBets
|
|
|
|
).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,
|
2022-07-10 18:05:44 +00:00
|
|
|
shares: number,
|
|
|
|
unfilledBets: LimitBet[]
|
2022-01-12 19:01:04 +00:00
|
|
|
) {
|
2022-03-15 22:27:51 +00:00
|
|
|
return contract.mechanism === 'cpmm-1'
|
2022-07-10 18:05:44 +00:00
|
|
|
? getCpmmProbabilityAfterSale(
|
|
|
|
contract,
|
|
|
|
shares,
|
|
|
|
outcome as 'YES' | 'NO',
|
|
|
|
unfilledBets
|
|
|
|
)
|
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) {
|
2022-07-02 19:37:59 +00:00
|
|
|
return contract.mechanism === 'cpmm-1' &&
|
|
|
|
(contract.outcomeType === 'BINARY' ||
|
|
|
|
contract.outcomeType === 'PSEUDO_NUMERIC')
|
2022-03-15 22:27:51 +00:00
|
|
|
? 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-07-02 19:37:59 +00:00
|
|
|
return contract.mechanism === 'cpmm-1' &&
|
|
|
|
(contract.outcomeType === 'BINARY' ||
|
|
|
|
contract.outcomeType === 'PSEUDO_NUMERIC')
|
2022-03-15 22:27:51 +00:00
|
|
|
? calculateFixedPayout(contract, bet, outcome)
|
|
|
|
: calculateDpmPayout(contract, bet, outcome)
|
2022-02-13 01:19:17 +00:00
|
|
|
}
|
2022-04-03 19:48:53 +00:00
|
|
|
|
2022-08-16 00:04:35 +00:00
|
|
|
function getCpmmInvested(yourBets: Bet[]) {
|
|
|
|
const totalShares: { [outcome: string]: number } = {}
|
|
|
|
const totalSpent: { [outcome: string]: number } = {}
|
|
|
|
|
|
|
|
const sortedBets = sortBy(yourBets, 'createdTime')
|
|
|
|
for (const bet of sortedBets) {
|
|
|
|
const { outcome, shares, amount } = bet
|
2022-08-27 22:13:29 +00:00
|
|
|
if (floatingEqual(shares, 0)) continue
|
|
|
|
|
2022-09-15 23:32:30 +00:00
|
|
|
const spent = totalSpent[outcome] ?? 0
|
|
|
|
const position = totalShares[outcome] ?? 0
|
|
|
|
|
2022-08-16 00:04:35 +00:00
|
|
|
if (amount > 0) {
|
2022-09-15 23:32:30 +00:00
|
|
|
totalShares[outcome] = position + shares
|
|
|
|
totalSpent[outcome] = spent + amount
|
2022-08-16 00:04:35 +00:00
|
|
|
} else if (amount < 0) {
|
2022-09-15 23:32:30 +00:00
|
|
|
const averagePrice = position === 0 ? 0 : spent / position
|
|
|
|
totalShares[outcome] = position + shares
|
|
|
|
totalSpent[outcome] = spent + averagePrice * shares
|
2022-08-16 00:04:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-15 23:32:30 +00:00
|
|
|
return sum([0, ...Object.values(totalSpent)])
|
2022-08-16 00:04:35 +00:00
|
|
|
}
|
|
|
|
|
2022-08-16 02:33:29 +00:00
|
|
|
function getDpmInvested(yourBets: Bet[]) {
|
|
|
|
const sortedBets = sortBy(yourBets, 'createdTime')
|
|
|
|
|
|
|
|
return sumBy(sortedBets, (bet) => {
|
|
|
|
const { amount, sale } = bet
|
|
|
|
|
|
|
|
if (sale) {
|
|
|
|
const originalBet = sortedBets.find((b) => b.id === sale.betId)
|
|
|
|
if (originalBet) return -originalBet.amount
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return amount
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-03 19:48:53 +00:00
|
|
|
export function getContractBetMetrics(contract: Contract, yourBets: Bet[]) {
|
|
|
|
const { resolution } = contract
|
2022-08-16 00:04:35 +00:00
|
|
|
const isCpmm = contract.mechanism === 'cpmm-1'
|
2022-04-03 19:48:53 +00:00
|
|
|
|
2022-04-14 03:28:29 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-08-16 02:33:29 +00:00
|
|
|
const invested = isCpmm ? getCpmmInvested(yourBets) : getDpmInvested(yourBets)
|
2022-07-10 18:05:44 +00:00
|
|
|
const hasShares = Object.values(totalShares).some(
|
|
|
|
(shares) => !floatingEqual(shares, 0)
|
|
|
|
)
|
2022-06-21 15:14:53 +00:00
|
|
|
|
2022-04-03 19:48:53 +00:00
|
|
|
return {
|
2022-08-16 00:04:35 +00:00
|
|
|
invested,
|
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
|
|
|
|
2022-07-28 02:40:33 +00:00
|
|
|
export function getTopAnswer(
|
|
|
|
contract: FreeResponseContract | MultipleChoiceContract
|
|
|
|
) {
|
2022-04-20 21:36:41 +00:00
|
|
|
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
|
|
|
|
}
|
2022-09-18 22:57:50 +00:00
|
|
|
|
|
|
|
export function getLargestPosition(contract: Contract, userBets: Bet[]) {
|
|
|
|
if (userBets.length === 0) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
if (contract.outcomeType === 'FREE_RESPONSE') {
|
|
|
|
const answerCounts: { [outcome: string]: number } = {}
|
|
|
|
for (const bet of userBets) {
|
|
|
|
if (bet.outcome) {
|
|
|
|
if (!answerCounts[bet.outcome]) {
|
|
|
|
answerCounts[bet.outcome] = bet.amount
|
|
|
|
} else {
|
|
|
|
answerCounts[bet.outcome] += bet.amount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const majorityAnswer =
|
|
|
|
maxBy(Object.keys(answerCounts), (outcome) => answerCounts[outcome]) ?? ''
|
|
|
|
return {
|
|
|
|
prob: undefined,
|
|
|
|
shares: answerCounts[majorityAnswer] || 0,
|
|
|
|
outcome: majorityAnswer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const [yesBets, noBets] = partition(userBets, (bet) => bet.outcome === 'YES')
|
2022-09-20 22:39:34 +00:00
|
|
|
const yesShares = sumBy(yesBets, (bet) => bet.shares)
|
|
|
|
const noShares = sumBy(noBets, (bet) => bet.shares)
|
|
|
|
return {
|
|
|
|
shares: Math.abs(yesShares - noShares),
|
|
|
|
outcome: yesShares > noShares ? 'YES' : 'NO',
|
|
|
|
}
|
2022-09-18 22:57:50 +00:00
|
|
|
}
|