2022-05-10 21:49:24 +00:00
|
|
|
import React, { useEffect, useMemo, useState } from 'react'
|
2022-04-11 21:13:26 +00:00
|
|
|
import { ArrowLeftIcon } from '@heroicons/react/outline'
|
2022-05-22 08:36:05 +00:00
|
|
|
import { keyBy, sortBy, groupBy, sumBy, mapValues } from 'lodash'
|
2021-12-16 03:14:00 +00:00
|
|
|
|
2022-05-09 13:04:36 +00:00
|
|
|
import { useContractWithPreload } from 'web/hooks/use-contract'
|
|
|
|
import { ContractOverview } from 'web/components/contract/contract-overview'
|
|
|
|
import { BetPanel } from 'web/components/bet-panel'
|
|
|
|
import { Col } from 'web/components/layout/col'
|
|
|
|
import { useUser } from 'web/hooks/use-user'
|
|
|
|
import { ResolutionPanel } from 'web/components/resolution-panel'
|
|
|
|
import { Title } from 'web/components/title'
|
|
|
|
import { Spacer } from 'web/components/layout/spacer'
|
|
|
|
import { listUsers, User } from 'web/lib/firebase/users'
|
2022-01-02 04:52:55 +00:00
|
|
|
import {
|
|
|
|
Contract,
|
|
|
|
getContractFromSlug,
|
2022-01-26 20:08:03 +00:00
|
|
|
tradingAllowed,
|
2022-02-17 23:00:19 +00:00
|
|
|
getBinaryProbPercent,
|
2022-05-09 13:04:36 +00:00
|
|
|
} from 'web/lib/firebase/contracts'
|
|
|
|
import { SEO } from 'web/components/SEO'
|
|
|
|
import { Page } from 'web/components/page'
|
|
|
|
import { Bet, listAllBets } from 'web/lib/firebase/bets'
|
|
|
|
import { Comment, listAllComments } from 'web/lib/firebase/comments'
|
2022-01-16 03:09:15 +00:00
|
|
|
import Custom404 from '../404'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { AnswersPanel } from 'web/components/answers/answers-panel'
|
|
|
|
import { fromPropz, usePropz } from 'web/hooks/use-propz'
|
|
|
|
import { Leaderboard } from 'web/components/leaderboard'
|
|
|
|
import { resolvedPayout } from 'common/calculate'
|
|
|
|
import { formatMoney } from 'common/util/format'
|
|
|
|
import { useUserById } from 'web/hooks/use-users'
|
|
|
|
import { ContractTabs } from 'web/components/contract/contract-tabs'
|
|
|
|
import { contractTextDetails } from 'web/components/contract/contract-details'
|
|
|
|
import { useWindowSize } from 'web/hooks/use-window-size'
|
2022-04-28 23:01:50 +00:00
|
|
|
import Confetti from 'react-confetti'
|
2022-05-19 17:42:03 +00:00
|
|
|
import { NumericBetPanel } from '../../components/numeric-bet-panel'
|
|
|
|
import { NumericResolutionPanel } from '../../components/numeric-resolution-panel'
|
2022-05-18 14:42:13 +00:00
|
|
|
import { FeedComment } from 'web/components/feed/feed-comments'
|
|
|
|
import { FeedBet } from 'web/components/feed/feed-bets'
|
2022-05-20 00:34:08 +00:00
|
|
|
import { useIsIframe } from 'web/hooks/use-is-iframe'
|
|
|
|
import ContractEmbedPage from '../embed/[username]/[contractSlug]'
|
2021-12-09 21:31:02 +00:00
|
|
|
|
2022-03-09 02:43:30 +00:00
|
|
|
export const getStaticProps = fromPropz(getStaticPropz)
|
|
|
|
export async function getStaticPropz(props: {
|
2022-01-21 23:21:46 +00:00
|
|
|
params: { username: string; contractSlug: string }
|
|
|
|
}) {
|
2021-12-19 05:50:47 +00:00
|
|
|
const { username, contractSlug } = props.params
|
|
|
|
const contract = (await getContractFromSlug(contractSlug)) || null
|
2022-01-15 00:16:25 +00:00
|
|
|
const contractId = contract?.id
|
|
|
|
|
2022-04-11 21:13:26 +00:00
|
|
|
const [bets, comments] = await Promise.all([
|
2022-02-01 18:06:42 +00:00
|
|
|
contractId ? listAllBets(contractId) : [],
|
|
|
|
contractId ? listAllComments(contractId) : [],
|
2022-01-15 00:16:25 +00:00
|
|
|
])
|
2021-12-09 22:05:55 +00:00
|
|
|
|
2021-12-16 03:24:11 +00:00
|
|
|
return {
|
|
|
|
props: {
|
2022-02-01 18:06:42 +00:00
|
|
|
contract,
|
2021-12-16 18:40:23 +00:00
|
|
|
username,
|
2021-12-19 05:50:47 +00:00
|
|
|
slug: contractSlug,
|
2022-01-15 00:16:25 +00:00
|
|
|
bets,
|
|
|
|
comments,
|
2021-12-16 03:24:11 +00:00
|
|
|
},
|
2021-12-09 22:05:55 +00:00
|
|
|
|
2021-12-16 06:36:51 +00:00
|
|
|
revalidate: 60, // regenerate after a minute
|
2021-12-09 22:05:55 +00:00
|
|
|
}
|
2021-12-16 03:24:11 +00:00
|
|
|
}
|
2021-12-09 21:31:02 +00:00
|
|
|
|
2021-12-16 06:36:51 +00:00
|
|
|
export async function getStaticPaths() {
|
|
|
|
return { paths: [], fallback: 'blocking' }
|
|
|
|
}
|
2021-12-16 03:14:00 +00:00
|
|
|
|
2021-12-16 06:36:51 +00:00
|
|
|
export default function ContractPage(props: {
|
2021-12-16 18:21:16 +00:00
|
|
|
contract: Contract | null
|
2021-12-16 18:40:23 +00:00
|
|
|
username: string
|
2022-02-01 18:06:42 +00:00
|
|
|
bets: Bet[]
|
|
|
|
comments: Comment[]
|
|
|
|
slug: string
|
2022-04-11 21:13:26 +00:00
|
|
|
backToHome?: () => void
|
2021-12-16 06:36:51 +00:00
|
|
|
}) {
|
2022-03-09 02:43:30 +00:00
|
|
|
props = usePropz(props, getStaticPropz) ?? {
|
|
|
|
contract: null,
|
|
|
|
username: '',
|
|
|
|
comments: [],
|
|
|
|
bets: [],
|
|
|
|
slug: '',
|
|
|
|
}
|
2022-04-11 21:13:26 +00:00
|
|
|
return <ContractPageContent {...props} />
|
|
|
|
}
|
|
|
|
|
2022-06-04 20:30:54 +00:00
|
|
|
export function ContractPageContent(props: Parameters<typeof ContractPage>[0]) {
|
2022-04-11 21:13:26 +00:00
|
|
|
const { backToHome } = props
|
|
|
|
|
2021-12-16 05:56:12 +00:00
|
|
|
const user = useUser()
|
2022-04-28 23:01:50 +00:00
|
|
|
const { width, height } = useWindowSize()
|
2021-12-09 22:05:55 +00:00
|
|
|
|
2022-04-06 17:32:57 +00:00
|
|
|
const contract = useContractWithPreload(props.contract)
|
2022-02-06 22:55:14 +00:00
|
|
|
const { bets, comments } = props
|
2022-04-28 23:01:50 +00:00
|
|
|
const [showConfetti, setShowConfetti] = useState(false)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const shouldSeeConfetti = !!(
|
|
|
|
user &&
|
|
|
|
contract &&
|
|
|
|
contract.creatorId === user.id &&
|
|
|
|
Date.now() - contract.createdTime < 10 * 1000
|
|
|
|
)
|
|
|
|
setShowConfetti(shouldSeeConfetti)
|
|
|
|
}, [contract, user])
|
2022-02-06 22:55:14 +00:00
|
|
|
|
2022-05-20 00:34:08 +00:00
|
|
|
const inIframe = useIsIframe()
|
|
|
|
if (inIframe) {
|
|
|
|
return <ContractEmbedPage {...props} />
|
|
|
|
}
|
|
|
|
|
2022-02-18 00:18:40 +00:00
|
|
|
// Sort for now to see if bug is fixed.
|
|
|
|
comments.sort((c1, c2) => c1.createdTime - c2.createdTime)
|
|
|
|
bets.sort((bet1, bet2) => bet1.createdTime - bet2.createdTime)
|
|
|
|
|
2021-12-10 14:56:17 +00:00
|
|
|
if (!contract) {
|
2022-01-16 03:09:15 +00:00
|
|
|
return <Custom404 />
|
2021-12-09 22:05:55 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 17:42:03 +00:00
|
|
|
const { creatorId, isResolved, question, outcomeType } = contract
|
2022-02-17 23:00:19 +00:00
|
|
|
|
2021-12-14 06:12:25 +00:00
|
|
|
const isCreator = user?.id === creatorId
|
2022-02-17 23:00:19 +00:00
|
|
|
const isBinary = outcomeType === 'BINARY'
|
2022-05-19 17:42:03 +00:00
|
|
|
const isNumeric = outcomeType === 'NUMERIC'
|
2022-01-26 20:08:03 +00:00
|
|
|
const allowTrade = tradingAllowed(contract)
|
2022-01-10 16:32:54 +00:00
|
|
|
const allowResolve = !isResolved && isCreator && !!user
|
2022-05-19 17:42:03 +00:00
|
|
|
const hasSidePanel = (isBinary || isNumeric) && (allowTrade || allowResolve)
|
2021-12-14 00:00:02 +00:00
|
|
|
|
2022-02-18 00:34:11 +00:00
|
|
|
const ogCardProps = getOpenGraphProps(contract)
|
2022-01-10 07:05:24 +00:00
|
|
|
|
2022-03-31 05:35:20 +00:00
|
|
|
const rightSidebar = hasSidePanel ? (
|
|
|
|
<Col className="gap-4">
|
2022-05-19 17:42:03 +00:00
|
|
|
{allowTrade &&
|
|
|
|
(isNumeric ? (
|
2022-06-01 02:42:35 +00:00
|
|
|
<NumericBetPanel className="hidden xl:flex" contract={contract} />
|
2022-05-19 17:42:03 +00:00
|
|
|
) : (
|
|
|
|
<BetPanel className="hidden xl:flex" contract={contract} />
|
|
|
|
))}
|
|
|
|
{allowResolve &&
|
|
|
|
(isNumeric ? (
|
2022-06-01 02:42:35 +00:00
|
|
|
<NumericResolutionPanel creator={user} contract={contract} />
|
2022-05-19 17:42:03 +00:00
|
|
|
) : (
|
2022-06-01 02:42:35 +00:00
|
|
|
<ResolutionPanel creator={user} contract={contract} />
|
2022-05-19 17:42:03 +00:00
|
|
|
))}
|
2022-03-31 05:35:20 +00:00
|
|
|
</Col>
|
|
|
|
) : null
|
|
|
|
|
2021-12-09 22:05:55 +00:00
|
|
|
return (
|
2022-03-31 05:35:20 +00:00
|
|
|
<Page rightSidebar={rightSidebar}>
|
2022-04-28 23:01:50 +00:00
|
|
|
{showConfetti && (
|
|
|
|
<Confetti
|
|
|
|
width={width ? width : 500}
|
|
|
|
height={height ? height : 500}
|
|
|
|
recycle={false}
|
|
|
|
numberOfPieces={300}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
{ogCardProps && (
|
|
|
|
<SEO
|
|
|
|
title={question}
|
|
|
|
description={ogCardProps.description}
|
|
|
|
url={`/${props.username}/${props.slug}`}
|
|
|
|
ogCardProps={ogCardProps}
|
|
|
|
/>
|
|
|
|
)}
|
2021-12-16 03:14:00 +00:00
|
|
|
|
2022-05-23 22:09:40 +00:00
|
|
|
<Col className="w-full justify-between rounded border-0 border-gray-100 bg-white py-6 pl-1 pr-2 sm:px-2 md:px-6 md:py-8">
|
2022-04-11 21:13:26 +00:00
|
|
|
{backToHome && (
|
|
|
|
<button
|
|
|
|
className="btn btn-sm mb-4 items-center gap-2 self-start border-0 border-gray-700 bg-white normal-case text-gray-700 hover:bg-white hover:text-gray-700 lg:hidden"
|
|
|
|
onClick={backToHome}
|
|
|
|
>
|
|
|
|
<ArrowLeftIcon className="h-5 w-5 text-gray-700" />
|
|
|
|
Back
|
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
|
2022-04-03 21:57:38 +00:00
|
|
|
<ContractOverview
|
|
|
|
contract={contract}
|
|
|
|
bets={bets ?? []}
|
|
|
|
comments={comments ?? []}
|
2022-04-18 23:02:40 +00:00
|
|
|
/>
|
|
|
|
|
2022-05-03 21:54:00 +00:00
|
|
|
{outcomeType === 'FREE_RESPONSE' && (
|
|
|
|
<>
|
|
|
|
<Spacer h={4} />
|
2022-06-01 02:42:35 +00:00
|
|
|
<AnswersPanel contract={contract} />
|
2022-05-03 21:54:00 +00:00
|
|
|
<Spacer h={4} />
|
|
|
|
</>
|
|
|
|
)}
|
2022-04-03 21:57:38 +00:00
|
|
|
|
2022-06-07 22:03:22 +00:00
|
|
|
{isNumeric && allowTrade && (
|
2022-06-01 02:42:35 +00:00
|
|
|
<NumericBetPanel className="xl:hidden" contract={contract} />
|
2022-05-19 17:42:03 +00:00
|
|
|
)}
|
|
|
|
|
2022-05-03 20:57:39 +00:00
|
|
|
{isResolved && (
|
2022-04-03 21:57:38 +00:00
|
|
|
<>
|
|
|
|
<div className="grid grid-cols-1 sm:grid-cols-2">
|
2022-05-10 21:22:57 +00:00
|
|
|
<ContractLeaderboard contract={contract} bets={bets} />
|
2022-04-03 21:57:38 +00:00
|
|
|
<ContractTopTrades
|
|
|
|
contract={contract}
|
|
|
|
bets={bets}
|
|
|
|
comments={comments}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<Spacer h={12} />
|
|
|
|
</>
|
|
|
|
)}
|
2022-04-08 21:13:10 +00:00
|
|
|
|
|
|
|
<ContractTabs
|
|
|
|
contract={contract}
|
|
|
|
user={user}
|
|
|
|
bets={bets}
|
|
|
|
comments={comments}
|
|
|
|
/>
|
2021-12-12 22:14:52 +00:00
|
|
|
</Col>
|
2021-12-20 04:06:30 +00:00
|
|
|
</Page>
|
2021-12-09 22:05:55 +00:00
|
|
|
)
|
|
|
|
}
|
2021-12-16 04:30:24 +00:00
|
|
|
|
2022-05-10 21:22:57 +00:00
|
|
|
function ContractLeaderboard(props: { contract: Contract; bets: Bet[] }) {
|
|
|
|
const { contract, bets } = props
|
2022-03-21 01:07:45 +00:00
|
|
|
const [users, setUsers] = useState<User[]>()
|
|
|
|
|
2022-05-10 21:49:24 +00:00
|
|
|
const { userProfits, top5Ids } = useMemo(() => {
|
|
|
|
// Create a map of userIds to total profits (including sales)
|
2022-05-22 08:36:05 +00:00
|
|
|
const betsByUser = groupBy(bets, 'userId')
|
|
|
|
const userProfits = mapValues(betsByUser, (bets) =>
|
|
|
|
sumBy(bets, (bet) => resolvedPayout(contract, bet) - bet.amount)
|
2022-05-10 21:49:24 +00:00
|
|
|
)
|
|
|
|
// Find the 5 users with the most profits
|
2022-05-22 08:36:05 +00:00
|
|
|
const top5Ids = Object.entries(userProfits)
|
|
|
|
.sort(([_i1, p1], [_i2, p2]) => p2 - p1)
|
2022-05-10 21:49:24 +00:00
|
|
|
.filter(([, p]) => p > 0)
|
|
|
|
.slice(0, 5)
|
|
|
|
.map(([id]) => id)
|
|
|
|
return { userProfits, top5Ids }
|
|
|
|
}, [contract, bets])
|
2022-05-10 20:58:38 +00:00
|
|
|
|
2022-05-10 21:22:57 +00:00
|
|
|
useEffect(() => {
|
2022-05-10 21:49:24 +00:00
|
|
|
console.log('foo')
|
2022-03-21 01:07:45 +00:00
|
|
|
if (top5Ids.length > 0) {
|
|
|
|
listUsers(top5Ids).then((users) => {
|
2022-05-22 08:36:05 +00:00
|
|
|
const sortedUsers = sortBy(users, (user) => -userProfits[user.id])
|
2022-03-21 01:07:45 +00:00
|
|
|
setUsers(sortedUsers)
|
|
|
|
})
|
|
|
|
}
|
2022-05-10 21:49:24 +00:00
|
|
|
}, [userProfits, top5Ids])
|
2022-03-21 01:07:45 +00:00
|
|
|
|
|
|
|
return users && users.length > 0 ? (
|
|
|
|
<Leaderboard
|
2022-04-26 14:42:45 +00:00
|
|
|
title="🏅 Top bettors"
|
2022-03-21 01:07:45 +00:00
|
|
|
users={users || []}
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
header: 'Total profit',
|
|
|
|
renderCell: (user) => formatMoney(userProfits[user.id] || 0),
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
className="mt-12 max-w-sm"
|
|
|
|
/>
|
|
|
|
) : null
|
|
|
|
}
|
|
|
|
|
|
|
|
function ContractTopTrades(props: {
|
|
|
|
contract: Contract
|
|
|
|
bets: Bet[]
|
|
|
|
comments: Comment[]
|
|
|
|
}) {
|
|
|
|
const { contract, bets, comments } = props
|
2022-05-22 08:36:05 +00:00
|
|
|
const commentsById = keyBy(comments, 'id')
|
|
|
|
const betsById = keyBy(bets, 'id')
|
2022-03-21 01:07:45 +00:00
|
|
|
|
|
|
|
// If 'id2' is the sale of 'id1', both are logged with (id2 - id1) of profit
|
|
|
|
// Otherwise, we record the profit at resolution time
|
|
|
|
const profitById: Record<string, number> = {}
|
|
|
|
for (const bet of bets) {
|
|
|
|
if (bet.sale) {
|
|
|
|
const originalBet = betsById[bet.sale.betId]
|
|
|
|
const profit = bet.sale.amount - originalBet.amount
|
|
|
|
profitById[bet.id] = profit
|
|
|
|
profitById[originalBet.id] = profit
|
|
|
|
} else {
|
|
|
|
profitById[bet.id] = resolvedPayout(contract, bet) - bet.amount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now find the betId with the highest profit
|
2022-05-22 08:36:05 +00:00
|
|
|
const topBetId = sortBy(bets, (b) => -profitById[b.id])[0]?.id
|
2022-03-21 22:19:42 +00:00
|
|
|
const topBettor = useUserById(betsById[topBetId]?.userId)
|
2022-03-21 01:07:45 +00:00
|
|
|
|
|
|
|
// And also the commentId of the comment with the highest profit
|
2022-05-22 08:36:05 +00:00
|
|
|
const topCommentId = sortBy(
|
2022-04-21 17:09:06 +00:00
|
|
|
comments,
|
|
|
|
(c) => c.betId && -profitById[c.betId]
|
|
|
|
)[0]?.id
|
2022-03-21 01:07:45 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="mt-12 max-w-sm">
|
2022-03-23 19:06:39 +00:00
|
|
|
{topCommentId && profitById[topCommentId] > 0 && (
|
2022-03-21 01:07:45 +00:00
|
|
|
<>
|
|
|
|
<Title text="💬 Proven correct" className="!mt-0" />
|
|
|
|
<div className="relative flex items-start space-x-3 rounded-md bg-gray-50 px-2 py-4">
|
|
|
|
<FeedComment
|
|
|
|
contract={contract}
|
|
|
|
comment={commentsById[topCommentId]}
|
2022-04-29 21:11:04 +00:00
|
|
|
betsBySameUser={[betsById[topCommentId]]}
|
2022-03-21 01:07:45 +00:00
|
|
|
truncate={false}
|
|
|
|
smallAvatar={false}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="mt-2 text-sm text-gray-500">
|
|
|
|
{commentsById[topCommentId].userName} made{' '}
|
|
|
|
{formatMoney(profitById[topCommentId] || 0)}!
|
|
|
|
</div>
|
|
|
|
<Spacer h={16} />
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{/* If they're the same, only show the comment; otherwise show both */}
|
2022-03-23 19:06:39 +00:00
|
|
|
{topBettor && topBetId !== topCommentId && profitById[topBetId] > 0 && (
|
2022-03-21 01:07:45 +00:00
|
|
|
<>
|
|
|
|
<Title text="💸 Smartest money" className="!mt-0" />
|
|
|
|
<div className="relative flex items-start space-x-3 rounded-md bg-gray-50 px-2 py-4">
|
|
|
|
<FeedBet
|
|
|
|
contract={contract}
|
|
|
|
bet={betsById[topBetId]}
|
|
|
|
hideOutcome={false}
|
|
|
|
smallAvatar={false}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="mt-2 text-sm text-gray-500">
|
|
|
|
{topBettor?.name} made {formatMoney(profitById[topBetId] || 0)}!
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
const getOpenGraphProps = (contract: Contract) => {
|
2022-04-29 23:38:31 +00:00
|
|
|
const {
|
|
|
|
resolution,
|
|
|
|
question,
|
|
|
|
creatorName,
|
|
|
|
creatorUsername,
|
|
|
|
outcomeType,
|
|
|
|
creatorAvatarUrl,
|
|
|
|
} = contract
|
2022-02-18 00:34:11 +00:00
|
|
|
const probPercent =
|
|
|
|
outcomeType === 'BINARY' ? getBinaryProbPercent(contract) : undefined
|
2022-02-17 23:00:19 +00:00
|
|
|
|
|
|
|
const description = resolution
|
|
|
|
? `Resolved ${resolution}. ${contract.description}`
|
2022-02-22 20:17:58 +00:00
|
|
|
: probPercent
|
|
|
|
? `${probPercent} chance. ${contract.description}`
|
|
|
|
: contract.description
|
2022-02-17 23:00:19 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
question,
|
|
|
|
probability: probPercent,
|
|
|
|
metadata: contractTextDetails(contract),
|
2022-04-29 23:38:31 +00:00
|
|
|
creatorName,
|
|
|
|
creatorUsername,
|
|
|
|
creatorAvatarUrl,
|
2022-02-17 23:00:19 +00:00
|
|
|
description,
|
|
|
|
}
|
|
|
|
}
|