2021-12-15 07:41:50 +00:00
|
|
|
import Link from 'next/link'
|
|
|
|
import _ from 'lodash'
|
|
|
|
import dayjs from 'dayjs'
|
2021-12-16 03:04:38 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
2022-01-05 18:23:58 +00:00
|
|
|
import clsx from 'clsx'
|
|
|
|
|
2021-12-15 07:41:50 +00:00
|
|
|
import { useUserBets } from '../hooks/use-user-bets'
|
|
|
|
import { Bet } from '../lib/firebase/bets'
|
|
|
|
import { User } from '../lib/firebase/users'
|
2021-12-31 20:13:06 +00:00
|
|
|
import {
|
|
|
|
formatMoney,
|
|
|
|
formatPercent,
|
|
|
|
formatWithCommas,
|
|
|
|
} from '../lib/util/format'
|
2021-12-15 07:41:50 +00:00
|
|
|
import { Col } from './layout/col'
|
2021-12-15 18:41:18 +00:00
|
|
|
import { Spacer } from './layout/spacer'
|
2021-12-17 23:16:42 +00:00
|
|
|
import { Contract, getContractFromId, path } from '../lib/firebase/contracts'
|
2021-12-15 22:57:40 +00:00
|
|
|
import { Row } from './layout/row'
|
2021-12-16 02:11:29 +00:00
|
|
|
import { UserLink } from './user-page'
|
2021-12-16 02:27:09 +00:00
|
|
|
import {
|
|
|
|
calculatePayout,
|
2021-12-24 21:06:01 +00:00
|
|
|
calculateSaleAmount,
|
2021-12-16 02:27:09 +00:00
|
|
|
resolvedPayout,
|
2021-12-24 21:06:01 +00:00
|
|
|
} from '../lib/calculate'
|
2022-01-05 18:23:58 +00:00
|
|
|
import { sellBet } from '../lib/firebase/api-call'
|
2021-12-24 21:06:01 +00:00
|
|
|
import { ConfirmationButton } from './confirmation-button'
|
2022-01-04 05:44:54 +00:00
|
|
|
import { OutcomeLabel, YesLabel, NoLabel, MarketLabel } from './outcome-label'
|
2021-12-15 07:41:50 +00:00
|
|
|
|
|
|
|
export function BetsList(props: { user: User }) {
|
|
|
|
const { user } = props
|
|
|
|
const bets = useUserBets(user?.id ?? '')
|
|
|
|
|
2021-12-16 03:04:38 +00:00
|
|
|
const [contracts, setContracts] = useState<Contract[]>([])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const loadedBets = bets === 'loading' ? [] : bets
|
|
|
|
const contractIds = _.uniq(loadedBets.map((bet) => bet.contractId))
|
|
|
|
|
|
|
|
let disposed = false
|
2021-12-17 23:16:42 +00:00
|
|
|
Promise.all(contractIds.map((id) => getContractFromId(id))).then(
|
|
|
|
(contracts) => {
|
|
|
|
if (!disposed) setContracts(contracts.filter(Boolean) as Contract[])
|
|
|
|
}
|
|
|
|
)
|
2021-12-16 03:04:38 +00:00
|
|
|
|
|
|
|
return () => {
|
|
|
|
disposed = true
|
|
|
|
}
|
|
|
|
}, [bets])
|
|
|
|
|
2021-12-15 07:41:50 +00:00
|
|
|
if (bets === 'loading') {
|
|
|
|
return <></>
|
|
|
|
}
|
|
|
|
|
2021-12-17 23:07:08 +00:00
|
|
|
if (bets.length === 0)
|
2021-12-18 10:28:01 +00:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
You have not made any bets yet.{' '}
|
|
|
|
<Link href="/">
|
|
|
|
<a className="text-green-500 hover:underline hover:decoration-2">
|
|
|
|
Find a prediction market!
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
)
|
2021-12-15 07:41:50 +00:00
|
|
|
|
2021-12-16 04:40:48 +00:00
|
|
|
// Decending creation time.
|
|
|
|
bets.sort((bet1, bet2) => bet2.createdTime - bet1.createdTime)
|
|
|
|
|
2021-12-15 07:41:50 +00:00
|
|
|
const contractBets = _.groupBy(bets, 'contractId')
|
|
|
|
|
2021-12-16 03:04:38 +00:00
|
|
|
const [resolved, unresolved] = _.partition(
|
|
|
|
contracts,
|
|
|
|
(contract) => contract.isResolved
|
|
|
|
)
|
2021-12-24 21:06:01 +00:00
|
|
|
|
2021-12-16 03:04:38 +00:00
|
|
|
const currentBets = _.sumBy(unresolved, (contract) =>
|
2021-12-24 21:06:01 +00:00
|
|
|
_.sumBy(contractBets[contract.id], (bet) => {
|
|
|
|
if (bet.isSold || bet.sale) return 0
|
|
|
|
return bet.amount
|
|
|
|
})
|
2021-12-16 03:04:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const currentBetsValue = _.sumBy(unresolved, (contract) =>
|
2021-12-24 21:06:01 +00:00
|
|
|
_.sumBy(contractBets[contract.id], (bet) => {
|
|
|
|
if (bet.isSold || bet.sale) return 0
|
2022-01-04 05:44:54 +00:00
|
|
|
return calculatePayout(contract, bet, 'MKT')
|
2021-12-24 21:06:01 +00:00
|
|
|
})
|
2021-12-16 03:04:38 +00:00
|
|
|
)
|
|
|
|
|
2021-12-15 07:41:50 +00:00
|
|
|
return (
|
2021-12-18 10:37:26 +00:00
|
|
|
<Col className="mt-6 gap-6">
|
2021-12-16 09:09:36 +00:00
|
|
|
<Row className="gap-8">
|
2021-12-16 03:04:38 +00:00
|
|
|
<Col>
|
2021-12-24 21:06:01 +00:00
|
|
|
<div className="text-sm text-gray-500">Currently invested</div>
|
2021-12-16 03:04:38 +00:00
|
|
|
<div>{formatMoney(currentBets)}</div>
|
|
|
|
</Col>
|
|
|
|
<Col>
|
|
|
|
<div className="text-sm text-gray-500">Current value</div>
|
|
|
|
<div>{formatMoney(currentBetsValue)}</div>
|
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
|
|
|
|
{[...unresolved, ...resolved].map((contract) => (
|
2021-12-15 22:57:40 +00:00
|
|
|
<MyContractBets
|
2021-12-16 03:04:38 +00:00
|
|
|
key={contract.id}
|
|
|
|
contract={contract}
|
2021-12-16 21:28:56 +00:00
|
|
|
bets={contractBets[contract.id] ?? []}
|
2021-12-15 07:41:50 +00:00
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-16 03:04:38 +00:00
|
|
|
function MyContractBets(props: { contract: Contract; bets: Bet[] }) {
|
|
|
|
const { bets, contract } = props
|
2021-12-15 22:57:40 +00:00
|
|
|
const { resolution } = contract
|
|
|
|
|
2021-12-16 21:22:00 +00:00
|
|
|
const [collapsed, setCollapsed] = useState(true)
|
|
|
|
|
2021-12-15 07:41:50 +00:00
|
|
|
return (
|
2021-12-16 21:22:00 +00:00
|
|
|
<div
|
|
|
|
tabIndex={0}
|
|
|
|
className={clsx(
|
2021-12-19 00:11:40 +00:00
|
|
|
'p-6 bg-white card card-body shadow-xl collapse collapse-arrow cursor-pointer relative',
|
2021-12-17 04:27:22 +00:00
|
|
|
collapsed ? 'collapse-close' : 'collapse-open pb-2'
|
2021-12-16 21:22:00 +00:00
|
|
|
)}
|
|
|
|
onClick={() => setCollapsed((collapsed) => !collapsed)}
|
|
|
|
>
|
2021-12-16 21:51:47 +00:00
|
|
|
<Row className="flex-wrap gap-4">
|
2021-12-16 21:22:00 +00:00
|
|
|
<Col className="flex-[2] gap-1">
|
2021-12-20 04:06:30 +00:00
|
|
|
<Row className="mr-6">
|
2021-12-16 21:22:00 +00:00
|
|
|
<Link href={path(contract)}>
|
2021-12-16 21:28:56 +00:00
|
|
|
<a
|
|
|
|
className="font-medium text-indigo-700 hover:underline hover:decoration-indigo-400 hover:decoration-2"
|
|
|
|
onClick={(e) => e.stopPropagation()}
|
|
|
|
>
|
2021-12-16 09:03:41 +00:00
|
|
|
{contract.question}
|
2021-12-16 21:22:00 +00:00
|
|
|
</a>
|
|
|
|
</Link>
|
2021-12-18 23:40:39 +00:00
|
|
|
|
|
|
|
{/* Show carrot for collapsing. Hack the positioning. */}
|
|
|
|
<div
|
2021-12-19 00:11:40 +00:00
|
|
|
className="collapse-title p-0 absolute w-0 h-0 min-h-0"
|
|
|
|
style={{ top: -10, right: 4 }}
|
2021-12-18 23:40:39 +00:00
|
|
|
/>
|
|
|
|
</Row>
|
2021-12-16 09:03:41 +00:00
|
|
|
|
|
|
|
<Row className="gap-2 text-gray-500 text-sm">
|
2021-12-16 02:04:36 +00:00
|
|
|
<div>
|
2021-12-18 07:27:29 +00:00
|
|
|
<UserLink username={contract.creatorUsername} />
|
2021-12-16 02:04:36 +00:00
|
|
|
</div>
|
2021-12-16 09:03:41 +00:00
|
|
|
{resolution && (
|
|
|
|
<>
|
|
|
|
<div>•</div>
|
2021-12-16 21:51:47 +00:00
|
|
|
<div className="whitespace-nowrap">
|
2021-12-16 21:22:00 +00:00
|
|
|
Resolved <OutcomeLabel outcome={resolution} />
|
2021-12-16 09:03:41 +00:00
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Row>
|
|
|
|
</Col>
|
2021-12-15 22:57:40 +00:00
|
|
|
|
2021-12-18 23:40:39 +00:00
|
|
|
<MyBetsSummary
|
2021-12-20 04:06:30 +00:00
|
|
|
className="flex-1 justify-end mr-5 sm:mr-8"
|
2021-12-18 23:40:39 +00:00
|
|
|
contract={contract}
|
|
|
|
bets={bets}
|
|
|
|
/>
|
2021-12-16 09:03:41 +00:00
|
|
|
</Row>
|
2021-12-15 22:57:40 +00:00
|
|
|
|
2021-12-19 05:27:52 +00:00
|
|
|
<div
|
|
|
|
className="collapse-content !px-0"
|
|
|
|
style={{ backgroundColor: 'white' }}
|
|
|
|
>
|
2021-12-16 21:22:00 +00:00
|
|
|
<Spacer h={8} />
|
2021-12-15 22:57:40 +00:00
|
|
|
|
2021-12-16 21:22:00 +00:00
|
|
|
<ContractBetsTable contract={contract} bets={bets} />
|
|
|
|
</div>
|
2021-12-15 07:41:50 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-16 04:30:24 +00:00
|
|
|
export function MyBetsSummary(props: {
|
|
|
|
contract: Contract
|
|
|
|
bets: Bet[]
|
2022-01-04 05:44:54 +00:00
|
|
|
showMKT?: boolean
|
2021-12-16 04:30:24 +00:00
|
|
|
className?: string
|
|
|
|
}) {
|
2022-01-04 05:44:54 +00:00
|
|
|
const { bets, contract, showMKT, className } = props
|
2021-12-16 04:30:24 +00:00
|
|
|
const { resolution } = contract
|
|
|
|
|
2021-12-24 21:06:01 +00:00
|
|
|
const excludeSales = bets.filter((b) => !b.isSold && !b.sale)
|
|
|
|
const betsTotal = _.sumBy(excludeSales, (bet) => bet.amount)
|
2021-12-16 04:30:24 +00:00
|
|
|
|
|
|
|
const betsPayout = resolution
|
2022-01-02 06:27:58 +00:00
|
|
|
? _.sumBy(excludeSales, (bet) => resolvedPayout(contract, bet))
|
2021-12-16 04:30:24 +00:00
|
|
|
: 0
|
|
|
|
|
2021-12-24 21:06:01 +00:00
|
|
|
const yesWinnings = _.sumBy(excludeSales, (bet) =>
|
2021-12-16 04:30:24 +00:00
|
|
|
calculatePayout(contract, bet, 'YES')
|
|
|
|
)
|
2021-12-24 21:06:01 +00:00
|
|
|
const noWinnings = _.sumBy(excludeSales, (bet) =>
|
2021-12-16 04:30:24 +00:00
|
|
|
calculatePayout(contract, bet, 'NO')
|
|
|
|
)
|
|
|
|
|
2022-01-04 05:44:54 +00:00
|
|
|
const marketWinnings = _.sumBy(excludeSales, (bet) =>
|
|
|
|
calculatePayout(contract, bet, 'MKT')
|
|
|
|
)
|
|
|
|
|
2021-12-16 04:30:24 +00:00
|
|
|
return (
|
2022-01-04 05:44:54 +00:00
|
|
|
<Row
|
|
|
|
className={clsx(
|
|
|
|
'gap-4 sm:gap-6',
|
|
|
|
showMKT && 'flex-wrap sm:flex-nowrap',
|
|
|
|
className
|
|
|
|
)}
|
|
|
|
>
|
2021-12-16 04:30:24 +00:00
|
|
|
<Col>
|
2021-12-24 21:29:15 +00:00
|
|
|
<div className="text-sm text-gray-500 whitespace-nowrap">Invested</div>
|
2021-12-16 21:51:47 +00:00
|
|
|
<div className="whitespace-nowrap">{formatMoney(betsTotal)}</div>
|
2021-12-16 04:30:24 +00:00
|
|
|
</Col>
|
|
|
|
{resolution ? (
|
2022-01-04 05:44:54 +00:00
|
|
|
<Col>
|
|
|
|
<div className="text-sm text-gray-500">Payout</div>
|
|
|
|
<div className="whitespace-nowrap">{formatMoney(betsPayout)}</div>
|
|
|
|
</Col>
|
2021-12-16 04:30:24 +00:00
|
|
|
) : (
|
2022-01-04 05:44:54 +00:00
|
|
|
<Row className="gap-4 sm:gap-6">
|
2021-12-16 04:30:24 +00:00
|
|
|
<Col>
|
2021-12-16 21:51:47 +00:00
|
|
|
<div className="text-sm text-gray-500 whitespace-nowrap">
|
2021-12-16 21:35:06 +00:00
|
|
|
Payout if <YesLabel />
|
2021-12-16 04:30:24 +00:00
|
|
|
</div>
|
2021-12-16 21:51:47 +00:00
|
|
|
<div className="whitespace-nowrap">{formatMoney(yesWinnings)}</div>
|
2021-12-16 04:30:24 +00:00
|
|
|
</Col>
|
|
|
|
<Col>
|
2021-12-16 21:51:47 +00:00
|
|
|
<div className="text-sm text-gray-500 whitespace-nowrap">
|
2021-12-16 21:35:06 +00:00
|
|
|
Payout if <NoLabel />
|
2021-12-16 04:30:24 +00:00
|
|
|
</div>
|
2021-12-16 21:51:47 +00:00
|
|
|
<div className="whitespace-nowrap">{formatMoney(noWinnings)}</div>
|
2021-12-16 04:30:24 +00:00
|
|
|
</Col>
|
2022-01-04 05:44:54 +00:00
|
|
|
{showMKT && (
|
|
|
|
<Col>
|
|
|
|
<div className="text-sm text-gray-500 whitespace-nowrap">
|
|
|
|
Payout if <MarketLabel />
|
|
|
|
</div>
|
|
|
|
<div className="whitespace-nowrap">
|
|
|
|
{formatMoney(marketWinnings)}
|
|
|
|
</div>
|
|
|
|
</Col>
|
|
|
|
)}
|
|
|
|
</Row>
|
2021-12-16 04:30:24 +00:00
|
|
|
)}
|
|
|
|
</Row>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-16 21:22:00 +00:00
|
|
|
export function ContractBetsTable(props: {
|
|
|
|
contract: Contract
|
|
|
|
bets: Bet[]
|
|
|
|
className?: string
|
|
|
|
}) {
|
|
|
|
const { contract, bets, className } = props
|
2021-12-15 22:57:40 +00:00
|
|
|
|
2021-12-24 21:06:01 +00:00
|
|
|
const [sales, buys] = _.partition(bets, (bet) => bet.sale)
|
|
|
|
const salesDict = _.fromPairs(
|
|
|
|
sales.map((sale) => [sale.sale?.betId ?? '', sale])
|
|
|
|
)
|
|
|
|
|
2021-12-16 02:27:09 +00:00
|
|
|
const { isResolved } = contract
|
|
|
|
|
2021-12-15 22:57:40 +00:00
|
|
|
return (
|
2021-12-16 21:22:00 +00:00
|
|
|
<div className={clsx('overflow-x-auto', className)}>
|
2021-12-15 22:57:40 +00:00
|
|
|
<table className="table table-zebra table-compact text-gray-500 w-full">
|
|
|
|
<thead>
|
|
|
|
<tr className="p-2">
|
2021-12-15 23:27:02 +00:00
|
|
|
<th>Date</th>
|
2021-12-15 22:57:40 +00:00
|
|
|
<th>Outcome</th>
|
2021-12-24 21:06:01 +00:00
|
|
|
<th>Amount</th>
|
2021-12-15 22:57:40 +00:00
|
|
|
<th>Probability</th>
|
2021-12-31 20:13:06 +00:00
|
|
|
<th>Shares</th>
|
2022-01-03 06:57:22 +00:00
|
|
|
<th>{isResolved ? <>Payout</> : <>Sale price</>}</th>
|
2021-12-24 21:06:01 +00:00
|
|
|
<th></th>
|
2021-12-15 22:57:40 +00:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
2021-12-24 21:06:01 +00:00
|
|
|
{buys.map((bet) => (
|
|
|
|
<BetRow
|
|
|
|
key={bet.id}
|
|
|
|
bet={bet}
|
|
|
|
sale={salesDict[bet.id]}
|
|
|
|
contract={contract}
|
|
|
|
/>
|
2021-12-15 22:57:40 +00:00
|
|
|
))}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-12-24 21:06:01 +00:00
|
|
|
function BetRow(props: { bet: Bet; contract: Contract; sale?: Bet }) {
|
|
|
|
const { bet, sale, contract } = props
|
|
|
|
const {
|
|
|
|
amount,
|
|
|
|
outcome,
|
|
|
|
createdTime,
|
|
|
|
probBefore,
|
|
|
|
probAfter,
|
|
|
|
shares,
|
|
|
|
isSold,
|
|
|
|
} = bet
|
2021-12-16 02:27:09 +00:00
|
|
|
const { isResolved } = contract
|
2021-12-15 07:41:50 +00:00
|
|
|
|
|
|
|
return (
|
2021-12-15 18:41:18 +00:00
|
|
|
<tr>
|
2021-12-24 21:06:01 +00:00
|
|
|
<td>{dayjs(createdTime).format('MMM D, h:mma')}</td>
|
2021-12-16 21:22:00 +00:00
|
|
|
<td>
|
|
|
|
<OutcomeLabel outcome={outcome} />
|
|
|
|
</td>
|
2021-12-15 18:41:18 +00:00
|
|
|
<td>{formatMoney(amount)}</td>
|
|
|
|
<td>
|
|
|
|
{formatPercent(probBefore)} → {formatPercent(probAfter)}
|
|
|
|
</td>
|
2021-12-31 20:13:06 +00:00
|
|
|
<td>{formatWithCommas(shares)}</td>
|
2021-12-16 02:27:09 +00:00
|
|
|
<td>
|
2022-01-03 18:56:02 +00:00
|
|
|
{sale ? (
|
2022-01-04 05:05:41 +00:00
|
|
|
<>{formatMoney(Math.abs(sale.amount))} (sold)</>
|
2022-01-03 18:56:02 +00:00
|
|
|
) : (
|
|
|
|
formatMoney(
|
|
|
|
isResolved
|
|
|
|
? resolvedPayout(contract, bet)
|
|
|
|
: calculateSaleAmount(contract, bet)
|
|
|
|
)
|
|
|
|
)}
|
2021-12-16 02:27:09 +00:00
|
|
|
</td>
|
2021-12-24 21:06:01 +00:00
|
|
|
|
2022-01-03 18:56:02 +00:00
|
|
|
{!isResolved && !isSold && (
|
|
|
|
<td className="text-neutral">
|
|
|
|
<SellButton contract={contract} bet={bet} />
|
|
|
|
</td>
|
2021-12-24 21:06:01 +00:00
|
|
|
)}
|
2021-12-15 18:41:18 +00:00
|
|
|
</tr>
|
2021-12-15 07:41:50 +00:00
|
|
|
)
|
|
|
|
}
|
2021-12-16 02:27:09 +00:00
|
|
|
|
2021-12-24 21:06:01 +00:00
|
|
|
function SellButton(props: { contract: Contract; bet: Bet }) {
|
2022-01-05 18:23:58 +00:00
|
|
|
useEffect(() => {
|
|
|
|
// warm up cloud function
|
|
|
|
sellBet({}).catch()
|
|
|
|
}, [])
|
|
|
|
|
2021-12-24 21:06:01 +00:00
|
|
|
const { contract, bet } = props
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ConfirmationButton
|
|
|
|
id={`sell-${bet.id}`}
|
|
|
|
openModelBtn={{
|
|
|
|
className: clsx('btn-sm', isSubmitting && 'btn-disabled loading'),
|
|
|
|
label: 'Sell',
|
|
|
|
}}
|
|
|
|
submitBtn={{ className: 'btn-primary' }}
|
|
|
|
onSubmit={async () => {
|
|
|
|
setIsSubmitting(true)
|
|
|
|
await sellBet({ contractId: contract.id, betId: bet.id })
|
|
|
|
setIsSubmitting(false)
|
|
|
|
}}
|
|
|
|
>
|
2022-01-03 06:57:22 +00:00
|
|
|
<div className="text-2xl mb-4">
|
|
|
|
Sell <OutcomeLabel outcome={bet.outcome} />
|
|
|
|
</div>
|
2021-12-24 21:06:01 +00:00
|
|
|
<div>
|
2022-01-03 06:57:22 +00:00
|
|
|
Do you want to sell {formatWithCommas(bet.shares)} shares of{' '}
|
2021-12-24 21:06:01 +00:00
|
|
|
<OutcomeLabel outcome={bet.outcome} /> for{' '}
|
|
|
|
{formatMoney(calculateSaleAmount(contract, bet))}?
|
|
|
|
</div>
|
|
|
|
</ConfirmationButton>
|
|
|
|
)
|
|
|
|
}
|