@gaberoo/kalshitools 1.0.2 → 1.1.0
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.
- package/README.md +328 -27
- package/dist/commands/config/init.js +4 -4
- package/dist/commands/config/show.js +5 -5
- package/dist/commands/markets/list.d.ts +5 -1
- package/dist/commands/markets/list.js +28 -8
- package/dist/commands/markets/orderbook.d.ts +13 -0
- package/dist/commands/markets/orderbook.js +83 -0
- package/dist/commands/markets/scan.d.ts +18 -0
- package/dist/commands/markets/scan.js +237 -0
- package/dist/commands/markets/show.d.ts +3 -3
- package/dist/commands/markets/show.js +7 -7
- package/dist/commands/orders/cancel.d.ts +3 -3
- package/dist/commands/orders/cancel.js +7 -7
- package/dist/commands/orders/create.d.ts +5 -5
- package/dist/commands/orders/create.js +33 -33
- package/dist/commands/orders/list.d.ts +1 -1
- package/dist/commands/orders/list.js +9 -9
- package/dist/commands/portfolio/analytics.d.ts +12 -0
- package/dist/commands/portfolio/analytics.js +192 -0
- package/dist/commands/portfolio/fills.d.ts +1 -1
- package/dist/commands/portfolio/fills.js +7 -7
- package/dist/commands/portfolio/history.d.ts +14 -0
- package/dist/commands/portfolio/history.js +245 -0
- package/dist/commands/portfolio/positions.d.ts +1 -0
- package/dist/commands/portfolio/positions.js +11 -2
- package/dist/commands/portfolio/risk.d.ts +11 -0
- package/dist/commands/portfolio/risk.js +206 -0
- package/dist/lib/analytics.d.ts +64 -0
- package/dist/lib/analytics.js +236 -0
- package/dist/lib/base-command.d.ts +2 -2
- package/dist/lib/base-command.js +8 -8
- package/dist/lib/config/manager.d.ts +25 -25
- package/dist/lib/config/manager.js +51 -51
- package/dist/lib/config/schema.d.ts +11 -11
- package/dist/lib/config/schema.js +6 -6
- package/dist/lib/errors/base.d.ts +10 -10
- package/dist/lib/errors/base.js +7 -7
- package/dist/lib/kalshi/auth.d.ts +4 -4
- package/dist/lib/kalshi/auth.js +24 -24
- package/dist/lib/kalshi/client.d.ts +35 -35
- package/dist/lib/kalshi/client.js +93 -91
- package/dist/lib/kalshi/index.d.ts +1 -1
- package/dist/lib/kalshi/index.js +1 -1
- package/dist/lib/kalshi/types.d.ts +53 -53
- package/dist/lib/logger.js +3 -3
- package/dist/lib/output/formatter.d.ts +20 -20
- package/dist/lib/output/formatter.js +55 -55
- package/dist/lib/retry.d.ts +2 -2
- package/dist/lib/retry.js +8 -10
- package/dist/lib/risk.d.ts +51 -0
- package/dist/lib/risk.js +153 -0
- package/dist/lib/sanitize.js +9 -9
- package/dist/lib/scanner.d.ts +58 -0
- package/dist/lib/scanner.js +160 -0
- package/dist/lib/shutdown.d.ts +4 -4
- package/dist/lib/shutdown.js +7 -7
- package/dist/lib/validation.d.ts +5 -5
- package/dist/lib/validation.js +14 -20
- package/docs/TRADING_STRATEGIES.md +538 -0
- package/oclif.manifest.json +559 -170
- package/package.json +1 -1
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import Conf from 'conf';
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
2
3
|
import { homedir } from 'node:os';
|
|
3
4
|
import { join } from 'node:path';
|
|
4
|
-
import { readFileSync } from 'node:fs';
|
|
5
|
-
import { defaultConfig } from './schema.js';
|
|
6
5
|
import { ConfigError } from '../errors/base.js';
|
|
7
6
|
import { logger } from '../logger.js';
|
|
7
|
+
import { defaultConfig } from './schema.js';
|
|
8
8
|
/**
|
|
9
9
|
* Configuration manager for kalshitools
|
|
10
10
|
*/
|
|
11
11
|
export class ConfigManager {
|
|
12
|
-
store;
|
|
13
12
|
static instance = null;
|
|
13
|
+
store;
|
|
14
14
|
constructor() {
|
|
15
15
|
this.store = new Conf({
|
|
16
|
-
projectName: 'kalshitools',
|
|
17
16
|
defaults: defaultConfig,
|
|
17
|
+
projectName: 'kalshitools',
|
|
18
18
|
});
|
|
19
19
|
}
|
|
20
20
|
/**
|
|
@@ -26,6 +26,24 @@ export class ConfigManager {
|
|
|
26
26
|
}
|
|
27
27
|
return ConfigManager.instance;
|
|
28
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* Get API configuration for the current environment
|
|
31
|
+
*/
|
|
32
|
+
getApiConfig() {
|
|
33
|
+
const env = this.getEnvironment();
|
|
34
|
+
const config = this.store.get(`api.${env}`);
|
|
35
|
+
// Override with environment variables if present
|
|
36
|
+
const keyId = process.env.KALSHI_API_KEY_ID || config.keyId;
|
|
37
|
+
const privateKeyPath = process.env.KALSHI_PRIVATE_KEY_PATH || config.privateKeyPath;
|
|
38
|
+
if (!keyId || !privateKeyPath) {
|
|
39
|
+
throw new ConfigError(`API credentials not configured for ${env} environment. Run 'kalshitools config init' to set up.`, { environment: env });
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
baseUrl: config.baseUrl,
|
|
43
|
+
keyId,
|
|
44
|
+
privateKeyPath,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
29
47
|
/**
|
|
30
48
|
* Get the full configuration
|
|
31
49
|
*/
|
|
@@ -33,6 +51,12 @@ export class ConfigManager {
|
|
|
33
51
|
const config = this.store.store;
|
|
34
52
|
return config;
|
|
35
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Get the configuration file path
|
|
56
|
+
*/
|
|
57
|
+
getConfigPath() {
|
|
58
|
+
return this.store.path;
|
|
59
|
+
}
|
|
36
60
|
/**
|
|
37
61
|
* Get the current environment (demo or production)
|
|
38
62
|
*/
|
|
@@ -40,28 +64,24 @@ export class ConfigManager {
|
|
|
40
64
|
return process.env.KALSHI_ENV === 'production' ? 'production' : this.store.get('environment', 'demo');
|
|
41
65
|
}
|
|
42
66
|
/**
|
|
43
|
-
*
|
|
67
|
+
* Get output configuration
|
|
44
68
|
*/
|
|
45
|
-
|
|
46
|
-
this.store.
|
|
69
|
+
getOutputConfig() {
|
|
70
|
+
return this.store.get('output');
|
|
47
71
|
}
|
|
48
72
|
/**
|
|
49
|
-
* Get
|
|
73
|
+
* Get trading configuration
|
|
50
74
|
*/
|
|
51
|
-
|
|
75
|
+
getTradingConfig() {
|
|
76
|
+
return this.store.get('trading');
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Check if configuration is initialized
|
|
80
|
+
*/
|
|
81
|
+
isConfigured() {
|
|
52
82
|
const env = this.getEnvironment();
|
|
53
83
|
const config = this.store.get(`api.${env}`);
|
|
54
|
-
|
|
55
|
-
const keyId = process.env.KALSHI_API_KEY_ID || config.keyId;
|
|
56
|
-
const privateKeyPath = process.env.KALSHI_PRIVATE_KEY_PATH || config.privateKeyPath;
|
|
57
|
-
if (!keyId || !privateKeyPath) {
|
|
58
|
-
throw new ConfigError(`API credentials not configured for ${env} environment. Run 'kalshitools config init' to set up.`, { environment: env });
|
|
59
|
-
}
|
|
60
|
-
return {
|
|
61
|
-
baseUrl: config.baseUrl,
|
|
62
|
-
keyId,
|
|
63
|
-
privateKeyPath,
|
|
64
|
-
};
|
|
84
|
+
return Boolean(config.keyId && config.privateKeyPath);
|
|
65
85
|
}
|
|
66
86
|
/**
|
|
67
87
|
* Read private key from file
|
|
@@ -78,29 +98,16 @@ export class ConfigManager {
|
|
|
78
98
|
}
|
|
79
99
|
catch (error) {
|
|
80
100
|
throw new ConfigError(`Failed to read private key from ${expandedPath}`, {
|
|
81
|
-
path: expandedPath,
|
|
82
101
|
error: error instanceof Error ? error.message : String(error),
|
|
102
|
+
path: expandedPath,
|
|
83
103
|
});
|
|
84
104
|
}
|
|
85
105
|
}
|
|
86
106
|
/**
|
|
87
|
-
*
|
|
88
|
-
*/
|
|
89
|
-
setApiConfig(env, config) {
|
|
90
|
-
const current = this.store.get(`api.${env}`);
|
|
91
|
-
this.store.set(`api.${env}`, { ...current, ...config });
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Get output configuration
|
|
95
|
-
*/
|
|
96
|
-
getOutputConfig() {
|
|
97
|
-
return this.store.get('output');
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Get trading configuration
|
|
107
|
+
* Reset configuration to defaults
|
|
101
108
|
*/
|
|
102
|
-
|
|
103
|
-
|
|
109
|
+
reset() {
|
|
110
|
+
this.store.clear();
|
|
104
111
|
}
|
|
105
112
|
/**
|
|
106
113
|
* Set a configuration value
|
|
@@ -109,24 +116,17 @@ export class ConfigManager {
|
|
|
109
116
|
this.store.set(key, value);
|
|
110
117
|
}
|
|
111
118
|
/**
|
|
112
|
-
*
|
|
113
|
-
*/
|
|
114
|
-
getConfigPath() {
|
|
115
|
-
return this.store.path;
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* Reset configuration to defaults
|
|
119
|
+
* Set API configuration for an environment
|
|
119
120
|
*/
|
|
120
|
-
|
|
121
|
-
this.store.
|
|
121
|
+
setApiConfig(env, config) {
|
|
122
|
+
const current = this.store.get(`api.${env}`);
|
|
123
|
+
this.store.set(`api.${env}`, { ...current, ...config });
|
|
122
124
|
}
|
|
123
125
|
/**
|
|
124
|
-
*
|
|
126
|
+
* Set the environment
|
|
125
127
|
*/
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
const config = this.store.get(`api.${env}`);
|
|
129
|
-
return Boolean(config.keyId && config.privateKeyPath);
|
|
128
|
+
setEnvironment(env) {
|
|
129
|
+
this.store.set('environment', env);
|
|
130
130
|
}
|
|
131
131
|
}
|
|
132
132
|
/**
|
|
@@ -19,14 +19,14 @@ declare const apiEnvSchema: z.ZodObject<{
|
|
|
19
19
|
* Output configuration schema
|
|
20
20
|
*/
|
|
21
21
|
declare const outputSchema: z.ZodObject<{
|
|
22
|
-
defaultFormat: z.ZodDefault<z.ZodEnum<["table", "json"]>>;
|
|
23
22
|
colors: z.ZodDefault<z.ZodBoolean>;
|
|
23
|
+
defaultFormat: z.ZodDefault<z.ZodEnum<["table", "json"]>>;
|
|
24
24
|
}, "strip", z.ZodTypeAny, {
|
|
25
|
-
defaultFormat: "json" | "table";
|
|
26
25
|
colors: boolean;
|
|
26
|
+
defaultFormat: "json" | "table";
|
|
27
27
|
}, {
|
|
28
|
-
defaultFormat?: "json" | "table" | undefined;
|
|
29
28
|
colors?: boolean | undefined;
|
|
29
|
+
defaultFormat?: "json" | "table" | undefined;
|
|
30
30
|
}>;
|
|
31
31
|
/**
|
|
32
32
|
* Trading safety configuration schema
|
|
@@ -45,8 +45,6 @@ declare const tradingSchema: z.ZodObject<{
|
|
|
45
45
|
* Main configuration schema
|
|
46
46
|
*/
|
|
47
47
|
export declare const configSchema: z.ZodObject<{
|
|
48
|
-
version: z.ZodLiteral<"1">;
|
|
49
|
-
environment: z.ZodDefault<z.ZodEnum<["demo", "production"]>>;
|
|
50
48
|
api: z.ZodObject<{
|
|
51
49
|
demo: z.ZodObject<{
|
|
52
50
|
baseUrl: z.ZodString;
|
|
@@ -97,15 +95,16 @@ export declare const configSchema: z.ZodObject<{
|
|
|
97
95
|
privateKeyPath?: string | undefined;
|
|
98
96
|
};
|
|
99
97
|
}>;
|
|
98
|
+
environment: z.ZodDefault<z.ZodEnum<["demo", "production"]>>;
|
|
100
99
|
output: z.ZodDefault<z.ZodObject<{
|
|
101
|
-
defaultFormat: z.ZodDefault<z.ZodEnum<["table", "json"]>>;
|
|
102
100
|
colors: z.ZodDefault<z.ZodBoolean>;
|
|
101
|
+
defaultFormat: z.ZodDefault<z.ZodEnum<["table", "json"]>>;
|
|
103
102
|
}, "strip", z.ZodTypeAny, {
|
|
104
|
-
defaultFormat: "json" | "table";
|
|
105
103
|
colors: boolean;
|
|
104
|
+
defaultFormat: "json" | "table";
|
|
106
105
|
}, {
|
|
107
|
-
defaultFormat?: "json" | "table" | undefined;
|
|
108
106
|
colors?: boolean | undefined;
|
|
107
|
+
defaultFormat?: "json" | "table" | undefined;
|
|
109
108
|
}>>;
|
|
110
109
|
trading: z.ZodDefault<z.ZodObject<{
|
|
111
110
|
confirmations: z.ZodDefault<z.ZodBoolean>;
|
|
@@ -117,13 +116,13 @@ export declare const configSchema: z.ZodObject<{
|
|
|
117
116
|
confirmations?: boolean | undefined;
|
|
118
117
|
maxOrderSize?: number | undefined;
|
|
119
118
|
}>>;
|
|
119
|
+
version: z.ZodLiteral<"1">;
|
|
120
120
|
}, "strip", z.ZodTypeAny, {
|
|
121
121
|
version: "1";
|
|
122
122
|
output: {
|
|
123
|
-
defaultFormat: "json" | "table";
|
|
124
123
|
colors: boolean;
|
|
124
|
+
defaultFormat: "json" | "table";
|
|
125
125
|
};
|
|
126
|
-
environment: "demo" | "production";
|
|
127
126
|
api: {
|
|
128
127
|
demo: {
|
|
129
128
|
baseUrl: string;
|
|
@@ -136,6 +135,7 @@ export declare const configSchema: z.ZodObject<{
|
|
|
136
135
|
privateKeyPath?: string | undefined;
|
|
137
136
|
};
|
|
138
137
|
};
|
|
138
|
+
environment: "demo" | "production";
|
|
139
139
|
trading: {
|
|
140
140
|
confirmations: boolean;
|
|
141
141
|
maxOrderSize: number;
|
|
@@ -155,8 +155,8 @@ export declare const configSchema: z.ZodObject<{
|
|
|
155
155
|
};
|
|
156
156
|
};
|
|
157
157
|
output?: {
|
|
158
|
-
defaultFormat?: "json" | "table" | undefined;
|
|
159
158
|
colors?: boolean | undefined;
|
|
159
|
+
defaultFormat?: "json" | "table" | undefined;
|
|
160
160
|
} | undefined;
|
|
161
161
|
environment?: "demo" | "production" | undefined;
|
|
162
162
|
trading?: {
|
|
@@ -11,8 +11,8 @@ const apiEnvSchema = z.object({
|
|
|
11
11
|
* Output configuration schema
|
|
12
12
|
*/
|
|
13
13
|
const outputSchema = z.object({
|
|
14
|
-
defaultFormat: z.enum(['table', 'json']).default('table'),
|
|
15
14
|
colors: z.boolean().default(true),
|
|
15
|
+
defaultFormat: z.enum(['table', 'json']).default('table'),
|
|
16
16
|
});
|
|
17
17
|
/**
|
|
18
18
|
* Trading safety configuration schema
|
|
@@ -25,21 +25,19 @@ const tradingSchema = z.object({
|
|
|
25
25
|
* Main configuration schema
|
|
26
26
|
*/
|
|
27
27
|
export const configSchema = z.object({
|
|
28
|
-
version: z.literal('1'),
|
|
29
|
-
environment: z.enum(['demo', 'production']).default('demo'),
|
|
30
28
|
api: z.object({
|
|
31
29
|
demo: apiEnvSchema,
|
|
32
30
|
production: apiEnvSchema,
|
|
33
31
|
}),
|
|
32
|
+
environment: z.enum(['demo', 'production']).default('demo'),
|
|
34
33
|
output: outputSchema.default({}),
|
|
35
34
|
trading: tradingSchema.default({}),
|
|
35
|
+
version: z.literal('1'),
|
|
36
36
|
});
|
|
37
37
|
/**
|
|
38
38
|
* Default configuration
|
|
39
39
|
*/
|
|
40
40
|
export const defaultConfig = {
|
|
41
|
-
version: '1',
|
|
42
|
-
environment: 'demo',
|
|
43
41
|
api: {
|
|
44
42
|
demo: {
|
|
45
43
|
baseUrl: 'https://demo-api.kalshi.co/trade-api/v2',
|
|
@@ -48,12 +46,14 @@ export const defaultConfig = {
|
|
|
48
46
|
baseUrl: 'https://trading-api.kalshi.com/trade-api/v2',
|
|
49
47
|
},
|
|
50
48
|
},
|
|
49
|
+
environment: 'demo',
|
|
51
50
|
output: {
|
|
52
|
-
defaultFormat: 'table',
|
|
53
51
|
colors: true,
|
|
52
|
+
defaultFormat: 'table',
|
|
54
53
|
},
|
|
55
54
|
trading: {
|
|
56
55
|
confirmations: true,
|
|
57
56
|
maxOrderSize: 1000,
|
|
58
57
|
},
|
|
58
|
+
version: '1',
|
|
59
59
|
};
|
|
@@ -2,36 +2,36 @@
|
|
|
2
2
|
* Exit codes for the CLI
|
|
3
3
|
*/
|
|
4
4
|
export declare enum ExitCode {
|
|
5
|
-
Success = 0,
|
|
6
|
-
GeneralError = 1,
|
|
7
|
-
ConfigError = 2,
|
|
8
|
-
AuthError = 3,
|
|
9
5
|
APIError = 4,
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
AuthError = 3,
|
|
7
|
+
ConfigError = 2,
|
|
8
|
+
GeneralError = 1,
|
|
9
|
+
RateLimitError = 6,
|
|
10
|
+
Success = 0,
|
|
11
|
+
ValidationError = 5
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
14
|
* Error code type for structured error responses
|
|
15
15
|
*/
|
|
16
|
-
export type ErrorCode = '
|
|
16
|
+
export type ErrorCode = 'API_ERROR' | 'AUTH_ERROR' | 'CONFIG_ERROR' | 'GENERAL_ERROR' | 'NETWORK_ERROR' | 'NOT_FOUND' | 'RATE_LIMIT_ERROR' | 'TIMEOUT' | 'VALIDATION_ERROR';
|
|
17
17
|
/**
|
|
18
18
|
* Base error class for all kalshitools errors
|
|
19
19
|
*/
|
|
20
20
|
export declare class KalshiToolsError extends Error {
|
|
21
21
|
readonly code: ErrorCode;
|
|
22
|
-
readonly exitCode: ExitCode;
|
|
23
22
|
readonly details?: Record<string, unknown>;
|
|
23
|
+
readonly exitCode: ExitCode;
|
|
24
24
|
constructor(message: string, code?: ErrorCode, exitCode?: ExitCode, details?: Record<string, unknown>);
|
|
25
25
|
/**
|
|
26
26
|
* Convert error to JSON format for structured output
|
|
27
27
|
*/
|
|
28
28
|
toJSON(): {
|
|
29
|
-
success: false;
|
|
30
29
|
error: {
|
|
31
30
|
code: ErrorCode;
|
|
32
|
-
message: string;
|
|
33
31
|
details?: Record<string, unknown>;
|
|
32
|
+
message: string;
|
|
34
33
|
};
|
|
34
|
+
success: false;
|
|
35
35
|
};
|
|
36
36
|
}
|
|
37
37
|
/**
|
package/dist/lib/errors/base.js
CHANGED
|
@@ -3,21 +3,21 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export var ExitCode;
|
|
5
5
|
(function (ExitCode) {
|
|
6
|
-
ExitCode[ExitCode["Success"] = 0] = "Success";
|
|
7
|
-
ExitCode[ExitCode["GeneralError"] = 1] = "GeneralError";
|
|
8
|
-
ExitCode[ExitCode["ConfigError"] = 2] = "ConfigError";
|
|
9
|
-
ExitCode[ExitCode["AuthError"] = 3] = "AuthError";
|
|
10
6
|
ExitCode[ExitCode["APIError"] = 4] = "APIError";
|
|
11
|
-
ExitCode[ExitCode["
|
|
7
|
+
ExitCode[ExitCode["AuthError"] = 3] = "AuthError";
|
|
8
|
+
ExitCode[ExitCode["ConfigError"] = 2] = "ConfigError";
|
|
9
|
+
ExitCode[ExitCode["GeneralError"] = 1] = "GeneralError";
|
|
12
10
|
ExitCode[ExitCode["RateLimitError"] = 6] = "RateLimitError";
|
|
11
|
+
ExitCode[ExitCode["Success"] = 0] = "Success";
|
|
12
|
+
ExitCode[ExitCode["ValidationError"] = 5] = "ValidationError";
|
|
13
13
|
})(ExitCode || (ExitCode = {}));
|
|
14
14
|
/**
|
|
15
15
|
* Base error class for all kalshitools errors
|
|
16
16
|
*/
|
|
17
17
|
export class KalshiToolsError extends Error {
|
|
18
18
|
code;
|
|
19
|
-
exitCode;
|
|
20
19
|
details;
|
|
20
|
+
exitCode;
|
|
21
21
|
constructor(message, code = 'GENERAL_ERROR', exitCode = ExitCode.GeneralError, details) {
|
|
22
22
|
super(message);
|
|
23
23
|
this.name = this.constructor.name;
|
|
@@ -31,12 +31,12 @@ export class KalshiToolsError extends Error {
|
|
|
31
31
|
*/
|
|
32
32
|
toJSON() {
|
|
33
33
|
return {
|
|
34
|
-
success: false,
|
|
35
34
|
error: {
|
|
36
35
|
code: this.code,
|
|
37
36
|
message: this.message,
|
|
38
37
|
...(this.details && { details: this.details }),
|
|
39
38
|
},
|
|
39
|
+
success: false,
|
|
40
40
|
};
|
|
41
41
|
}
|
|
42
42
|
}
|
|
@@ -6,12 +6,12 @@ export declare class KalshiAuth {
|
|
|
6
6
|
private keyId;
|
|
7
7
|
private privateKey;
|
|
8
8
|
constructor(keyId: string, privateKey: string);
|
|
9
|
-
/**
|
|
10
|
-
* Generate authentication headers for a request
|
|
11
|
-
*/
|
|
12
|
-
generateAuthHeaders(method: string, path: string, timestamp?: number): Record<string, string>;
|
|
13
9
|
/**
|
|
14
10
|
* Validate private key format
|
|
15
11
|
*/
|
|
16
12
|
static validatePrivateKey(privateKey: string): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Generate authentication headers for a request
|
|
15
|
+
*/
|
|
16
|
+
generateAuthHeaders(method: string, path: string, timestamp?: number): Record<string, string>;
|
|
17
17
|
}
|
package/dist/lib/kalshi/auth.js
CHANGED
|
@@ -12,6 +12,30 @@ export class KalshiAuth {
|
|
|
12
12
|
this.keyId = keyId;
|
|
13
13
|
this.privateKey = privateKey;
|
|
14
14
|
}
|
|
15
|
+
/**
|
|
16
|
+
* Validate private key format
|
|
17
|
+
*/
|
|
18
|
+
static validatePrivateKey(privateKey) {
|
|
19
|
+
try {
|
|
20
|
+
// Check if it's PEM format
|
|
21
|
+
if (!privateKey.includes('-----BEGIN') || !privateKey.includes('-----END')) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
// Try to create a sign instance to validate the key
|
|
25
|
+
const sign = createSign('RSA-SHA256');
|
|
26
|
+
sign.update('test');
|
|
27
|
+
sign.end();
|
|
28
|
+
sign.sign({
|
|
29
|
+
key: privateKey,
|
|
30
|
+
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
|
|
31
|
+
saltLength: crypto.constants.RSA_PSS_SALTLEN_DIGEST,
|
|
32
|
+
}, 'base64');
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
15
39
|
/**
|
|
16
40
|
* Generate authentication headers for a request
|
|
17
41
|
*/
|
|
@@ -44,28 +68,4 @@ export class KalshiAuth {
|
|
|
44
68
|
});
|
|
45
69
|
}
|
|
46
70
|
}
|
|
47
|
-
/**
|
|
48
|
-
* Validate private key format
|
|
49
|
-
*/
|
|
50
|
-
static validatePrivateKey(privateKey) {
|
|
51
|
-
try {
|
|
52
|
-
// Check if it's PEM format
|
|
53
|
-
if (!privateKey.includes('-----BEGIN') || !privateKey.includes('-----END')) {
|
|
54
|
-
return false;
|
|
55
|
-
}
|
|
56
|
-
// Try to create a sign instance to validate the key
|
|
57
|
-
const sign = createSign('RSA-SHA256');
|
|
58
|
-
sign.update('test');
|
|
59
|
-
sign.end();
|
|
60
|
-
sign.sign({
|
|
61
|
-
key: privateKey,
|
|
62
|
-
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
|
|
63
|
-
saltLength: crypto.constants.RSA_PSS_SALTLEN_DIGEST,
|
|
64
|
-
}, 'base64');
|
|
65
|
-
return true;
|
|
66
|
-
}
|
|
67
|
-
catch {
|
|
68
|
-
return false;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
71
|
}
|
|
@@ -3,82 +3,82 @@ import type { Balance, CreateOrderRequest, Fill, FillsQuery, Market, MarketsQuer
|
|
|
3
3
|
* Kalshi API client
|
|
4
4
|
*/
|
|
5
5
|
export declare class KalshiClient {
|
|
6
|
-
private axios;
|
|
7
6
|
private auth;
|
|
7
|
+
private axios;
|
|
8
8
|
private baseUrl;
|
|
9
9
|
constructor(baseUrl: string, keyId: string, privateKey: string);
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
12
|
-
*/
|
|
13
|
-
private get;
|
|
14
|
-
/**
|
|
15
|
-
* Make a rate-limited POST request
|
|
11
|
+
* Cancel an order
|
|
16
12
|
*/
|
|
17
|
-
|
|
13
|
+
cancelOrder(orderId: string): Promise<Order>;
|
|
18
14
|
/**
|
|
19
|
-
*
|
|
15
|
+
* Create an order
|
|
20
16
|
*/
|
|
21
|
-
|
|
17
|
+
createOrder(request: CreateOrderRequest): Promise<Order>;
|
|
22
18
|
/**
|
|
23
19
|
* Get account balance
|
|
24
20
|
*/
|
|
25
21
|
getBalance(): Promise<Balance>;
|
|
26
22
|
/**
|
|
27
|
-
* Get
|
|
23
|
+
* Get the base URL
|
|
28
24
|
*/
|
|
29
|
-
|
|
30
|
-
ticker?: string;
|
|
31
|
-
settlement_status?: string;
|
|
32
|
-
}): Promise<Position[]>;
|
|
25
|
+
getBaseUrl(): string;
|
|
33
26
|
/**
|
|
34
|
-
* Get
|
|
27
|
+
* Get fills (executed trades)
|
|
35
28
|
*/
|
|
36
|
-
|
|
37
|
-
markets: Market[];
|
|
29
|
+
getFills(query?: FillsQuery): Promise<{
|
|
38
30
|
cursor?: string;
|
|
31
|
+
fills: Fill[];
|
|
39
32
|
}>;
|
|
40
33
|
/**
|
|
41
34
|
* Get single market by ticker
|
|
42
35
|
*/
|
|
43
36
|
getMarket(ticker: string): Promise<Market>;
|
|
44
37
|
/**
|
|
45
|
-
* Get
|
|
46
|
-
*/
|
|
47
|
-
getOrderBook(ticker: string, depth?: number): Promise<OrderBook>;
|
|
48
|
-
/**
|
|
49
|
-
* Get user orders
|
|
38
|
+
* Get markets
|
|
50
39
|
*/
|
|
51
|
-
|
|
52
|
-
orders: Order[];
|
|
40
|
+
getMarkets(query?: MarketsQuery): Promise<{
|
|
53
41
|
cursor?: string;
|
|
42
|
+
markets: Market[];
|
|
54
43
|
}>;
|
|
55
44
|
/**
|
|
56
45
|
* Get single order by ID
|
|
57
46
|
*/
|
|
58
47
|
getOrder(orderId: string): Promise<Order>;
|
|
59
48
|
/**
|
|
60
|
-
*
|
|
61
|
-
*/
|
|
62
|
-
createOrder(request: CreateOrderRequest): Promise<Order>;
|
|
63
|
-
/**
|
|
64
|
-
* Cancel an order
|
|
49
|
+
* Get order book for a market
|
|
65
50
|
*/
|
|
66
|
-
|
|
51
|
+
getOrderBook(ticker: string, depth?: number): Promise<OrderBook>;
|
|
67
52
|
/**
|
|
68
|
-
* Get
|
|
53
|
+
* Get user orders
|
|
69
54
|
*/
|
|
70
|
-
|
|
71
|
-
fills: Fill[];
|
|
55
|
+
getOrders(query?: OrdersQuery): Promise<{
|
|
72
56
|
cursor?: string;
|
|
57
|
+
orders: Order[];
|
|
73
58
|
}>;
|
|
59
|
+
/**
|
|
60
|
+
* Get portfolio positions
|
|
61
|
+
*/
|
|
62
|
+
getPositions(params?: {
|
|
63
|
+
settlement_status?: string;
|
|
64
|
+
ticker?: string;
|
|
65
|
+
}): Promise<Position[]>;
|
|
74
66
|
/**
|
|
75
67
|
* Test connection to API
|
|
76
68
|
*/
|
|
77
69
|
testConnection(): Promise<boolean>;
|
|
78
70
|
/**
|
|
79
|
-
*
|
|
71
|
+
* Make a rate-limited DELETE request
|
|
80
72
|
*/
|
|
81
|
-
|
|
73
|
+
private delete;
|
|
74
|
+
/**
|
|
75
|
+
* Make a rate-limited GET request
|
|
76
|
+
*/
|
|
77
|
+
private get;
|
|
78
|
+
/**
|
|
79
|
+
* Make a rate-limited POST request
|
|
80
|
+
*/
|
|
81
|
+
private post;
|
|
82
82
|
}
|
|
83
83
|
/**
|
|
84
84
|
* Create a Kalshi client instance
|