2022-02-13 01:19:17 +00:00
|
|
|
import * as _ from 'lodash'
|
|
|
|
|
2022-01-10 23:52:03 +00:00
|
|
|
import { Bet } from './bet'
|
2022-03-15 22:27:51 +00:00
|
|
|
import {
|
|
|
|
Binary,
|
|
|
|
Contract,
|
|
|
|
DPM,
|
|
|
|
FixedPayouts,
|
|
|
|
FreeResponse,
|
|
|
|
FullContract,
|
|
|
|
Multi,
|
|
|
|
} from './contract'
|
|
|
|
import { Fees } from './fees'
|
|
|
|
import { LiquidityProvision } from './liquidity-provision'
|
|
|
|
import {
|
|
|
|
getDpmCancelPayouts,
|
|
|
|
getDpmMktPayouts,
|
|
|
|
getDpmStandardPayouts,
|
|
|
|
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)
|
|
|
|
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-03-15 22:27:51 +00:00
|
|
|
return _.toPairs(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[]) => {
|
|
|
|
const groups = _.groupBy(payouts, (payout) => payout.userId)
|
|
|
|
return _.mapValues(groups, (group) => _.sumBy(group, (g) => g.payout))
|
|
|
|
}
|
|
|
|
|
|
|
|
export type PayoutInfo = {
|
|
|
|
payouts: Payout[]
|
|
|
|
creatorPayout: number
|
|
|
|
liquidityPayouts: Payout[]
|
|
|
|
collectedFees: Fees
|
|
|
|
}
|
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
export const getPayouts = (
|
|
|
|
outcome: string,
|
|
|
|
resolutions: {
|
|
|
|
[outcome: string]: number
|
|
|
|
},
|
2022-01-30 21:51:30 +00:00
|
|
|
contract: Contract,
|
|
|
|
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
|
|
|
): PayoutInfo => {
|
2022-03-15 22:27:51 +00:00
|
|
|
if (contract.mechanism === 'cpmm-1' && contract.outcomeType === 'BINARY') {
|
2022-05-09 21:04:40 +00:00
|
|
|
return getFixedPayouts(
|
2022-03-15 22:27:51 +00:00
|
|
|
outcome,
|
|
|
|
contract,
|
|
|
|
bets,
|
|
|
|
liquidities,
|
|
|
|
resolutionProbability
|
|
|
|
)
|
|
|
|
}
|
2022-01-10 22:49:04 +00:00
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
return getDpmPayouts(
|
|
|
|
outcome,
|
|
|
|
resolutions,
|
|
|
|
contract,
|
|
|
|
bets,
|
|
|
|
resolutionProbability
|
2022-02-13 01:19:17 +00:00
|
|
|
)
|
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-02-17 23:00:19 +00:00
|
|
|
outcome: string,
|
2022-03-15 22:27:51 +00:00
|
|
|
contract: FullContract<FixedPayouts, Binary>,
|
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 = (
|
|
|
|
outcome: string,
|
|
|
|
resolutions: {
|
|
|
|
[outcome: string]: number
|
|
|
|
},
|
2022-02-20 07:26:26 +00:00
|
|
|
contract: Contract,
|
2022-03-15 22:27:51 +00:00
|
|
|
bets: Bet[],
|
|
|
|
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-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':
|
|
|
|
return contract.outcomeType === 'FREE_RESPONSE'
|
2022-05-09 21:04:40 +00:00
|
|
|
? getPayoutsMultiOutcome(
|
2022-03-15 22:27:51 +00:00
|
|
|
resolutions,
|
|
|
|
contract as FullContract<DPM, Multi | FreeResponse>,
|
|
|
|
openBets
|
2022-05-09 21:04:40 +00:00
|
|
|
)
|
|
|
|
: getDpmMktPayouts(contract, openBets, resolutionProbability)
|
2022-03-15 22:27:51 +00:00
|
|
|
case 'CANCEL':
|
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:
|
|
|
|
// 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
|
|
|
}
|