From 86625798cdd6992fe4951f7ed132fa98691ff91c Mon Sep 17 00:00:00 2001 From: Marshall Polaris Date: Fri, 27 May 2022 14:02:02 -0700 Subject: [PATCH] Clean up some mess related to nullable `collectedFees` (#352) * contract.collectedFees is no longer sometimes nonexistent * Fix typing issues around payouts code --- common/contract.ts | 1 + common/payouts-dpm.ts | 60 +++++-------------- common/payouts.ts | 21 ++++--- functions/src/sell-bet.ts | 2 +- functions/src/sell-shares.ts | 2 +- .../contract/contract-info-dialog.tsx | 2 +- 6 files changed, 31 insertions(+), 57 deletions(-) diff --git a/common/contract.ts b/common/contract.ts index e9b768ea..ac4848ec 100644 --- a/common/contract.ts +++ b/common/contract.ts @@ -46,6 +46,7 @@ export type Contract = FullContract< export type BinaryContract = FullContract export type FreeResponseContract = FullContract export type NumericContract = FullContract +export type AnyOutcome = Binary | Multi | FreeResponse | Numeric export type DPM = { mechanism: 'dpm-2' diff --git a/common/payouts-dpm.ts b/common/payouts-dpm.ts index c138d302..a3208ab3 100644 --- a/common/payouts-dpm.ts +++ b/common/payouts-dpm.ts @@ -2,18 +2,12 @@ import { sum, groupBy, sumBy, mapValues } from 'lodash' import { Bet, NumericBet } from './bet' import { deductDpmFees, getDpmProbability } from './calculate-dpm' -import { DPM, FreeResponse, FullContract, Multi } from './contract' -import { - DPM_CREATOR_FEE, - DPM_FEES, - DPM_PLATFORM_FEE, - Fees, - noFees, -} from './fees' +import { DPM, FreeResponse, FullContract, Multi, AnyOutcome } from './contract' +import { DPM_CREATOR_FEE, DPM_FEES, DPM_PLATFORM_FEE } from './fees' import { addObjects } from './util/object' export const getDpmCancelPayouts = ( - contract: FullContract, + contract: FullContract, bets: Bet[] ) => { const { pool } = contract @@ -31,13 +25,13 @@ export const getDpmCancelPayouts = ( payouts, creatorPayout: 0, liquidityPayouts: [], - collectedFees: contract.collectedFees ?? noFees, + collectedFees: contract.collectedFees, } } export const getDpmStandardPayouts = ( outcome: string, - contract: FullContract, + contract: FullContract, bets: Bet[] ) => { 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 creatorFee = DPM_CREATOR_FEE * profits const platformFee = DPM_PLATFORM_FEE * profits - - const finalFees: Fees = { + const collectedFees = addObjects(contract.collectedFees, { creatorFee, platformFee, liquidityFee: 0, - } - - const collectedFees = addObjects( - finalFees, - contract.collectedFees ?? {} - ) + }) console.log( 'resolved', @@ -90,7 +78,7 @@ export const getDpmStandardPayouts = ( export const getNumericDpmPayouts = ( outcome: string, - contract: FullContract, + contract: FullContract, bets: NumericBet[] ) => { 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 creatorFee = DPM_CREATOR_FEE * profits const platformFee = DPM_PLATFORM_FEE * profits - - const finalFees: Fees = { + const collectedFees = addObjects(contract.collectedFees, { creatorFee, platformFee, liquidityFee: 0, - } - - const collectedFees = addObjects( - finalFees, - contract.collectedFees ?? {} - ) + }) console.log( 'resolved numeric bucket: ', @@ -147,7 +129,7 @@ export const getNumericDpmPayouts = ( } export const getDpmMktPayouts = ( - contract: FullContract, + contract: FullContract, bets: Bet[], resolutionProbability?: number ) => { @@ -174,17 +156,11 @@ export const getDpmMktPayouts = ( const creatorFee = DPM_CREATOR_FEE * profits const platformFee = DPM_PLATFORM_FEE * profits - - const finalFees: Fees = { + const collectedFees = addObjects(contract.collectedFees, { creatorFee, platformFee, liquidityFee: 0, - } - - const collectedFees = addObjects( - finalFees, - contract.collectedFees ?? {} - ) + }) console.log( 'resolved MKT', @@ -233,17 +209,11 @@ export const getPayoutsMultiOutcome = ( const creatorFee = DPM_CREATOR_FEE * profits const platformFee = DPM_PLATFORM_FEE * profits - - const finalFees: Fees = { + const collectedFees = addObjects(contract.collectedFees, { creatorFee, platformFee, liquidityFee: 0, - } - - const collectedFees = addObjects( - finalFees, - contract.collectedFees ?? noFees - ) + }) console.log( 'resolved', diff --git a/common/payouts.ts b/common/payouts.ts index 68fb8694..54c850f0 100644 --- a/common/payouts.ts +++ b/common/payouts.ts @@ -9,6 +9,7 @@ import { FreeResponse, FullContract, Multi, + AnyOutcome, } from './contract' import { Fees } from './fees' import { LiquidityProvision } from './liquidity-provision' @@ -72,15 +73,17 @@ export const getPayouts = ( liquidities, 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 = ( @@ -112,7 +115,7 @@ export const getDpmPayouts = ( resolutions: { [outcome: string]: number }, - contract: Contract, + contract: FullContract, bets: Bet[], resolutionProbability?: number ): PayoutInfo => { diff --git a/functions/src/sell-bet.ts b/functions/src/sell-bet.ts index fff88716..39ed8017 100644 --- a/functions/src/sell-bet.ts +++ b/functions/src/sell-bet.ts @@ -78,7 +78,7 @@ export const sellBet = functions.runWith({ minInstances: 1 }).https.onCall( pool: newPool, totalShares: newTotalShares, totalBets: newTotalBets, - collectedFees: addObjects(fees ?? {}, collectedFees ?? {}), + collectedFees: addObjects(fees, collectedFees), volume: volume + Math.abs(newBet.amount), }) ) diff --git a/functions/src/sell-shares.ts b/functions/src/sell-shares.ts index 08e7d7c5..c4166b8b 100644 --- a/functions/src/sell-shares.ts +++ b/functions/src/sell-shares.ts @@ -101,7 +101,7 @@ export const sellShares = functions.runWith({ minInstances: 1 }).https.onCall( removeUndefinedProps({ pool: newPool, p: newP, - collectedFees: addObjects(fees ?? {}, collectedFees ?? {}), + collectedFees: addObjects(fees, collectedFees), volume: volume + Math.abs(newBet.amount), }) ) diff --git a/web/components/contract/contract-info-dialog.tsx b/web/components/contract/contract-info-dialog.tsx index 8a238727..28cd91f4 100644 --- a/web/components/contract/contract-info-dialog.tsx +++ b/web/components/contract/contract-info-dialog.tsx @@ -95,7 +95,7 @@ export function ContractInfoDialog(props: { contract: Contract; bets: Bet[] }) { Creator earnings - {formatMoney(contract.collectedFees?.creatorFee ?? 0)} + {formatMoney(contract.collectedFees.creatorFee)}