2022-05-22 08:36:05 +00:00
|
|
|
import { sumBy, groupBy, mapValues } from 'lodash'
|
2022-02-13 01:19:17 +00:00
|
|
|
|
2022-05-19 17:42:03 +00:00
|
|
|
import { Bet, NumericBet } from './bet'
|
2022-07-02 19:37:59 +00:00
|
|
|
import {
|
|
|
|
Contract,
|
|
|
|
CPMMBinaryContract,
|
|
|
|
DPMContract,
|
|
|
|
PseudoNumericContract,
|
|
|
|
} from './contract'
|
2022-03-15 22:27:51 +00:00
|
|
|
import { Fees } from './fees'
|
|
|
|
import { LiquidityProvision } from './liquidity-provision'
|
|
|
|
import {
|
|
|
|
getDpmCancelPayouts,
|
|
|
|
getDpmMktPayouts,
|
|
|
|
getDpmStandardPayouts,
|
2022-05-19 17:42:03 +00:00
|
|
|
getNumericDpmPayouts,
|
2022-03-15 22:27:51 +00:00
|
|
|
getPayoutsMultiOutcome,
|
|
|
|
} from './payouts-dpm'
|
|
|
|
import {
|
|
|
|
getFixedCancelPayouts,
|
|
|
|
getMktFixedPayouts,
|
|
|
|
getStandardFixedPayouts,
|
|
|
|
} from './payouts-fixed'
|
|
|
|
|
|
|
|
export type Payout = {
|
|
|
|
userId: string
|
|
|
|
payout: number
|
2022-01-10 23:52:03 +00:00
|
|
|
}
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
export const getLoanPayouts = (bets: Bet[]): Payout[] => {
|
|
|
|
const betsWithLoans = bets.filter((bet) => bet.loanAmount)
|
2022-05-22 08:36:05 +00:00
|
|
|
const betsByUser = groupBy(betsWithLoans, (bet) => bet.userId)
|
|
|
|
const loansByUser = mapValues(betsByUser, (bets) =>
|
|
|
|
sumBy(bets, (bet) => -(bet.loanAmount ?? 0))
|
2022-01-14 17:11:01 +00:00
|
|
|
)
|
2022-05-22 08:36:05 +00:00
|
|
|
return Object.entries(loansByUser).map(([userId, payout]) => ({
|
|
|
|
userId,
|
|
|
|
payout,
|
|
|
|
}))
|
2022-01-10 23:52:03 +00:00
|
|
|
}
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-05-09 21:04:40 +00:00
|
|
|
export const groupPayoutsByUser = (payouts: Payout[]) => {
|
2022-05-22 08:36:05 +00:00
|
|
|
const groups = groupBy(payouts, (payout) => payout.userId)
|
|
|
|
return mapValues(groups, (group) => sumBy(group, (g) => g.payout))
|
2022-05-09 21:04:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export type PayoutInfo = {
|
|
|
|
payouts: Payout[]
|
|
|
|
creatorPayout: number
|
|
|
|
liquidityPayouts: Payout[]
|
|
|
|
collectedFees: Fees
|
|
|
|
}
|
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
export const getPayouts = (
|
2022-06-01 02:42:35 +00:00
|
|
|
outcome: string | undefined,
|
2022-01-30 21:51:30 +00:00
|
|
|
contract: Contract,
|
|
|
|
bets: Bet[],
|
2022-03-15 22:27:51 +00:00
|
|
|
liquidities: LiquidityProvision[],
|
2022-06-29 23:47:06 +00:00
|
|
|
resolutions?: {
|
|
|
|
[outcome: string]: number
|
|
|
|
},
|
2022-01-30 21:51:30 +00:00
|
|
|
resolutionProbability?: number
|
2022-05-09 21:04:40 +00:00
|
|
|
): PayoutInfo => {
|
2022-07-02 19:37:59 +00:00
|
|
|
if (
|
|
|
|
contract.mechanism === 'cpmm-1' &&
|
|
|
|
(contract.outcomeType === 'BINARY' ||
|
|
|
|
contract.outcomeType === 'PSEUDO_NUMERIC')
|
|
|
|
) {
|
2022-05-09 21:04:40 +00:00
|
|
|
return getFixedPayouts(
|
2022-03-15 22:27:51 +00:00
|
|
|
outcome,
|
|
|
|
contract,
|
|
|
|
bets,
|
|
|
|
liquidities,
|
|
|
|
resolutionProbability
|
|
|
|
)
|
|
|
|
}
|
2022-05-27 21:23:57 +00:00
|
|
|
return getDpmPayouts(
|
|
|
|
outcome,
|
|
|
|
contract,
|
|
|
|
bets,
|
2022-06-29 23:47:06 +00:00
|
|
|
resolutions,
|
2022-05-27 21:23:57 +00:00
|
|
|
resolutionProbability
|
|
|
|
)
|
2022-01-10 23:52:03 +00:00
|
|
|
}
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
export const getFixedPayouts = (
|
2022-06-01 02:42:35 +00:00
|
|
|
outcome: string | undefined,
|
2022-07-02 19:37:59 +00:00
|
|
|
contract: CPMMBinaryContract | PseudoNumericContract,
|
2022-01-30 21:51:30 +00:00
|
|
|
bets: Bet[],
|
2022-03-15 22:27:51 +00:00
|
|
|
liquidities: LiquidityProvision[],
|
2022-01-30 21:51:30 +00:00
|
|
|
resolutionProbability?: number
|
2022-05-09 21:04:40 +00:00
|
|
|
) => {
|
2022-01-22 23:59:50 +00:00
|
|
|
switch (outcome) {
|
|
|
|
case 'YES':
|
|
|
|
case 'NO':
|
2022-03-15 22:27:51 +00:00
|
|
|
return getStandardFixedPayouts(outcome, contract, bets, liquidities)
|
2022-01-22 23:59:50 +00:00
|
|
|
case 'MKT':
|
2022-03-15 22:27:51 +00:00
|
|
|
return getMktFixedPayouts(
|
|
|
|
contract,
|
|
|
|
bets,
|
|
|
|
liquidities,
|
|
|
|
resolutionProbability
|
|
|
|
)
|
2022-02-17 23:00:19 +00:00
|
|
|
default:
|
2022-03-15 22:27:51 +00:00
|
|
|
case 'CANCEL':
|
|
|
|
return getFixedCancelPayouts(bets, liquidities)
|
2022-01-22 23:59:50 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-20 07:26:26 +00:00
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
export const getDpmPayouts = (
|
2022-06-01 02:42:35 +00:00
|
|
|
outcome: string | undefined,
|
|
|
|
contract: DPMContract,
|
2022-03-15 22:27:51 +00:00
|
|
|
bets: Bet[],
|
2022-06-29 23:47:06 +00:00
|
|
|
resolutions?: {
|
|
|
|
[outcome: string]: number
|
|
|
|
},
|
2022-03-15 22:27:51 +00:00
|
|
|
resolutionProbability?: number
|
2022-05-09 21:04:40 +00:00
|
|
|
): PayoutInfo => {
|
2022-03-15 22:27:51 +00:00
|
|
|
const openBets = bets.filter((b) => !b.isSold && !b.sale)
|
2022-07-28 02:40:33 +00:00
|
|
|
const { outcomeType } = contract
|
2022-02-20 07:26:26 +00:00
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
switch (outcome) {
|
|
|
|
case 'YES':
|
|
|
|
case 'NO':
|
2022-05-09 21:04:40 +00:00
|
|
|
return getDpmStandardPayouts(outcome, contract, openBets)
|
2022-02-20 07:26:26 +00:00
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
case 'MKT':
|
2022-07-28 02:40:33 +00:00
|
|
|
return outcomeType === 'FREE_RESPONSE' ||
|
|
|
|
outcomeType === 'MULTIPLE_CHOICE' // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
2022-06-29 23:47:06 +00:00
|
|
|
? getPayoutsMultiOutcome(resolutions!, contract, openBets)
|
2022-05-09 21:04:40 +00:00
|
|
|
: getDpmMktPayouts(contract, openBets, resolutionProbability)
|
2022-03-15 22:27:51 +00:00
|
|
|
case 'CANCEL':
|
2022-06-01 02:42:35 +00:00
|
|
|
case undefined:
|
2022-05-09 21:04:40 +00:00
|
|
|
return getDpmCancelPayouts(contract, openBets)
|
2022-03-02 03:31:48 +00:00
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
default:
|
2022-07-28 02:40:33 +00:00
|
|
|
if (outcomeType === 'NUMERIC')
|
2022-05-19 17:42:03 +00:00
|
|
|
return getNumericDpmPayouts(outcome, contract, openBets as NumericBet[])
|
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
// Outcome is a free response answer id.
|
2022-05-09 21:04:40 +00:00
|
|
|
return getDpmStandardPayouts(outcome, contract, openBets)
|
2022-03-15 22:27:51 +00:00
|
|
|
}
|
2022-03-02 03:31:48 +00:00
|
|
|
}
|