compute elasticity

This commit is contained in:
mantikoros 2022-10-06 16:36:16 -05:00
parent ac37f94cf7
commit 7ca0fb72fc
5 changed files with 63 additions and 5 deletions

View File

@ -1,9 +1,15 @@
import { last, sortBy, sum, sumBy } from 'lodash'
import { calculatePayout } from './calculate'
import { Bet } from './bet'
import { Contract } from './contract'
import { Bet, LimitBet } from './bet'
import {
Contract,
CPMMContract,
DPMContract,
} from './contract'
import { PortfolioMetrics, User } from './user'
import { DAY_MS } from './util/time'
import { getBinaryCpmmBetInfo, getNewMultiBetInfo } from './new-bet'
import { getCpmmProbability } from './calculate-cpmm'
const computeInvestmentValue = (
bets: Bet[],
@ -40,6 +46,55 @@ export const computeInvestmentValueCustomProb = (
})
}
export const computeElasticity = (
bets: Bet[],
contract: Contract,
betAmount = 50
) => {
const { mechanism, outcomeType } = contract
return mechanism === 'cpmm-1' &&
(outcomeType === 'BINARY' || outcomeType === 'PSEUDO_NUMERIC')
? computeBinaryCpmmElasticity(bets, contract, betAmount)
: computeDpmElasticity(contract, betAmount)
}
export const computeBinaryCpmmElasticity = (
bets: Bet[],
contract: CPMMContract,
betAmount = 50
) => {
const limitBets = bets
.filter(
(b) =>
!b.isFilled && !b.isSold && !b.isRedemption && !b.sale && !b.isCancelled
)
.sort((a, b) => a.createdTime - b.createdTime)
const { newPool: poolY, newP: pY } = getBinaryCpmmBetInfo(
'YES',
betAmount,
contract,
undefined,
limitBets as LimitBet[]
)
const resultYes = getCpmmProbability(poolY, pY)
const { newPool: poolN, newP: pN } = getBinaryCpmmBetInfo(
'NO',
betAmount,
contract,
undefined,
limitBets as LimitBet[]
)
const resultNo = getCpmmProbability(poolN, pN)
return resultYes - resultNo
}
export const computeDpmElasticity = (contract: DPMContract, betAmount = 50) => {
return getNewMultiBetInfo('', betAmount, contract).newBet.probAfter
}
const computeTotalPool = (userContracts: Contract[], startTime = 0) => {
const periodFilteredContracts = userContracts.filter(
(contract) => contract.createdTime >= startTime

View File

@ -49,6 +49,7 @@ export type Contract<T extends AnyContractType = AnyContractType> = {
volume: number
volume24Hours: number
volume7Days: number
elasticity: number
collectedFees: Fees

View File

@ -17,8 +17,7 @@ import {
import {
CPMMBinaryContract,
DPMBinaryContract,
FreeResponseContract,
MultipleChoiceContract,
DPMContract,
NumericContract,
PseudoNumericContract,
} from './contract'
@ -325,7 +324,7 @@ export const getNewBinaryDpmBetInfo = (
export const getNewMultiBetInfo = (
outcome: string,
amount: number,
contract: FreeResponseContract | MultipleChoiceContract
contract: DPMContract
) => {
const { pool, totalShares, totalBets } = contract

View File

@ -70,6 +70,7 @@ export function getNewContract(
volume: 0,
volume24Hours: 0,
volume7Days: 0,
elasticity: propsByOutcomeType.mechanism === 'cpmm-1' ? 0.38 : 0.56,
collectedFees: {
creatorFee: 0,

View File

@ -14,6 +14,7 @@ import {
calculateNewPortfolioMetrics,
calculateNewProfit,
calculateProbChanges,
computeElasticity,
computeVolume,
} from '../../common/calculate-metrics'
import { getProbability } from '../../common/calculate'
@ -103,6 +104,7 @@ export async function updateMetricsCore() {
fields: {
volume24Hours: computeVolume(contractBets, now - DAY_MS),
volume7Days: computeVolume(contractBets, now - DAY_MS * 7),
elasticity: computeElasticity(contractBets, contract),
...cpmmFields,
},
}