diff --git a/common/calculate-metrics.ts b/common/calculate-metrics.ts index 7c2153c1..524dfadd 100644 --- a/common/calculate-metrics.ts +++ b/common/calculate-metrics.ts @@ -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 diff --git a/common/contract.ts b/common/contract.ts index 1255874d..2656b5d5 100644 --- a/common/contract.ts +++ b/common/contract.ts @@ -49,6 +49,7 @@ export type Contract = { volume: number volume24Hours: number volume7Days: number + elasticity: number collectedFees: Fees diff --git a/common/new-bet.ts b/common/new-bet.ts index 91faf640..e9f5c554 100644 --- a/common/new-bet.ts +++ b/common/new-bet.ts @@ -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 diff --git a/common/new-contract.ts b/common/new-contract.ts index 3580b164..8ab44d2e 100644 --- a/common/new-contract.ts +++ b/common/new-contract.ts @@ -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, diff --git a/functions/src/update-metrics.ts b/functions/src/update-metrics.ts index 70c7c742..24dc07e7 100644 --- a/functions/src/update-metrics.ts +++ b/functions/src/update-metrics.ts @@ -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, }, }