Cache prob and prob changes on cpmm contracts
This commit is contained in:
parent
085b9aeb2a
commit
9060abde8e
|
@ -1,4 +1,4 @@
|
||||||
import { sortBy, sum, sumBy } from 'lodash'
|
import { last, sortBy, sum, sumBy } from 'lodash'
|
||||||
import { calculatePayout } from './calculate'
|
import { calculatePayout } from './calculate'
|
||||||
import { Bet } from './bet'
|
import { Bet } from './bet'
|
||||||
import { Contract } from './contract'
|
import { Contract } from './contract'
|
||||||
|
@ -36,6 +36,33 @@ export const computeVolume = (contractBets: Bet[], since: number) => {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const calculateProbChangeSince = (descendingBets: Bet[], since: number) => {
|
||||||
|
const newestBet = descendingBets[0]
|
||||||
|
if (!newestBet) return 0
|
||||||
|
|
||||||
|
const betBeforeSince = descendingBets.find((b) => b.createdTime < since)
|
||||||
|
|
||||||
|
if (!betBeforeSince) {
|
||||||
|
const oldestBet = last(descendingBets) ?? newestBet
|
||||||
|
return newestBet.probAfter - oldestBet.probBefore
|
||||||
|
}
|
||||||
|
|
||||||
|
return newestBet.probAfter - betBeforeSince.probAfter
|
||||||
|
}
|
||||||
|
|
||||||
|
export const calculateProbChanges = (descendingBets: Bet[]) => {
|
||||||
|
const now = Date.now()
|
||||||
|
const yesterday = now - DAY_MS
|
||||||
|
const weekAgo = now - 7 * DAY_MS
|
||||||
|
const monthAgo = now - 30 * DAY_MS
|
||||||
|
|
||||||
|
return {
|
||||||
|
day: calculateProbChangeSince(descendingBets, yesterday),
|
||||||
|
week: calculateProbChangeSince(descendingBets, weekAgo),
|
||||||
|
month: calculateProbChangeSince(descendingBets, monthAgo),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const calculateCreatorVolume = (userContracts: Contract[]) => {
|
export const calculateCreatorVolume = (userContracts: Contract[]) => {
|
||||||
const allTimeCreatorVolume = computeTotalPool(userContracts, 0)
|
const allTimeCreatorVolume = computeTotalPool(userContracts, 0)
|
||||||
const monthlyCreatorVolume = computeTotalPool(
|
const monthlyCreatorVolume = computeTotalPool(
|
||||||
|
|
|
@ -87,6 +87,12 @@ export type CPMM = {
|
||||||
pool: { [outcome: string]: number }
|
pool: { [outcome: string]: number }
|
||||||
p: number // probability constant in y^p * n^(1-p) = k
|
p: number // probability constant in y^p * n^(1-p) = k
|
||||||
totalLiquidity: number // in M$
|
totalLiquidity: number // in M$
|
||||||
|
prob: number
|
||||||
|
probChanges: {
|
||||||
|
day: number
|
||||||
|
week: number
|
||||||
|
month: number
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Binary = {
|
export type Binary = {
|
||||||
|
|
|
@ -123,6 +123,8 @@ const getBinaryCpmmProps = (initialProb: number, ante: number) => {
|
||||||
initialProbability: p,
|
initialProbability: p,
|
||||||
p,
|
p,
|
||||||
pool: pool,
|
pool: pool,
|
||||||
|
prob: initialProb,
|
||||||
|
probChanges: { day: 0, week: 0, month: 0 },
|
||||||
}
|
}
|
||||||
|
|
||||||
return system
|
return system
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import * as functions from 'firebase-functions'
|
import * as functions from 'firebase-functions'
|
||||||
import * as admin from 'firebase-admin'
|
import * as admin from 'firebase-admin'
|
||||||
import { groupBy, isEmpty, keyBy, last } from 'lodash'
|
import { groupBy, isEmpty, keyBy, last, sortBy } from 'lodash'
|
||||||
import { getValues, log, logMemory, writeAsync } from './utils'
|
import { getValues, log, logMemory, writeAsync } from './utils'
|
||||||
import { Bet } from '../../common/bet'
|
import { Bet } from '../../common/bet'
|
||||||
import { Contract } from '../../common/contract'
|
import { Contract, CPMM } from '../../common/contract'
|
||||||
import { PortfolioMetrics, User } from '../../common/user'
|
import { PortfolioMetrics, User } from '../../common/user'
|
||||||
import { DAY_MS } from '../../common/util/time'
|
import { DAY_MS } from '../../common/util/time'
|
||||||
import { getLoanUpdates } from '../../common/loans'
|
import { getLoanUpdates } from '../../common/loans'
|
||||||
|
@ -11,8 +11,10 @@ import {
|
||||||
calculateCreatorVolume,
|
calculateCreatorVolume,
|
||||||
calculateNewPortfolioMetrics,
|
calculateNewPortfolioMetrics,
|
||||||
calculateNewProfit,
|
calculateNewProfit,
|
||||||
|
calculateProbChanges,
|
||||||
computeVolume,
|
computeVolume,
|
||||||
} from '../../common/calculate-metrics'
|
} from '../../common/calculate-metrics'
|
||||||
|
import { getProbability } from '../../common/calculate'
|
||||||
|
|
||||||
const firestore = admin.firestore()
|
const firestore = admin.firestore()
|
||||||
|
|
||||||
|
@ -43,11 +45,29 @@ export async function updateMetricsCore() {
|
||||||
.filter((contract) => contract.id)
|
.filter((contract) => contract.id)
|
||||||
.map((contract) => {
|
.map((contract) => {
|
||||||
const contractBets = betsByContract[contract.id] ?? []
|
const contractBets = betsByContract[contract.id] ?? []
|
||||||
|
const descendingBets = sortBy(
|
||||||
|
contractBets,
|
||||||
|
(bet) => bet.createdTime
|
||||||
|
).reverse()
|
||||||
|
|
||||||
|
let cpmmFields: Partial<CPMM> = {}
|
||||||
|
if (contract.mechanism === 'cpmm-1') {
|
||||||
|
const prob = descendingBets[0]
|
||||||
|
? descendingBets[0].probAfter
|
||||||
|
: getProbability(contract)
|
||||||
|
|
||||||
|
cpmmFields = {
|
||||||
|
prob,
|
||||||
|
probChanges: calculateProbChanges(descendingBets),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
doc: firestore.collection('contracts').doc(contract.id),
|
doc: firestore.collection('contracts').doc(contract.id),
|
||||||
fields: {
|
fields: {
|
||||||
volume24Hours: computeVolume(contractBets, now - DAY_MS),
|
volume24Hours: computeVolume(contractBets, now - DAY_MS),
|
||||||
volume7Days: computeVolume(contractBets, now - DAY_MS * 7),
|
volume7Days: computeVolume(contractBets, now - DAY_MS * 7),
|
||||||
|
...cpmmFields,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue
Block a user