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-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-03-15 22:27:51 +00:00
|
|
|
): [Payout[], Fees] => {
|
|
|
|
if (contract.mechanism === 'cpmm-1' && contract.outcomeType === 'BINARY') {
|
|
|
|
const payouts = getFixedPayouts(
|
|
|
|
outcome,
|
|
|
|
contract,
|
|
|
|
bets,
|
|
|
|
liquidities,
|
|
|
|
resolutionProbability
|
|
|
|
)
|
|
|
|
return [payouts, contract.collectedFees]
|
|
|
|
}
|
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-03-15 22:27:51 +00:00
|
|
|
): Payout[] => {
|
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-02-20 07:26:26 +00:00
|
|
|
) => {
|
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':
|
|
|
|
return getDpmStandardPayouts(outcome, contract, openBets) as [
|
|
|
|
Payout[],
|
|
|
|
Fees
|
|
|
|
]
|
2022-02-20 07:26:26 +00:00
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
case 'MKT':
|
|
|
|
return contract.outcomeType === 'FREE_RESPONSE'
|
|
|
|
? (getPayoutsMultiOutcome(
|
|
|
|
resolutions,
|
|
|
|
contract as FullContract<DPM, Multi | FreeResponse>,
|
|
|
|
openBets
|
|
|
|
) as [Payout[], Fees])
|
|
|
|
: (getDpmMktPayouts(contract, openBets, resolutionProbability) as [
|
|
|
|
Payout[],
|
|
|
|
Fees
|
|
|
|
])
|
|
|
|
case 'CANCEL':
|
|
|
|
return getDpmCancelPayouts(contract, openBets) as [Payout[], Fees]
|
2022-03-02 03:31:48 +00:00
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
default:
|
|
|
|
// Outcome is a free response answer id.
|
|
|
|
return getDpmStandardPayouts(outcome, contract, openBets) as [
|
|
|
|
Payout[],
|
|
|
|
Fees
|
|
|
|
]
|
|
|
|
}
|
2022-03-02 03:31:48 +00:00
|
|
|
}
|