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,
|
2022-01-30 21:51:30 +00:00
|
|
|
} from '../../common/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'
|
2022-01-12 19:01:04 +00:00
|
|
|
import {
|
|
|
|
Contract,
|
|
|
|
getContractFromId,
|
|
|
|
contractPath,
|
2022-02-17 23:00:19 +00:00
|
|
|
getBinaryProbPercent,
|
2022-01-12 19:01:04 +00:00
|
|
|
} 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'
|
2022-03-15 22:27:51 +00:00
|
|
|
import { sellBet } from '../lib/firebase/api-call'
|
|
|
|
import { ConfirmationButton } from './confirmation-button'
|
|
|
|
import { OutcomeLabel, YesLabel, NoLabel } from './outcome-label'
|
|
|
|
import { filterDefined } from '../../common/util/array'
|
|
|
|
import { LoadingIndicator } from './loading-indicator'
|
|
|
|
import { SiteLink } from './site-link'
|
2021-12-16 02:27:09 +00:00
|
|
|
import {
|
|
|
|
calculatePayout,
|
2021-12-24 21:06:01 +00:00
|
|
|
calculateSaleAmount,
|
2022-02-18 00:24:00 +00:00
|
|
|
getOutcomeProbability,
|
2022-02-14 02:20:46 +00:00
|
|
|
getProbability,
|
2022-02-18 00:24:00 +00:00
|
|
|
getProbabilityAfterSale,
|
2022-04-03 19:48:53 +00:00
|
|
|
getContractBetMetrics,
|
2021-12-16 02:27:09 +00:00
|
|
|
resolvedPayout,
|
2022-04-03 19:48:53 +00:00
|
|
|
getContractBetNullMetrics,
|
2022-01-10 21:07:57 +00:00
|
|
|
} from '../../common/calculate'
|
2021-12-15 07:41:50 +00:00
|
|
|
|
2022-03-21 02:30:04 +00:00
|
|
|
type BetSort = 'newest' | 'profit' | 'resolutionTime' | 'value' | 'closeTime'
|
2022-03-13 20:55:42 +00:00
|
|
|
type BetFilter = 'open' | 'closed' | 'resolved' | 'all'
|
2022-02-09 05:28:20 +00:00
|
|
|
|
2021-12-15 07:41:50 +00:00
|
|
|
export function BetsList(props: { user: User }) {
|
|
|
|
const { user } = props
|
2022-02-04 00:13:04 +00:00
|
|
|
const bets = useUserBets(user.id)
|
2021-12-15 07:41:50 +00:00
|
|
|
|
2022-02-21 04:26:22 +00:00
|
|
|
const [contracts, setContracts] = useState<Contract[] | undefined>()
|
2021-12-16 03:04:38 +00:00
|
|
|
|
2022-02-17 22:44:23 +00:00
|
|
|
const [sort, setSort] = useState<BetSort>('value')
|
2022-03-13 20:55:42 +00:00
|
|
|
const [filter, setFilter] = useState<BetFilter>('open')
|
2022-02-09 05:28:20 +00:00
|
|
|
|
2021-12-16 03:04:38 +00:00
|
|
|
useEffect(() => {
|
2022-02-21 04:26:22 +00:00
|
|
|
if (bets) {
|
|
|
|
const contractIds = _.uniq(bets.map((bet) => bet.contractId))
|
2021-12-16 03:04:38 +00:00
|
|
|
|
2022-02-21 04:26:22 +00:00
|
|
|
let disposed = false
|
|
|
|
Promise.all(contractIds.map((id) => getContractFromId(id))).then(
|
|
|
|
(contracts) => {
|
|
|
|
if (!disposed) setContracts(filterDefined(contracts))
|
|
|
|
}
|
|
|
|
)
|
2021-12-16 03:04:38 +00:00
|
|
|
|
2022-02-21 04:26:22 +00:00
|
|
|
return () => {
|
|
|
|
disposed = true
|
|
|
|
}
|
2021-12-16 03:04:38 +00:00
|
|
|
}
|
|
|
|
}, [bets])
|
|
|
|
|
2022-02-21 04:26:22 +00:00
|
|
|
if (!bets || !contracts) {
|
|
|
|
return <LoadingIndicator />
|
2021-12-15 07:41:50 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 22:55:06 +00:00
|
|
|
if (bets.length === 0) return <NoBets />
|
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')
|
2022-03-13 20:55:42 +00:00
|
|
|
const contractsById = _.fromPairs(contracts.map((c) => [c.id, c]))
|
2021-12-15 07:41:50 +00:00
|
|
|
|
2022-04-03 19:48:53 +00:00
|
|
|
const contractsMetrics = _.mapValues(contractBets, (bets, contractId) => {
|
|
|
|
const contract = contractsById[contractId]
|
|
|
|
if (!contract) return getContractBetNullMetrics()
|
|
|
|
return getContractBetMetrics(contract, bets)
|
2022-02-09 05:28:20 +00:00
|
|
|
})
|
|
|
|
|
2022-03-13 20:55:42 +00:00
|
|
|
const FILTERS: Record<BetFilter, (c: Contract) => boolean> = {
|
|
|
|
resolved: (c) => !!c.resolutionTime,
|
|
|
|
closed: (c) =>
|
|
|
|
!FILTERS.resolved(c) && (c.closeTime ?? Infinity) < Date.now(),
|
|
|
|
open: (c) => !(FILTERS.closed(c) || FILTERS.resolved(c)),
|
|
|
|
all: () => true,
|
|
|
|
// Pepe notes: most users want "settled", to see when their bets or sold; or "realized profit"
|
|
|
|
}
|
|
|
|
const SORTS: Record<BetSort, (c: Contract) => number> = {
|
2022-04-03 19:48:53 +00:00
|
|
|
profit: (c) => contractsMetrics[c.id].profit,
|
|
|
|
value: (c) => contractsMetrics[c.id].totalValue,
|
2022-03-13 20:55:42 +00:00
|
|
|
newest: (c) =>
|
|
|
|
Math.max(...contractBets[c.id].map((bet) => bet.createdTime)),
|
2022-03-21 02:30:04 +00:00
|
|
|
resolutionTime: (c) => -(c.resolutionTime ?? c.closeTime ?? Infinity),
|
|
|
|
closeTime: (c) => -(c.closeTime ?? Infinity),
|
2022-03-13 20:55:42 +00:00
|
|
|
}
|
|
|
|
const displayedContracts = _.sortBy(contracts, SORTS[sort])
|
|
|
|
.reverse()
|
|
|
|
.filter(FILTERS[filter])
|
2022-02-09 05:28:20 +00:00
|
|
|
|
2022-02-26 00:57:28 +00:00
|
|
|
const [settled, unsettled] = _.partition(
|
2022-03-13 20:55:42 +00:00
|
|
|
contracts,
|
2022-04-03 19:48:53 +00:00
|
|
|
(c) => c.isResolved || contractsMetrics[c.id].invested === 0
|
2021-12-16 03:04:38 +00:00
|
|
|
)
|
|
|
|
|
2022-04-03 19:48:53 +00:00
|
|
|
const currentInvested = _.sumBy(
|
|
|
|
unsettled,
|
|
|
|
(c) => contractsMetrics[c.id].invested
|
|
|
|
)
|
2022-02-09 05:28:20 +00:00
|
|
|
const currentBetsValue = _.sumBy(
|
2022-02-26 00:57:28 +00:00
|
|
|
unsettled,
|
2022-04-03 19:48:53 +00:00
|
|
|
(c) => contractsMetrics[c.id].payout
|
2021-12-16 03:04:38 +00:00
|
|
|
)
|
2022-04-03 19:48:53 +00:00
|
|
|
const currentNetInvestment = _.sumBy(
|
2022-04-03 06:08:57 +00:00
|
|
|
unsettled,
|
2022-04-03 19:48:53 +00:00
|
|
|
(c) => contractsMetrics[c.id].netInvestment
|
2022-04-03 06:08:57 +00:00
|
|
|
)
|
2021-12-16 03:04:38 +00:00
|
|
|
|
2022-04-03 19:48:53 +00:00
|
|
|
const totalPortfolio = currentNetInvestment + user.balance
|
2022-02-14 19:00:20 +00:00
|
|
|
|
2022-02-14 23:29:29 +00:00
|
|
|
const totalPnl = totalPortfolio - user.totalDeposits
|
2022-03-02 23:42:59 +00:00
|
|
|
const totalProfitPercent = (totalPnl / user.totalDeposits) * 100
|
|
|
|
const investedProfitPercent =
|
2022-04-03 19:48:53 +00:00
|
|
|
((currentBetsValue - currentInvested) / currentInvested) * 100
|
2022-02-14 22:00:35 +00:00
|
|
|
|
2021-12-15 07:41:50 +00:00
|
|
|
return (
|
2022-02-14 23:39:59 +00:00
|
|
|
<Col className="mt-6 gap-4 sm:gap-6">
|
2022-02-11 18:40:22 +00:00
|
|
|
<Col className="mx-4 gap-4 sm:flex-row sm:justify-between md:mx-0">
|
2022-02-09 05:28:20 +00:00
|
|
|
<Row className="gap-8">
|
2022-02-14 19:00:20 +00:00
|
|
|
<Col>
|
2022-03-02 23:48:38 +00:00
|
|
|
<div className="text-sm text-gray-500">Investment value</div>
|
2022-02-14 23:29:29 +00:00
|
|
|
<div className="text-lg">
|
2022-04-03 19:48:53 +00:00
|
|
|
{formatMoney(currentNetInvestment)}{' '}
|
2022-03-02 23:42:59 +00:00
|
|
|
<ProfitBadge profitPercent={investedProfitPercent} />
|
2022-02-14 19:00:20 +00:00
|
|
|
</div>
|
|
|
|
</Col>
|
2022-02-09 05:28:20 +00:00
|
|
|
<Col>
|
2022-03-02 23:42:59 +00:00
|
|
|
<div className="text-sm text-gray-500">Total profit</div>
|
2022-02-14 23:29:29 +00:00
|
|
|
<div className="text-lg">
|
2022-03-02 23:42:59 +00:00
|
|
|
{formatMoney(totalPnl)}{' '}
|
|
|
|
<ProfitBadge profitPercent={totalProfitPercent} />
|
2022-02-14 23:29:29 +00:00
|
|
|
</div>
|
2022-02-09 05:28:20 +00:00
|
|
|
</Col>
|
|
|
|
</Row>
|
|
|
|
|
2022-03-13 20:55:42 +00:00
|
|
|
<Row className="gap-8">
|
|
|
|
<select
|
|
|
|
className="select select-bordered self-start"
|
|
|
|
value={filter}
|
|
|
|
onChange={(e) => setFilter(e.target.value as BetFilter)}
|
|
|
|
>
|
|
|
|
<option value="open">Open</option>
|
|
|
|
<option value="closed">Closed</option>
|
|
|
|
<option value="resolved">Resolved</option>
|
|
|
|
<option value="all">All</option>
|
|
|
|
</select>
|
|
|
|
|
|
|
|
<select
|
|
|
|
className="select select-bordered self-start"
|
|
|
|
value={sort}
|
|
|
|
onChange={(e) => setSort(e.target.value as BetSort)}
|
|
|
|
>
|
|
|
|
<option value="value">By value</option>
|
|
|
|
<option value="profit">By profit</option>
|
|
|
|
<option value="newest">Most recent</option>
|
2022-03-21 02:30:04 +00:00
|
|
|
<option value="closeTime">Closing soonest</option>
|
|
|
|
<option value="resolutionTime">Resolved soonest</option>
|
2022-03-13 20:55:42 +00:00
|
|
|
</select>
|
|
|
|
</Row>
|
2022-02-09 05:28:20 +00:00
|
|
|
</Col>
|
2021-12-16 03:04:38 +00:00
|
|
|
|
2022-02-22 22:55:06 +00:00
|
|
|
{displayedContracts.length === 0 ? (
|
|
|
|
<NoBets />
|
|
|
|
) : (
|
|
|
|
displayedContracts.map((contract) => (
|
|
|
|
<MyContractBets
|
|
|
|
key={contract.id}
|
|
|
|
contract={contract}
|
|
|
|
bets={contractBets[contract.id] ?? []}
|
2022-04-03 06:08:57 +00:00
|
|
|
metric={sort === 'profit' ? 'profit' : 'value'}
|
2022-02-22 22:55:06 +00:00
|
|
|
/>
|
|
|
|
))
|
|
|
|
)}
|
2021-12-15 07:41:50 +00:00
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-02-22 22:55:06 +00:00
|
|
|
const NoBets = () => {
|
|
|
|
return (
|
|
|
|
<div className="mx-4 text-gray-500">
|
|
|
|
You have not made any bets yet.{' '}
|
|
|
|
<SiteLink href="/" className="underline">
|
|
|
|
Find a prediction market!
|
|
|
|
</SiteLink>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-04-03 06:08:57 +00:00
|
|
|
function MyContractBets(props: {
|
|
|
|
contract: Contract
|
|
|
|
bets: Bet[]
|
|
|
|
metric: 'profit' | 'value'
|
|
|
|
}) {
|
|
|
|
const { bets, contract, metric } = props
|
2022-02-17 23:00:19 +00:00
|
|
|
const { resolution, outcomeType } = contract
|
2021-12-15 22:57:40 +00:00
|
|
|
|
2021-12-16 21:22:00 +00:00
|
|
|
const [collapsed, setCollapsed] = useState(true)
|
2022-02-17 23:00:19 +00:00
|
|
|
|
|
|
|
const isBinary = outcomeType === 'BINARY'
|
|
|
|
const probPercent = getBinaryProbPercent(contract)
|
|
|
|
|
2022-04-03 19:48:53 +00:00
|
|
|
const { totalValue, profit, profitPercent } = getContractBetMetrics(
|
|
|
|
contract,
|
|
|
|
bets
|
|
|
|
)
|
|
|
|
|
2021-12-15 07:41:50 +00:00
|
|
|
return (
|
2021-12-16 21:22:00 +00:00
|
|
|
<div
|
|
|
|
tabIndex={0}
|
|
|
|
className={clsx(
|
2022-02-11 18:40:22 +00:00
|
|
|
'card card-body collapse collapse-arrow relative cursor-pointer bg-white p-6 shadow-xl',
|
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)}
|
|
|
|
>
|
2022-02-14 23:39:59 +00:00
|
|
|
<Row className="flex-wrap gap-2">
|
2021-12-16 21:22:00 +00:00
|
|
|
<Col className="flex-[2] gap-1">
|
2022-02-14 23:39:59 +00:00
|
|
|
<Row className="mr-2 max-w-lg">
|
2022-01-12 19:01:04 +00:00
|
|
|
<Link href={contractPath(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
|
2022-02-11 18:40:22 +00:00
|
|
|
className="collapse-title absolute h-0 min-h-0 w-0 p-0"
|
2021-12-19 00:11:40 +00:00
|
|
|
style={{ top: -10, right: 4 }}
|
2021-12-18 23:40:39 +00:00
|
|
|
/>
|
|
|
|
</Row>
|
2021-12-16 09:03:41 +00:00
|
|
|
|
2022-02-26 00:57:28 +00:00
|
|
|
<Row className="flex-1 items-center gap-2 text-sm text-gray-500">
|
2022-02-17 23:00:19 +00:00
|
|
|
{isBinary && (
|
|
|
|
<>
|
|
|
|
{resolution ? (
|
|
|
|
<div>
|
|
|
|
Resolved <OutcomeLabel outcome={resolution} />
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<div className="text-primary text-lg">{probPercent}</div>
|
|
|
|
)}
|
|
|
|
<div>•</div>
|
|
|
|
</>
|
2022-02-09 04:52:09 +00:00
|
|
|
)}
|
2022-01-13 21:16:47 +00:00
|
|
|
<UserLink
|
|
|
|
name={contract.creatorName}
|
|
|
|
username={contract.creatorUsername}
|
|
|
|
/>
|
2021-12-16 09:03:41 +00:00
|
|
|
</Row>
|
|
|
|
</Col>
|
2021-12-15 22:57:40 +00:00
|
|
|
|
2022-04-03 19:48:53 +00:00
|
|
|
<Row className="mr-5 justify-end sm:mr-8">
|
|
|
|
<Col>
|
|
|
|
<div className="whitespace-nowrap text-right text-lg">
|
|
|
|
{formatMoney(metric === 'profit' ? profit : totalValue)}
|
|
|
|
</div>
|
|
|
|
<div className="text-right">
|
|
|
|
<ProfitBadge profitPercent={profitPercent} />
|
|
|
|
</div>
|
|
|
|
</Col>
|
|
|
|
</Row>
|
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
|
|
|
|
2022-02-14 22:00:35 +00:00
|
|
|
<MyBetsSummary
|
|
|
|
className="mr-5 flex-1 sm:mr-8"
|
|
|
|
contract={contract}
|
|
|
|
bets={bets}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<Spacer h={8} />
|
|
|
|
|
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[]
|
|
|
|
className?: string
|
|
|
|
}) {
|
2022-04-03 19:48:53 +00:00
|
|
|
const { bets, contract, className } = props
|
2022-04-13 20:47:10 +00:00
|
|
|
const { resolution, outcomeType, mechanism } = contract
|
2022-02-17 23:00:19 +00:00
|
|
|
const isBinary = outcomeType === 'BINARY'
|
2022-04-13 20:47:10 +00:00
|
|
|
const isCpmm = mechanism === 'cpmm-1'
|
2021-12-16 04:30:24 +00:00
|
|
|
|
2021-12-24 21:06:01 +00:00
|
|
|
const excludeSales = bets.filter((b) => !b.isSold && !b.sale)
|
|
|
|
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-04-03 19:48:53 +00:00
|
|
|
const { invested, profitPercent, payout } = getContractBetMetrics(
|
|
|
|
contract,
|
|
|
|
bets
|
2022-02-14 22:00:35 +00:00
|
|
|
)
|
|
|
|
|
2021-12-16 04:30:24 +00:00
|
|
|
return (
|
2022-04-03 19:48:53 +00:00
|
|
|
<Row className={clsx('flex-wrap gap-4 sm:flex-nowrap sm:gap-6', className)}>
|
|
|
|
<Row className="gap-4 sm:gap-6">
|
2022-04-13 20:47:10 +00:00
|
|
|
{!isCpmm && (
|
|
|
|
<Col>
|
|
|
|
<div className="whitespace-nowrap text-sm text-gray-500">
|
|
|
|
Invested
|
|
|
|
</div>
|
|
|
|
<div className="whitespace-nowrap">{formatMoney(invested)}</div>
|
|
|
|
</Col>
|
|
|
|
)}
|
2022-04-03 19:48:53 +00:00
|
|
|
{resolution ? (
|
2021-12-16 04:30:24 +00:00
|
|
|
<Col>
|
2022-04-03 19:48:53 +00:00
|
|
|
<div className="text-sm text-gray-500">Payout</div>
|
|
|
|
<div className="whitespace-nowrap">
|
|
|
|
{formatMoney(payout)}{' '}
|
|
|
|
<ProfitBadge profitPercent={profitPercent} />
|
2021-12-16 04:30:24 +00:00
|
|
|
</div>
|
|
|
|
</Col>
|
2022-04-03 19:48:53 +00:00
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
{isBinary && (
|
|
|
|
<>
|
|
|
|
<Col>
|
|
|
|
<div className="whitespace-nowrap text-sm text-gray-500">
|
|
|
|
Payout if <YesLabel />
|
|
|
|
</div>
|
|
|
|
<div className="whitespace-nowrap">
|
|
|
|
{formatMoney(yesWinnings)}
|
|
|
|
</div>
|
|
|
|
</Col>
|
|
|
|
<Col>
|
|
|
|
<div className="whitespace-nowrap text-sm text-gray-500">
|
|
|
|
Payout if <NoLabel />
|
|
|
|
</div>
|
|
|
|
<div className="whitespace-nowrap">
|
|
|
|
{formatMoney(noWinnings)}
|
|
|
|
</div>
|
|
|
|
</Col>
|
|
|
|
</>
|
|
|
|
)}
|
2022-04-03 06:08:57 +00:00
|
|
|
<Col>
|
2022-04-03 19:48:53 +00:00
|
|
|
<div className="whitespace-nowrap text-sm text-gray-500">
|
|
|
|
{isBinary ? (
|
|
|
|
<>
|
|
|
|
Payout at{' '}
|
|
|
|
<span className="text-blue-400">
|
|
|
|
{formatPercent(getProbability(contract))}
|
|
|
|
</span>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>Current payout</>
|
|
|
|
)}
|
2022-04-03 06:08:57 +00:00
|
|
|
</div>
|
2022-04-03 19:48:53 +00:00
|
|
|
<div className="whitespace-nowrap">{formatMoney(payout)}</div>
|
2022-04-03 06:08:57 +00:00
|
|
|
</Col>
|
2022-04-03 19:48:53 +00:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</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)
|
2022-03-15 22:27:51 +00:00
|
|
|
|
2021-12-24 21:06:01 +00:00
|
|
|
const salesDict = _.fromPairs(
|
|
|
|
sales.map((sale) => [sale.sale?.betId ?? '', sale])
|
|
|
|
)
|
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
const [redemptions, normalBets] = _.partition(buys, (b) => b.isRedemption)
|
|
|
|
const amountRedeemed = Math.floor(
|
|
|
|
-0.5 * _.sumBy(redemptions, (b) => b.shares)
|
|
|
|
)
|
|
|
|
|
2022-03-17 17:04:44 +00:00
|
|
|
const amountLoaned = _.sumBy(
|
|
|
|
bets.filter((bet) => !bet.isSold && !bet.sale),
|
|
|
|
(bet) => bet.loanAmount ?? 0
|
|
|
|
)
|
2022-03-16 23:08:33 +00:00
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
const { isResolved, mechanism } = contract
|
|
|
|
const isCPMM = mechanism === 'cpmm-1'
|
|
|
|
|
2021-12-15 22:57:40 +00:00
|
|
|
return (
|
2021-12-16 21:22:00 +00:00
|
|
|
<div className={clsx('overflow-x-auto', className)}>
|
2022-03-15 22:27:51 +00:00
|
|
|
{amountRedeemed > 0 && (
|
|
|
|
<>
|
2022-03-21 02:30:04 +00:00
|
|
|
<div className="pl-2 text-sm text-gray-500">
|
2022-03-15 22:27:51 +00:00
|
|
|
{amountRedeemed} YES shares and {amountRedeemed} NO shares
|
|
|
|
automatically redeemed for {formatMoney(amountRedeemed)}.
|
|
|
|
</div>
|
|
|
|
<Spacer h={4} />
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
2022-03-16 23:08:33 +00:00
|
|
|
{!isResolved && amountLoaned > 0 && (
|
|
|
|
<>
|
2022-03-21 02:30:04 +00:00
|
|
|
<div className="pl-2 text-sm text-gray-500">
|
2022-03-16 23:08:33 +00:00
|
|
|
You currently have a loan of {formatMoney(amountLoaned)}.
|
|
|
|
</div>
|
|
|
|
<Spacer h={4} />
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
2022-02-11 18:40:22 +00:00
|
|
|
<table className="table-zebra table-compact table w-full text-gray-500">
|
2021-12-15 22:57:40 +00:00
|
|
|
<thead>
|
|
|
|
<tr className="p-2">
|
2022-02-21 20:22:36 +00:00
|
|
|
<th></th>
|
2022-03-29 19:56:56 +00:00
|
|
|
{isCPMM && <th>Type</th>}
|
2021-12-15 22:57:40 +00:00
|
|
|
<th>Outcome</th>
|
2021-12-24 21:06:01 +00:00
|
|
|
<th>Amount</th>
|
2022-03-15 22:27:51 +00:00
|
|
|
{!isCPMM && <th>{isResolved ? <>Payout</> : <>Sale price</>}</th>}
|
|
|
|
{!isCPMM && !isResolved && <th>Payout if chosen</th>}
|
2021-12-31 20:13:06 +00:00
|
|
|
<th>Shares</th>
|
2022-03-15 22:27:51 +00:00
|
|
|
<th>Probability</th>
|
2022-02-21 20:22:36 +00:00
|
|
|
<th>Date</th>
|
2021-12-15 22:57:40 +00:00
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
2022-03-15 22:27:51 +00:00
|
|
|
{normalBets.map((bet) => (
|
2021-12-24 21:06:01 +00:00
|
|
|
<BetRow
|
|
|
|
key={bet.id}
|
|
|
|
bet={bet}
|
2022-01-15 22:51:09 +00:00
|
|
|
saleBet={salesDict[bet.id]}
|
2021-12-24 21:06:01 +00:00
|
|
|
contract={contract}
|
|
|
|
/>
|
2021-12-15 22:57:40 +00:00
|
|
|
))}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-01-15 22:51:09 +00:00
|
|
|
function BetRow(props: { bet: Bet; contract: Contract; saleBet?: Bet }) {
|
|
|
|
const { bet, saleBet, contract } = props
|
2021-12-24 21:06:01 +00:00
|
|
|
const {
|
|
|
|
amount,
|
|
|
|
outcome,
|
|
|
|
createdTime,
|
|
|
|
probBefore,
|
|
|
|
probAfter,
|
|
|
|
shares,
|
|
|
|
isSold,
|
2022-01-19 22:36:55 +00:00
|
|
|
isAnte,
|
2021-12-24 21:06:01 +00:00
|
|
|
} = bet
|
2022-01-19 22:36:55 +00:00
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
const { isResolved, closeTime, mechanism } = contract
|
|
|
|
|
2022-01-10 16:32:20 +00:00
|
|
|
const isClosed = closeTime && Date.now() > closeTime
|
2021-12-15 07:41:50 +00:00
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
const isCPMM = mechanism === 'cpmm-1'
|
|
|
|
|
2022-01-19 22:36:55 +00:00
|
|
|
const saleAmount = saleBet?.sale?.amount
|
|
|
|
|
2022-03-02 03:31:48 +00:00
|
|
|
const saleDisplay = isAnte ? (
|
2022-01-19 22:36:55 +00:00
|
|
|
'ANTE'
|
|
|
|
) : saleAmount !== undefined ? (
|
|
|
|
<>{formatMoney(saleAmount)} (sold)</>
|
|
|
|
) : (
|
|
|
|
formatMoney(
|
|
|
|
isResolved
|
|
|
|
? resolvedPayout(contract, bet)
|
|
|
|
: calculateSaleAmount(contract, bet)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-03-01 00:44:25 +00:00
|
|
|
const payoutIfChosenDisplay =
|
|
|
|
bet.outcome === '0' && bet.isAnte
|
|
|
|
? 'N/A'
|
|
|
|
: formatMoney(calculatePayout(contract, bet, bet.outcome))
|
2022-03-01 00:38:09 +00:00
|
|
|
|
2021-12-15 07:41:50 +00:00
|
|
|
return (
|
2021-12-15 18:41:18 +00:00
|
|
|
<tr>
|
2022-02-21 20:22:36 +00:00
|
|
|
<td className="text-neutral">
|
2022-03-15 22:27:51 +00:00
|
|
|
{!isCPMM && !isResolved && !isClosed && !isSold && !isAnte && (
|
2022-02-21 20:22:36 +00:00
|
|
|
<SellButton contract={contract} bet={bet} />
|
|
|
|
)}
|
|
|
|
</td>
|
2022-03-29 19:56:56 +00:00
|
|
|
{isCPMM && <td>{shares >= 0 ? 'BUY' : 'SELL'}</td>}
|
2021-12-16 21:22:00 +00:00
|
|
|
<td>
|
|
|
|
<OutcomeLabel outcome={outcome} />
|
|
|
|
</td>
|
2022-03-29 19:56:56 +00:00
|
|
|
<td>{formatMoney(Math.abs(amount))}</td>
|
2022-03-15 22:27:51 +00:00
|
|
|
{!isCPMM && <td>{saleDisplay}</td>}
|
|
|
|
{!isCPMM && !isResolved && <td>{payoutIfChosenDisplay}</td>}
|
2022-03-29 19:56:56 +00:00
|
|
|
<td>{formatWithCommas(Math.abs(shares))}</td>
|
2021-12-15 18:41:18 +00:00
|
|
|
<td>
|
|
|
|
{formatPercent(probBefore)} → {formatPercent(probAfter)}
|
|
|
|
</td>
|
2022-02-21 20:22:36 +00:00
|
|
|
<td>{dayjs(createdTime).format('MMM D, h:mma')}</td>
|
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
|
2022-03-02 03:31:48 +00:00
|
|
|
const { outcome, shares, loanAmount } = bet
|
|
|
|
|
2021-12-24 21:06:01 +00:00
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false)
|
|
|
|
|
2022-02-18 00:24:00 +00:00
|
|
|
const initialProb = getOutcomeProbability(
|
2022-03-15 22:27:51 +00:00
|
|
|
contract,
|
2022-03-02 03:31:48 +00:00
|
|
|
outcome === 'NO' ? 'YES' : outcome
|
2022-02-18 00:24:00 +00:00
|
|
|
)
|
|
|
|
|
2022-03-15 22:27:51 +00:00
|
|
|
const outcomeProb = getProbabilityAfterSale(contract, outcome, shares)
|
2022-02-18 00:24:00 +00:00
|
|
|
|
|
|
|
const saleAmount = calculateSaleAmount(contract, bet)
|
2022-03-03 10:10:20 +00:00
|
|
|
const profit = saleAmount - bet.amount
|
2022-02-18 00:24:00 +00:00
|
|
|
|
2021-12-24 21:06:01 +00:00
|
|
|
return (
|
|
|
|
<ConfirmationButton
|
|
|
|
id={`sell-${bet.id}`}
|
2022-03-31 08:38:57 +00:00
|
|
|
openModalBtn={{
|
2021-12-24 21:06:01 +00:00
|
|
|
className: clsx('btn-sm', isSubmitting && 'btn-disabled loading'),
|
|
|
|
label: 'Sell',
|
|
|
|
}}
|
2022-03-02 03:31:48 +00:00
|
|
|
submitBtn={{ className: 'btn-primary', label: 'Sell' }}
|
2021-12-24 21:06:01 +00:00
|
|
|
onSubmit={async () => {
|
|
|
|
setIsSubmitting(true)
|
|
|
|
await sellBet({ contractId: contract.id, betId: bet.id })
|
|
|
|
setIsSubmitting(false)
|
|
|
|
}}
|
|
|
|
>
|
2022-02-11 18:40:22 +00:00
|
|
|
<div className="mb-4 text-2xl">
|
2022-03-02 03:31:48 +00:00
|
|
|
Sell {formatWithCommas(shares)} shares of{' '}
|
|
|
|
<OutcomeLabel outcome={outcome} /> for {formatMoney(saleAmount)}?
|
2022-02-18 00:24:00 +00:00
|
|
|
</div>
|
2022-03-02 03:31:48 +00:00
|
|
|
{!!loanAmount && (
|
|
|
|
<div className="mt-2">
|
|
|
|
You will also pay back {formatMoney(loanAmount)} of your loan, for a
|
|
|
|
net of {formatMoney(saleAmount - loanAmount)}.
|
|
|
|
</div>
|
|
|
|
)}
|
2022-02-18 00:24:00 +00:00
|
|
|
|
2022-03-02 03:31:48 +00:00
|
|
|
<div className="mt-2 mb-1 text-sm">
|
2022-03-03 10:10:20 +00:00
|
|
|
{profit > 0 ? 'Profit' : 'Loss'}: {formatMoney(profit).replace('-', '')}
|
|
|
|
<br />
|
2022-03-02 03:31:48 +00:00
|
|
|
Market probability: {formatPercent(initialProb)} →{' '}
|
|
|
|
{formatPercent(outcomeProb)}
|
2021-12-24 21:06:01 +00:00
|
|
|
</div>
|
|
|
|
</ConfirmationButton>
|
|
|
|
)
|
|
|
|
}
|
2022-02-14 23:29:29 +00:00
|
|
|
|
|
|
|
function ProfitBadge(props: { profitPercent: number }) {
|
|
|
|
const { profitPercent } = props
|
|
|
|
if (!profitPercent) return null
|
|
|
|
const colors =
|
|
|
|
profitPercent > 0
|
|
|
|
? 'bg-green-100 text-green-800'
|
|
|
|
: 'bg-red-100 text-red-800'
|
|
|
|
|
|
|
|
return (
|
|
|
|
<span
|
|
|
|
className={clsx(
|
|
|
|
'ml-1 inline-flex items-center rounded-full px-3 py-0.5 text-sm font-medium',
|
|
|
|
colors
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{(profitPercent > 0 ? '+' : '') + profitPercent.toFixed(1) + '%'}
|
|
|
|
</span>
|
|
|
|
)
|
|
|
|
}
|