2022-04-09 18:51:22 +00:00
|
|
|
import * as _ 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'
|
|
|
|
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
|
|
|
}
|
2022-04-03 19:48:53 +00:00
|
|
|
|
|
|
|
export function getContractBetMetrics(contract: Contract, yourBets: Bet[]) {
|
|
|
|
const { resolution } = contract
|
|
|
|
|
|
|
|
let invested = 0
|
|
|
|
let salesInvested = 0
|
|
|
|
let payout = 0
|
|
|
|
let loan = 0
|
|
|
|
let sellProfit = 0
|
|
|
|
let redeemed = 0
|
|
|
|
|
|
|
|
for (const bet of yourBets) {
|
|
|
|
const { isSold, sale, amount, loanAmount, isRedemption } = bet
|
|
|
|
if (isSold) {
|
|
|
|
sellProfit -= amount
|
|
|
|
salesInvested += amount
|
|
|
|
} else if (sale) {
|
|
|
|
sellProfit += sale.amount
|
|
|
|
} else {
|
|
|
|
if (isRedemption) {
|
|
|
|
redeemed += -1 * amount
|
|
|
|
} else if (amount > 0) {
|
|
|
|
invested += amount
|
|
|
|
}
|
|
|
|
|
|
|
|
loan += loanAmount ?? 0
|
|
|
|
payout += resolution
|
|
|
|
? calculatePayout(contract, bet, resolution)
|
|
|
|
: calculatePayout(contract, bet, 'MKT')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const investedIncludingSales = invested + salesInvested
|
|
|
|
|
|
|
|
const totalValue = payout + sellProfit + redeemed
|
|
|
|
const profit = totalValue - invested
|
|
|
|
const profitPercent = (profit / investedIncludingSales) * 100
|
|
|
|
const netInvestment = payout - loan
|
|
|
|
|
|
|
|
return {
|
|
|
|
invested,
|
|
|
|
payout,
|
|
|
|
totalValue,
|
|
|
|
profit,
|
|
|
|
profitPercent,
|
|
|
|
netInvestment,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getContractBetNullMetrics() {
|
|
|
|
return {
|
|
|
|
invested: 0,
|
|
|
|
payout: 0,
|
|
|
|
totalValue: 0,
|
|
|
|
profit: 0,
|
|
|
|
profitPercent: 0,
|
|
|
|
netInvestment: 0,
|
|
|
|
}
|
|
|
|
}
|