diff options
Diffstat (limited to 'ui-web/src/Users.tsx')
| -rw-r--r-- | ui-web/src/Users.tsx | 298 |
1 files changed, 298 insertions, 0 deletions
diff --git a/ui-web/src/Users.tsx b/ui-web/src/Users.tsx new file mode 100644 index 0000000..b2593d9 --- /dev/null +++ b/ui-web/src/Users.tsx | |||
| @@ -0,0 +1,298 @@ | |||
| 1 | import { useState } from 'react'; | ||
| 2 | import { api, EventSummary, feeLabel, MarketData, mechanismLabel, money, num, same, SEP } from './api'; | ||
| 3 | import type { Act } from './App'; | ||
| 4 | import { balanceSeries, LineChart } from './Chart'; | ||
| 5 | import { Sparkline } from './graphics'; | ||
| 6 | |||
| 7 | export function Users(props: { | ||
| 8 | data: MarketData; | ||
| 9 | user: string; | ||
| 10 | selected: EventSummary | undefined; | ||
| 11 | onSelect: (key: string) => void; | ||
| 12 | act: Act; | ||
| 13 | onDeposit: () => void; | ||
| 14 | }) { | ||
| 15 | const { data, user, selected } = props; | ||
| 16 | const account = data.account; | ||
| 17 | const balance = num(account.balance); | ||
| 18 | |||
| 19 | const myEvents = data.events.flatMap((e) => { | ||
| 20 | const me = data.details[e.key]?.participants.find((p) => same(p.user, user)); | ||
| 21 | const owner = same(e.marketMaker, user); | ||
| 22 | return owner || (me && (me.yes > 0 || me.no > 0)) ? [{ e, me, owner }] : []; | ||
| 23 | }); | ||
| 24 | const isMm = myEvents.some((row) => row.owner); | ||
| 25 | |||
| 26 | return ( | ||
| 27 | <> | ||
| 28 | <section className="card side users"> | ||
| 29 | <div className="row xs bold ink2"> | ||
| 30 | <span className="grow">User</span> | ||
| 31 | <span>Balance</span> | ||
| 32 | </div> | ||
| 33 | {/* A directory of everyone on the server; you can only ever act as yourself */} | ||
| 34 | <div className="list"> | ||
| 35 | {data.users.map((u) => { | ||
| 36 | const me = same(u.username, user); | ||
| 37 | return ( | ||
| 38 | <div key={u.username} className={me ? 'listrow plain on' : 'listrow plain'}> | ||
| 39 | <span className="grow"> | ||
| 40 | <span className="bold ellipsis" style={{ display: 'block' }}> | ||
| 41 | {u.username} | ||
| 42 | </span> | ||
| 43 | <span className="xs ink2"> | ||
| 44 | {u.marketMaker ? 'Market Maker' : 'Trader'} | ||
| 45 | {me ? ' (you)' : u.online ? '' : ' · offline'} | ||
| 46 | </span> | ||
| 47 | </span> | ||
| 48 | <span className={num(u.balance) < 0 ? 'balcell neg' : 'balcell'}>{money(u.balance)}</span> | ||
| 49 | </div> | ||
| 50 | ); | ||
| 51 | })} | ||
| 52 | </div> | ||
| 53 | <button className="btn primary" onClick={props.onDeposit}> | ||
| 54 | Deposit Funds | ||
| 55 | </button> | ||
| 56 | </section> | ||
| 57 | |||
| 58 | <section className="card main"> | ||
| 59 | <div className="head" style={{ alignItems: 'center' }}> | ||
| 60 | <div className="grow"> | ||
| 61 | <div className="row" style={{ gap: 9 }}> | ||
| 62 | <h2 className="xl bold">{user}</h2> | ||
| 63 | <span className={account.blocked ? 'pill blocked' : isMm ? 'pill mm' : 'pill'}> | ||
| 64 | {account.blocked ? 'BLOCKED' : isMm ? 'MARKET MAKER' : 'TRADER'} | ||
| 65 | </span> | ||
| 66 | </div> | ||
| 67 | <p className="base ink2">Active in {myEvents.length} events</p> | ||
| 68 | </div> | ||
| 69 | <Sparkline negative={balance < 0} /> | ||
| 70 | <div style={{ textAlign: 'right' }}> | ||
| 71 | <div className="base ink2">Balance</div> | ||
| 72 | <div className={balance < 0 ? 'balance neg' : 'balance'}>{money(account.balance)}</div> | ||
| 73 | </div> | ||
| 74 | </div> | ||
| 75 | |||
| 76 | <div className="body"> | ||
| 77 | {account.blocked && ( | ||
| 78 | <div className="notice blocked"> | ||
| 79 | <span className="pill blocked">BLOCKED</span> | ||
| 80 | <span> | ||
| 81 | This account reached a negative balance. All trading actions are disabled until you deposit enough | ||
| 82 | funds to bring it back above zero. | ||
| 83 | </span> | ||
| 84 | </div> | ||
| 85 | )} | ||
| 86 | |||
| 87 | <div className="section"> | ||
| 88 | <div className="section-title"> | ||
| 89 | Account balance over time <span className="grow" /> | ||
| 90 | <span className="sm ink2" style={{ fontWeight: 400 }}> | ||
| 91 | every trade, fee and payout | ||
| 92 | </span> | ||
| 93 | </div> | ||
| 94 | <LineChart series={[{ points: balanceSeries(account), tone: balance < 0 ? 'no' : 'accent' }]} /> | ||
| 95 | </div> | ||
| 96 | |||
| 97 | <div className="section"> | ||
| 98 | <div className="section-title">Events participation / owner</div> | ||
| 99 | <div className="table"> | ||
| 100 | <div className="thead"> | ||
| 101 | <span className="grow">Event</span> | ||
| 102 | <span className="w92">Role</span> | ||
| 103 | <span className="w78">Type</span> | ||
| 104 | <span className="w78 num">YES</span> | ||
| 105 | <span className="w78 num">NO</span> | ||
| 106 | <span className="w92 num">P/L</span> | ||
| 107 | </div> | ||
| 108 | {myEvents.map(({ e, me, owner }) => { | ||
| 109 | const pnl = e.status === 'SETTLED' ? (me?.pnl ?? '$0.00') : '—'; | ||
| 110 | return ( | ||
| 111 | <div | ||
| 112 | key={e.key} | ||
| 113 | className={e.key === selected?.key ? 'trow click on' : 'trow click'} | ||
| 114 | onClick={() => props.onSelect(e.key)} | ||
| 115 | > | ||
| 116 | <span className="grow ellipsis">{e.name}</span> | ||
| 117 | <span className="w92 ink2">{owner ? 'Market Maker' : 'Trader'}</span> | ||
| 118 | <span className="w78 ink2">{mechanismLabel(e.mechanism)}</span> | ||
| 119 | <span className="w78 num">{me?.yes ?? 0}</span> | ||
| 120 | <span className="w78 num">{me?.no ?? 0}</span> | ||
| 121 | <span className={num(pnl) < 0 ? 'w92 num pnl neg' : 'w92 num pnl'}>{pnl}</span> | ||
| 122 | </div> | ||
| 123 | ); | ||
| 124 | })} | ||
| 125 | {myEvents.length === 0 && <div className="trow ink2">You are not part of any event yet</div>} | ||
| 126 | </div> | ||
| 127 | </div> | ||
| 128 | |||
| 129 | {/* key: a different event starts with a fresh form */} | ||
| 130 | {selected ? ( | ||
| 131 | <TradePanel key={selected.key} {...props} event={selected} /> | ||
| 132 | ) : ( | ||
| 133 | <div className="card2 base ink2" style={{ padding: '13px 14px' }}> | ||
| 134 | Select an event to trade. | ||
| 135 | </div> | ||
| 136 | )} | ||
| 137 | |||
| 138 | <div className="section"> | ||
| 139 | <div className="section-title"> | ||
| 140 | Account activity <span className="grow" /> | ||
| 141 | <span className="sm ink2" style={{ fontWeight: 400 }}> | ||
| 142 | newest first | ||
| 143 | </span> | ||
| 144 | </div> | ||
| 145 | <div className="table"> | ||
| 146 | <div className="thead"> | ||
| 147 | <span className="w150">Time</span> | ||
| 148 | <span className="w92">Type</span> | ||
| 149 | <span className="grow">Details</span> | ||
| 150 | <span className="w92 num">Amount</span> | ||
| 151 | <span className="w92 num">Balance</span> | ||
| 152 | </div> | ||
| 153 | {/* seq is not unique across entries, so the position is the key */} | ||
| 154 | {[...account.entries].reverse().map((row, i) => ( | ||
| 155 | <div className="trow" key={account.entries.length - i}> | ||
| 156 | <span className="w150 ink2">{row.at}</span> | ||
| 157 | <span className="w92 ink2">{row.type}</span> | ||
| 158 | <span className="grow ellipsis">{row.note}</span> | ||
| 159 | <span className="w92 num">{row.amount}</span> | ||
| 160 | <span className="w92 num">{row.balanceAfter}</span> | ||
| 161 | </div> | ||
| 162 | ))} | ||
| 163 | {account.entries.length === 0 && ( | ||
| 164 | <div className="trow ink2">Nothing yet. Deposit funds to start trading.</div> | ||
| 165 | )} | ||
| 166 | </div> | ||
| 167 | </div> | ||
| 168 | </div> | ||
| 169 | </section> | ||
| 170 | </> | ||
| 171 | ); | ||
| 172 | } | ||
| 173 | |||
| 174 | function TradePanel(props: { data: MarketData; user: string; act: Act; event: EventSummary }) { | ||
| 175 | const { data, user, event: e } = props; | ||
| 176 | const detail = data.details[e.key]; | ||
| 177 | const markets = detail?.state.markets ?? []; | ||
| 178 | const me = detail?.participants.find((p) => same(p.user, user)); | ||
| 179 | const held = [me?.yes ?? 0, me?.no ?? 0]; | ||
| 180 | const isBook = e.mechanism === 'ORDER_BOOK'; | ||
| 181 | |||
| 182 | const [option, setOption] = useState(0); | ||
| 183 | const [side, setSide] = useState<'BUY' | 'SELL'>('BUY'); | ||
| 184 | const [quantity, setQuantity] = useState('50'); | ||
| 185 | const [price, setPrice] = useState('0.62'); | ||
| 186 | |||
| 187 | const qty = Number(quantity); | ||
| 188 | const qtyOk = Number.isInteger(qty) && qty > 0; | ||
| 189 | const unit = isBook ? Number(price) : num(markets[option]?.price); | ||
| 190 | const priceOk = unit > 0; | ||
| 191 | const cost = qty * unit; | ||
| 192 | |||
| 193 | let hint: string; | ||
| 194 | let allowed = false; | ||
| 195 | if (!qtyOk) hint = 'Shares must be a positive whole number.'; | ||
| 196 | else if (!priceOk) hint = 'Price per share must be positive.'; | ||
| 197 | else if (data.account.blocked) hint = 'Trading is disabled because this account is blocked.'; | ||
| 198 | else if (e.status !== 'ACTIVE') hint = 'Trading is only available for ACTIVE events.'; | ||
| 199 | else if (side === 'SELL' && !same(e.marketMaker, user) && qty > held[option]) | ||
| 200 | hint = `Cannot sell ${qty} shares; you only hold ${held[option]}.`; | ||
| 201 | else if (side === 'BUY' && num(data.account.balance) < cost) | ||
| 202 | hint = `Insufficient balance. Required: ${money(cost)}, available: ${money(data.account.balance)}`; | ||
| 203 | else { | ||
| 204 | allowed = true; | ||
| 205 | hint = | ||
| 206 | side === 'SELL' | ||
| 207 | ? 'Sell order will match existing bids or rest as an ask.' | ||
| 208 | : isBook | ||
| 209 | ? 'Buy order will match existing asks or rest as a bid.' | ||
| 210 | : 'Orders execute immediately against the market maker.'; | ||
| 211 | } | ||
| 212 | |||
| 213 | const submit = () => | ||
| 214 | props.act( | ||
| 215 | () => | ||
| 216 | api('/orders', { | ||
| 217 | event: e.name, | ||
| 218 | option: markets[option].key, | ||
| 219 | side, | ||
| 220 | price: isBook ? price.trim() : markets[option].price, | ||
| 221 | quantity: String(qty), | ||
| 222 | }), | ||
| 223 | `Order placed ${SEP} ${side === 'BUY' ? 'Bought' : 'Sold'} ${qty} ${e.optionNames[option]}`, | ||
| 224 | ); | ||
| 225 | |||
| 226 | return ( | ||
| 227 | <div className="card2 section" style={{ padding: '13px 14px', gap: 9 }}> | ||
| 228 | <div className="row" style={{ alignItems: 'baseline' }}> | ||
| 229 | <span className="lg bold grow ellipsis"> | ||
| 230 | {e.displayId}. {e.name} | ||
| 231 | </span> | ||
| 232 | <span className="sm ink2"> | ||
| 233 | {mechanismLabel(e.mechanism)} {SEP} Status: {e.status} {SEP} Fee: {feeLabel(e)} | ||
| 234 | </span> | ||
| 235 | </div> | ||
| 236 | <div className="pair"> | ||
| 237 | {markets.map((m, i) => ( | ||
| 238 | <button | ||
| 239 | key={m.key} | ||
| 240 | className={`optcard pick ${i === 0 ? 'yes' : 'no'}${option === i ? ' on' : ''}`} | ||
| 241 | onClick={() => setOption(i)} | ||
| 242 | > | ||
| 243 | <span className="row"> | ||
| 244 | <span className={i === 0 ? 'bold yes grow ellipsis' : 'bold no grow ellipsis'}> | ||
| 245 | {i === 0 ? 'YES' : 'NO'} <span className="sm">{m.name}</span> | ||
| 246 | </span> | ||
| 247 | <span className={i === 0 ? 'xl bold yes' : 'xl bold no'}>{money(m.price)}</span> | ||
| 248 | </span> | ||
| 249 | <span className="sm ink2">You hold {held[i]} shares</span> | ||
| 250 | </button> | ||
| 251 | ))} | ||
| 252 | </div> | ||
| 253 | <div className="tradeform"> | ||
| 254 | <label> | ||
| 255 | Action | ||
| 256 | <span> | ||
| 257 | <button className={side === 'BUY' ? 'seg on' : 'seg'} onClick={() => setSide('BUY')}> | ||
| 258 | Buy | ||
| 259 | </button> | ||
| 260 | <button className={side === 'SELL' ? 'seg on' : 'seg'} onClick={() => setSide('SELL')}> | ||
| 261 | Sell | ||
| 262 | </button> | ||
| 263 | </span> | ||
| 264 | </label> | ||
| 265 | <label> | ||
| 266 | Shares | ||
| 267 | <input | ||
| 268 | className={qtyOk ? 'field' : 'field bad'} | ||
| 269 | inputMode="numeric" | ||
| 270 | value={quantity} | ||
| 271 | onChange={(ev) => setQuantity(ev.target.value)} | ||
| 272 | /> | ||
| 273 | </label> | ||
| 274 | {isBook && ( | ||
| 275 | <label> | ||
| 276 | Price / share | ||
| 277 | <input | ||
| 278 | className={priceOk ? 'field' : 'field bad'} | ||
| 279 | inputMode="decimal" | ||
| 280 | value={price} | ||
| 281 | onChange={(ev) => setPrice(ev.target.value)} | ||
| 282 | /> | ||
| 283 | </label> | ||
| 284 | )} | ||
| 285 | <label> | ||
| 286 | Estimated cost | ||
| 287 | <span className="lg bold" style={{ color: 'var(--ink)', padding: '7px 0' }}> | ||
| 288 | {qtyOk && priceOk ? money(cost) : '—'} | ||
| 289 | </span> | ||
| 290 | </label> | ||
| 291 | <button className="btn primary" disabled={!allowed} onClick={submit}> | ||
| 292 | Place {side === 'BUY' ? 'Buy' : 'Sell'} Order | ||
| 293 | </button> | ||
| 294 | </div> | ||
| 295 | <span className="sm ink2">{hint}</span> | ||
| 296 | </div> | ||
| 297 | ); | ||
| 298 | } | ||