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 FreeResponseContract = FullContract<DPM | CPMM, FreeResponse>
export type NumericContract = FullContract<DPM, Numeric>
export type AnyOutcome = Binary | Multi | FreeResponse | Numeric
export type DPM = {
mechanism: 'dpm-2'

View File

@ -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<DPM, any>,
contract: FullContract<DPM, AnyOutcome>,
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<DPM, any>,
contract: FullContract<DPM, AnyOutcome>,
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<Fees>(
finalFees,
contract.collectedFees ?? {}
)
})
console.log(
'resolved',
@ -90,7 +78,7 @@ export const getDpmStandardPayouts = (
export const getNumericDpmPayouts = (
outcome: string,
contract: FullContract<DPM, any>,
contract: FullContract<DPM, AnyOutcome>,
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<Fees>(
finalFees,
contract.collectedFees ?? {}
)
})
console.log(
'resolved numeric bucket: ',
@ -147,7 +129,7 @@ export const getNumericDpmPayouts = (
}
export const getDpmMktPayouts = (
contract: FullContract<DPM, any>,
contract: FullContract<DPM, AnyOutcome>,
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<Fees>(
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<Fees>(
finalFees,
contract.collectedFees ?? noFees
)
})
console.log(
'resolved',

View File

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

View File

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

View File

@ -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),
})
)

View File

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