Include answer probabilities in FreeResponse API results (#526)

* Include answer probabilities in FreeResponse API results

* Appease ESLint
This commit is contained in:
Ben Congdon 2022-06-17 00:20:43 -07:00 committed by GitHub
parent 61a2bcb609
commit 172f14c16f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 30 deletions

View File

@ -18,7 +18,7 @@ import { range, sortBy, sum } from 'lodash'
import { app } from './init'
import { getValues, listenForValue, listenForValues } from './utils'
import { BinaryContract, Contract, FreeResponseContract } from 'common/contract'
import { BinaryContract, Contract } from 'common/contract'
import { getDpmProbability } from 'common/calculate-dpm'
import { createRNG, shuffle } from 'common/util/random'
import { getCpmmProbability } from 'common/calculate-cpmm'
@ -28,7 +28,7 @@ import { MAX_FEED_CONTRACTS } from 'common/recommended-contracts'
import { Bet } from 'common/bet'
import { Comment } from 'common/comment'
import { ENV_CONFIG } from 'common/envs/constants'
export type { Contract, FreeResponseContract }
export type { Contract }
export function contractPath(contract: Contract) {
return `/${contract.creatorUsername}/${contract.slug}`

View File

@ -1,6 +1,6 @@
import { Bet } from 'common/bet'
import { Answer } from 'common/answer'
import { getProbability } from 'common/calculate'
import { getOutcomeProbability, getProbability } from 'common/calculate'
import { Comment } from 'common/comment'
import { Contract } from 'common/contract'
import { removeUndefinedProps } from 'common/util/object'
@ -38,10 +38,14 @@ export type LiteMarket = {
resolutionTime?: number
}
export type ApiAnswer = Answer & {
probability?: number
}
export type FullMarket = LiteMarket & {
bets: Exclude<Bet, 'userId'>[]
comments: Comment[]
answers?: Answer[]
answers?: ApiAnswer[]
}
export type ApiError = {
@ -104,3 +108,35 @@ export function toLiteMarket(contract: Contract): LiteMarket {
resolutionTime,
})
}
export function toFullMarket(
contract: Contract,
comments: Comment[],
bets: Bet[]
): FullMarket {
const liteMarket = toLiteMarket(contract)
const answers =
contract.outcomeType === 'FREE_RESPONSE'
? contract.answers.map((answer) =>
augmentAnswerWithProbability(contract, answer)
)
: undefined
return {
...liteMarket,
answers,
comments,
bets,
}
}
function augmentAnswerWithProbability(
contract: Contract,
answer: Answer
): ApiAnswer {
const probability = getOutcomeProbability(contract, answer.id)
return {
...answer,
probability,
}
}

View File

@ -1,12 +1,9 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { Bet, listAllBets } from 'web/lib/firebase/bets'
import { listAllComments } from 'web/lib/firebase/comments'
import {
getContractFromId,
FreeResponseContract,
} from 'web/lib/firebase/contracts'
import { getContractFromId } from 'web/lib/firebase/contracts'
import { applyCorsHeaders, CORS_UNRESTRICTED } from 'web/lib/api/cors'
import { FullMarket, ApiError, toLiteMarket } from '../_types'
import { FullMarket, ApiError, toFullMarket } from '../_types'
export default async function handler(
req: NextApiRequest,
@ -28,8 +25,6 @@ export default async function handler(
'userId'
>[]
const answers = (contract as FreeResponseContract).answers
if (!contract) {
res.status(404).json({ error: 'Contract not found' })
return
@ -37,10 +32,5 @@ export default async function handler(
// Cache on Vercel edge servers for 2min
res.setHeader('Cache-Control', 'max-age=0, s-maxage=120')
return res.status(200).json({
...toLiteMarket(contract),
bets,
comments,
answers,
})
return res.status(200).json(toFullMarket(contract, comments, bets))
}

View File

@ -2,11 +2,8 @@ import { NextApiRequest, NextApiResponse } from 'next'
import { applyCorsHeaders, CORS_UNRESTRICTED } from 'web/lib/api/cors'
import { Bet, listAllBets } from 'web/lib/firebase/bets'
import { listAllComments } from 'web/lib/firebase/comments'
import {
getContractFromSlug,
FreeResponseContract,
} from 'web/lib/firebase/contracts'
import { FullMarket, ApiError, toLiteMarket } from '../_types'
import { getContractFromSlug } from 'web/lib/firebase/contracts'
import { FullMarket, ApiError, toFullMarket } from '../_types'
export default async function handler(
req: NextApiRequest,
@ -27,8 +24,6 @@ export default async function handler(
listAllComments(contract.id),
])
const answers = (contract as FreeResponseContract).answers
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const bets = allBets.map(({ userId, ...bet }) => bet) as Exclude<
Bet,
@ -37,10 +32,5 @@ export default async function handler(
// Cache on Vercel edge servers for 2min
res.setHeader('Cache-Control', 'max-age=0, s-maxage=120')
return res.status(200).json({
...toLiteMarket(contract),
bets,
comments,
answers,
})
return res.status(200).json(toFullMarket(contract, comments, bets))
}