import Link from 'next/link' import _ from 'lodash' import dayjs from 'dayjs' import { useContract } from '../hooks/use-contract' 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, 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 ?? '') if (bets === 'loading') { return <> } if (bets.length === 0) return
You have not made any bets yet!
const contractBets = _.groupBy(bets, 'contractId') return ( {Object.keys(contractBets).map((contractId) => ( ))} ) } function MyContractBets(props: { contractId: string; bets: Bet[] }) { const { contractId, bets } = props const contract = useContract(contractId) if (contract === 'loading' || contract === null) return <> 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 bets
{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 }