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-04 23:31:04 +00:00
|
|
|
import { Contract, DPM, FreeResponse, FullContract, Multi } from './contract'
|
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-01-22 23:59:50 +00:00
|
|
|
export const getPayouts = (
|
2022-03-04 23:31:04 +00:00
|
|
|
outcome:
|
|
|
|
| string
|
|
|
|
| {
|
|
|
|
[outcome: string]: number
|
|
|
|
},
|
2022-01-22 23:59:50 +00:00
|
|
|
contract: Contract,
|
2022-01-30 21:51:30 +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-01-22 23:59:50 +00:00
|
|
|
) => {
|
2022-03-04 23:31:04 +00:00
|
|
|
if (contract.mechanism === 'cpmm-1' && contract.outcomeType === 'BINARY') {
|
2022-03-02 21:12:27 +00:00
|
|
|
switch (outcome) {
|
|
|
|
case 'YES':
|
|
|
|
case 'NO':
|
2022-03-07 17:29:58 +00:00
|
|
|
return getStandardFixedPayouts(outcome, contract, bets, liquidities)
|
2022-03-02 21:12:27 +00:00
|
|
|
case 'MKT':
|
2022-03-07 17:29:58 +00:00
|
|
|
return getMktFixedPayouts(
|
|
|
|
contract,
|
|
|
|
bets,
|
|
|
|
liquidities,
|
|
|
|
resolutionProbability
|
|
|
|
)
|
2022-03-02 21:12:27 +00:00
|
|
|
case 'CANCEL':
|
2022-03-07 17:29:58 +00:00
|
|
|
return getFixedCancelPayouts(contract, bets, liquidities)
|
2022-03-02 21:12:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-22 23:59:50 +00:00
|
|
|
switch (outcome) {
|
|
|
|
case 'YES':
|
|
|
|
case 'NO':
|
2022-03-04 23:31:04 +00:00
|
|
|
return getDpmStandardPayouts(outcome, contract, bets)
|
2022-01-22 23:59:50 +00:00
|
|
|
case 'MKT':
|
2022-03-04 23:31:04 +00:00
|
|
|
return getDpmMktPayouts(contract, bets, resolutionProbability)
|
2022-01-22 23:59:50 +00:00
|
|
|
case 'CANCEL':
|
2022-03-02 21:12:27 +00:00
|
|
|
return getDpmCancelPayouts(contract, bets)
|
2022-02-17 23:00:19 +00:00
|
|
|
default:
|
|
|
|
// Multi outcome.
|
2022-03-04 23:31:04 +00:00
|
|
|
return getPayoutsMultiOutcome(
|
|
|
|
outcome as {
|
|
|
|
[outcome: string]: number
|
|
|
|
},
|
|
|
|
contract as FullContract<DPM, Multi | FreeResponse>,
|
|
|
|
bets
|
|
|
|
)
|
2022-01-22 23:59:50 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-20 07:26:26 +00:00
|
|
|
|
2022-03-02 03:31:48 +00:00
|
|
|
export const getLoanPayouts = (bets: Bet[]) => {
|
|
|
|
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 }))
|
|
|
|
}
|