import { sumBy } from 'lodash' import clsx from 'clsx' import { Bet } from 'web/lib/firebase/bets' import { formatMoney, formatWithCommas } from 'common/util/format' import { Col } from './layout/col' import { Contract } from 'web/lib/firebase/contracts' import { Row } from './layout/row' import { YesLabel, NoLabel } from './outcome-label' import { calculatePayout, getContractBetMetrics, getProbability, } from 'common/calculate' import { InfoTooltip } from './info-tooltip' import { ProfitBadge } from './profit-badge' export function BetsSummary(props: { contract: Contract userBets: Bet[] className?: string }) { const { contract, className } = props const { resolution, outcomeType } = contract const isBinary = outcomeType === 'BINARY' const bets = props.userBets.filter((b) => !b.isAnte) const { profitPercent, payout, profit, invested, hasShares } = getContractBetMetrics(contract, bets) const excludeSales = bets.filter((b) => !b.isSold && !b.sale) const yesWinnings = sumBy(excludeSales, (bet) => calculatePayout(contract, bet, 'YES') ) const noWinnings = sumBy(excludeSales, (bet) => calculatePayout(contract, bet, 'NO') ) const position = yesWinnings - noWinnings const outcome = hasShares ? (position > 0 ? 'YES' : 'NO') : undefined const prob = isBinary ? getProbability(contract) : 0 const expectation = prob * yesWinnings + (1 - prob) * noWinnings if (bets.length === 0) return <> return ( {resolution ? (
Payout
{formatMoney(payout)}{' '}
) : isBinary ? (
Position{' '}
{position > 1e-7 ? ( <> {formatWithCommas(position)} ) : position < -1e-7 ? ( <> {formatWithCommas(-position)} ) : ( '——' )}
) : (
Expectation{''}
{formatMoney(payout)}
)}
Invested{' '}
{formatMoney(invested)}
{isBinary && !resolution && (
Expectation{' '}
{formatMoney(expectation)}
)}
Profit{' '}
{formatMoney(profit)}
) }