import * as admin from 'firebase-admin' import { z } from 'zod' import { difference, uniq, mapValues, groupBy, sumBy } from 'lodash' import { Contract, FreeResponseContract, RESOLUTIONS, } from '../../common/contract' import { User } from '../../common/user' import { Bet } from '../../common/bet' import { getUser, isProd, payUser } from './utils' import { sendMarketResolutionEmail } from './emails' import { getLoanPayouts, getPayouts, groupPayoutsByUser, Payout, } from '../../common/payouts' import { removeUndefinedProps } from '../../common/util/object' import { LiquidityProvision } from '../../common/liquidity-provision' import { APIError, newEndpoint, validate } from './api' const bodySchema = z.object({ contractId: z.string(), }) const binarySchema = z.object({ outcome: z.enum(RESOLUTIONS), probabilityInt: z.number().gte(0).lte(100).optional(), }) const freeResponseSchema = z.union([ z.object({ outcome: z.literal('CANCEL'), }), z.object({ outcome: z.literal('MKT'), resolutions: z.array( z.object({ answer: z.number().int().nonnegative(), pct: z.number().gte(0).lte(100), }) ), }), z.object({ outcome: z.number().int().nonnegative(), }), ]) const numericSchema = z.object({ outcome: z.union([z.literal('CANCEL'), z.string()]), value: z.number().optional(), }) const pseudoNumericSchema = z.union([ z.object({ outcome: z.literal('CANCEL'), }), z.object({ outcome: z.literal('MKT'), value: z.number(), probabilityInt: z.number().gte(0).lte(100), }), ]) const opts = { secrets: ['MAILGUN_KEY'] } export const resolvemarket = newEndpoint(opts, async (req, auth) => { const { contractId } = validate(bodySchema, req.body) const userId = auth.uid const contractDoc = firestore.doc(`contracts/${contractId}`) const contractSnap = await contractDoc.get() if (!contractSnap.exists) throw new APIError(404, 'No contract exists with the provided ID') const contract = contractSnap.data() as Contract const { creatorId, closeTime } = contract const { value, resolutions, probabilityInt, outcome } = getResolutionParams( contract, req.body ) if (creatorId !== userId) throw new APIError(403, 'User is not creator of contract') if (contract.resolution) throw new APIError(400, 'Contract already resolved') const creator = await getUser(creatorId) if (!creator) throw new APIError(500, 'Creator not found') const resolutionProbability = probabilityInt !== undefined ? probabilityInt / 100 : undefined const resolutionTime = Date.now() const newCloseTime = closeTime ? Math.min(closeTime, resolutionTime) : closeTime const betsSnap = await firestore .collection(`contracts/${contractId}/bets`) .get() const bets = betsSnap.docs.map((doc) => doc.data() as Bet) const liquiditiesSnap = await firestore .collection(`contracts/${contractId}/liquidity`) .get() const liquidities = liquiditiesSnap.docs.map( (doc) => doc.data() as LiquidityProvision ) const { payouts, creatorPayout, liquidityPayouts, collectedFees } = getPayouts( outcome, contract, bets, liquidities, resolutions, resolutionProbability ) const updatedContract = { ...contract, ...removeUndefinedProps({ isResolved: true, resolution: outcome, resolutionValue: value, resolutionTime, closeTime: newCloseTime, resolutionProbability, resolutions, collectedFees, }), } await contractDoc.update(updatedContract) console.log('contract ', contractId, 'resolved to:', outcome) const openBets = bets.filter((b) => !b.isSold && !b.sale) const loanPayouts = getLoanPayouts(openBets) if (!isProd()) console.log( 'payouts:', payouts, 'creator payout:', creatorPayout, 'liquidity payout:' ) if (creatorPayout) await processPayouts([{ userId: creatorId, payout: creatorPayout }], true) await processPayouts(liquidityPayouts, true) await processPayouts([...payouts, ...loanPayouts]) const userPayoutsWithoutLoans = groupPayoutsByUser(payouts) await sendResolutionEmails( openBets, userPayoutsWithoutLoans, creator, creatorPayout, contract, outcome, resolutionProbability, resolutions ) return updatedContract }) const processPayouts = async (payouts: Payout[], isDeposit = false) => { const userPayouts = groupPayoutsByUser(payouts) const payoutPromises = Object.entries(userPayouts).map(([userId, payout]) => payUser(userId, payout, isDeposit) ) return await Promise.all(payoutPromises) .catch((e) => ({ status: 'error', message: e })) .then(() => ({ status: 'success' })) } const sendResolutionEmails = async ( openBets: Bet[], userPayouts: { [userId: string]: number }, creator: User, creatorPayout: number, contract: Contract, outcome: string, resolutionProbability?: number, resolutions?: { [outcome: string]: number } ) => { const nonWinners = difference( uniq(openBets.map(({ userId }) => userId)), Object.keys(userPayouts) ) const investedByUser = mapValues( groupBy(openBets, (bet) => bet.userId), (bets) => sumBy(bets, (bet) => bet.amount) ) const emailPayouts = [ ...Object.entries(userPayouts), ...nonWinners.map((userId) => [userId, 0] as const), ].map(([userId, payout]) => ({ userId, investment: investedByUser[userId] ?? 0, payout, })) await Promise.all( emailPayouts.map(({ userId, investment, payout }) => sendMarketResolutionEmail( userId, investment, payout, creator, creatorPayout, contract, outcome, resolutionProbability, resolutions ) ) ) } function getResolutionParams(contract: Contract, body: string) { const { outcomeType } = contract if (outcomeType === 'NUMERIC') { return { ...validate(numericSchema, body), resolutions: undefined, probabilityInt: undefined, } } else if (outcomeType === 'PSEUDO_NUMERIC') { return { ...validate(pseudoNumericSchema, body), resolutions: undefined, } } else if (outcomeType === 'FREE_RESPONSE') { const freeResponseParams = validate(freeResponseSchema, body) const { outcome } = freeResponseParams switch (outcome) { case 'CANCEL': return { outcome: outcome.toString(), resolutions: undefined, value: undefined, probabilityInt: undefined, } case 'MKT': { const { resolutions } = freeResponseParams resolutions.forEach(({ answer }) => validateAnswer(contract, answer)) const pctSum = sumBy(resolutions, ({ pct }) => pct) if (Math.abs(pctSum - 100) > 0.1) { throw new APIError(400, 'Resolution percentages must sum to 100') } return { outcome: outcome.toString(), resolutions: Object.fromEntries( resolutions.map((r) => [r.answer, r.pct]) ), value: undefined, probabilityInt: undefined, } } default: { validateAnswer(contract, outcome) return { outcome: outcome.toString(), resolutions: undefined, value: undefined, probabilityInt: undefined, } } } } else if (outcomeType === 'BINARY') { return { ...validate(binarySchema, body), value: undefined, resolutions: undefined, } } throw new APIError(500, `Invalid outcome type: ${outcomeType}`) } function validateAnswer(contract: FreeResponseContract, answer: number) { const validIds = contract.answers.map((a) => a.id) if (!validIds.includes(answer.toString())) { throw new APIError(400, `${answer} is not a valid answer ID`) } } const firestore = admin.firestore()