2022-09-09 22:43:33 +00:00
|
|
|
import { groupBy, sumBy, mapValues } from 'lodash'
|
2022-03-15 22:27:51 +00:00
|
|
|
|
2022-02-02 03:23:13 +00:00
|
|
|
import { Bet } from './bet'
|
2022-09-09 22:42:51 +00:00
|
|
|
import { getContractBetMetrics } from './calculate'
|
2022-06-01 02:42:35 +00:00
|
|
|
import { Contract } from './contract'
|
2022-01-22 23:59:50 +00:00
|
|
|
|
2022-05-26 00:12:36 +00:00
|
|
|
export function scoreCreators(contracts: Contract[]) {
|
2022-05-22 08:36:05 +00:00
|
|
|
const creatorScore = mapValues(
|
|
|
|
groupBy(contracts, ({ creatorId }) => creatorId),
|
2022-06-22 16:35:50 +00:00
|
|
|
(contracts) =>
|
|
|
|
sumBy(
|
|
|
|
contracts.map((contract) => {
|
|
|
|
return contract.volume
|
|
|
|
})
|
|
|
|
)
|
2022-01-22 23:59:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return creatorScore
|
|
|
|
}
|
|
|
|
|
|
|
|
export function scoreTraders(contracts: Contract[], bets: Bet[][]) {
|
|
|
|
const userScoresByContract = contracts.map((contract, index) =>
|
|
|
|
scoreUsersByContract(contract, bets[index])
|
|
|
|
)
|
|
|
|
const userScores: { [userId: string]: number } = {}
|
|
|
|
for (const scores of userScoresByContract) {
|
2022-02-02 03:23:13 +00:00
|
|
|
addUserScores(scores, userScores)
|
2022-01-22 23:59:50 +00:00
|
|
|
}
|
|
|
|
return userScores
|
|
|
|
}
|
|
|
|
|
2022-06-01 02:42:35 +00:00
|
|
|
export function scoreUsersByContract(contract: Contract, bets: Bet[]) {
|
2022-09-09 22:42:51 +00:00
|
|
|
const betsByUser = groupBy(bets, bet => bet.userId)
|
|
|
|
return mapValues(betsByUser, bets => getContractBetMetrics(contract, bets).profit)
|
2022-01-22 23:59:50 +00:00
|
|
|
}
|
2022-02-02 03:23:13 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|