import { useState } from 'react';
import { api, EventSummary, feeLabel, MarketData, mechanismLabel, money, num, same, SEP } from './api';
import type { Act } from './App';
import { balanceSeries, LineChart } from './Chart';
import { Sparkline } from './graphics';
export function Users(props: {
data: MarketData;
user: string;
selected: EventSummary | undefined;
onSelect: (key: string) => void;
act: Act;
onDeposit: () => void;
}) {
const { data, user, selected } = props;
const account = data.account;
const balance = num(account.balance);
const myEvents = data.events.flatMap((e) => {
const me = data.details[e.key]?.participants.find((p) => same(p.user, user));
const owner = same(e.marketMaker, user);
return owner || (me && (me.yes > 0 || me.no > 0)) ? [{ e, me, owner }] : [];
});
const isMm = myEvents.some((row) => row.owner);
return (
<>
User
Balance
{/* A directory of everyone on the server; you can only ever act as yourself */}
{data.users.map((u) => {
const me = same(u.username, user);
return (
{u.username}
{u.marketMaker ? 'Market Maker' : 'Trader'}
{me ? ' (you)' : u.online ? '' : ' · offline'}
{money(u.balance)}
);
})}
{user}
{account.blocked ? 'BLOCKED' : isMm ? 'MARKET MAKER' : 'TRADER'}
Active in {myEvents.length} events
Balance
{money(account.balance)}
{account.blocked && (
BLOCKED
This account reached a negative balance. All trading actions are disabled until you deposit enough
funds to bring it back above zero.
)}
Account balance over time
every trade, fee and payout
Events participation / owner
Event
Role
Type
YES
NO
P/L
{myEvents.map(({ e, me, owner }) => {
const pnl = e.status === 'SETTLED' ? (me?.pnl ?? '$0.00') : '—';
return (
props.onSelect(e.key)}
>
{e.name}
{owner ? 'Market Maker' : 'Trader'}
{mechanismLabel(e.mechanism)}
{me?.yes ?? 0}
{me?.no ?? 0}
{pnl}
);
})}
{myEvents.length === 0 &&
You are not part of any event yet
}
{/* key: a different event starts with a fresh form */}
{selected ? (
) : (
Select an event to trade.
)}
Account activity
newest first
Time
Type
Details
Amount
Balance
{/* seq is not unique across entries, so the position is the key */}
{[...account.entries].reverse().map((row, i) => (
{row.at}
{row.type}
{row.note}
{row.amount}
{row.balanceAfter}
))}
{account.entries.length === 0 && (
Nothing yet. Deposit funds to start trading.
)}
>
);
}
function TradePanel(props: { data: MarketData; user: string; act: Act; event: EventSummary }) {
const { data, user, event: e } = props;
const detail = data.details[e.key];
const markets = detail?.state.markets ?? [];
const me = detail?.participants.find((p) => same(p.user, user));
const held = [me?.yes ?? 0, me?.no ?? 0];
const isBook = e.mechanism === 'ORDER_BOOK';
const [option, setOption] = useState(0);
const [side, setSide] = useState<'BUY' | 'SELL'>('BUY');
const [quantity, setQuantity] = useState('50');
const [price, setPrice] = useState('0.62');
const qty = Number(quantity);
const qtyOk = Number.isInteger(qty) && qty > 0;
const unit = isBook ? Number(price) : num(markets[option]?.price);
const priceOk = unit > 0;
const cost = qty * unit;
let hint: string;
let allowed = false;
if (!qtyOk) hint = 'Shares must be a positive whole number.';
else if (!priceOk) hint = 'Price per share must be positive.';
else if (data.account.blocked) hint = 'Trading is disabled because this account is blocked.';
else if (e.status !== 'ACTIVE') hint = 'Trading is only available for ACTIVE events.';
else if (side === 'SELL' && !same(e.marketMaker, user) && qty > held[option])
hint = `Cannot sell ${qty} shares; you only hold ${held[option]}.`;
else if (side === 'BUY' && num(data.account.balance) < cost)
hint = `Insufficient balance. Required: ${money(cost)}, available: ${money(data.account.balance)}`;
else {
allowed = true;
hint =
side === 'SELL'
? 'Sell order will match existing bids or rest as an ask.'
: isBook
? 'Buy order will match existing asks or rest as a bid.'
: 'Orders execute immediately against the market maker.';
}
const submit = () =>
props.act(
() =>
api('/orders', {
event: e.name,
option: markets[option].key,
side,
price: isBook ? price.trim() : markets[option].price,
quantity: String(qty),
}),
`Order placed ${SEP} ${side === 'BUY' ? 'Bought' : 'Sold'} ${qty} ${e.optionNames[option]}`,
);
return (
);
}