Calculate global leaderboard from contract bets

This commit is contained in:
James Grugett 2022-02-01 21:23:13 -06:00
parent 375ce69b7d
commit ca29a43da9
3 changed files with 42 additions and 22 deletions

View File

@ -1,7 +1,7 @@
import _ from 'lodash'
import * as _ from 'lodash'
import { Bet } from './bet'
import { Contract } from './contract'
import { getPayouts } from './payouts'
import { Bet } from '../web/lib/firebase/bets'
export function scoreCreators(contracts: Contract[], bets: Bet[][]) {
const creatorScore = _.mapValues(
@ -18,15 +18,12 @@ export function scoreTraders(contracts: Contract[], bets: Bet[][]) {
)
const userScores: { [userId: string]: number } = {}
for (const scores of userScoresByContract) {
for (const [userId, score] of Object.entries(scores)) {
if (userScores[userId] === undefined) userScores[userId] = 0
userScores[userId] += score
}
addUserScores(scores, userScores)
}
return userScores
}
function scoreUsersByContract(contract: Contract, bets: Bet[]) {
export function scoreUsersByContract(contract: Contract, bets: Bet[]) {
const { resolution, resolutionProbability } = contract
const [closedBets, openBets] = _.partition(
@ -61,3 +58,13 @@ function scoreUsersByContract(contract: Contract, bets: Bet[]) {
return userScore
}
export function addUserScores(
src: { [userId: string]: number },
dest: { [userId: string]: number }
) {
for (const [userId, score] of Object.entries(src)) {
if (dest[userId] === undefined) dest[userId] = 0
dest[userId] += score
}
}

View File

@ -5,6 +5,7 @@ import * as _ from 'lodash'
import { getValues } from './utils'
import { Contract } from '../../common/contract'
import { Bet } from '../../common/bet'
import { addUserScores, scoreUsersByContract } from '../../common/scoring'
const firestore = admin.firestore()
@ -16,8 +17,16 @@ export const updateContractMetrics = functions.pubsub
const contracts = await getValues<Contract>(
firestore.collection('contracts')
)
const userScores: { [userId: string]: number } = {}
await Promise.all(
contracts.map(async (contract) => {
const bets = await getValues<Bet>(
firestore.collection(`contracts/${contract.id}/bets`)
)
const contractUserScores = scoreUsersByContract(contract, bets)
addUserScores(contractUserScores, userScores)
const volume24Hours = await computeVolumeFrom(contract, oneDay)
const volume7Days = await computeVolumeFrom(contract, oneDay * 7)
@ -28,6 +37,12 @@ export const updateContractMetrics = functions.pubsub
})
})
)
for (const [userId, score] of Object.entries(userScores)) {
await firestore.collection('users').doc(userId).update({
totalPnLCached: score,
})
}
})
const computeVolumeFrom = async (contract: Contract, timeAgoMs: number) => {

View File

@ -7,7 +7,6 @@ import { Contract } from '../../common/contract'
import { Bet } from '../../common/bet'
import { User } from '../../common/user'
import { calculatePayout } from '../../common/calculate'
import { StripeTransaction } from '.'
const firestore = admin.firestore()
@ -25,25 +24,24 @@ export const updateUserMetrics = functions.pubsub
await Promise.all(
users.map(async (user) => {
const investmentValue = await computeInvestmentValue(
user,
contractsDict
)
const deposits = await getValues<StripeTransaction>(
firestore
.collection('stripe-transactions')
.where('userId', '==', user.id)
)
const totalDeposits =
1000 + _.sumBy(deposits, (deposit) => deposit.manticDollarQuantity)
const totalValue = user.balance + investmentValue
// const investmentValue = await computeInvestmentValue(
// user,
// contractsDict
// )
// const deposits = await getValues<StripeTransaction>(
// firestore
// .collection('stripe-transactions')
// .where('userId', '==', user.id)
// )
// const totalDeposits =
// 1000 + _.sumBy(deposits, (deposit) => deposit.manticDollarQuantity)
// const totalValue = user.balance + investmentValue
const totalPnL = totalValue - totalDeposits
// const totalPnL = totalValue - totalDeposits
const creatorVolume = await computeTotalVolume(user, contractsDict)
return firestore.collection('users').doc(user.id).update({
totalPnLCached: totalPnL,
creatorVolumeCached: creatorVolume,
})
})