From 6b4417d8b818ecb3568b85cbbc561f896696ea44 Mon Sep 17 00:00:00 2001 From: jahooma Date: Sun, 9 Jan 2022 13:34:42 -0600 Subject: [PATCH] Every 15 minutes, compute contract volume in last 24 hours and 7 days --- functions/src/create-contract.ts | 3 ++ functions/src/index.ts | 1 + functions/src/types/contract.ts | 3 ++ functions/src/update-contract-metrics.ts | 40 ++++++++++++++++++++++++ web/lib/firebase/contracts.ts | 3 ++ 5 files changed, 50 insertions(+) create mode 100644 functions/src/update-contract-metrics.ts diff --git a/functions/src/create-contract.ts b/functions/src/create-contract.ts index dd0bbce3..e40a8db9 100644 --- a/functions/src/create-contract.ts +++ b/functions/src/create-contract.ts @@ -114,6 +114,9 @@ function getNewContract( createdTime: Date.now(), lastUpdatedTime: Date.now(), + + volume24Hours: 0, + volume7Days: 0, } if (closeTime) contract.closeTime = closeTime diff --git a/functions/src/index.ts b/functions/src/index.ts index cd2d429a..7d58f8a0 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -8,3 +8,4 @@ export * from './resolve-market' export * from './stripe' export * from './sell-bet' export * from './create-contract' +export * from './update-contract-metrics' diff --git a/functions/src/types/contract.ts b/functions/src/types/contract.ts index 816cb9f7..7c894ce5 100644 --- a/functions/src/types/contract.ts +++ b/functions/src/types/contract.ts @@ -23,4 +23,7 @@ export type Contract = { isResolved: boolean resolutionTime?: number // When the contract creator resolved the market resolution?: 'YES' | 'NO' | 'CANCEL' // Chosen by creator; must be one of outcomes + + volume24Hours: number + volume7Days: number } diff --git a/functions/src/update-contract-metrics.ts b/functions/src/update-contract-metrics.ts new file mode 100644 index 00000000..8e37b88a --- /dev/null +++ b/functions/src/update-contract-metrics.ts @@ -0,0 +1,40 @@ +import * as functions from 'firebase-functions' +import * as admin from 'firebase-admin' +import * as _ from 'lodash' +import { Contract } from './types/contract' +import { getValues } from './utils' +import { Bet } from './types/bet' + +const firestore = admin.firestore() + +const oneDay = 1000 * 60 * 60 * 24 + +export const updateContractMetrics = functions.pubsub + .schedule('every 15 minutes') + .onRun(async () => { + const contracts = await getValues( + firestore.collection('contracts') + ) + await Promise.all( + contracts.map(async (contract) => { + const volume24Hours = await computeVolumeFrom(contract, oneDay) + const volume7Days = await computeVolumeFrom(contract, oneDay * 7) + + const contractRef = firestore.doc(`contracts/${contract.id}`) + return contractRef.update({ + volume24Hours, + volume7Days, + }) + }) + ) + }) + +const computeVolumeFrom = async (contract: Contract, timeAgoMs: number) => { + const bets = await getValues( + firestore + .collection(`contracts/${contract.id}/bets`) + .where('createdTime', '>', Date.now() - timeAgoMs) + ) + + return _.sumBy(bets, (bet) => Math.abs(bet.amount)) +} diff --git a/web/lib/firebase/contracts.ts b/web/lib/firebase/contracts.ts index 4ce20dbe..1a51b9dc 100644 --- a/web/lib/firebase/contracts.ts +++ b/web/lib/firebase/contracts.ts @@ -42,6 +42,9 @@ export type Contract = { isResolved: boolean resolutionTime?: number // When the contract creator resolved the market resolution?: 'YES' | 'NO' | 'CANCEL' // Chosen by creator; must be one of outcomes + + volume24Hours: number + volume7Days: number } export function path(contract: Contract) {