diff --git a/web/components/bets-list.tsx b/web/components/bets-list.tsx index 1eae2c53..8a9f816c 100644 --- a/web/components/bets-list.tsx +++ b/web/components/bets-list.tsx @@ -14,7 +14,11 @@ import { } from '../lib/util/format' import { Col } from './layout/col' import { Spacer } from './layout/spacer' -import { Contract, getContractFromId, path } from '../lib/firebase/contracts' +import { + Contract, + getContractFromId, + contractPath, +} from '../lib/firebase/contracts' import { Row } from './layout/row' import { UserLink } from './user-page' import { @@ -130,7 +134,7 @@ function MyContractBets(props: { contract: Contract; bets: Bet[] }) { - + e.stopPropagation()} diff --git a/web/components/contract-card.tsx b/web/components/contract-card.tsx index 4571ab9e..d902b80d 100644 --- a/web/components/contract-card.tsx +++ b/web/components/contract-card.tsx @@ -4,7 +4,11 @@ import { Row } from '../components/layout/row' import { formatMoney } from '../lib/util/format' import { UserLink } from './user-page' import { Linkify } from './linkify' -import { Contract, compute, path } from '../lib/firebase/contracts' +import { + Contract, + contractMetrics, + contractPath, +} from '../lib/firebase/contracts' import { Col } from './layout/col' import { parseTags } from '../lib/util/parse' import dayjs from 'dayjs' @@ -15,10 +19,10 @@ export function ContractCard(props: { }) { const { contract, showHotVolume } = props const { question, resolution } = contract - const { probPercent } = compute(contract) + const { probPercent } = contractMetrics(contract) return ( - +
  • @@ -98,7 +102,7 @@ export function AbbrContractDetails(props: { }) { const { contract, showHotVolume } = props const { volume24Hours } = contract - const { truePool } = compute(contract) + const { truePool } = contractMetrics(contract) return ( @@ -122,7 +126,7 @@ export function AbbrContractDetails(props: { export function ContractDetails(props: { contract: Contract }) { const { contract } = props const { question, description, closeTime } = contract - const { truePool, createdDate, resolvedDate } = compute(contract) + const { truePool, createdDate, resolvedDate } = contractMetrics(contract) const tags = parseTags(`${question} ${description}`).map((tag) => `#${tag}`) @@ -169,7 +173,7 @@ export function ContractDetails(props: { contract: Contract }) { // String version of the above, to send to the OpenGraph image generator export function contractTextDetails(contract: Contract) { const { question, description, closeTime } = contract - const { truePool, createdDate, resolvedDate } = compute(contract) + const { truePool, createdDate, resolvedDate } = contractMetrics(contract) const tags = parseTags(`${question} ${description}`).map((tag) => `#${tag}`) diff --git a/web/components/contract-feed.tsx b/web/components/contract-feed.tsx index c6f31d1a..0a317c82 100644 --- a/web/components/contract-feed.tsx +++ b/web/components/contract-feed.tsx @@ -17,9 +17,9 @@ import dayjs from 'dayjs' import relativeTime from 'dayjs/plugin/relativeTime' import { OutcomeLabel } from './outcome-label' import { - compute, + contractMetrics, Contract, - path, + contractPath, updateContract, } from '../lib/firebase/contracts' import { useUser } from '../hooks/use-user' @@ -201,7 +201,7 @@ export function ContractDescription(props: { function FeedQuestion(props: { contract: Contract }) { const { contract } = props - const { probPercent } = compute(contract) + const { probPercent } = contractMetrics(contract) return ( <> @@ -218,7 +218,10 @@ function FeedQuestion(props: { contract: Contract }) {
    - + {contract.question} { const { contract, className } = props const { resolution, creatorId, creatorName } = contract - const { probPercent, truePool } = compute(contract) + const { probPercent, truePool } = contractMetrics(contract) const user = useUser() const isCreator = user?.id === creatorId @@ -35,7 +35,7 @@ export const ContractOverview = (props: { ? `Resolved ${resolution}!` : `Resolved ${resolution} by ${creatorName}:` : `Currently ${probPercent} chance, place your bets here:` - const url = `https://manifold.markets${path(contract)}` + const url = `https://manifold.markets${contractPath(contract)}` const tweetText = `${tweetQuestion}\n\n${tweetDescription}\n\n${url}` return ( diff --git a/web/components/contracts-list.tsx b/web/components/contracts-list.tsx index 1a3765b0..c76fa72d 100644 --- a/web/components/contracts-list.tsx +++ b/web/components/contracts-list.tsx @@ -3,7 +3,11 @@ import Link from 'next/link' import clsx from 'clsx' import { useEffect, useState } from 'react' -import { compute, Contract, listContracts } from '../lib/firebase/contracts' +import { + contractMetrics, + Contract, + listContracts, +} from '../lib/firebase/contracts' import { User } from '../lib/firebase/users' import { Col } from './layout/col' import { SiteLink } from './site-link' @@ -189,7 +193,9 @@ export function SearchableGrid(props: { if (sort === 'newest' || sort === 'resolved' || sort === 'all') { matches.sort((a, b) => b.createdTime - a.createdTime) } else if (sort === 'most-traded') { - matches.sort((a, b) => compute(b).truePool - compute(a).truePool) + matches.sort( + (a, b) => contractMetrics(b).truePool - contractMetrics(a).truePool + ) } else if (sort === 'creator' || sort === 'tag') { matches.sort((a, b) => b.volume7Days - a.volume7Days) } diff --git a/web/lib/firebase/contracts.ts b/web/lib/firebase/contracts.ts index 2fd8e786..8a48843d 100644 --- a/web/lib/firebase/contracts.ts +++ b/web/lib/firebase/contracts.ts @@ -18,24 +18,36 @@ import { import { app } from './init' import { getValues, listenForValues } from './utils' import { Contract } from '../../../common/contract' +import { getProbability } from '../../../common/calculate' export type { Contract } -export function path(contract: Contract) { +export function contractPath(contract: Contract) { // For now, derive username from creatorName return `/${contract.creatorUsername}/${contract.slug}` } -export function compute(contract: Contract) { - const { pool, startPool, createdTime, resolutionTime, isResolved } = contract - const truePool = pool.YES + pool.NO - startPool.YES - startPool.NO - const prob = pool.YES ** 2 / (pool.YES ** 2 + pool.NO ** 2) +export function contractMetrics(contract: Contract) { + const { + pool, + startPool, + totalShares, + createdTime, + resolutionTime, + isResolved, + } = contract + + const truePool = pool.YES + pool.NO + const prob = getProbability(totalShares) const probPercent = Math.round(prob * 100) + '%' - const startProb = - startPool.YES ** 2 / (startPool.YES ** 2 + startPool.NO ** 2) + + const startProb = getProbability(startPool) + const createdDate = dayjs(createdTime).format('MMM D') + const resolvedDate = isResolved ? dayjs(resolutionTime).format('MMM D') : undefined + return { truePool, probPercent, startProb, createdDate, resolvedDate } } diff --git a/web/pages/[username]/[contractSlug].tsx b/web/pages/[username]/[contractSlug].tsx index 6dc7629b..30092509 100644 --- a/web/pages/[username]/[contractSlug].tsx +++ b/web/pages/[username]/[contractSlug].tsx @@ -12,7 +12,7 @@ import { Title } from '../../components/title' import { Spacer } from '../../components/layout/spacer' import { User } from '../../lib/firebase/users' import { - compute, + contractMetrics, Contract, getContractFromSlug, } from '../../lib/firebase/contracts' @@ -58,7 +58,7 @@ export default function ContractPage(props: { !isResolved && (!contract.closeTime || contract.closeTime > Date.now()) const allowResolve = !isResolved && isCreator && !!user - const { probPercent } = compute(contract) + const { probPercent } = contractMetrics(contract) const description = resolution ? `Resolved ${resolution}. ${contract.description}` diff --git a/web/pages/create.tsx b/web/pages/create.tsx index 7e00191b..c4f0de27 100644 --- a/web/pages/create.tsx +++ b/web/pages/create.tsx @@ -8,7 +8,7 @@ import { CreatorContractsList } from '../components/contracts-list' import { Spacer } from '../components/layout/spacer' import { Title } from '../components/title' import { useUser } from '../hooks/use-user' -import { Contract, path } from '../lib/firebase/contracts' +import { Contract, contractPath } from '../lib/firebase/contracts' import { Page } from '../components/page' import { AdvancedPanel } from '../components/advanced-panel' import { createContract } from '../lib/firebase/api-call' @@ -73,7 +73,7 @@ export default function NewContract() { return } - await router.push(path(result.contract as Contract)) + await router.push(contractPath(result.contract as Contract)) } const descriptionPlaceholder = `e.g. This market will resolve to “Yes” if, by June 2, 2021, 11:59:59 PM ET, Paxlovid (also known under PF-07321332)...` diff --git a/web/pages/make-predictions.tsx b/web/pages/make-predictions.tsx index c2a2fe5f..31a947d9 100644 --- a/web/pages/make-predictions.tsx +++ b/web/pages/make-predictions.tsx @@ -10,7 +10,11 @@ import { Page } from '../components/page' import { Title } from '../components/title' import { useUser } from '../hooks/use-user' import { createContract } from '../lib/firebase/api-call' -import { compute, Contract, path } from '../lib/firebase/contracts' +import { + contractMetrics, + Contract, + contractPath, +} from '../lib/firebase/contracts' type Prediction = { question: string @@ -20,12 +24,12 @@ type Prediction = { } function toPrediction(contract: Contract): Prediction { - const { startProb } = compute(contract) + const { startProb } = contractMetrics(contract) return { question: contract.question, description: contract.description, initialProb: startProb * 100, - createdUrl: path(contract), + createdUrl: contractPath(contract), } }