aboutsummaryrefslogtreecommitdiffstats
path: root/ui-web/src/api.ts
diff options
context:
space:
mode:
Diffstat (limited to 'ui-web/src/api.ts')
-rw-r--r--ui-web/src/api.ts176
1 files changed, 176 insertions, 0 deletions
diff --git a/ui-web/src/api.ts b/ui-web/src/api.ts
new file mode 100644
index 0000000..2ce1a8d
--- /dev/null
+++ b/ui-web/src/api.ts
@@ -0,0 +1,176 @@
1
2export type Mechanism = 'LMSR' | 'ORDER_BOOK';
3export type Status = 'DRAFT' | 'ACTIVE' | 'SETTLED';
4export type FeeTiming = 'ON_PURCHASE' | 'ON_CLOSE';
5
6export interface EventSummary {
7 key: string;
8 displayId: number;
9 name: string;
10 description: string;
11 commissionPercent: number;
12 commissionTiming: FeeTiming;
13 mechanism: Mechanism;
14 status: Status;
15 optionNames: string[];
16 accountBalance: string;
17 marketMaker: string;
18}
19
20export interface Market {
21 key: string;
22 name: string;
23 price: string;
24 volume: string;
25}
26
27export interface BookOrder {
28 side: string;
29 user: string;
30 quantity: number;
31 price: string;
32 isBid: boolean;
33}
34
35export interface OrderBook {
36 optionKey: string;
37 optionName: string;
38 bestBid: string | null;
39 bestAsk: string | null;
40 spread: string | null;
41 orders: BookOrder[];
42}
43
44export interface TradeRow {
45 at: string;
46 userName: string;
47 optionName: string;
48 quantity: string;
49 pricePaid: string;
50}
51
52export interface Participant {
53 user: string;
54 tag: string;
55 yes: number;
56 no: number;
57 value: string;
58 fees: string;
59 pnl: string;
60}
61
62export interface EventDetail {
63 summary: EventSummary;
64 state: { markets: Market[]; accountBalance: string; commissionCollected: string };
65 history: TradeRow[];
66 settledMarket: string;
67 orderBooks: OrderBook[];
68 participants: Participant[];
69}
70
71export interface LedgerEntry {
72 seq: number;
73 at: string;
74 type: string;
75 amount: string;
76 balanceAfter: string;
77 note: string;
78}
79
80export interface Ledger {
81 owner: string;
82 balance: string;
83 blocked: boolean;
84 entries: LedgerEntry[];
85}
86
87export interface UserSummary {
88 username: string;
89 balance: string;
90 marketMaker: boolean;
91 online: boolean;
92}
93
94export interface Sync {
95 changed: boolean;
96 version: number;
97 events: EventSummary[];
98 users: UserSummary[];
99 account: Ledger;
100}
101
102export interface ChatMessage {
103 id: number;
104 at: string;
105 sender: string;
106 text: string;
107}
108
109export interface MarketData {
110 events: EventSummary[];
111 details: Record<string, EventDetail>;
112 users: UserSummary[];
113 account: Ledger;
114}
115
116export class ApiError extends Error {
117 constructor(
118 public status: number,
119 message: string,
120 ) {
121 super(message);
122 }
123}
124
125const BASE = '/guess-market/api';
126const UNREACHABLE = 'Cannot reach the server. Is Tomcat running on port 8080?';
127
128export async function api<T>(path: string, form?: Record<string, string>): Promise<T> {
129 let res: Response;
130 try {
131 res = await fetch(
132 BASE + path,
133 form ? { method: 'POST', body: new URLSearchParams(form) } : undefined,
134 );
135 } catch {
136 throw new ApiError(0, UNREACHABLE);
137 }
138 const text = await res.text();
139 let body: any = null;
140 try {
141 body = text ? JSON.parse(text) : null;
142 } catch {
143 }
144 if (!res.ok) {
145 throw new ApiError(res.status, body?.message ?? (res.status >= 500 ? UNREACHABLE : `The server answered ${res.status}.`));
146 }
147 return body as T;
148}
149
150export const eventPath = (name: string) => '/events/' + encodeURIComponent(name);
151
152export function num(value: string | number | null | undefined): number {
153 const n = typeof value === 'number' ? value : Number(String(value ?? '').replace(/[$,]/g, ''));
154 return Number.isNaN(n) ? 0 : n;
155}
156
157export const same = (a: string, b: string) => a.toLowerCase() === b.toLowerCase();
158
159export function money(value: string | number | null | undefined): string {
160 if (value === null || value === undefined || value === '') return '—';
161 const n = typeof value === 'number' ? value : Number(String(value).replace(/[$,]/g, ''));
162 if (Number.isNaN(n)) return String(value);
163 return (n < 0 ? '-$' : '$') + Math.abs(n).toFixed(2);
164}
165
166export const mechanismLabel = (m: Mechanism) => (m === 'LMSR' ? 'LMSR' : 'Order Book');
167
168export const CHEVRON = '\ue0b1';
169export const SEP = '\ueb04';
170
171export const optionTitle = (e: EventSummary, i: number) => `${i === 0 ? 'YES' : 'NO'} ${SEP} ${e.optionNames[i]}`;
172
173export const feeLabel = (e: EventSummary) =>
174 `${e.commissionPercent}% ${e.commissionTiming === 'ON_PURCHASE' ? 'on purchase' : 'on close'}`;
175
176export const GLYPH = { file: '\uf15b', bolt: '\uf0e7', users: '\uf0c0', chat: '\u{f0b79}' };