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