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 { calculateWinnings, currentValue } 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 betsValue = _.sumBy(bets, (bet) => currentValue(contract, bet)) const yesWinnings = _.sumBy(bets, (bet) => calculateWinnings(contract, bet, 'YES') ) const noWinnings = _.sumBy(bets, (bet) => calculateWinnings(contract, bet, 'NO') ) return (
{contract.question}
By {contract.creatorName}
{resolution &&
} {resolution === 'YES' && (
Resolved YES
)} {resolution === 'NO' && (
Resolved NO
)} {resolution === 'CANCEL' && (
Resolved CANCEL
)}
Total bets
{formatMoney(betsTotal)}
{resolution ? ( <>
Winnings
{formatMoney(yesWinnings)}
) : ( <> {/*
Current value
{formatMoney(betsValue)}
*/}
If YES
{formatMoney(yesWinnings)}
If NO
{formatMoney(noWinnings)}
)}
) } function ContractBetsTable(props: { contract: Contract; bets: Bet[] }) { const { contract, bets } = props return (
{bets.map((bet) => ( ))}
Date Outcome Bet Probability Est. max payout Current value
) } function BetRow(props: { bet: Bet; contract: Contract }) { const { bet, contract } = props const { amount, outcome, createdTime, probBefore, probAfter, dpmWeight } = bet return ( {dayjs(createdTime).format('MMM D, H:mma')} {outcome} {formatMoney(amount)} {formatPercent(probBefore)} → {formatPercent(probAfter)} {formatMoney(amount + dpmWeight)} {formatMoney(currentValue(contract, bet))} ) }