2022-05-22 08:36:05 +00:00
|
|
|
import { groupBy, sumBy, mapValues, partition } from 'lodash'
|
2022-03-15 22:27:51 +00:00
|
|
|
|
2022-02-02 03:23:13 +00:00
|
|
|
import { Bet } from './bet'
|
2022-06-01 02:42:35 +00:00
|
|
|
import { Contract } from './contract'
|
2022-02-01 18:59:51 +00:00
|
|
|
import { getPayouts } from './payouts'
|
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[]) {
|
|
|
|
const { resolution } = contract
|
|
|
|
const resolutionProb =
|
|
|
|
contract.outcomeType == 'BINARY'
|
|
|
|
? contract.resolutionProbability
|
|
|
|
: undefined
|
2022-01-22 23:59:50 +00:00
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const [closedBets, openBets] = partition(
|
2022-01-22 23:59:50 +00:00
|
|
|
bets,
|
|
|
|
(bet) => bet.isSold || bet.sale
|
|
|
|
)
|
2022-05-09 21:04:40 +00:00
|
|
|
const { payouts: resolvePayouts } = getPayouts(
|
2022-06-01 02:42:35 +00:00
|
|
|
resolution as string,
|
2022-01-30 21:51:30 +00:00
|
|
|
contract,
|
|
|
|
openBets,
|
2022-03-15 22:27:51 +00:00
|
|
|
[],
|
2022-06-29 23:47:06 +00:00
|
|
|
{},
|
2022-06-01 02:42:35 +00:00
|
|
|
resolutionProb
|
2022-01-30 21:51:30 +00:00
|
|
|
)
|
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]
|
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const userScore = mapValues(
|
|
|
|
groupBy(netPayouts, (payout) => payout.userId),
|
|
|
|
(payouts) => sumBy(payouts, ({ payout }) => payout)
|
2022-01-22 23:59:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|