@jsnw/kalshi-client 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,463 @@
1
+ import { z } from "zod";
2
+ import { Agent } from "node:http";
3
+
4
+ //#region src/consts.d.ts
5
+ declare const HTTP_METHOD: readonly ["get", "post", "put", "patch", "delete", "head", "options"];
6
+ declare const KALSHI_ENVIRONMENT: readonly ["development", "production"];
7
+ type HttpMethod = typeof HTTP_METHOD[number] | Uppercase<typeof HTTP_METHOD[number]>;
8
+ type KalshiEnvironment = typeof KALSHI_ENVIRONMENT[number];
9
+ //#endregion
10
+ //#region src/request-signer.d.ts
11
+ interface SignatureParameters {
12
+ timestamp: Date | number;
13
+ httpMethod: HttpMethod;
14
+ path: string;
15
+ }
16
+ interface IRequestSigner {
17
+ getSignature(params: SignatureParameters): string;
18
+ }
19
+ interface RequestSignerOptions {
20
+ privateKey: string;
21
+ }
22
+ declare class RequestSigner implements IRequestSigner {
23
+ private readonly privkey;
24
+ /**
25
+ * @param {RequestSignerOptions} options
26
+ */
27
+ constructor(options: RequestSignerOptions);
28
+ /**
29
+ * @param {SignatureParameters} params
30
+ * @return {string}
31
+ */
32
+ getSignature({
33
+ timestamp,
34
+ httpMethod,
35
+ path
36
+ }: SignatureParameters): string;
37
+ }
38
+ //#endregion
39
+ //#region src/credentials-provider.d.ts
40
+ interface ICredentialsProvider {
41
+ getAccessKey(): string;
42
+ getSignature(options: SignatureParameters): string;
43
+ }
44
+ interface InMemoryCredentialsProviderOptions {
45
+ accessKey: string;
46
+ privateKey: string;
47
+ }
48
+ declare class InMemoryCredentialsProvider implements ICredentialsProvider {
49
+ private accessKey;
50
+ private privateKey;
51
+ private signer;
52
+ /**
53
+ * @param {InMemoryCredentialsProviderOptions} options
54
+ */
55
+ constructor(options: InMemoryCredentialsProviderOptions);
56
+ /**
57
+ * @param {string} accessKey
58
+ */
59
+ setAccessKey(accessKey: string): void;
60
+ /**
61
+ * @param {string} privateKey
62
+ */
63
+ setPrivateKey(privateKey: string): void;
64
+ /**
65
+ * @return {string}
66
+ */
67
+ getAccessKey(): string;
68
+ /**
69
+ * @param {SignatureParameters} options
70
+ * @return {string}
71
+ */
72
+ getSignature(options: SignatureParameters): string;
73
+ }
74
+ //#endregion
75
+ //#region src/schemas/primitives.d.ts
76
+ declare const primitives$fixedPointSchema: z.ZodString;
77
+ type Primitives$FixedPoint = z.infer<typeof primitives$fixedPointSchema>;
78
+ declare const primitives$orderSideSchema: z.ZodEnum<{
79
+ yes: "yes";
80
+ no: "no";
81
+ }>;
82
+ type Primitives$OrderSide = z.infer<typeof primitives$orderSideSchema>;
83
+ declare const primitives$bookSideSchema: z.ZodEnum<{
84
+ bid: "bid";
85
+ ask: "ask";
86
+ }>;
87
+ type Primitives$BookSide = z.infer<typeof primitives$bookSideSchema>;
88
+ declare const primitives$orderActionSchema: z.ZodEnum<{
89
+ buy: "buy";
90
+ sell: "sell";
91
+ }>;
92
+ type Primitives$OrderAction = z.infer<typeof primitives$orderActionSchema>;
93
+ //#endregion
94
+ //#region src/schemas/orders.d.ts
95
+ declare const orders$orderStatusSchema: z.ZodEnum<{
96
+ resting: "resting";
97
+ canceled: "canceled";
98
+ executed: "executed";
99
+ }>;
100
+ type Orders$OrderStatus = z.infer<typeof orders$orderStatusSchema>;
101
+ declare const orders$orderTypeSchema: z.ZodEnum<{
102
+ limit: "limit";
103
+ market: "market";
104
+ }>;
105
+ type orders$OrderType = z.infer<typeof orders$orderTypeSchema>;
106
+ declare const orders$selfTradePreventionTypeSchema: z.ZodEnum<{
107
+ taker_at_cross: "taker_at_cross";
108
+ maker: "maker";
109
+ }>;
110
+ type orders$SelfTradePreventionType = z.infer<typeof orders$selfTradePreventionTypeSchema>;
111
+ declare const orders$orderDetailedSchema: z.ZodObject<{
112
+ order_id: z.ZodUUID;
113
+ user_id: z.ZodUUID;
114
+ subaccount_number: z.ZodDefault<z.ZodNumber>;
115
+ order_group_id: z.ZodOptional<z.ZodNullable<z.ZodUUID>>;
116
+ exchange_index: z.ZodDefault<z.ZodNumber>;
117
+ client_order_id: z.ZodString;
118
+ ticker: z.ZodString;
119
+ outcome_side: z.ZodEnum<{
120
+ yes: "yes";
121
+ no: "no";
122
+ }>;
123
+ book_side: z.ZodEnum<{
124
+ bid: "bid";
125
+ ask: "ask";
126
+ }>;
127
+ type: z.ZodEnum<{
128
+ limit: "limit";
129
+ market: "market";
130
+ }>;
131
+ status: z.ZodEnum<{
132
+ resting: "resting";
133
+ canceled: "canceled";
134
+ executed: "executed";
135
+ }>;
136
+ yes_price_dollars: z.ZodString;
137
+ no_price_dollars: z.ZodString;
138
+ fill_count_fp: z.ZodString;
139
+ remaining_count_fp: z.ZodString;
140
+ initial_count_fp: z.ZodString;
141
+ taker_fill_cost_dollars: z.ZodString;
142
+ maker_fill_cost_dollars: z.ZodString;
143
+ taker_fees_dollars: z.ZodString;
144
+ maker_fees_dollars: z.ZodString;
145
+ self_trade_prevention_type: z.ZodOptional<z.ZodNullable<z.ZodEnum<{
146
+ taker_at_cross: "taker_at_cross";
147
+ maker: "maker";
148
+ }>>>;
149
+ cancel_order_on_pause: z.ZodBoolean;
150
+ created_time: z.ZodISODateTime;
151
+ expiration_time: z.ZodOptional<z.ZodNullable<z.ZodISODateTime>>;
152
+ last_update_time: z.ZodOptional<z.ZodNullable<z.ZodISODateTime>>;
153
+ }, z.core.$strip>;
154
+ type Orders$OrderDetailed = z.infer<typeof orders$orderDetailedSchema>;
155
+ declare const orders$orderWsSchema: z.ZodObject<{
156
+ outcome_side: z.ZodEnum<{
157
+ yes: "yes";
158
+ no: "no";
159
+ }>;
160
+ book_side: z.ZodEnum<{
161
+ bid: "bid";
162
+ ask: "ask";
163
+ }>;
164
+ status: z.ZodEnum<{
165
+ resting: "resting";
166
+ canceled: "canceled";
167
+ executed: "executed";
168
+ }>;
169
+ yes_price_dollars: z.ZodString;
170
+ fill_count_fp: z.ZodString;
171
+ remaining_count_fp: z.ZodString;
172
+ initial_count_fp: z.ZodString;
173
+ taker_fill_cost_dollars: z.ZodString;
174
+ maker_fill_cost_dollars: z.ZodString;
175
+ taker_fees_dollars: z.ZodString;
176
+ maker_fees_dollars: z.ZodString;
177
+ order_id: z.ZodUUID;
178
+ user_id: z.ZodUUID;
179
+ subaccount_number: z.ZodDefault<z.ZodNumber>;
180
+ order_group_id: z.ZodOptional<z.ZodNullable<z.ZodUUID>>;
181
+ client_order_id: z.ZodString;
182
+ ticker: z.ZodString;
183
+ self_trade_prevention_type: z.ZodOptional<z.ZodNullable<z.ZodEnum<{
184
+ taker_at_cross: "taker_at_cross";
185
+ maker: "maker";
186
+ }>>>;
187
+ created_time: z.ZodISODateTime;
188
+ expiration_time: z.ZodOptional<z.ZodNullable<z.ZodISODateTime>>;
189
+ last_update_time: z.ZodOptional<z.ZodNullable<z.ZodISODateTime>>;
190
+ side: z.ZodEnum<{
191
+ yes: "yes";
192
+ no: "no";
193
+ }>;
194
+ created_ts_ms: z.ZodNumber;
195
+ last_updated_ts_ms: z.ZodOptional<z.ZodNumber>;
196
+ expiration_ts_ms: z.ZodOptional<z.ZodNumber>;
197
+ }, z.core.$strip>;
198
+ type Orders$OrderWs = z.infer<typeof orders$orderWsSchema>;
199
+ //#endregion
200
+ //#region src/typed-emitter.d.ts
201
+ type Listener<T extends unknown[]> = (...args: T) => void;
202
+ declare class TypedEmitter<TEvents extends Record<string, unknown[]>> {
203
+ private readonly listeners;
204
+ private readonly wrappers;
205
+ on<K extends keyof TEvents>(event: K, listener: Listener<TEvents[K]>): void;
206
+ once<K extends keyof TEvents>(event: K, listener: Listener<TEvents[K]>): void;
207
+ off<K extends keyof TEvents>(event: K, listener: Listener<TEvents[K]>): void;
208
+ protected emit<K extends keyof TEvents>(event: K, ...args: TEvents[K]): void;
209
+ removeListeners<K extends keyof TEvents>(event: K): void;
210
+ removeAllListeners(): void;
211
+ listenersCount<K extends keyof TEvents>(event?: K): number;
212
+ }
213
+ //#endregion
214
+ //#region src/ws-wrapper/types.d.ts
215
+ type HeadersFn = () => Record<string, string>;
216
+ type ReconnectStrategy = ((attempt: number) => number | false) | {
217
+ interval: number | number[] | ((attempt: number) => number) | {
218
+ min: number;
219
+ max: number;
220
+ };
221
+ maxAttempts: number;
222
+ };
223
+ interface WsWrapperOptions {
224
+ address: string;
225
+ agent?: Agent;
226
+ headers?: Record<string, string> | HeadersFn;
227
+ reconnect: ReconnectStrategy;
228
+ }
229
+ //#endregion
230
+ //#region src/logging.d.ts
231
+ type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'fatal';
232
+ type LogFunction = (level: LogLevel, message: string, context?: any) => void;
233
+ //#endregion
234
+ //#region src/ws/types.d.ts
235
+ interface WsClientOptions {
236
+ id?: string;
237
+ environment?: KalshiEnvironment;
238
+ credentialsProvider: ICredentialsProvider;
239
+ wsOptions: Omit<WsWrapperOptions, 'address' | 'headers'>;
240
+ log?: LogFunction;
241
+ }
242
+ //#endregion
243
+ //#region src/ws/ws-client.d.ts
244
+ declare class WsClient extends TypedEmitter<any> {
245
+ readonly id: string;
246
+ private readonly baseURL;
247
+ private readonly credentialsProvider;
248
+ private readonly log;
249
+ private readonly ws;
250
+ private lastMessageId;
251
+ private readonly subscribeAwaiters;
252
+ /**
253
+ * @param {WsClientOptions} options
254
+ */
255
+ constructor(options: WsClientOptions);
256
+ private onReady;
257
+ /**
258
+ * @param {WebSocket.RawData} data
259
+ * @param {boolean} isBinary
260
+ * @return {Promise<void>}
261
+ */
262
+ private onMessage;
263
+ /**
264
+ * @param {number} attempt
265
+ */
266
+ private onReconnecting;
267
+ /**
268
+ * @param {Error} err
269
+ */
270
+ private onError;
271
+ /**
272
+ */
273
+ private onClose;
274
+ /**
275
+ * @param {WsAnyMessage} msg
276
+ * @private
277
+ */
278
+ private processMessage;
279
+ }
280
+ //#endregion
281
+ //#region src/ws/errors.d.ts
282
+ declare class WsKalshiError extends Error {
283
+ readonly id: number;
284
+ readonly code: number;
285
+ /**
286
+ * @param {number} id
287
+ * @param {number} code
288
+ * @param {string} message
289
+ */
290
+ constructor(id: number, code: number, message?: string);
291
+ }
292
+ //#endregion
293
+ //#region src/ws/common.d.ts
294
+ declare const WS_KALSHI_ERRORS: {
295
+ readonly UNABLE_TO_PROCESS: 1;
296
+ readonly PARAMS_REQUIRED: 2;
297
+ readonly CHANNELS_REQUIRED: 3;
298
+ readonly SUBSCRIPTION_IDS_REQUIRED: 4;
299
+ readonly UNKNOWN_COMMAND: 5;
300
+ readonly ALREADY_SUBSCRIBED: 6;
301
+ readonly UNKNOWN_SUBSCRIPTION_ID: 7;
302
+ readonly UNKNOWN_CHANNEL_NAME: 8;
303
+ readonly AUTHENTICATION_REQUIRED: 9;
304
+ readonly CHANNEL_ERROR: 10;
305
+ readonly INVALID_PARAMETER: 11;
306
+ readonly EXACTLY_ONE_SUBSCRIPTION_ID_IS_REQUIRED: 12;
307
+ readonly UNSUPPORTED_ACTION: 13;
308
+ readonly MARKET_TICKER_REQUIRED: 14;
309
+ readonly ACTION_REQUIRED: 15;
310
+ readonly MARKET_NOT_FOUND: 16;
311
+ readonly INTERNAL_ERROR: 17;
312
+ readonly COMMAND_TIMEOUT: 18;
313
+ readonly SHARD_FACTOR_MUST_BE_GT0: 19;
314
+ readonly SHARD_FACTOR_IS_REQUIRED: 20;
315
+ readonly INVALID_SHARD_KEY: 21;
316
+ readonly SHARD_FACTOR_MUST_BE_LT100: 22;
317
+ readonly SUBSCRIPTION_BUFFER_OVERFLOW: 25;
318
+ };
319
+ declare const WS_KALSHI_SERVER_ERRORS: number[];
320
+ //#endregion
321
+ //#region src/ws/messages.d.ts
322
+ declare const wsOkMessageSchema: z.ZodObject<{
323
+ id: z.ZodNumber;
324
+ sid: z.ZodNumber;
325
+ type: z.ZodLiteral<"ok">;
326
+ msg: z.ZodOptional<z.ZodAny>;
327
+ }, z.core.$strip>;
328
+ type WsOkMessage = z.infer<typeof wsOkMessageSchema>;
329
+ declare const wsErrorMessageSchema: z.ZodObject<{
330
+ id: z.ZodNumber;
331
+ type: z.ZodLiteral<"error">;
332
+ msg: z.ZodObject<{
333
+ code: z.ZodNumber;
334
+ msg: z.ZodString;
335
+ }, z.core.$strip>;
336
+ }, z.core.$strip>;
337
+ type WsErrorMessage = z.infer<typeof wsErrorMessageSchema>;
338
+ declare const wsSubscribedMessageSchema: z.ZodObject<{
339
+ id: z.ZodNumber;
340
+ type: z.ZodLiteral<"subscribed">;
341
+ msg: z.ZodObject<{
342
+ channel: z.ZodString;
343
+ sid: z.ZodNumber;
344
+ }, z.core.$strip>;
345
+ }, z.core.$strip>;
346
+ type WsSubscribedMessage = z.infer<typeof wsSubscribedMessageSchema>;
347
+ declare const wsUserOrderMessageSchema: z.ZodObject<{
348
+ sid: z.ZodNumber;
349
+ type: z.ZodLiteral<"user_order">;
350
+ msg: z.ZodObject<{
351
+ outcome_side: z.ZodEnum<{
352
+ yes: "yes";
353
+ no: "no";
354
+ }>;
355
+ book_side: z.ZodEnum<{
356
+ bid: "bid";
357
+ ask: "ask";
358
+ }>;
359
+ status: z.ZodEnum<{
360
+ resting: "resting";
361
+ canceled: "canceled";
362
+ executed: "executed";
363
+ }>;
364
+ yes_price_dollars: z.ZodString;
365
+ fill_count_fp: z.ZodString;
366
+ remaining_count_fp: z.ZodString;
367
+ initial_count_fp: z.ZodString;
368
+ taker_fill_cost_dollars: z.ZodString;
369
+ maker_fill_cost_dollars: z.ZodString;
370
+ taker_fees_dollars: z.ZodString;
371
+ maker_fees_dollars: z.ZodString;
372
+ order_id: z.ZodUUID;
373
+ user_id: z.ZodUUID;
374
+ subaccount_number: z.ZodDefault<z.ZodNumber>;
375
+ order_group_id: z.ZodOptional<z.ZodNullable<z.ZodUUID>>;
376
+ client_order_id: z.ZodString;
377
+ ticker: z.ZodString;
378
+ self_trade_prevention_type: z.ZodOptional<z.ZodNullable<z.ZodEnum<{
379
+ taker_at_cross: "taker_at_cross";
380
+ maker: "maker";
381
+ }>>>;
382
+ created_time: z.ZodISODateTime;
383
+ expiration_time: z.ZodOptional<z.ZodNullable<z.ZodISODateTime>>;
384
+ last_update_time: z.ZodOptional<z.ZodNullable<z.ZodISODateTime>>;
385
+ side: z.ZodEnum<{
386
+ yes: "yes";
387
+ no: "no";
388
+ }>;
389
+ created_ts_ms: z.ZodNumber;
390
+ last_updated_ts_ms: z.ZodOptional<z.ZodNumber>;
391
+ expiration_ts_ms: z.ZodOptional<z.ZodNumber>;
392
+ }, z.core.$strip>;
393
+ }, z.core.$strip>;
394
+ type WsUserOrderMessage = z.infer<typeof wsUserOrderMessageSchema>;
395
+ declare const wsAnyMessageSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
396
+ id: z.ZodNumber;
397
+ sid: z.ZodNumber;
398
+ type: z.ZodLiteral<"ok">;
399
+ msg: z.ZodOptional<z.ZodAny>;
400
+ }, z.core.$strip>, z.ZodObject<{
401
+ id: z.ZodNumber;
402
+ type: z.ZodLiteral<"error">;
403
+ msg: z.ZodObject<{
404
+ code: z.ZodNumber;
405
+ msg: z.ZodString;
406
+ }, z.core.$strip>;
407
+ }, z.core.$strip>, z.ZodObject<{
408
+ id: z.ZodNumber;
409
+ type: z.ZodLiteral<"subscribed">;
410
+ msg: z.ZodObject<{
411
+ channel: z.ZodString;
412
+ sid: z.ZodNumber;
413
+ }, z.core.$strip>;
414
+ }, z.core.$strip>, z.ZodObject<{
415
+ sid: z.ZodNumber;
416
+ type: z.ZodLiteral<"user_order">;
417
+ msg: z.ZodObject<{
418
+ outcome_side: z.ZodEnum<{
419
+ yes: "yes";
420
+ no: "no";
421
+ }>;
422
+ book_side: z.ZodEnum<{
423
+ bid: "bid";
424
+ ask: "ask";
425
+ }>;
426
+ status: z.ZodEnum<{
427
+ resting: "resting";
428
+ canceled: "canceled";
429
+ executed: "executed";
430
+ }>;
431
+ yes_price_dollars: z.ZodString;
432
+ fill_count_fp: z.ZodString;
433
+ remaining_count_fp: z.ZodString;
434
+ initial_count_fp: z.ZodString;
435
+ taker_fill_cost_dollars: z.ZodString;
436
+ maker_fill_cost_dollars: z.ZodString;
437
+ taker_fees_dollars: z.ZodString;
438
+ maker_fees_dollars: z.ZodString;
439
+ order_id: z.ZodUUID;
440
+ user_id: z.ZodUUID;
441
+ subaccount_number: z.ZodDefault<z.ZodNumber>;
442
+ order_group_id: z.ZodOptional<z.ZodNullable<z.ZodUUID>>;
443
+ client_order_id: z.ZodString;
444
+ ticker: z.ZodString;
445
+ self_trade_prevention_type: z.ZodOptional<z.ZodNullable<z.ZodEnum<{
446
+ taker_at_cross: "taker_at_cross";
447
+ maker: "maker";
448
+ }>>>;
449
+ created_time: z.ZodISODateTime;
450
+ expiration_time: z.ZodOptional<z.ZodNullable<z.ZodISODateTime>>;
451
+ last_update_time: z.ZodOptional<z.ZodNullable<z.ZodISODateTime>>;
452
+ side: z.ZodEnum<{
453
+ yes: "yes";
454
+ no: "no";
455
+ }>;
456
+ created_ts_ms: z.ZodNumber;
457
+ last_updated_ts_ms: z.ZodOptional<z.ZodNumber>;
458
+ expiration_ts_ms: z.ZodOptional<z.ZodNumber>;
459
+ }, z.core.$strip>;
460
+ }, z.core.$strip>], "type">;
461
+ type WsAnyMessage = z.infer<typeof wsAnyMessageSchema>;
462
+ //#endregion
463
+ export { type ICredentialsProvider, type IRequestSigner, InMemoryCredentialsProvider, type InMemoryCredentialsProviderOptions, Orders$OrderDetailed, Orders$OrderStatus, Orders$OrderWs, Primitives$BookSide, Primitives$FixedPoint, Primitives$OrderAction, Primitives$OrderSide, RequestSigner, type RequestSignerOptions, type SignatureParameters, WS_KALSHI_ERRORS, WS_KALSHI_SERVER_ERRORS, WsAnyMessage, WsClient, type WsClientOptions, WsErrorMessage, WsKalshiError, WsOkMessage, WsSubscribedMessage, WsUserOrderMessage, orders$OrderType, orders$SelfTradePreventionType, orders$orderDetailedSchema, orders$orderStatusSchema, orders$orderTypeSchema, orders$orderWsSchema, orders$selfTradePreventionTypeSchema, primitives$bookSideSchema, primitives$fixedPointSchema, primitives$orderActionSchema, primitives$orderSideSchema, wsAnyMessageSchema, wsErrorMessageSchema, wsOkMessageSchema, wsSubscribedMessageSchema, wsUserOrderMessageSchema };