import Link from 'next/link' import _ from 'lodash' import dayjs from 'dayjs' import { useEffect, useState } from 'react' import clsx from 'clsx' import { useUserBets } from '../hooks/use-user-bets' import { Bet } from '../lib/firebase/bets' import { User } from '../lib/firebase/users' import { formatMoney, formatPercent, formatWithCommas, } from '../../common/util/format' import { Col } from './layout/col' import { Spacer } from './layout/spacer' import { Contract, getContractFromId, contractPath, } from '../lib/firebase/contracts' import { Row } from './layout/row' import { UserLink } from './user-page' import { calculatePayout, calculateSaleAmount, resolvedPayout, } from '../../common/calculate' import { sellBet } from '../lib/firebase/api-call' import { ConfirmationButton } from './confirmation-button' import { OutcomeLabel, YesLabel, NoLabel, MarketLabel } from './outcome-label' export function BetsList(props: { user: User }) { const { user } = props const bets = useUserBets(user.id) const [contracts, setContracts] = useState([]) useEffect(() => { const loadedBets = bets ? 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) { 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) => { if (bet.isSold || bet.sale) return 0 return bet.amount }) ) const currentBetsValue = _.sumBy(unresolved, (contract) => _.sumBy(contractBets[contract.id], (bet) => { if (bet.isSold || bet.sale) return 0 return calculatePayout(contract, bet, 'MKT') }) ) return (
Currently invested
{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} {/* Show carrot for collapsing. Hack the positioning. */}
{resolution && ( <>
Resolved
)}
) } export function MyBetsSummary(props: { contract: Contract bets: Bet[] showMKT?: boolean className?: string }) { const { bets, contract, showMKT, className } = props const { resolution } = contract const excludeSales = bets.filter((b) => !b.isSold && !b.sale) const betsTotal = _.sumBy(excludeSales, (bet) => bet.amount) const betsPayout = resolution ? _.sumBy(excludeSales, (bet) => resolvedPayout(contract, bet)) : 0 const yesWinnings = _.sumBy(excludeSales, (bet) => calculatePayout(contract, bet, 'YES') ) const noWinnings = _.sumBy(excludeSales, (bet) => calculatePayout(contract, bet, 'NO') ) const marketWinnings = _.sumBy(excludeSales, (bet) => calculatePayout(contract, bet, 'MKT') ) return (
Invested
{formatMoney(betsTotal)}
{resolution ? (
Payout
{formatMoney(betsPayout)}
) : (
Payout if
{formatMoney(yesWinnings)}
Payout if
{formatMoney(noWinnings)}
{showMKT && (
Payout if
{formatMoney(marketWinnings)}
)}
)}
) } export function ContractBetsTable(props: { contract: Contract bets: Bet[] className?: string }) { const { contract, bets, className } = props const [sales, buys] = _.partition(bets, (bet) => bet.sale) const salesDict = _.fromPairs( sales.map((sale) => [sale.sale?.betId ?? '', sale]) ) const { isResolved } = contract return (
{buys.map((bet) => ( ))}
Date Outcome Amount Probability Shares {isResolved ? <>Payout : <>Sale price}
) } function BetRow(props: { bet: Bet; contract: Contract; saleBet?: Bet }) { const { bet, saleBet, contract } = props const { amount, outcome, createdTime, probBefore, probAfter, shares, isSold, isAnte, } = bet const { isResolved, closeTime } = contract const isClosed = closeTime && Date.now() > closeTime const saleAmount = saleBet?.sale?.amount const saleDisplay = bet.isAnte ? ( 'ANTE' ) : saleAmount !== undefined ? ( <>{formatMoney(saleAmount)} (sold) ) : ( formatMoney( isResolved ? resolvedPayout(contract, bet) : calculateSaleAmount(contract, bet) ) ) return ( {dayjs(createdTime).format('MMM D, h:mma')} {formatMoney(amount)} {formatPercent(probBefore)} → {formatPercent(probAfter)} {formatWithCommas(shares)} {saleDisplay} {!isResolved && !isClosed && !isSold && !isAnte && ( )} ) } function SellButton(props: { contract: Contract; bet: Bet }) { useEffect(() => { // warm up cloud function sellBet({}).catch() }, []) const { contract, bet } = props const [isSubmitting, setIsSubmitting] = useState(false) return ( { setIsSubmitting(true) await sellBet({ contractId: contract.id, betId: bet.id }) setIsSubmitting(false) }} >
Sell
Do you want to sell {formatWithCommas(bet.shares)} shares of{' '} for{' '} {formatMoney(calculateSaleAmount(contract, bet))}?
) }