From 693652935d97959db2e29181db9608fe785761a4 Mon Sep 17 00:00:00 2001 From: James Grugett Date: Sat, 19 Feb 2022 12:14:45 -0600 Subject: [PATCH] Switch to pool size instead of volume for creator leaderboard. --- functions/src/update-user-metrics.ts | 32 ++++++++++++++++------------ 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/functions/src/update-user-metrics.ts b/functions/src/update-user-metrics.ts index f7d5a34e..f59d4a34 100644 --- a/functions/src/update-user-metrics.ts +++ b/functions/src/update-user-metrics.ts @@ -22,20 +22,22 @@ export const updateUserMetrics = functions.pubsub contracts.map((contract) => [contract.id, contract]) ) - for (const user of users) { - const [investmentValue, creatorVolume] = await Promise.all([ - computeInvestmentValue(user, contractsDict), - computeTotalVolume(user, contractsDict), - ]) + await Promise.all( + users.map(async (user) => { + const [investmentValue, creatorVolume] = await Promise.all([ + computeInvestmentValue(user, contractsDict), + computeTotalPool(user, contractsDict), + ]) - const totalValue = user.balance + investmentValue - const totalPnL = totalValue - user.totalDeposits + const totalValue = user.balance + investmentValue + const totalPnL = totalValue - user.totalDeposits - await firestore.collection('users').doc(user.id).update({ - totalPnLCached: totalPnL, - creatorVolumeCached: creatorVolume, + await firestore.collection('users').doc(user.id).update({ + totalPnLCached: totalPnL, + creatorVolumeCached: creatorVolume, + }) }) - } + ) }) const computeInvestmentValue = async ( @@ -54,15 +56,17 @@ const computeInvestmentValue = async ( }) } -const computeTotalVolume = async ( +const computeTotalPool = async ( user: User, contractsDict: _.Dictionary ) => { const creatorContracts = Object.values(contractsDict).filter( (contract) => contract.creatorId === user.id ) - const volumes = await Promise.all(creatorContracts.map(computeVolume)) - return _.sum(volumes) + const pools = creatorContracts.map((contract) => + _.sum(Object.values(contract.pool)) + ) + return _.sum(pools) } const computeVolume = async (contract: Contract) => {