2021-12-15 07:41:50 +00:00
|
|
|
import Link from 'next/link'
|
2022-07-11 00:19:35 +00:00
|
|
|
import { groupBy, mapValues, sortBy, partition, sumBy } from 'lodash'
|
2021-12-15 07:41:50 +00:00
|
|
|
import dayjs from 'dayjs'
|
2022-07-10 23:41:33 +00:00
|
|
|
import { useEffect, useMemo, useState } from 'react'
|
2022-01-05 18:23:58 +00:00
|
|
|
import clsx from 'clsx'
|
|
|
|
|
2022-05-09 13:04:36 +00:00
|
|
|
import { Bet } from 'web/lib/firebase/bets'
|
|
|
|
import { User } from 'web/lib/firebase/users'
|
2021-12-31 20:13:06 +00:00
|
|
|
import {
|
2022-07-02 19:37:59 +00:00
|
|
|
formatLargeNumber,
|
2021-12-31 20:13:06 +00:00
|
|
|
formatMoney,
|
|
|
|
formatPercent,
|
|
|
|
formatWithCommas,
|
2022-05-09 13:04:36 +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,
|
|
|
|
contractPath,
|
2022-02-17 23:00:19 +00:00
|
|
|
getBinaryProbPercent,
|
2022-05-09 13:04:36 +00:00
|
|
|
} from 'web/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-07-10 22:03:15 +00:00
|
|
|
import { sellBet } from 'web/lib/firebase/api'
|
2022-03-15 22:27:51 +00:00
|
|
|
import { ConfirmationButton } from './confirmation-button'
|
|
|
|
import { OutcomeLabel, YesLabel, NoLabel } from './outcome-label'
|
|
|
|
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,
|
|
|
|
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-05-09 13:04:36 +00:00
|
|
|
} from 'common/calculate'
|
|
|
|
import { useTimeSinceFirstRender } from 'web/hooks/use-time-since-first-render'
|
|
|
|
import { trackLatency } from 'web/lib/firebase/tracking'
|
2022-05-19 17:42:03 +00:00
|
|
|
import { NumericContract } from 'common/contract'
|
2022-07-02 19:37:59 +00:00
|
|
|
import { formatNumericProbability } from 'common/pseudo-numeric'
|
2022-06-01 00:36:58 +00:00
|
|
|
import { useUser } from 'web/hooks/use-user'
|
|
|
|
import { SellSharesModal } from './sell-modal'
|
2022-07-10 18:05:44 +00:00
|
|
|
import { useUnfilledBets } from 'web/hooks/use-bets'
|
|
|
|
import { LimitBet } from 'common/bet'
|
|
|
|
import { floatingEqual } from 'common/util/math'
|
2022-07-10 23:41:33 +00:00
|
|
|
import { Pagination } from './pagination'
|
2022-07-15 02:27:00 +00:00
|
|
|
import { LimitOrderTable } from './limit-bets'
|
2021-12-15 07:41:50 +00:00
|
|
|
|
2022-04-13 23:56:28 +00:00
|
|
|
type BetSort = 'newest' | 'profit' | 'closeTime' | 'value'
|
2022-07-11 03:15:07 +00:00
|
|
|
type BetFilter = 'open' | 'limit_bet' | 'sold' | 'closed' | 'resolved' | 'all'
|
2022-02-09 05:28:20 +00:00
|
|
|
|
2022-07-19 20:43:37 +00:00
|
|
|
const CONTRACTS_PER_PAGE = 50
|
2022-07-10 23:41:33 +00:00
|
|
|
|
2022-07-10 23:22:21 +00:00
|
|
|
export function BetsList(props: {
|
|
|
|
user: User
|
|
|
|
bets: Bet[] | undefined
|
2022-07-11 00:19:35 +00:00
|
|
|
contractsById: { [id: string]: Contract } | undefined
|
2022-07-10 23:22:21 +00:00
|
|
|
hideBetsBefore?: number
|
|
|
|
}) {
|
2022-07-11 00:19:35 +00:00
|
|
|
const { user, bets: allBets, contractsById, hideBetsBefore } = props
|
2022-06-01 16:28:47 +00:00
|
|
|
|
|
|
|
const signedInUser = useUser()
|
|
|
|
const isYourBets = user.id === signedInUser?.id
|
|
|
|
|
2022-06-01 01:06:27 +00:00
|
|
|
// Hide bets before 06-01-2022 if this isn't your own profile
|
|
|
|
// NOTE: This means public profits also begin on 06-01-2022 as well.
|
2022-07-10 23:41:33 +00:00
|
|
|
const bets = useMemo(
|
|
|
|
() => allBets?.filter((bet) => bet.createdTime >= (hideBetsBefore ?? 0)),
|
|
|
|
[allBets, hideBetsBefore]
|
2022-06-01 01:06:27 +00:00
|
|
|
)
|
2021-12-16 03:04:38 +00:00
|
|
|
|
2022-04-14 05:16:08 +00:00
|
|
|
const [sort, setSort] = useState<BetSort>('newest')
|
2022-03-13 20:55:42 +00:00
|
|
|
const [filter, setFilter] = useState<BetFilter>('open')
|
2022-07-10 23:41:33 +00:00
|
|
|
const [page, setPage] = useState(0)
|
|
|
|
const start = page * CONTRACTS_PER_PAGE
|
|
|
|
const end = start + CONTRACTS_PER_PAGE
|
2022-02-09 05:28:20 +00:00
|
|
|
|
2022-04-21 06:00:08 +00:00
|
|
|
const getTime = useTimeSinceFirstRender()
|
|
|
|
useEffect(() => {
|
2022-07-11 00:19:35 +00:00
|
|
|
if (bets && contractsById) {
|
2022-04-21 06:00:08 +00:00
|
|
|
trackLatency('portfolio', getTime())
|
|
|
|
}
|
2022-07-11 00:19:35 +00:00
|
|
|
}, [bets, contractsById, getTime])
|
2022-04-21 06:00:08 +00:00
|
|
|
|
2022-07-11 00:19:35 +00:00
|
|
|
if (!bets || !contractsById) {
|
2022-02-21 04:26:22 +00:00
|
|
|
return <LoadingIndicator />
|
2021-12-15 07:41:50 +00:00
|
|
|
}
|
2022-06-03 01:07:47 +00:00
|
|
|
if (bets.length === 0) return <NoBets user={user} />
|
2022-07-11 00:19:35 +00:00
|
|
|
|
2021-12-16 04:40:48 +00:00
|
|
|
// Decending creation time.
|
|
|
|
bets.sort((bet1, bet2) => bet2.createdTime - bet1.createdTime)
|
2022-05-22 08:36:05 +00:00
|
|
|
const contractBets = groupBy(bets, 'contractId')
|
2022-07-11 00:19:35 +00:00
|
|
|
|
|
|
|
// Keep only contracts that have bets.
|
|
|
|
const contracts = Object.values(contractsById).filter(
|
|
|
|
(c) => contractBets[c.id]
|
|
|
|
)
|
2021-12-15 07:41:50 +00:00
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const contractsMetrics = mapValues(contractBets, (bets, contractId) => {
|
2022-04-03 19:48:53 +00:00
|
|
|
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,
|
2022-05-27 20:51:55 +00:00
|
|
|
sold: () => true,
|
2022-07-11 03:15:07 +00:00
|
|
|
limit_bet: (c) => FILTERS.open(c),
|
2022-03-13 20:55:42 +00:00
|
|
|
}
|
|
|
|
const SORTS: Record<BetSort, (c: Contract) => number> = {
|
2022-04-03 19:48:53 +00:00
|
|
|
profit: (c) => contractsMetrics[c.id].profit,
|
2022-04-13 22:25:32 +00:00
|
|
|
value: (c) => contractsMetrics[c.id].payout,
|
2022-03-13 20:55:42 +00:00
|
|
|
newest: (c) =>
|
|
|
|
Math.max(...contractBets[c.id].map((bet) => bet.createdTime)),
|
2022-04-13 23:56:28 +00:00
|
|
|
closeTime: (c) =>
|
|
|
|
// This is in fact the intuitive sort direction.
|
|
|
|
(filter === 'open' ? -1 : 1) *
|
|
|
|
(c.resolutionTime ?? c.closeTime ?? Infinity),
|
2022-03-13 20:55:42 +00:00
|
|
|
}
|
2022-07-10 23:41:33 +00:00
|
|
|
const filteredContracts = sortBy(contracts, SORTS[sort])
|
2022-03-13 20:55:42 +00:00
|
|
|
.reverse()
|
|
|
|
.filter(FILTERS[filter])
|
2022-04-21 23:32:43 +00:00
|
|
|
.filter((c) => {
|
2022-05-27 20:51:55 +00:00
|
|
|
if (filter === 'all') return true
|
|
|
|
|
2022-06-21 15:14:53 +00:00
|
|
|
const { hasShares } = contractsMetrics[c.id]
|
2022-05-27 20:51:55 +00:00
|
|
|
|
2022-06-21 15:14:53 +00:00
|
|
|
if (filter === 'sold') return !hasShares
|
2022-07-11 03:15:07 +00:00
|
|
|
if (filter === 'limit_bet')
|
2022-07-11 03:28:29 +00:00
|
|
|
return (contractBets[c.id] ?? []).some(
|
|
|
|
(b) => b.limitProb !== undefined && !b.isCancelled && !b.isFilled
|
|
|
|
)
|
2022-06-21 15:14:53 +00:00
|
|
|
return hasShares
|
2022-04-21 23:32:43 +00:00
|
|
|
})
|
2022-07-10 23:41:33 +00:00
|
|
|
const displayedContracts = filteredContracts.slice(start, end)
|
2022-02-09 05:28:20 +00:00
|
|
|
|
2022-06-13 04:42:41 +00:00
|
|
|
const unsettled = contracts.filter(
|
|
|
|
(c) => !c.isResolved && contractsMetrics[c.id].invested !== 0
|
2021-12-16 03:04:38 +00:00
|
|
|
)
|
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const currentInvested = sumBy(
|
2022-04-03 19:48:53 +00:00
|
|
|
unsettled,
|
|
|
|
(c) => contractsMetrics[c.id].invested
|
|
|
|
)
|
2022-05-22 08:36:05 +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-05-22 08:36:05 +00:00
|
|
|
const currentNetInvestment = sumBy(
|
2022-04-03 06:08:57 +00:00
|
|
|
unsettled,
|
2022-04-14 03:28:29 +00:00
|
|
|
(c) => contractsMetrics[c.id].netPayout
|
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-14 03:28:29 +00:00
|
|
|
((currentBetsValue - currentInvested) / (currentInvested + 0.1)) * 100
|
2022-02-14 22:00:35 +00:00
|
|
|
|
2021-12-15 07:41:50 +00:00
|
|
|
return (
|
2022-04-14 05:06:13 +00:00
|
|
|
<Col className="mt-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>
|
2022-07-12 21:46:03 +00:00
|
|
|
<option value="limit_bet">Limit orders</option>
|
2022-05-27 20:51:55 +00:00
|
|
|
<option value="sold">Sold</option>
|
2022-03-13 20:55:42 +00:00
|
|
|
<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)}
|
|
|
|
>
|
2022-04-15 22:55:26 +00:00
|
|
|
<option value="newest">Recent</option>
|
2022-04-13 23:56:28 +00:00
|
|
|
<option value="value">Value</option>
|
|
|
|
<option value="profit">Profit</option>
|
|
|
|
<option value="closeTime">Close date</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-04-14 05:06:13 +00:00
|
|
|
<Col className="mt-6 divide-y">
|
|
|
|
{displayedContracts.length === 0 ? (
|
2022-06-03 01:07:47 +00:00
|
|
|
<NoBets user={user} />
|
2022-04-14 05:06:13 +00:00
|
|
|
) : (
|
|
|
|
displayedContracts.map((contract) => (
|
2022-06-01 16:28:47 +00:00
|
|
|
<ContractBets
|
2022-04-14 05:06:13 +00:00
|
|
|
key={contract.id}
|
|
|
|
contract={contract}
|
|
|
|
bets={contractBets[contract.id] ?? []}
|
|
|
|
metric={sort === 'profit' ? 'profit' : 'value'}
|
2022-06-01 16:28:47 +00:00
|
|
|
isYourBets={isYourBets}
|
2022-04-14 05:06:13 +00:00
|
|
|
/>
|
|
|
|
))
|
|
|
|
)}
|
|
|
|
</Col>
|
2022-07-10 23:41:33 +00:00
|
|
|
|
|
|
|
<Pagination
|
|
|
|
page={page}
|
|
|
|
itemsPerPage={CONTRACTS_PER_PAGE}
|
|
|
|
totalItems={filteredContracts.length}
|
|
|
|
setPage={setPage}
|
|
|
|
/>
|
2021-12-15 07:41:50 +00:00
|
|
|
</Col>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-03 01:07:47 +00:00
|
|
|
const NoBets = ({ user }: { user: User }) => {
|
|
|
|
const me = useUser()
|
2022-02-22 22:55:06 +00:00
|
|
|
return (
|
|
|
|
<div className="mx-4 text-gray-500">
|
2022-06-03 01:07:47 +00:00
|
|
|
{user.id === me?.id ? (
|
|
|
|
<>
|
|
|
|
You have not made any bets yet.{' '}
|
|
|
|
<SiteLink href="/home" className="underline">
|
|
|
|
Find a prediction market!
|
|
|
|
</SiteLink>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>{user.name} has not made any public bets yet.</>
|
|
|
|
)}
|
2022-02-22 22:55:06 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-01 16:28:47 +00:00
|
|
|
function ContractBets(props: {
|
2022-04-03 06:08:57 +00:00
|
|
|
contract: Contract
|
|
|
|
bets: Bet[]
|
|
|
|
metric: 'profit' | 'value'
|
2022-06-01 16:28:47 +00:00
|
|
|
isYourBets: boolean
|
2022-04-03 06:08:57 +00:00
|
|
|
}) {
|
2022-06-01 16:28:47 +00:00
|
|
|
const { bets, contract, metric, isYourBets } = props
|
2022-02-17 23:00:19 +00:00
|
|
|
const { resolution, outcomeType } = contract
|
2021-12-15 22:57:40 +00:00
|
|
|
|
2022-07-11 03:07:42 +00:00
|
|
|
const limitBets = bets.filter(
|
2022-07-11 03:28:29 +00:00
|
|
|
(bet) => bet.limitProb !== undefined && !bet.isCancelled && !bet.isFilled
|
2022-07-11 03:07:42 +00:00
|
|
|
) as LimitBet[]
|
2022-05-19 17:42:03 +00:00
|
|
|
const resolutionValue = (contract as NumericContract).resolutionValue
|
|
|
|
|
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'
|
|
|
|
|
2022-06-13 04:42:41 +00:00
|
|
|
const { payout, profit, profitPercent } = getContractBetMetrics(
|
2022-04-03 19:48:53 +00:00
|
|
|
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-06-01 00:36:58 +00:00
|
|
|
'collapse collapse-arrow relative bg-white p-4 pr-6',
|
2021-12-17 04:27:22 +00:00
|
|
|
collapsed ? 'collapse-close' : 'collapse-open pb-2'
|
2021-12-16 21:22:00 +00:00
|
|
|
)}
|
|
|
|
>
|
2022-06-01 00:36:58 +00:00
|
|
|
<Row
|
|
|
|
className="cursor-pointer flex-wrap gap-2"
|
|
|
|
onClick={() => setCollapsed((collapsed) => !collapsed)}
|
|
|
|
>
|
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"
|
2022-04-14 05:06:13 +00:00
|
|
|
style={{ top: -10, right: 0 }}
|
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-06-01 02:42:35 +00:00
|
|
|
{resolution ? (
|
2022-02-17 23:00:19 +00:00
|
|
|
<>
|
2022-06-01 02:42:35 +00:00
|
|
|
<div>
|
|
|
|
Resolved{' '}
|
|
|
|
<OutcomeLabel
|
|
|
|
outcome={resolution}
|
|
|
|
value={resolutionValue}
|
|
|
|
contract={contract}
|
|
|
|
truncate="short"
|
|
|
|
/>
|
|
|
|
</div>
|
2022-02-17 23:00:19 +00:00
|
|
|
<div>•</div>
|
|
|
|
</>
|
2022-06-01 02:42:35 +00:00
|
|
|
) : isBinary ? (
|
|
|
|
<>
|
|
|
|
<div className="text-primary text-lg">
|
|
|
|
{getBinaryProbPercent(contract)}
|
|
|
|
</div>
|
|
|
|
<div>•</div>
|
|
|
|
</>
|
|
|
|
) : null}
|
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">
|
2022-04-13 22:25:32 +00:00
|
|
|
{formatMoney(metric === 'profit' ? profit : payout)}
|
2022-04-03 19:48:53 +00:00
|
|
|
</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-06-01 16:28:47 +00:00
|
|
|
<BetsSummary
|
2022-02-14 22:00:35 +00:00
|
|
|
className="mr-5 flex-1 sm:mr-8"
|
|
|
|
contract={contract}
|
|
|
|
bets={bets}
|
2022-06-01 16:28:47 +00:00
|
|
|
isYourBets={isYourBets}
|
2022-02-14 22:00:35 +00:00
|
|
|
/>
|
|
|
|
|
2022-07-11 03:07:42 +00:00
|
|
|
<Spacer h={4} />
|
|
|
|
|
|
|
|
{contract.mechanism === 'cpmm-1' && limitBets.length > 0 && (
|
|
|
|
<>
|
2022-07-15 02:27:00 +00:00
|
|
|
<div className="max-w-md">
|
|
|
|
<div className="bg-gray-50 px-4 py-2">Limit orders</div>
|
|
|
|
<LimitOrderTable
|
|
|
|
contract={contract}
|
|
|
|
limitBets={limitBets}
|
|
|
|
isYou={true}
|
|
|
|
/>
|
|
|
|
</div>
|
2022-07-11 03:07:42 +00:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<Spacer h={4} />
|
2022-02-14 22:00:35 +00:00
|
|
|
|
2022-07-15 02:27:00 +00:00
|
|
|
<div className="bg-gray-50 px-4 py-2">Bets</div>
|
2022-06-01 16:28:47 +00:00
|
|
|
<ContractBetsTable
|
|
|
|
contract={contract}
|
|
|
|
bets={bets}
|
|
|
|
isYourBets={isYourBets}
|
|
|
|
/>
|
2021-12-16 21:22:00 +00:00
|
|
|
</div>
|
2021-12-15 07:41:50 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-01 16:28:47 +00:00
|
|
|
export function BetsSummary(props: {
|
2021-12-16 04:30:24 +00:00
|
|
|
contract: Contract
|
|
|
|
bets: Bet[]
|
2022-06-01 16:28:47 +00:00
|
|
|
isYourBets: boolean
|
2021-12-16 04:30:24 +00:00
|
|
|
className?: string
|
|
|
|
}) {
|
2022-06-01 16:28:47 +00:00
|
|
|
const { contract, isYourBets, className } = props
|
2022-06-21 14:59:12 +00:00
|
|
|
const { resolution, closeTime, outcomeType, mechanism } = contract
|
2022-02-17 23:00:19 +00:00
|
|
|
const isBinary = outcomeType === 'BINARY'
|
2022-07-02 19:37:59 +00:00
|
|
|
const isPseudoNumeric = outcomeType === 'PSEUDO_NUMERIC'
|
2022-04-13 20:47:10 +00:00
|
|
|
const isCpmm = mechanism === 'cpmm-1'
|
2022-06-21 14:59:12 +00:00
|
|
|
const isClosed = closeTime && Date.now() > closeTime
|
2021-12-16 04:30:24 +00:00
|
|
|
|
2022-05-19 19:26:44 +00:00
|
|
|
const bets = props.bets.filter((b) => !b.isAnte)
|
2022-06-21 15:14:53 +00:00
|
|
|
const { hasShares } = getContractBetMetrics(contract, bets)
|
2022-05-19 19:26:44 +00:00
|
|
|
|
|
|
|
const excludeSalesAndAntes = bets.filter(
|
|
|
|
(b) => !b.isAnte && !b.isSold && !b.sale
|
|
|
|
)
|
2022-05-22 08:36:05 +00:00
|
|
|
const yesWinnings = sumBy(excludeSalesAndAntes, (bet) =>
|
2021-12-16 04:30:24 +00:00
|
|
|
calculatePayout(contract, bet, 'YES')
|
|
|
|
)
|
2022-05-22 08:36:05 +00:00
|
|
|
const noWinnings = sumBy(excludeSalesAndAntes, (bet) =>
|
2021-12-16 04:30:24 +00:00
|
|
|
calculatePayout(contract, bet, 'NO')
|
|
|
|
)
|
2022-06-01 00:36:58 +00:00
|
|
|
const { invested, profitPercent, payout, profit, totalShares } =
|
|
|
|
getContractBetMetrics(contract, bets)
|
|
|
|
|
|
|
|
const [showSellModal, setShowSellModal] = useState(false)
|
|
|
|
const user = useUser()
|
2022-02-14 22:00:35 +00:00
|
|
|
|
2022-07-13 16:56:59 +00:00
|
|
|
const sharesOutcome = floatingEqual(totalShares.YES ?? 0, 0)
|
|
|
|
? floatingEqual(totalShares.NO ?? 0, 0)
|
2022-07-10 18:05:44 +00:00
|
|
|
? undefined
|
|
|
|
: 'NO'
|
|
|
|
: 'YES'
|
|
|
|
|
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)}>
|
2022-06-02 17:56:56 +00:00
|
|
|
<Row className="flex-wrap 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
|
|
|
) : (
|
|
|
|
<>
|
2022-04-13 22:57:46 +00:00
|
|
|
{isBinary ? (
|
2022-04-03 19:48:53 +00:00
|
|
|
<>
|
|
|
|
<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-07-02 19:37:59 +00:00
|
|
|
) : isPseudoNumeric ? (
|
|
|
|
<>
|
|
|
|
<Col>
|
|
|
|
<div className="whitespace-nowrap text-sm text-gray-500">
|
|
|
|
Payout if {'>='} {formatLargeNumber(contract.max)}
|
|
|
|
</div>
|
|
|
|
<div className="whitespace-nowrap">
|
|
|
|
{formatMoney(yesWinnings)}
|
|
|
|
</div>
|
|
|
|
</Col>
|
|
|
|
<Col>
|
|
|
|
<div className="whitespace-nowrap text-sm text-gray-500">
|
|
|
|
Payout if {'<='} {formatLargeNumber(contract.min)}
|
|
|
|
</div>
|
|
|
|
<div className="whitespace-nowrap">
|
|
|
|
{formatMoney(noWinnings)}
|
|
|
|
</div>
|
|
|
|
</Col>
|
|
|
|
</>
|
2022-04-13 22:57:46 +00:00
|
|
|
) : (
|
|
|
|
<Col>
|
|
|
|
<div className="whitespace-nowrap text-sm text-gray-500">
|
|
|
|
Current value
|
|
|
|
</div>
|
|
|
|
<div className="whitespace-nowrap">{formatMoney(payout)}</div>
|
|
|
|
</Col>
|
2022-04-03 19:48:53 +00:00
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
2022-04-13 22:57:46 +00:00
|
|
|
<Col>
|
|
|
|
<div className="whitespace-nowrap text-sm text-gray-500">Profit</div>
|
|
|
|
<div className="whitespace-nowrap">
|
|
|
|
{formatMoney(profit)} <ProfitBadge profitPercent={profitPercent} />
|
2022-06-01 16:28:47 +00:00
|
|
|
{isYourBets &&
|
|
|
|
isCpmm &&
|
2022-07-13 16:56:59 +00:00
|
|
|
(isBinary || isPseudoNumeric) &&
|
2022-06-21 14:59:12 +00:00
|
|
|
!isClosed &&
|
2022-06-01 16:28:47 +00:00
|
|
|
!resolution &&
|
2022-06-21 15:14:53 +00:00
|
|
|
hasShares &&
|
2022-07-10 18:05:44 +00:00
|
|
|
sharesOutcome &&
|
2022-06-01 16:28:47 +00:00
|
|
|
user && (
|
|
|
|
<>
|
|
|
|
<button
|
|
|
|
className="btn btn-sm ml-2"
|
|
|
|
onClick={() => setShowSellModal(true)}
|
|
|
|
>
|
|
|
|
Sell
|
|
|
|
</button>
|
|
|
|
{showSellModal && (
|
|
|
|
<SellSharesModal
|
|
|
|
contract={contract}
|
|
|
|
user={user}
|
|
|
|
userBets={bets}
|
2022-07-10 18:05:44 +00:00
|
|
|
shares={totalShares[sharesOutcome]}
|
|
|
|
sharesOutcome={sharesOutcome}
|
2022-06-01 16:28:47 +00:00
|
|
|
setOpen={setShowSellModal}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
)}
|
2022-04-13 22:57:46 +00:00
|
|
|
</div>
|
|
|
|
</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[]
|
2022-06-01 16:28:47 +00:00
|
|
|
isYourBets: boolean
|
2021-12-16 21:22:00 +00:00
|
|
|
className?: string
|
|
|
|
}) {
|
2022-06-01 16:28:47 +00:00
|
|
|
const { contract, className, isYourBets } = props
|
2022-05-19 19:26:44 +00:00
|
|
|
|
2022-06-14 16:27:11 +00:00
|
|
|
const bets = sortBy(
|
2022-07-10 18:05:44 +00:00
|
|
|
props.bets.filter((b) => !b.isAnte && b.amount !== 0),
|
2022-06-14 16:27:11 +00:00
|
|
|
(bet) => bet.createdTime
|
|
|
|
).reverse()
|
2021-12-15 22:57:40 +00:00
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const [sales, buys] = partition(bets, (bet) => bet.sale)
|
2022-03-15 22:27:51 +00:00
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const salesDict = Object.fromEntries(
|
2021-12-24 21:06:01 +00:00
|
|
|
sales.map((sale) => [sale.sale?.betId ?? '', sale])
|
|
|
|
)
|
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const [redemptions, normalBets] = partition(
|
2022-04-18 22:02:53 +00:00
|
|
|
contract.mechanism === 'cpmm-1' ? bets : buys,
|
|
|
|
(b) => b.isRedemption
|
|
|
|
)
|
2022-05-22 08:36:05 +00:00
|
|
|
const amountRedeemed = Math.floor(-0.5 * sumBy(redemptions, (b) => b.shares))
|
2022-03-15 22:27:51 +00:00
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const amountLoaned = sumBy(
|
2022-03-17 17:04:44 +00:00
|
|
|
bets.filter((bet) => !bet.isSold && !bet.sale),
|
|
|
|
(bet) => bet.loanAmount ?? 0
|
|
|
|
)
|
2022-03-16 23:08:33 +00:00
|
|
|
|
2022-05-19 17:42:03 +00:00
|
|
|
const { isResolved, mechanism, outcomeType } = contract
|
2022-03-15 22:27:51 +00:00
|
|
|
const isCPMM = mechanism === 'cpmm-1'
|
2022-05-19 17:42:03 +00:00
|
|
|
const isNumeric = outcomeType === 'NUMERIC'
|
2022-07-02 19:37:59 +00:00
|
|
|
const isPseudoNumeric = outcomeType === 'PSEUDO_NUMERIC'
|
2022-03-15 22:27:51 +00:00
|
|
|
|
2022-07-10 18:05:44 +00:00
|
|
|
const unfilledBets = useUnfilledBets(contract.id) ?? []
|
|
|
|
|
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-07-02 19:37:59 +00:00
|
|
|
{amountRedeemed} {isPseudoNumeric ? 'HIGHER' : 'YES'} shares and{' '}
|
|
|
|
{amountRedeemed} {isPseudoNumeric ? 'LOWER' : 'NO'} shares
|
2022-03-15 22:27:51 +00:00
|
|
|
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-05-19 17:42:03 +00:00
|
|
|
{!isCPMM && !isNumeric && (
|
|
|
|
<th>{isResolved ? <>Payout</> : <>Sale price</>}</th>
|
|
|
|
)}
|
2022-03-15 22:27:51 +00:00
|
|
|
{!isCPMM && !isResolved && <th>Payout if chosen</th>}
|
2021-12-31 20:13:06 +00:00
|
|
|
<th>Shares</th>
|
2022-07-02 19:37:59 +00:00
|
|
|
{!isPseudoNumeric && <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}
|
2022-06-01 16:28:47 +00:00
|
|
|
isYourBet={isYourBets}
|
2022-07-10 18:05:44 +00:00
|
|
|
unfilledBets={unfilledBets}
|
2021-12-24 21:06:01 +00:00
|
|
|
/>
|
2021-12-15 22:57:40 +00:00
|
|
|
))}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-01 16:28:47 +00:00
|
|
|
function BetRow(props: {
|
|
|
|
bet: Bet
|
|
|
|
contract: Contract
|
|
|
|
saleBet?: Bet
|
|
|
|
isYourBet: boolean
|
2022-07-10 18:05:44 +00:00
|
|
|
unfilledBets: LimitBet[]
|
2022-06-01 16:28:47 +00:00
|
|
|
}) {
|
2022-07-10 18:05:44 +00:00
|
|
|
const { bet, saleBet, contract, isYourBet, unfilledBets } = 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-05-19 17:42:03 +00:00
|
|
|
const { isResolved, closeTime, mechanism, outcomeType } = contract
|
2022-03-15 22:27:51 +00:00
|
|
|
|
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-05-19 17:42:03 +00:00
|
|
|
const isNumeric = outcomeType === 'NUMERIC'
|
2022-07-02 19:37:59 +00:00
|
|
|
const isPseudoNumeric = outcomeType === 'PSEUDO_NUMERIC'
|
2022-03-15 22:27:51 +00:00
|
|
|
|
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)
|
2022-07-10 18:05:44 +00:00
|
|
|
: calculateSaleAmount(contract, bet, unfilledBets)
|
2022-01-19 22:36:55 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-03-01 00:44:25 +00:00
|
|
|
const payoutIfChosenDisplay =
|
2022-05-19 17:42:03 +00:00
|
|
|
bet.isAnte && outcomeType === 'FREE_RESPONSE' && bet.outcome === '0'
|
2022-03-01 00:44:25 +00:00
|
|
|
? 'N/A'
|
|
|
|
: formatMoney(calculatePayout(contract, bet, bet.outcome))
|
2022-03-01 00:38:09 +00:00
|
|
|
|
2022-07-11 15:49:33 +00:00
|
|
|
const hadPoolMatch =
|
2022-07-11 20:53:11 +00:00
|
|
|
(bet.limitProb === undefined ||
|
|
|
|
bet.fills?.some((fill) => fill.matchedBetId === null)) ??
|
|
|
|
false
|
2022-07-11 15:49:33 +00:00
|
|
|
|
|
|
|
const ofTotalAmount =
|
|
|
|
bet.limitProb === undefined || bet.orderAmount === undefined
|
|
|
|
? ''
|
|
|
|
: ` / ${formatMoney(bet.orderAmount)}`
|
|
|
|
|
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-06-01 16:28:47 +00:00
|
|
|
{isYourBet &&
|
|
|
|
!isCPMM &&
|
2022-05-19 17:42:03 +00:00
|
|
|
!isResolved &&
|
|
|
|
!isClosed &&
|
|
|
|
!isSold &&
|
|
|
|
!isAnte &&
|
|
|
|
!isNumeric && <SellButton contract={contract} bet={bet} />}
|
2022-02-21 20:22:36 +00:00
|
|
|
</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>
|
2022-05-19 17:42:03 +00:00
|
|
|
{bet.isAnte ? (
|
2022-04-18 23:02:40 +00:00
|
|
|
'ANTE'
|
|
|
|
) : (
|
|
|
|
<OutcomeLabel
|
|
|
|
outcome={outcome}
|
2022-05-19 17:42:03 +00:00
|
|
|
value={(bet as any).value}
|
2022-04-18 23:02:40 +00:00
|
|
|
contract={contract}
|
|
|
|
truncate="short"
|
|
|
|
/>
|
|
|
|
)}
|
2022-07-02 19:37:59 +00:00
|
|
|
{isPseudoNumeric &&
|
|
|
|
' than ' + formatNumericProbability(bet.probAfter, contract)}
|
2021-12-16 21:22:00 +00:00
|
|
|
</td>
|
2022-07-11 15:49:33 +00:00
|
|
|
<td>
|
|
|
|
{formatMoney(Math.abs(amount))}
|
|
|
|
{ofTotalAmount}
|
|
|
|
</td>
|
2022-05-19 17:42:03 +00:00
|
|
|
{!isCPMM && !isNumeric && <td>{saleDisplay}</td>}
|
2022-03-15 22:27:51 +00:00
|
|
|
{!isCPMM && !isResolved && <td>{payoutIfChosenDisplay}</td>}
|
2022-03-29 19:56:56 +00:00
|
|
|
<td>{formatWithCommas(Math.abs(shares))}</td>
|
2022-07-02 19:37:59 +00:00
|
|
|
{!isPseudoNumeric && (
|
|
|
|
<td>
|
2022-07-11 15:49:33 +00:00
|
|
|
{outcomeType === 'FREE_RESPONSE' || hadPoolMatch ? (
|
|
|
|
<>
|
|
|
|
{formatPercent(probBefore)} → {formatPercent(probAfter)}
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
formatPercent(bet.limitProb ?? 0)
|
|
|
|
)}
|
2022-07-02 19:37:59 +00:00
|
|
|
</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 }) {
|
|
|
|
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-07-10 18:05:44 +00:00
|
|
|
const unfilledBets = useUnfilledBets(contract.id) ?? []
|
|
|
|
|
|
|
|
const outcomeProb = getProbabilityAfterSale(
|
|
|
|
contract,
|
|
|
|
outcome,
|
|
|
|
shares,
|
|
|
|
unfilledBets
|
|
|
|
)
|
2022-02-18 00:24:00 +00:00
|
|
|
|
2022-07-10 18:05:44 +00:00
|
|
|
const saleAmount = calculateSaleAmount(contract, bet, unfilledBets)
|
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
|
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{' '}
|
2022-04-18 23:02:40 +00:00
|
|
|
<OutcomeLabel outcome={outcome} contract={contract} truncate="long" />{' '}
|
|
|
|
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>
|
|
|
|
)
|
|
|
|
}
|