2022-02-02 03:23:13 +00:00
|
|
|
import * as _ from 'lodash'
|
2022-03-15 22:27:51 +00:00
|
|
|
|
2022-02-02 03:23:13 +00:00
|
|
|
import { Bet } from './bet'
|
2022-03-15 22:27:51 +00:00
|
|
|
import { Binary, Contract, FullContract } from './contract'
|
2022-02-01 18:59:51 +00:00
|
|
|
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) {
|
2022-02-02 03:23:13 +00:00
|
|
|
addUserScores(scores, userScores)
|
2022-01-22 23:59:50 +00:00
|
|
|
}
|
|
|
|
return userScores
|
|
|
|
}
|
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
export function scoreUsersByContract(
|
|
|
|
contract: FullContract<any, Binary>,
|
|
|
|
bets: Bet[]
|
|
|
|
) {
|
2022-01-30 21:51:30 +00:00
|
|
|
const { resolution, resolutionProbability } = contract
|
2022-01-22 23:59:50 +00:00
|
|
|
|
|
|
|
const [closedBets, openBets] = _.partition(
|
|
|
|
bets,
|
|
|
|
(bet) => bet.isSold || bet.sale
|
|
|
|
)
|
2022-05-09 21:04:40 +00:00
|
|
|
const { payouts: resolvePayouts } = getPayouts(
|
2022-03-15 22:27:51 +00:00
|
|
|
resolution,
|
|
|
|
{},
|
2022-01-30 21:51:30 +00:00
|
|
|
contract,
|
|
|
|
openBets,
|
2022-03-15 22:27:51 +00:00
|
|
|
[],
|
2022-01-30 21:51:30 +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) => {
|
2022-03-02 05:23:37 +00:00
|
|
|
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
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
}
|