Include answer probabilities in FreeResponse API results (#526)
* Include answer probabilities in FreeResponse API results * Appease ESLint
This commit is contained in:
parent
61a2bcb609
commit
172f14c16f
|
@ -18,7 +18,7 @@ import { range, sortBy, sum } from 'lodash'
|
||||||
|
|
||||||
import { app } from './init'
|
import { app } from './init'
|
||||||
import { getValues, listenForValue, listenForValues } from './utils'
|
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 { getDpmProbability } from 'common/calculate-dpm'
|
||||||
import { createRNG, shuffle } from 'common/util/random'
|
import { createRNG, shuffle } from 'common/util/random'
|
||||||
import { getCpmmProbability } from 'common/calculate-cpmm'
|
import { getCpmmProbability } from 'common/calculate-cpmm'
|
||||||
|
@ -28,7 +28,7 @@ import { MAX_FEED_CONTRACTS } from 'common/recommended-contracts'
|
||||||
import { Bet } from 'common/bet'
|
import { Bet } from 'common/bet'
|
||||||
import { Comment } from 'common/comment'
|
import { Comment } from 'common/comment'
|
||||||
import { ENV_CONFIG } from 'common/envs/constants'
|
import { ENV_CONFIG } from 'common/envs/constants'
|
||||||
export type { Contract, FreeResponseContract }
|
export type { Contract }
|
||||||
|
|
||||||
export function contractPath(contract: Contract) {
|
export function contractPath(contract: Contract) {
|
||||||
return `/${contract.creatorUsername}/${contract.slug}`
|
return `/${contract.creatorUsername}/${contract.slug}`
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { Bet } from 'common/bet'
|
import { Bet } from 'common/bet'
|
||||||
import { Answer } from 'common/answer'
|
import { Answer } from 'common/answer'
|
||||||
import { getProbability } from 'common/calculate'
|
import { getOutcomeProbability, getProbability } from 'common/calculate'
|
||||||
import { Comment } from 'common/comment'
|
import { Comment } from 'common/comment'
|
||||||
import { Contract } from 'common/contract'
|
import { Contract } from 'common/contract'
|
||||||
import { removeUndefinedProps } from 'common/util/object'
|
import { removeUndefinedProps } from 'common/util/object'
|
||||||
|
@ -38,10 +38,14 @@ export type LiteMarket = {
|
||||||
resolutionTime?: number
|
resolutionTime?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type ApiAnswer = Answer & {
|
||||||
|
probability?: number
|
||||||
|
}
|
||||||
|
|
||||||
export type FullMarket = LiteMarket & {
|
export type FullMarket = LiteMarket & {
|
||||||
bets: Exclude<Bet, 'userId'>[]
|
bets: Exclude<Bet, 'userId'>[]
|
||||||
comments: Comment[]
|
comments: Comment[]
|
||||||
answers?: Answer[]
|
answers?: ApiAnswer[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ApiError = {
|
export type ApiError = {
|
||||||
|
@ -104,3 +108,35 @@ export function toLiteMarket(contract: Contract): LiteMarket {
|
||||||
resolutionTime,
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,12 +1,9 @@
|
||||||
import { NextApiRequest, NextApiResponse } from 'next'
|
import { NextApiRequest, NextApiResponse } from 'next'
|
||||||
import { Bet, listAllBets } from 'web/lib/firebase/bets'
|
import { Bet, listAllBets } from 'web/lib/firebase/bets'
|
||||||
import { listAllComments } from 'web/lib/firebase/comments'
|
import { listAllComments } from 'web/lib/firebase/comments'
|
||||||
import {
|
import { getContractFromId } from 'web/lib/firebase/contracts'
|
||||||
getContractFromId,
|
|
||||||
FreeResponseContract,
|
|
||||||
} from 'web/lib/firebase/contracts'
|
|
||||||
import { applyCorsHeaders, CORS_UNRESTRICTED } from 'web/lib/api/cors'
|
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(
|
export default async function handler(
|
||||||
req: NextApiRequest,
|
req: NextApiRequest,
|
||||||
|
@ -28,8 +25,6 @@ export default async function handler(
|
||||||
'userId'
|
'userId'
|
||||||
>[]
|
>[]
|
||||||
|
|
||||||
const answers = (contract as FreeResponseContract).answers
|
|
||||||
|
|
||||||
if (!contract) {
|
if (!contract) {
|
||||||
res.status(404).json({ error: 'Contract not found' })
|
res.status(404).json({ error: 'Contract not found' })
|
||||||
return
|
return
|
||||||
|
@ -37,10 +32,5 @@ export default async function handler(
|
||||||
|
|
||||||
// Cache on Vercel edge servers for 2min
|
// Cache on Vercel edge servers for 2min
|
||||||
res.setHeader('Cache-Control', 'max-age=0, s-maxage=120')
|
res.setHeader('Cache-Control', 'max-age=0, s-maxage=120')
|
||||||
return res.status(200).json({
|
return res.status(200).json(toFullMarket(contract, comments, bets))
|
||||||
...toLiteMarket(contract),
|
|
||||||
bets,
|
|
||||||
comments,
|
|
||||||
answers,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,11 +2,8 @@ import { NextApiRequest, NextApiResponse } from 'next'
|
||||||
import { applyCorsHeaders, CORS_UNRESTRICTED } from 'web/lib/api/cors'
|
import { applyCorsHeaders, CORS_UNRESTRICTED } from 'web/lib/api/cors'
|
||||||
import { Bet, listAllBets } from 'web/lib/firebase/bets'
|
import { Bet, listAllBets } from 'web/lib/firebase/bets'
|
||||||
import { listAllComments } from 'web/lib/firebase/comments'
|
import { listAllComments } from 'web/lib/firebase/comments'
|
||||||
import {
|
import { getContractFromSlug } from 'web/lib/firebase/contracts'
|
||||||
getContractFromSlug,
|
import { FullMarket, ApiError, toFullMarket } from '../_types'
|
||||||
FreeResponseContract,
|
|
||||||
} from 'web/lib/firebase/contracts'
|
|
||||||
import { FullMarket, ApiError, toLiteMarket } from '../_types'
|
|
||||||
|
|
||||||
export default async function handler(
|
export default async function handler(
|
||||||
req: NextApiRequest,
|
req: NextApiRequest,
|
||||||
|
@ -27,8 +24,6 @@ export default async function handler(
|
||||||
listAllComments(contract.id),
|
listAllComments(contract.id),
|
||||||
])
|
])
|
||||||
|
|
||||||
const answers = (contract as FreeResponseContract).answers
|
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
const bets = allBets.map(({ userId, ...bet }) => bet) as Exclude<
|
const bets = allBets.map(({ userId, ...bet }) => bet) as Exclude<
|
||||||
Bet,
|
Bet,
|
||||||
|
@ -37,10 +32,5 @@ export default async function handler(
|
||||||
|
|
||||||
// Cache on Vercel edge servers for 2min
|
// Cache on Vercel edge servers for 2min
|
||||||
res.setHeader('Cache-Control', 'max-age=0, s-maxage=120')
|
res.setHeader('Cache-Control', 'max-age=0, s-maxage=120')
|
||||||
return res.status(200).json({
|
return res.status(200).json(toFullMarket(contract, comments, bets))
|
||||||
...toLiteMarket(contract),
|
|
||||||
bets,
|
|
||||||
comments,
|
|
||||||
answers,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user