manifold/common/scoring.ts

77 lines
2.0 KiB
TypeScript
Raw Normal View History

import * as _ from 'lodash'
2022-03-07 17:29:58 +00:00
import { Bet } from './bet'
import { Binary, Contract, FullContract } from './contract'
import { getPayouts } from './payouts'
2022-01-22 23:59:50 +00:00
export function scoreCreators(contracts: Contract[], bets: Bet[][]) {
const creatorScore = _.mapValues(
_.groupBy(contracts, ({ creatorId }) => creatorId),
(contracts) => _.sumBy(contracts, ({ pool }) => pool.YES + pool.NO)
)
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) {
addUserScores(scores, userScores)
2022-01-22 23:59:50 +00:00
}
return userScores
}
export function scoreUsersByContract(
contract: FullContract<any, Binary>,
bets: Bet[]
) {
const { resolution, resolutionProbability } = contract
2022-01-22 23:59:50 +00:00
const [closedBets, openBets] = _.partition(
bets,
(bet) => bet.isSold || bet.sale
)
const resolvePayouts = getPayouts(
resolution ?? 'MKT',
contract,
openBets,
2022-03-07 17:29:58 +00:00
[],
resolutionProbability
)
2022-01-22 23:59:50 +00:00
const salePayouts = closedBets.map((bet) => {
const { userId, sale } = bet
return { userId, payout: sale ? sale.amount : 0 }
})
const investments = bets
.filter((bet) => !bet.sale)
.map((bet) => {
const { userId, amount, loanAmount } = bet
const payout = -amount - (loanAmount ?? 0)
return { userId, payout }
2022-01-22 23:59:50 +00:00
})
const netPayouts = [...resolvePayouts, ...salePayouts, ...investments]
const userScore = _.mapValues(
_.groupBy(netPayouts, (payout) => payout.userId),
(payouts) => _.sumBy(payouts, ({ payout }) => payout)
)
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
}
}