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, getContract, path } from '../lib/firebase/contracts' import { Row } from './layout/row' import { UserLink } from './user-page' import { calculatePayout, currentValue, resolvedPayout, } from '../lib/calculation/contract' 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) => getContract(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!
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 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 (
{contract.question}
{resolution && ( <>
Resolved {resolution === 'YES' && } {resolution === 'NO' && } {resolution === 'CANCEL' && }
)}
Total bet
{formatMoney(betsTotal)}
{resolution ? ( <>
Winnings
{formatMoney(betsPayout)}
) : ( <>
If
{formatMoney(yesWinnings)}
If
{formatMoney(noWinnings)}
)}
) } function ContractBetsTable(props: { contract: Contract; bets: Bet[] }) { const { contract, bets } = 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')} {outcome} {formatMoney(amount)} {formatPercent(probBefore)} → {formatPercent(probAfter)} {!isResolved && {formatMoney(amount + dpmWeight)}} {formatMoney( isResolved ? resolvedPayout(contract, bet) : currentValue(contract, bet) )} ) } function YesLabel() { return YES } function NoLabel() { return NO } function CancelLabel() { return CANCEL }