2021-12-14 07:02:50 +00:00
|
|
|
import * as admin from 'firebase-admin'
|
2022-06-29 23:47:06 +00:00
|
|
|
import { z } from 'zod'
|
2022-06-15 04:31:20 +00:00
|
|
|
import { difference, uniq, mapValues, groupBy, sumBy } from 'lodash'
|
2021-12-14 07:02:50 +00:00
|
|
|
|
2022-06-30 22:11:45 +00:00
|
|
|
import {
|
|
|
|
Contract,
|
|
|
|
FreeResponseContract,
|
2022-07-28 02:40:33 +00:00
|
|
|
MultipleChoiceContract,
|
2022-06-30 22:11:45 +00:00
|
|
|
RESOLUTIONS,
|
|
|
|
} from '../../common/contract'
|
2022-05-15 17:39:42 +00:00
|
|
|
import { User } from '../../common/user'
|
|
|
|
import { Bet } from '../../common/bet'
|
2022-03-15 22:27:51 +00:00
|
|
|
import { getUser, isProd, payUser } from './utils'
|
2022-01-02 00:08:52 +00:00
|
|
|
import { sendMarketResolutionEmail } from './emails'
|
2022-05-09 21:04:40 +00:00
|
|
|
import {
|
|
|
|
getLoanPayouts,
|
|
|
|
getPayouts,
|
|
|
|
groupPayoutsByUser,
|
|
|
|
Payout,
|
2022-05-15 17:39:42 +00:00
|
|
|
} from '../../common/payouts'
|
2022-08-11 05:38:15 +00:00
|
|
|
import { isManifoldId } from '../../common/envs/constants'
|
2022-05-15 17:39:42 +00:00
|
|
|
import { removeUndefinedProps } from '../../common/util/object'
|
|
|
|
import { LiquidityProvision } from '../../common/liquidity-provision'
|
2022-06-29 23:47:06 +00:00
|
|
|
import { APIError, newEndpoint, validate } from './api'
|
|
|
|
|
|
|
|
const bodySchema = z.object({
|
|
|
|
contractId: z.string(),
|
|
|
|
})
|
|
|
|
|
|
|
|
const binarySchema = z.object({
|
|
|
|
outcome: z.enum(RESOLUTIONS),
|
2022-07-02 19:37:59 +00:00
|
|
|
probabilityInt: z.number().gte(0).lte(100).optional(),
|
2022-06-29 23:47:06 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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(),
|
2022-07-02 19:37:59 +00:00
|
|
|
pct: z.number().gte(0).lte(100),
|
2022-06-29 23:47:06 +00:00
|
|
|
})
|
|
|
|
),
|
|
|
|
}),
|
|
|
|
z.object({
|
|
|
|
outcome: z.number().int().nonnegative(),
|
|
|
|
}),
|
|
|
|
])
|
|
|
|
|
|
|
|
const numericSchema = z.object({
|
|
|
|
outcome: z.union([z.literal('CANCEL'), z.string()]),
|
|
|
|
value: z.number().optional(),
|
|
|
|
})
|
|
|
|
|
2022-07-02 19:37:59 +00:00
|
|
|
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),
|
|
|
|
}),
|
|
|
|
])
|
|
|
|
|
2022-06-29 23:47:06 +00:00
|
|
|
const opts = { secrets: ['MAILGUN_KEY'] }
|
2022-07-02 19:37:59 +00:00
|
|
|
|
2022-06-29 23:47:06 +00:00
|
|
|
export const resolvemarket = newEndpoint(opts, async (req, auth) => {
|
|
|
|
const { contractId } = validate(bodySchema, req.body)
|
|
|
|
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
|
2022-06-30 22:11:45 +00:00
|
|
|
const { creatorId, closeTime } = contract
|
2022-06-29 23:47:06 +00:00
|
|
|
|
|
|
|
const { value, resolutions, probabilityInt, outcome } = getResolutionParams(
|
2022-06-30 22:11:45 +00:00
|
|
|
contract,
|
2022-06-29 23:47:06 +00:00
|
|
|
req.body
|
|
|
|
)
|
2021-12-14 07:02:50 +00:00
|
|
|
|
2022-08-11 05:38:15 +00:00
|
|
|
if (creatorId !== auth.uid && !isManifoldId(auth.uid))
|
2022-06-29 23:47:06 +00:00
|
|
|
throw new APIError(403, 'User is not creator of contract')
|
2022-03-15 22:27:51 +00:00
|
|
|
|
2022-06-29 23:47:06 +00:00
|
|
|
if (contract.resolution) throw new APIError(400, 'Contract already resolved')
|
2021-12-17 22:15:09 +00:00
|
|
|
|
2022-06-29 23:47:06 +00:00
|
|
|
const creator = await getUser(creatorId)
|
|
|
|
if (!creator) throw new APIError(500, 'Creator not found')
|
2022-01-02 00:08:52 +00:00
|
|
|
|
2022-06-29 23:47:06 +00:00
|
|
|
const resolutionProbability =
|
|
|
|
probabilityInt !== undefined ? probabilityInt / 100 : undefined
|
2022-06-14 20:01:32 +00:00
|
|
|
|
2022-06-29 23:47:06 +00:00
|
|
|
const resolutionTime = Date.now()
|
|
|
|
const newCloseTime = closeTime
|
|
|
|
? Math.min(closeTime, resolutionTime)
|
|
|
|
: closeTime
|
2022-06-14 20:01:32 +00:00
|
|
|
|
2022-06-29 23:47:06 +00:00
|
|
|
const betsSnap = await firestore
|
|
|
|
.collection(`contracts/${contractId}/bets`)
|
|
|
|
.get()
|
2022-06-14 20:01:32 +00:00
|
|
|
|
2022-06-29 23:47:06 +00:00
|
|
|
const bets = betsSnap.docs.map((doc) => doc.data() as Bet)
|
2022-06-14 20:01:32 +00:00
|
|
|
|
2022-06-29 23:47:06 +00:00
|
|
|
const liquiditiesSnap = await firestore
|
|
|
|
.collection(`contracts/${contractId}/liquidity`)
|
|
|
|
.get()
|
2022-06-14 20:01:32 +00:00
|
|
|
|
2022-06-29 23:47:06 +00:00
|
|
|
const liquidities = liquiditiesSnap.docs.map(
|
|
|
|
(doc) => doc.data() as LiquidityProvision
|
|
|
|
)
|
2022-06-15 04:31:20 +00:00
|
|
|
|
2022-06-29 23:47:06 +00:00
|
|
|
const { payouts, creatorPayout, liquidityPayouts, collectedFees } =
|
|
|
|
getPayouts(
|
|
|
|
outcome,
|
|
|
|
contract,
|
|
|
|
bets,
|
|
|
|
liquidities,
|
|
|
|
resolutions,
|
|
|
|
resolutionProbability
|
|
|
|
)
|
2022-06-15 04:31:20 +00:00
|
|
|
|
2022-06-29 23:47:06 +00:00
|
|
|
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:'
|
|
|
|
)
|
2022-06-15 04:31:20 +00:00
|
|
|
|
2022-06-29 23:47:06 +00:00
|
|
|
if (creatorPayout)
|
|
|
|
await processPayouts([{ userId: creatorId, payout: creatorPayout }], true)
|
2022-06-15 04:31:20 +00:00
|
|
|
|
2022-06-29 23:47:06 +00:00
|
|
|
await processPayouts(liquidityPayouts, true)
|
2022-06-15 04:31:20 +00:00
|
|
|
|
2022-06-29 23:47:06 +00:00
|
|
|
await processPayouts([...payouts, ...loanPayouts])
|
2022-06-15 04:31:20 +00:00
|
|
|
|
2022-06-29 23:47:06 +00:00
|
|
|
const userPayoutsWithoutLoans = groupPayoutsByUser(payouts)
|
2022-06-15 04:31:20 +00:00
|
|
|
|
2022-06-29 23:47:06 +00:00
|
|
|
await sendResolutionEmails(
|
|
|
|
openBets,
|
|
|
|
userPayoutsWithoutLoans,
|
|
|
|
creator,
|
|
|
|
creatorPayout,
|
|
|
|
contract,
|
|
|
|
outcome,
|
|
|
|
resolutionProbability,
|
|
|
|
resolutions
|
2021-12-17 22:15:09 +00:00
|
|
|
)
|
2021-12-14 07:02:50 +00:00
|
|
|
|
2022-06-29 23:47:06 +00:00
|
|
|
return updatedContract
|
|
|
|
})
|
|
|
|
|
2022-05-09 21:04:40 +00:00
|
|
|
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' }))
|
|
|
|
}
|
|
|
|
|
2022-01-02 01:13:30 +00:00
|
|
|
const sendResolutionEmails = async (
|
|
|
|
openBets: Bet[],
|
|
|
|
userPayouts: { [userId: string]: number },
|
|
|
|
creator: User,
|
2022-05-10 13:49:14 +00:00
|
|
|
creatorPayout: number,
|
2022-01-02 01:13:30 +00:00
|
|
|
contract: Contract,
|
2022-02-20 07:26:26 +00:00
|
|
|
outcome: string,
|
|
|
|
resolutionProbability?: number,
|
|
|
|
resolutions?: { [outcome: string]: number }
|
2022-01-02 01:13:30 +00:00
|
|
|
) => {
|
2022-05-22 08:36:05 +00:00
|
|
|
const nonWinners = difference(
|
|
|
|
uniq(openBets.map(({ userId }) => userId)),
|
2022-01-02 01:13:30 +00:00
|
|
|
Object.keys(userPayouts)
|
|
|
|
)
|
2022-05-22 08:36:05 +00:00
|
|
|
const investedByUser = mapValues(
|
|
|
|
groupBy(openBets, (bet) => bet.userId),
|
|
|
|
(bets) => sumBy(bets, (bet) => bet.amount)
|
2022-02-24 07:04:24 +00:00
|
|
|
)
|
2022-01-02 01:13:30 +00:00
|
|
|
const emailPayouts = [
|
|
|
|
...Object.entries(userPayouts),
|
|
|
|
...nonWinners.map((userId) => [userId, 0] as const),
|
2022-02-24 07:04:24 +00:00
|
|
|
].map(([userId, payout]) => ({
|
|
|
|
userId,
|
2022-03-17 04:56:59 +00:00
|
|
|
investment: investedByUser[userId] ?? 0,
|
2022-02-24 07:04:24 +00:00
|
|
|
payout,
|
|
|
|
}))
|
|
|
|
|
2022-01-02 01:13:30 +00:00
|
|
|
await Promise.all(
|
2022-02-24 07:04:24 +00:00
|
|
|
emailPayouts.map(({ userId, investment, payout }) =>
|
2022-01-30 21:51:30 +00:00
|
|
|
sendMarketResolutionEmail(
|
|
|
|
userId,
|
2022-02-24 07:04:24 +00:00
|
|
|
investment,
|
2022-01-30 21:51:30 +00:00
|
|
|
payout,
|
|
|
|
creator,
|
2022-05-10 13:49:14 +00:00
|
|
|
creatorPayout,
|
2022-01-30 21:51:30 +00:00
|
|
|
contract,
|
|
|
|
outcome,
|
2022-02-20 07:26:26 +00:00
|
|
|
resolutionProbability,
|
|
|
|
resolutions
|
2022-01-30 21:51:30 +00:00
|
|
|
)
|
2022-01-02 01:13:30 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-30 22:11:45 +00:00
|
|
|
function getResolutionParams(contract: Contract, body: string) {
|
|
|
|
const { outcomeType } = contract
|
2022-07-02 19:37:59 +00:00
|
|
|
|
2022-06-29 23:47:06 +00:00
|
|
|
if (outcomeType === 'NUMERIC') {
|
|
|
|
return {
|
|
|
|
...validate(numericSchema, body),
|
|
|
|
resolutions: undefined,
|
|
|
|
probabilityInt: undefined,
|
|
|
|
}
|
2022-07-02 19:37:59 +00:00
|
|
|
} else if (outcomeType === 'PSEUDO_NUMERIC') {
|
|
|
|
return {
|
|
|
|
...validate(pseudoNumericSchema, body),
|
|
|
|
resolutions: undefined,
|
|
|
|
}
|
2022-07-28 02:40:33 +00:00
|
|
|
} else if (
|
|
|
|
outcomeType === 'FREE_RESPONSE' ||
|
|
|
|
outcomeType === 'MULTIPLE_CHOICE'
|
|
|
|
) {
|
2022-06-29 23:47:06 +00:00
|
|
|
const freeResponseParams = validate(freeResponseSchema, body)
|
|
|
|
const { outcome } = freeResponseParams
|
2022-06-30 22:11:45 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
2022-06-29 23:47:06 +00:00
|
|
|
}
|
|
|
|
} else if (outcomeType === 'BINARY') {
|
|
|
|
return {
|
|
|
|
...validate(binarySchema, body),
|
|
|
|
value: undefined,
|
|
|
|
resolutions: undefined,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new APIError(500, `Invalid outcome type: ${outcomeType}`)
|
|
|
|
}
|
|
|
|
|
2022-07-28 02:40:33 +00:00
|
|
|
function validateAnswer(
|
|
|
|
contract: FreeResponseContract | MultipleChoiceContract,
|
|
|
|
answer: number
|
|
|
|
) {
|
2022-06-30 22:11:45 +00:00
|
|
|
const validIds = contract.answers.map((a) => a.id)
|
|
|
|
if (!validIds.includes(answer.toString())) {
|
|
|
|
throw new APIError(400, `${answer} is not a valid answer ID`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 07:02:50 +00:00
|
|
|
const firestore = admin.firestore()
|