Clean up some mess related to nullable collectedFees (#352)

* contract.collectedFees is no longer sometimes nonexistent

* Fix typing issues around payouts code
This commit is contained in:
Marshall Polaris 2022-05-27 14:02:02 -07:00 committed by GitHub
parent 279b139556
commit 86625798cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 57 deletions

View File

@ -46,6 +46,7 @@ export type Contract = FullContract<
export type BinaryContract = FullContract<DPM | CPMM, Binary> export type BinaryContract = FullContract<DPM | CPMM, Binary>
export type FreeResponseContract = FullContract<DPM | CPMM, FreeResponse> export type FreeResponseContract = FullContract<DPM | CPMM, FreeResponse>
export type NumericContract = FullContract<DPM, Numeric> export type NumericContract = FullContract<DPM, Numeric>
export type AnyOutcome = Binary | Multi | FreeResponse | Numeric
export type DPM = { export type DPM = {
mechanism: 'dpm-2' mechanism: 'dpm-2'

View File

@ -2,18 +2,12 @@ import { sum, groupBy, sumBy, mapValues } from 'lodash'
import { Bet, NumericBet } from './bet' import { Bet, NumericBet } from './bet'
import { deductDpmFees, getDpmProbability } from './calculate-dpm' import { deductDpmFees, getDpmProbability } from './calculate-dpm'
import { DPM, FreeResponse, FullContract, Multi } from './contract' import { DPM, FreeResponse, FullContract, Multi, AnyOutcome } from './contract'
import { import { DPM_CREATOR_FEE, DPM_FEES, DPM_PLATFORM_FEE } from './fees'
DPM_CREATOR_FEE,
DPM_FEES,
DPM_PLATFORM_FEE,
Fees,
noFees,
} from './fees'
import { addObjects } from './util/object' import { addObjects } from './util/object'
export const getDpmCancelPayouts = ( export const getDpmCancelPayouts = (
contract: FullContract<DPM, any>, contract: FullContract<DPM, AnyOutcome>,
bets: Bet[] bets: Bet[]
) => { ) => {
const { pool } = contract const { pool } = contract
@ -31,13 +25,13 @@ export const getDpmCancelPayouts = (
payouts, payouts,
creatorPayout: 0, creatorPayout: 0,
liquidityPayouts: [], liquidityPayouts: [],
collectedFees: contract.collectedFees ?? noFees, collectedFees: contract.collectedFees,
} }
} }
export const getDpmStandardPayouts = ( export const getDpmStandardPayouts = (
outcome: string, outcome: string,
contract: FullContract<DPM, any>, contract: FullContract<DPM, AnyOutcome>,
bets: Bet[] bets: Bet[]
) => { ) => {
const winningBets = bets.filter((bet) => bet.outcome === outcome) const winningBets = bets.filter((bet) => bet.outcome === outcome)
@ -57,17 +51,11 @@ export const getDpmStandardPayouts = (
const profits = sumBy(payouts, (po) => Math.max(0, po.profit)) const profits = sumBy(payouts, (po) => Math.max(0, po.profit))
const creatorFee = DPM_CREATOR_FEE * profits const creatorFee = DPM_CREATOR_FEE * profits
const platformFee = DPM_PLATFORM_FEE * profits const platformFee = DPM_PLATFORM_FEE * profits
const collectedFees = addObjects(contract.collectedFees, {
const finalFees: Fees = {
creatorFee, creatorFee,
platformFee, platformFee,
liquidityFee: 0, liquidityFee: 0,
} })
const collectedFees = addObjects<Fees>(
finalFees,
contract.collectedFees ?? {}
)
console.log( console.log(
'resolved', 'resolved',
@ -90,7 +78,7 @@ export const getDpmStandardPayouts = (
export const getNumericDpmPayouts = ( export const getNumericDpmPayouts = (
outcome: string, outcome: string,
contract: FullContract<DPM, any>, contract: FullContract<DPM, AnyOutcome>,
bets: NumericBet[] bets: NumericBet[]
) => { ) => {
const totalShares = sumBy(bets, (bet) => bet.allOutcomeShares[outcome] ?? 0) const totalShares = sumBy(bets, (bet) => bet.allOutcomeShares[outcome] ?? 0)
@ -115,17 +103,11 @@ export const getNumericDpmPayouts = (
const profits = sumBy(payouts, (po) => Math.max(0, po.profit)) const profits = sumBy(payouts, (po) => Math.max(0, po.profit))
const creatorFee = DPM_CREATOR_FEE * profits const creatorFee = DPM_CREATOR_FEE * profits
const platformFee = DPM_PLATFORM_FEE * profits const platformFee = DPM_PLATFORM_FEE * profits
const collectedFees = addObjects(contract.collectedFees, {
const finalFees: Fees = {
creatorFee, creatorFee,
platformFee, platformFee,
liquidityFee: 0, liquidityFee: 0,
} })
const collectedFees = addObjects<Fees>(
finalFees,
contract.collectedFees ?? {}
)
console.log( console.log(
'resolved numeric bucket: ', 'resolved numeric bucket: ',
@ -147,7 +129,7 @@ export const getNumericDpmPayouts = (
} }
export const getDpmMktPayouts = ( export const getDpmMktPayouts = (
contract: FullContract<DPM, any>, contract: FullContract<DPM, AnyOutcome>,
bets: Bet[], bets: Bet[],
resolutionProbability?: number resolutionProbability?: number
) => { ) => {
@ -174,17 +156,11 @@ export const getDpmMktPayouts = (
const creatorFee = DPM_CREATOR_FEE * profits const creatorFee = DPM_CREATOR_FEE * profits
const platformFee = DPM_PLATFORM_FEE * profits const platformFee = DPM_PLATFORM_FEE * profits
const collectedFees = addObjects(contract.collectedFees, {
const finalFees: Fees = {
creatorFee, creatorFee,
platformFee, platformFee,
liquidityFee: 0, liquidityFee: 0,
} })
const collectedFees = addObjects<Fees>(
finalFees,
contract.collectedFees ?? {}
)
console.log( console.log(
'resolved MKT', 'resolved MKT',
@ -233,17 +209,11 @@ export const getPayoutsMultiOutcome = (
const creatorFee = DPM_CREATOR_FEE * profits const creatorFee = DPM_CREATOR_FEE * profits
const platformFee = DPM_PLATFORM_FEE * profits const platformFee = DPM_PLATFORM_FEE * profits
const collectedFees = addObjects(contract.collectedFees, {
const finalFees: Fees = {
creatorFee, creatorFee,
platformFee, platformFee,
liquidityFee: 0, liquidityFee: 0,
} })
const collectedFees = addObjects<Fees>(
finalFees,
contract.collectedFees ?? noFees
)
console.log( console.log(
'resolved', 'resolved',

View File

@ -9,6 +9,7 @@ import {
FreeResponse, FreeResponse,
FullContract, FullContract,
Multi, Multi,
AnyOutcome,
} from './contract' } from './contract'
import { Fees } from './fees' import { Fees } from './fees'
import { LiquidityProvision } from './liquidity-provision' import { LiquidityProvision } from './liquidity-provision'
@ -72,15 +73,17 @@ export const getPayouts = (
liquidities, liquidities,
resolutionProbability resolutionProbability
) )
} else if (contract.mechanism === 'dpm-2') {
return getDpmPayouts(
outcome,
resolutions,
contract,
bets,
resolutionProbability
)
} else {
throw new Error('Unknown contract mechanism.')
} }
return getDpmPayouts(
outcome,
resolutions,
contract,
bets,
resolutionProbability
)
} }
export const getFixedPayouts = ( export const getFixedPayouts = (
@ -112,7 +115,7 @@ export const getDpmPayouts = (
resolutions: { resolutions: {
[outcome: string]: number [outcome: string]: number
}, },
contract: Contract, contract: FullContract<DPM, AnyOutcome>,
bets: Bet[], bets: Bet[],
resolutionProbability?: number resolutionProbability?: number
): PayoutInfo => { ): PayoutInfo => {

View File

@ -78,7 +78,7 @@ export const sellBet = functions.runWith({ minInstances: 1 }).https.onCall(
pool: newPool, pool: newPool,
totalShares: newTotalShares, totalShares: newTotalShares,
totalBets: newTotalBets, totalBets: newTotalBets,
collectedFees: addObjects<Fees>(fees ?? {}, collectedFees ?? {}), collectedFees: addObjects(fees, collectedFees),
volume: volume + Math.abs(newBet.amount), volume: volume + Math.abs(newBet.amount),
}) })
) )

View File

@ -101,7 +101,7 @@ export const sellShares = functions.runWith({ minInstances: 1 }).https.onCall(
removeUndefinedProps({ removeUndefinedProps({
pool: newPool, pool: newPool,
p: newP, p: newP,
collectedFees: addObjects(fees ?? {}, collectedFees ?? {}), collectedFees: addObjects(fees, collectedFees),
volume: volume + Math.abs(newBet.amount), volume: volume + Math.abs(newBet.amount),
}) })
) )

View File

@ -95,7 +95,7 @@ export function ContractInfoDialog(props: { contract: Contract; bets: Bet[] }) {
<tr> <tr>
<td>Creator earnings</td> <td>Creator earnings</td>
<td>{formatMoney(contract.collectedFees?.creatorFee ?? 0)}</td> <td>{formatMoney(contract.collectedFees.creatorFee)}</td>
</tr> </tr>
<tr> <tr>