2022-08-19 17:43:57 +00:00
|
|
|
import { Challenge } from './challenge'
|
|
|
|
import { BinaryContract, Contract } from './contract'
|
|
|
|
import { getFormattedMappedValue } from './pseudo-numeric'
|
|
|
|
import { getProbability } from './calculate'
|
|
|
|
import { richTextToString } from './util/parse'
|
|
|
|
import { getCpmmProbability } from './calculate-cpmm'
|
|
|
|
import { getDpmProbability } from './calculate-dpm'
|
|
|
|
import { formatMoney, formatPercent } from './util/format'
|
|
|
|
|
|
|
|
export function contractMetrics(contract: Contract) {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
|
|
const dayjs = require('dayjs')
|
|
|
|
const { createdTime, resolutionTime, isResolved } = contract
|
|
|
|
|
|
|
|
const createdDate = dayjs(createdTime).format('MMM D')
|
|
|
|
|
|
|
|
const resolvedDate = isResolved
|
|
|
|
? dayjs(resolutionTime).format('MMM D')
|
|
|
|
: undefined
|
|
|
|
|
|
|
|
const volumeLabel = `${formatMoney(contract.volume)} bet`
|
|
|
|
|
|
|
|
return { volumeLabel, createdDate, resolvedDate }
|
|
|
|
}
|
|
|
|
|
|
|
|
// String version of the above, to send to the OpenGraph image generator
|
|
|
|
export function contractTextDetails(contract: Contract) {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
|
|
const dayjs = require('dayjs')
|
2022-09-01 19:55:24 +00:00
|
|
|
const { closeTime, groupLinks } = contract
|
2022-08-19 17:43:57 +00:00
|
|
|
const { createdDate, resolvedDate, volumeLabel } = contractMetrics(contract)
|
|
|
|
|
2022-10-04 15:14:27 +00:00
|
|
|
const groupHashtags = groupLinks?.map((g) => `#${g.name.replace(/ /g, '')}`)
|
2022-08-19 17:43:57 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
`${resolvedDate ? `${createdDate} - ${resolvedDate}` : createdDate}` +
|
|
|
|
(closeTime
|
|
|
|
? ` • ${closeTime > Date.now() ? 'Closes' : 'Closed'} ${dayjs(
|
|
|
|
closeTime
|
|
|
|
).format('MMM D, h:mma')}`
|
|
|
|
: '') +
|
|
|
|
` • ${volumeLabel}` +
|
2022-09-01 19:55:24 +00:00
|
|
|
(groupHashtags ? ` • ${groupHashtags.join(' ')}` : '')
|
2022-08-19 17:43:57 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getBinaryProb(contract: BinaryContract) {
|
|
|
|
const { pool, resolutionProbability, mechanism } = contract
|
|
|
|
|
|
|
|
return (
|
|
|
|
resolutionProbability ??
|
|
|
|
(mechanism === 'cpmm-1'
|
|
|
|
? getCpmmProbability(pool, contract.p)
|
|
|
|
: getDpmProbability(contract.totalShares))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getOpenGraphProps = (contract: Contract) => {
|
|
|
|
const {
|
|
|
|
resolution,
|
|
|
|
question,
|
|
|
|
creatorName,
|
|
|
|
creatorUsername,
|
|
|
|
outcomeType,
|
|
|
|
creatorAvatarUrl,
|
|
|
|
description: desc,
|
|
|
|
} = contract
|
|
|
|
const probPercent =
|
|
|
|
outcomeType === 'BINARY'
|
|
|
|
? formatPercent(getBinaryProb(contract))
|
|
|
|
: undefined
|
|
|
|
|
|
|
|
const numericValue =
|
|
|
|
outcomeType === 'PSEUDO_NUMERIC'
|
|
|
|
? getFormattedMappedValue(contract)(getProbability(contract))
|
|
|
|
: undefined
|
|
|
|
|
|
|
|
const stringDesc = typeof desc === 'string' ? desc : richTextToString(desc)
|
|
|
|
|
|
|
|
const description = resolution
|
|
|
|
? `Resolved ${resolution}. ${stringDesc}`
|
|
|
|
: probPercent
|
|
|
|
? `${probPercent} chance. ${stringDesc}`
|
|
|
|
: stringDesc
|
|
|
|
|
|
|
|
return {
|
|
|
|
question,
|
|
|
|
probability: probPercent,
|
|
|
|
metadata: contractTextDetails(contract),
|
|
|
|
creatorName,
|
|
|
|
creatorUsername,
|
|
|
|
creatorAvatarUrl,
|
|
|
|
description,
|
|
|
|
numericValue,
|
2022-09-01 19:55:24 +00:00
|
|
|
resolution,
|
2022-08-19 17:43:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export type OgCardProps = {
|
|
|
|
question: string
|
|
|
|
probability?: string
|
|
|
|
metadata: string
|
|
|
|
creatorName: string
|
|
|
|
creatorUsername: string
|
|
|
|
creatorAvatarUrl?: string
|
|
|
|
numericValue?: string
|
2022-09-01 19:55:24 +00:00
|
|
|
resolution?: string
|
2022-08-19 17:43:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function buildCardUrl(props: OgCardProps, challenge?: Challenge) {
|
|
|
|
const {
|
|
|
|
creatorAmount,
|
|
|
|
acceptances,
|
|
|
|
acceptorAmount,
|
|
|
|
creatorOutcome,
|
|
|
|
acceptorOutcome,
|
|
|
|
} = challenge || {}
|
2022-09-01 19:55:24 +00:00
|
|
|
const {
|
|
|
|
probability,
|
|
|
|
numericValue,
|
|
|
|
resolution,
|
|
|
|
creatorAvatarUrl,
|
|
|
|
question,
|
|
|
|
metadata,
|
|
|
|
creatorUsername,
|
|
|
|
creatorName,
|
|
|
|
} = props
|
2022-08-19 17:43:57 +00:00
|
|
|
const { userName, userAvatarUrl } = acceptances?.[0] ?? {}
|
|
|
|
|
|
|
|
const probabilityParam =
|
2022-09-01 19:55:24 +00:00
|
|
|
probability === undefined
|
2022-08-19 17:43:57 +00:00
|
|
|
? ''
|
2022-09-01 19:55:24 +00:00
|
|
|
: `&probability=${encodeURIComponent(probability ?? '')}`
|
2022-08-19 17:43:57 +00:00
|
|
|
|
|
|
|
const numericValueParam =
|
2022-09-01 19:55:24 +00:00
|
|
|
numericValue === undefined
|
2022-08-19 17:43:57 +00:00
|
|
|
? ''
|
2022-09-01 19:55:24 +00:00
|
|
|
: `&numericValue=${encodeURIComponent(numericValue ?? '')}`
|
2022-08-19 17:43:57 +00:00
|
|
|
|
|
|
|
const creatorAvatarUrlParam =
|
2022-09-01 19:55:24 +00:00
|
|
|
creatorAvatarUrl === undefined
|
2022-08-19 17:43:57 +00:00
|
|
|
? ''
|
2022-09-01 19:55:24 +00:00
|
|
|
: `&creatorAvatarUrl=${encodeURIComponent(creatorAvatarUrl ?? '')}`
|
2022-08-19 17:43:57 +00:00
|
|
|
|
|
|
|
const challengeUrlParams = challenge
|
|
|
|
? `&creatorAmount=${creatorAmount}&creatorOutcome=${creatorOutcome}` +
|
|
|
|
`&challengerAmount=${acceptorAmount}&challengerOutcome=${acceptorOutcome}` +
|
|
|
|
`&acceptedName=${userName ?? ''}&acceptedAvatarUrl=${userAvatarUrl ?? ''}`
|
|
|
|
: ''
|
|
|
|
|
2022-09-01 19:55:24 +00:00
|
|
|
const resolutionUrlParam = resolution
|
|
|
|
? `&resolution=${encodeURIComponent(resolution)}`
|
|
|
|
: ''
|
|
|
|
|
2022-08-19 17:43:57 +00:00
|
|
|
// URL encode each of the props, then add them as query params
|
|
|
|
return (
|
|
|
|
`https://manifold-og-image.vercel.app/m.png` +
|
2022-09-01 19:55:24 +00:00
|
|
|
`?question=${encodeURIComponent(question)}` +
|
2022-08-19 17:43:57 +00:00
|
|
|
probabilityParam +
|
|
|
|
numericValueParam +
|
2022-09-01 19:55:24 +00:00
|
|
|
`&metadata=${encodeURIComponent(metadata)}` +
|
|
|
|
`&creatorName=${encodeURIComponent(creatorName)}` +
|
2022-08-19 17:43:57 +00:00
|
|
|
creatorAvatarUrlParam +
|
2022-09-01 19:55:24 +00:00
|
|
|
`&creatorUsername=${encodeURIComponent(creatorUsername)}` +
|
|
|
|
challengeUrlParams +
|
|
|
|
resolutionUrlParam
|
2022-08-19 17:43:57 +00:00
|
|
|
)
|
|
|
|
}
|