diff options
Diffstat (limited to 'ui-web/src/api.ts')
| -rw-r--r-- | ui-web/src/api.ts | 176 |
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 | |||
| 2 | export type Mechanism = 'LMSR' | 'ORDER_BOOK'; | ||
| 3 | export type Status = 'DRAFT' | 'ACTIVE' | 'SETTLED'; | ||
| 4 | export type FeeTiming = 'ON_PURCHASE' | 'ON_CLOSE'; | ||
| 5 | |||
| 6 | export 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 | |||
| 20 | export interface Market { | ||
| 21 | key: string; | ||
| 22 | name: string; | ||
| 23 | price: string; | ||
| 24 | volume: string; | ||
| 25 | } | ||
| 26 | |||
| 27 | export interface BookOrder { | ||
| 28 | side: string; | ||
| 29 | user: string; | ||
| 30 | quantity: number; | ||
| 31 | price: string; | ||
| 32 | isBid: boolean; | ||
| 33 | } | ||
| 34 | |||
| 35 | export 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 | |||
| 44 | export interface TradeRow { | ||
| 45 | at: string; | ||
| 46 | userName: string; | ||
| 47 | optionName: string; | ||
| 48 | quantity: string; | ||
| 49 | pricePaid: string; | ||
| 50 | } | ||
| 51 | |||
| 52 | export 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 | |||
| 62 | export 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 | |||
| 71 | export interface LedgerEntry { | ||
| 72 | seq: number; | ||
| 73 | at: string; | ||
| 74 | type: string; | ||
| 75 | amount: string; | ||
| 76 | balanceAfter: string; | ||
| 77 | note: string; | ||
| 78 | } | ||
| 79 | |||
| 80 | export interface Ledger { | ||
| 81 | owner: string; | ||
| 82 | balance: string; | ||
| 83 | blocked: boolean; | ||
| 84 | entries: LedgerEntry[]; | ||
| 85 | } | ||
| 86 | |||
| 87 | export interface UserSummary { | ||
| 88 | username: string; | ||
| 89 | balance: string; | ||
| 90 | marketMaker: boolean; | ||
| 91 | online: boolean; | ||
| 92 | } | ||
| 93 | |||
| 94 | export interface Sync { | ||
| 95 | changed: boolean; | ||
| 96 | version: number; | ||
| 97 | events: EventSummary[]; | ||
| 98 | users: UserSummary[]; | ||
| 99 | account: Ledger; | ||
| 100 | } | ||
| 101 | |||
| 102 | export interface ChatMessage { | ||
| 103 | id: number; | ||
| 104 | at: string; | ||
| 105 | sender: string; | ||
| 106 | text: string; | ||
| 107 | } | ||
| 108 | |||
| 109 | export interface MarketData { | ||
| 110 | events: EventSummary[]; | ||
| 111 | details: Record<string, EventDetail>; | ||
| 112 | users: UserSummary[]; | ||
| 113 | account: Ledger; | ||
| 114 | } | ||
| 115 | |||
| 116 | export class ApiError extends Error { | ||
| 117 | constructor( | ||
| 118 | public status: number, | ||
| 119 | message: string, | ||
| 120 | ) { | ||
| 121 | super(message); | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | const BASE = '/guess-market/api'; | ||
| 126 | const UNREACHABLE = 'Cannot reach the server. Is Tomcat running on port 8080?'; | ||
| 127 | |||
| 128 | export 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 | |||
| 150 | export const eventPath = (name: string) => '/events/' + encodeURIComponent(name); | ||
| 151 | |||
| 152 | export 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 | |||
| 157 | export const same = (a: string, b: string) => a.toLowerCase() === b.toLowerCase(); | ||
| 158 | |||
| 159 | export 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 | |||
| 166 | export const mechanismLabel = (m: Mechanism) => (m === 'LMSR' ? 'LMSR' : 'Order Book'); | ||
| 167 | |||
| 168 | export const CHEVRON = '\ue0b1'; | ||
| 169 | export const SEP = '\ueb04'; | ||
| 170 | |||
| 171 | export const optionTitle = (e: EventSummary, i: number) => `${i === 0 ? 'YES' : 'NO'} ${SEP} ${e.optionNames[i]}`; | ||
| 172 | |||
| 173 | export const feeLabel = (e: EventSummary) => | ||
| 174 | `${e.commissionPercent}% ${e.commissionTiming === 'ON_PURCHASE' ? 'on purchase' : 'on close'}`; | ||
| 175 | |||
| 176 | export const GLYPH = { file: '\uf15b', bolt: '\uf0e7', users: '\uf0c0', chat: '\u{f0b79}' }; | ||