Resolve market emails: fix negative amount bug with better invested calculation

This commit is contained in:
James Grugett 2022-08-15 21:48:00 -05:00
parent e5aef763cd
commit f2f77cb51e

View File

@ -1,6 +1,6 @@
import * as admin from 'firebase-admin' import * as admin from 'firebase-admin'
import { z } from 'zod' import { z } from 'zod'
import { difference, uniq, mapValues, groupBy, sumBy } from 'lodash' import { difference, mapValues, groupBy, sumBy } from 'lodash'
import { import {
Contract, Contract,
@ -22,6 +22,8 @@ import { isManifoldId } from '../../common/envs/constants'
import { removeUndefinedProps } from '../../common/util/object' import { removeUndefinedProps } from '../../common/util/object'
import { LiquidityProvision } from '../../common/liquidity-provision' import { LiquidityProvision } from '../../common/liquidity-provision'
import { APIError, newEndpoint, validate } from './api' import { APIError, newEndpoint, validate } from './api'
import { getContractBetMetrics } from '../../common/calculate'
import { floatingEqual } from '../../common/util/math'
const bodySchema = z.object({ const bodySchema = z.object({
contractId: z.string(), contractId: z.string(),
@ -162,7 +164,7 @@ export const resolvemarket = newEndpoint(opts, async (req, auth) => {
const userPayoutsWithoutLoans = groupPayoutsByUser(payouts) const userPayoutsWithoutLoans = groupPayoutsByUser(payouts)
await sendResolutionEmails( await sendResolutionEmails(
openBets, bets,
userPayoutsWithoutLoans, userPayoutsWithoutLoans,
creator, creator,
creatorPayout, creatorPayout,
@ -188,7 +190,7 @@ const processPayouts = async (payouts: Payout[], isDeposit = false) => {
} }
const sendResolutionEmails = async ( const sendResolutionEmails = async (
openBets: Bet[], bets: Bet[],
userPayouts: { [userId: string]: number }, userPayouts: { [userId: string]: number },
creator: User, creator: User,
creatorPayout: number, creatorPayout: number,
@ -197,14 +199,15 @@ const sendResolutionEmails = async (
resolutionProbability?: number, resolutionProbability?: number,
resolutions?: { [outcome: string]: number } resolutions?: { [outcome: string]: number }
) => { ) => {
const nonWinners = difference(
uniq(openBets.map(({ userId }) => userId)),
Object.keys(userPayouts)
)
const investedByUser = mapValues( const investedByUser = mapValues(
groupBy(openBets, (bet) => bet.userId), groupBy(bets, (bet) => bet.userId),
(bets) => sumBy(bets, (bet) => bet.amount) (bets) => getContractBetMetrics(contract, bets).invested
) )
const investedUsers = Object.keys(investedByUser).filter(
(userId) => !floatingEqual(investedByUser[userId], 0)
)
const nonWinners = difference(investedUsers, Object.keys(userPayouts))
const emailPayouts = [ const emailPayouts = [
...Object.entries(userPayouts), ...Object.entries(userPayouts),
...nonWinners.map((userId) => [userId, 0] as const), ...nonWinners.map((userId) => [userId, 0] as const),