1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
import { useState } from 'react';
import {
api,
EventDetail,
eventPath,
EventSummary,
feeLabel,
FeeTiming,
MarketData,
Mechanism,
mechanismLabel,
money,
num,
OrderBook,
same,
SEP,
Status,
} from './api';
import type { Act } from './App';
import { LineChart, priceSeries } from './Chart';
import { Equalizer } from './graphics';
const STATUS_PILL: Record<Status, [string, string]> = {
DRAFT: ['', 'IDLE'],
ACTIVE: ['active', 'ACTIVE'],
SETTLED: ['closed', 'CLOSED'],
};
function Filter<T extends string>(props: {
label: string;
value: T | null;
options: [T, string][];
onChange: (value: T | null) => void;
}) {
return (
<div className="filter">
<span>{props.label}</span>
<button className={props.value === null ? 'toggle on' : 'toggle'} onClick={() => props.onChange(null)}>
All
</button>
{props.options.map(([value, text]) => (
<button
key={value}
className={props.value === value ? 'toggle on' : 'toggle'}
onClick={() => props.onChange(value)}
>
{text}
</button>
))}
</div>
);
}
export function Events(props: {
data: MarketData;
user: string;
selected: EventSummary | undefined;
onSelect: (key: string) => void;
act: Act;
onTrade: () => void;
onResolve: () => void;
}) {
const { data, selected } = props;
const [method, setMethod] = useState<Mechanism | null>(null);
const [status, setStatus] = useState<Status | null>(null);
const [fee, setFee] = useState<FeeTiming | null>(null);
const shown = data.events.filter(
(e) =>
(method === null || e.mechanism === method) &&
(status === null || e.status === status) &&
(fee === null || e.commissionTiming === fee),
);
const detail = selected && data.details[selected.key];
return (
<>
<section className="card side events">
<Filter
label="Method"
value={method}
onChange={setMethod}
options={[
['LMSR', 'LMSR'],
['ORDER_BOOK', 'Order Book'],
]}
/>
<Filter
label="Status"
value={status}
onChange={setStatus}
options={[
['DRAFT', 'Not started'],
['ACTIVE', 'Active'],
['SETTLED', 'Closed'],
]}
/>
<Filter
label="Fee"
value={fee}
onChange={setFee}
options={[
['ON_PURCHASE', 'On purchase'],
['ON_CLOSE', 'On close'],
]}
/>
<div className="list">
{shown.map((e) => (
<button
key={e.key}
className={e.key === selected?.key ? 'listrow on' : 'listrow'}
style={{ flexDirection: 'column', alignItems: 'stretch', gap: 6 }}
onClick={() => props.onSelect(e.key)}
>
<span className="row">
<span className="sm ink2">#{e.displayId}</span>
<span className="bold ellipsis">{e.name}</span>
</span>
<span className="row sm ink2">
<span>{mechanismLabel(e.mechanism)}</span>
<span className={'pill ' + STATUS_PILL[e.status][0]}>{STATUS_PILL[e.status][1]}</span>
<span>{feeLabel(e)}</span>
<span className="grow" />
<span>{money(e.accountBalance)}</span>
</span>
</button>
))}
</div>
<span className="sm ink2">
{shown.length} of {data.events.length} events shown
</span>
</section>
{detail ? (
<EventView detail={detail} user={props.user} act={props.act} onTrade={props.onTrade} onResolve={props.onResolve} />
) : (
<section className="card main" />
)}
</>
);
}
function EventView(props: { detail: EventDetail; user: string; act: Act; onTrade: () => void; onResolve: () => void }) {
const { detail, user, act } = props;
const e = detail.summary;
const markets = detail.state.markets;
const isMm = same(e.marketMaker, user);
const live = e.status === 'ACTIVE' ? 'LIVE' : e.status === 'SETTLED' ? 'ENDED' : 'IDLE';
const yes = priceSeries(detail);
return (
<section className="card main">
<div className="head" style={{ flexDirection: 'column', gap: 9 }}>
<div className="row" style={{ alignItems: 'flex-start', gap: 14, width: '100%' }}>
<div className="grow">
<h2 className="xl bold">
{e.displayId}. {e.name}
</h2>
<p className="base ink2 ellipsis" title={e.description}>
{e.description}
</p>
</div>
{/* Open = go trade on an active event, or the Market Maker starting their own */}
{e.status === 'ACTIVE' && (
<button className="btn primary" onClick={props.onTrade}>
Trade
</button>
)}
{e.status === 'DRAFT' && isMm && (
<button
className="btn primary"
onClick={() => act(() => api(eventPath(e.name) + '/open', {}), `Event started`)}
>
Start Event
</button>
)}
{e.status === 'ACTIVE' && isMm && (
<button className="btn" onClick={props.onResolve}>
Close & Resolve…
</button>
)}
</div>
<div className="meta">
<span className={'pill ' + STATUS_PILL[e.status][0]}>
<Equalizer />
{live}
</span>
<span>
<span className="ink2">Method</span> <b>{mechanismLabel(e.mechanism)}</b>
</span>
<span>
<span className="ink2">Status</span> <b>{e.status}</b>
</span>
<span>
<span className="ink2">Market Maker</span> <b>{e.marketMaker}</b>
</span>
<span>
<span className="ink2">Fee</span> <b>{feeLabel(e)}</b>
</span>
<span>
<span className="ink2">Contract</span> <b>{money(e.accountBalance)}</b>
</span>
</div>
</div>
<div className="body">
{e.status === 'SETTLED' && (
<div className="notice">
<span className="pill resolved">RESOLVED</span>
<span>
Winning option: <b>{detail.settledMarket}</b> {SEP} contract emptied to its holders
</span>
</div>
)}
{e.mechanism === 'LMSR' ? (
<>
<div className="pair">
{markets.map((m, i) => (
<div key={m.key} className={i === 0 ? 'optcard yes' : 'optcard no'}>
<div className="row">
<span className="bold grow ellipsis">
{i === 0 ? 'YES' : 'NO'} <span className="sm">{m.name}</span>
</span>
<span className="bold" style={{ fontSize: 22 }}>
{money(m.price)}
</span>
</div>
<div className="bar">
<i style={{ width: `${num(m.price) * 100}%` }} />
</div>
<span className="sm ink2">
{m.volume} shares {SEP} p = {num(m.price).toFixed(2)}
</span>
</div>
))}
</div>
<div className="section">
<div className="section-title">
Share price over time <span className="grow" />
<span className="sm ink2" style={{ fontWeight: 400 }}>
YES / NO {SEP} LMSR
</span>
</div>
<LineChart
range={[0, 1]}
series={[
{ points: yes, tone: 'yes' },
{ points: yes.map(([t, v]) => [t, 1 - v]), tone: 'no' },
]}
/>
</div>
</>
) : (
<>
<div className="pair">
{detail.orderBooks.map((book, i) => (
<Book key={book.optionKey} book={book} last={markets[i]?.price} side={i === 0 ? 'YES' : 'NO'} />
))}
</div>
<div className="section">
<div className="section-title">
Last trade price over time <span className="grow" />
<span className="sm ink2" style={{ fontWeight: 400 }}>
last trade YES
</span>
</div>
<LineChart range={[0, 1]} series={[{ points: yes, tone: 'accent' }]} />
</div>
</>
)}
<div className="section">
<div className="section-title">
Trade history <span className="sm ink2">newest first</span>
</div>
<div className="table">
<div className="thead">
<span className="w66">#</span>
<span className="grow">User</span>
<span className="w150">Option</span>
<span className="w92 num">Shares</span>
<span className="w92 num">Paid</span>
</div>
{detail.history.map((t, i) => (
<div className="trow" key={i}>
<span className="w66 ink2 bold">#{detail.history.length - i}</span>
<span className="grow ellipsis">{t.userName}</span>
<span className={`w150 bold ellipsis ${t.optionName === e.optionNames[0] ? 'yes' : 'no'}`}>
{t.optionName}
</span>
<span className="w92 num">{t.quantity} shares</span>
<span className="w92 num bold accent">{money(t.pricePaid)}</span>
</div>
))}
{detail.history.length === 0 && <div className="trow ink2">No trades yet</div>}
</div>
</div>
<div className="section">
<div className="section-title">Participants</div>
<div className="table">
<div className="thead">
<span className="grow">User</span>
<span className="w78 num">YES</span>
<span className="w78 num">NO</span>
<span className="w92 num">Value</span>
<span className="w92 num">Fees paid</span>
</div>
{detail.participants.map((p) => (
<div className="trow" key={p.user}>
<span className="grow ellipsis bold">
{p.user}
{p.tag && ` (${p.tag})`}
</span>
<span className="w78 num">{p.yes}</span>
<span className="w78 num">{p.no}</span>
<span className="w92 num">{p.value}</span>
<span className="w92 num">{p.fees}</span>
</div>
))}
{detail.participants.length === 0 && <div className="trow ink2">Nobody yet</div>}
</div>
</div>
</div>
</section>
);
}
function Book({ book, last, side }: { book: OrderBook; last: string | undefined; side: string }) {
const mid = book.bestBid && book.bestAsk ? money((num(book.bestBid) + num(book.bestAsk)) / 2) : '—';
return (
<div className="table">
<div className="row" style={{ padding: '10px 11px 0' }}>
<span className={side === 'YES' ? 'bold yes' : 'bold no'}>{side} Order Book</span>
<span className="sm ink2 ellipsis grow">{book.optionName}</span>
<span className="sm ink2">{book.orders.length} orders</span>
</div>
<div className="stats">
{[
['Last', money(last)],
['Best bid', book.bestBid ?? '—'],
['Best ask', book.bestAsk ?? '—'],
['Mid', mid],
['Spread', book.spread ?? '—'],
].map(([label, value]) => (
<span key={label}>
<span className="xs ink2">{label}</span>
<b>{value}</b>
</span>
))}
</div>
<div className="thead">
<span className="w54 num">Price</span>
<span className="w54 num">Side</span>
<span className="w54 num">Qty</span>
<span className="grow">User</span>
</div>
{book.orders.map((o, i) => (
<div className={o.isBid ? 'trow bid' : 'trow ask'} key={i}>
<span className="w54 num bold">{o.price}</span>
<span className={o.isBid ? 'w54 num yes' : 'w54 num no'}>{o.side}</span>
<span className="w54 num">{o.quantity}</span>
<span className="grow ellipsis">{o.user}</span>
</div>
))}
{book.orders.length === 0 && <div className="trow ink2">No resting orders</div>}
</div>
);
}
|