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-10 20:55:23 +00:00
|
|
|
import {
|
|
|
|
Binary,
|
|
|
|
Contract,
|
|
|
|
DPM,
|
|
|
|
FixedPayouts,
|
|
|
|
FreeResponse,
|
|
|
|
FullContract,
|
|
|
|
Multi,
|
|
|
|
} from './contract'
|
|
|
|
import { Fees } from './fees'
|
2022-03-07 17:29:58 +00:00
|
|
|
import { LiquidityProvision } from './liquidity-provision'
|
2022-03-02 21:12:27 +00:00
|
|
|
import {
|
2022-03-04 23:31:04 +00:00
|
|
|
getDpmCancelPayouts,
|
|
|
|
getDpmMktPayouts,
|
|
|
|
getDpmStandardPayouts,
|
|
|
|
getPayoutsMultiOutcome,
|
|
|
|
} from './payouts-dpm'
|
|
|
|
import {
|
|
|
|
getFixedCancelPayouts,
|
|
|
|
getMktFixedPayouts,
|
|
|
|
getStandardFixedPayouts,
|
|
|
|
} from './payouts-fixed'
|
2022-03-02 21:12:27 +00:00
|
|
|
|
2022-03-10 20:55:23 +00:00
|
|
|
export type Payout = {
|
|
|
|
userId: string
|
|
|
|
payout: number
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
)
|
|
|
|
return _.toPairs(loansByUser).map(([userId, payout]) => ({ userId, payout }))
|
|
|
|
}
|
|
|
|
|
2022-01-22 23:59:50 +00:00
|
|
|
export const getPayouts = (
|
2022-03-10 20:55:23 +00:00
|
|
|
outcome: string,
|
|
|
|
resolutions: {
|
|
|
|
[outcome: string]: number
|
|
|
|
},
|
2022-01-22 23:59:50 +00:00
|
|
|
contract: Contract,
|
2022-03-10 20:55:23 +00:00
|
|
|
bets: Bet[],
|
2022-03-07 17:29:58 +00:00
|
|
|
liquidities: LiquidityProvision[],
|
2022-01-30 21:51:30 +00:00
|
|
|
resolutionProbability?: number
|
2022-03-10 20:55:23 +00:00
|
|
|
): [Payout[], Fees] => {
|
2022-03-04 23:31:04 +00:00
|
|
|
if (contract.mechanism === 'cpmm-1' && contract.outcomeType === 'BINARY') {
|
2022-03-10 20:55:23 +00:00
|
|
|
const payouts = getFixedPayouts(
|
|
|
|
outcome,
|
|
|
|
contract,
|
|
|
|
bets,
|
|
|
|
liquidities,
|
|
|
|
resolutionProbability
|
|
|
|
)
|
|
|
|
return [payouts, contract.collectedFees]
|
2022-03-02 21:12:27 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 20:55:23 +00:00
|
|
|
return getDpmPayouts(
|
|
|
|
outcome,
|
|
|
|
resolutions,
|
|
|
|
contract,
|
|
|
|
bets,
|
|
|
|
resolutionProbability
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getFixedPayouts = (
|
|
|
|
outcome: string,
|
|
|
|
contract: FullContract<FixedPayouts, Binary>,
|
|
|
|
bets: Bet[],
|
|
|
|
liquidities: LiquidityProvision[],
|
|
|
|
resolutionProbability?: number
|
|
|
|
): Payout[] => {
|
2022-01-22 23:59:50 +00:00
|
|
|
switch (outcome) {
|
|
|
|
case 'YES':
|
|
|
|
case 'NO':
|
2022-03-10 20:55:23 +00:00
|
|
|
return getStandardFixedPayouts(outcome, contract, bets, liquidities)
|
2022-01-22 23:59:50 +00:00
|
|
|
case 'MKT':
|
2022-03-10 20:55:23 +00:00
|
|
|
return getMktFixedPayouts(
|
|
|
|
contract,
|
|
|
|
bets,
|
|
|
|
liquidities,
|
|
|
|
resolutionProbability
|
|
|
|
)
|
|
|
|
default:
|
2022-01-22 23:59:50 +00:00
|
|
|
case 'CANCEL':
|
2022-03-10 20:55:23 +00:00
|
|
|
return getFixedCancelPayouts(bets, liquidities)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getDpmPayouts = (
|
|
|
|
outcome: string,
|
|
|
|
resolutions: {
|
|
|
|
[outcome: string]: number
|
|
|
|
},
|
|
|
|
contract: Contract,
|
|
|
|
bets: Bet[],
|
|
|
|
resolutionProbability?: number
|
|
|
|
) => {
|
|
|
|
const openBets = bets.filter((b) => !b.isSold && !b.sale)
|
|
|
|
|
|
|
|
switch (outcome) {
|
|
|
|
case 'YES':
|
|
|
|
case 'NO':
|
|
|
|
return getDpmStandardPayouts(outcome, contract, openBets) as [
|
|
|
|
Payout[],
|
|
|
|
Fees
|
|
|
|
]
|
|
|
|
case 'MKT':
|
|
|
|
return getDpmMktPayouts(contract, openBets, resolutionProbability) as [
|
|
|
|
Payout[],
|
|
|
|
Fees
|
|
|
|
]
|
|
|
|
case 'CANCEL':
|
|
|
|
return getDpmCancelPayouts(contract, openBets) as [Payout[], Fees]
|
2022-02-17 23:00:19 +00:00
|
|
|
default:
|
|
|
|
// Multi outcome.
|
2022-03-04 23:31:04 +00:00
|
|
|
return getPayoutsMultiOutcome(
|
2022-03-10 20:55:23 +00:00
|
|
|
resolutions,
|
2022-03-04 23:31:04 +00:00
|
|
|
contract as FullContract<DPM, Multi | FreeResponse>,
|
2022-03-10 20:55:23 +00:00
|
|
|
openBets
|
|
|
|
) as [Payout[], Fees]
|
2022-01-22 23:59:50 +00:00
|
|
|
}
|
|
|
|
}
|