import Link from 'next/link' import _ from 'lodash' import dayjs from 'dayjs' import { useEffect, useState } from 'react' import { useUserBets } from '../hooks/use-user-bets' import { Bet } from '../lib/firebase/bets' import { User } from '../lib/firebase/users' import { formatMoney, formatPercent } from '../lib/util/format' import { Col } from './layout/col' import { Spacer } from './layout/spacer' import { Contract, getContractFromId, path } from '../lib/firebase/contracts' import { Row } from './layout/row' import { UserLink } from './user-page' import { calculatePayout, currentValue, resolvedPayout, } from '../lib/calculation/contract' import clsx from 'clsx' export function BetsList(props: { user: User }) { const { user } = props const bets = useUserBets(user?.id ?? '') const [contracts, setContracts] = useState([]) useEffect(() => { const loadedBets = bets === 'loading' ? [] : bets const contractIds = _.uniq(loadedBets.map((bet) => bet.contractId)) let disposed = false Promise.all(contractIds.map((id) => getContractFromId(id))).then( (contracts) => { if (!disposed) setContracts(contracts.filter(Boolean) as Contract[]) } ) return () => { disposed = true } }, [bets]) if (bets === 'loading') { return <> } if (bets.length === 0) return (
You have not made any bets yet.{' '} Find a prediction market!
) // Decending creation time. bets.sort((bet1, bet2) => bet2.createdTime - bet1.createdTime) const contractBets = _.groupBy(bets, 'contractId') const [resolved, unresolved] = _.partition( contracts, (contract) => contract.isResolved ) const currentBets = _.sumBy(unresolved, (contract) => _.sumBy(contractBets[contract.id], (bet) => bet.amount) ) const currentBetsValue = _.sumBy(unresolved, (contract) => _.sumBy(contractBets[contract.id], (bet) => currentValue(contract, bet)) ) return (
Active bets
{formatMoney(currentBets)}
Current value
{formatMoney(currentBetsValue)}
{[...unresolved, ...resolved].map((contract) => ( ))} ) } function MyContractBets(props: { contract: Contract; bets: Bet[] }) { const { bets, contract } = props const { resolution } = contract const [collapsed, setCollapsed] = useState(true) return (
setCollapsed((collapsed) => !collapsed)} >
e.stopPropagation()} > {contract.question}
{resolution && ( <>
Resolved
)}
{/* Show carrot for collapsing. Hack the positioning. */}
) } export function MyBetsSummary(props: { contract: Contract bets: Bet[] className?: string }) { const { bets, contract, className } = props const { resolution } = contract const betsTotal = _.sumBy(bets, (bet) => bet.amount) const betsPayout = resolution ? _.sumBy(bets, (bet) => resolvedPayout(contract, bet)) : 0 const yesWinnings = _.sumBy(bets, (bet) => calculatePayout(contract, bet, 'YES') ) const noWinnings = _.sumBy(bets, (bet) => calculatePayout(contract, bet, 'NO') ) return (
Total bets
{formatMoney(betsTotal)}
{resolution ? ( <>
Winnings
{formatMoney(betsPayout)}
) : ( <>
Payout if
{formatMoney(yesWinnings)}
Payout if
{formatMoney(noWinnings)}
)}
) } export function ContractBetsTable(props: { contract: Contract bets: Bet[] className?: string }) { const { contract, bets, className } = props const { isResolved } = contract return (
{!isResolved && } {bets.map((bet) => ( ))}
Date Outcome Bet ProbabilityEst. max payout{isResolved ? <>Payout : <>Current value}
) } function BetRow(props: { bet: Bet; contract: Contract }) { const { bet, contract } = props const { amount, outcome, createdTime, probBefore, probAfter, dpmWeight } = bet const { isResolved } = contract return ( {dayjs(createdTime).format('MMM D, H:mma')} {formatMoney(amount)} {formatPercent(probBefore)} → {formatPercent(probAfter)} {!isResolved && {formatMoney(amount + dpmWeight)}} {formatMoney( isResolved ? resolvedPayout(contract, bet) : currentValue(contract, bet) )} ) } function OutcomeLabel(props: { outcome: 'YES' | 'NO' | 'CANCEL' }) { const { outcome } = props if (outcome === 'YES') return if (outcome === 'NO') return return } function YesLabel() { return YES } function NoLabel() { return NO } function CancelLabel() { return CANCEL }