@lynx-crypto/kraken-api 0.1.1 → 0.1.2
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 +54 -38
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
# @lynx-crypto/kraken-api
|
|
2
2
|
|
|
3
|
-

|
|
6
|
-

|
|
9
|
-
](https://github.com/lynx-laboratory/kraken-api/actions/workflows/coverage-badge.yml)
|
|
4
|
+
[](https://github.com/lynx-laboratory/kraken-api/actions/workflows/coverage-badge.yml)
|
|
5
|
+
[](https://www.npmjs.com/package/@lynx-crypto/kraken-api)
|
|
6
|
+
[](https://www.npmjs.com/package/@lynx-crypto/kraken-api)
|
|
7
|
+
[](https://bundlephobia.com/package/@lynx-crypto/kraken-api)
|
|
8
|
+
[](https://github.com/lynx-laboratory/kraken-api/commits/master)
|
|
9
|
+
[](./LICENSE.md)
|
|
10
10
|
|
|
11
11
|
TypeScript client for **Kraken SPOT**:
|
|
12
|
+
|
|
12
13
|
- **REST API** (public + private endpoints)
|
|
13
14
|
- **WebSocket v2** (public market-data + authenticated user-data/trading)
|
|
14
15
|
|
|
@@ -24,21 +25,25 @@ IMPORTANT
|
|
|
24
25
|
## Install
|
|
25
26
|
|
|
26
27
|
NPM:
|
|
28
|
+
|
|
27
29
|
```
|
|
28
30
|
npm i @lynx-crypto/kraken-api
|
|
29
31
|
```
|
|
30
32
|
|
|
31
33
|
Yarn:
|
|
34
|
+
|
|
32
35
|
```
|
|
33
36
|
yarn add @lynx-crypto/kraken-api
|
|
34
37
|
```
|
|
35
38
|
|
|
36
39
|
pnpm:
|
|
40
|
+
|
|
37
41
|
```
|
|
38
42
|
pnpm add @lynx-crypto/kraken-api
|
|
39
43
|
```
|
|
40
44
|
|
|
41
45
|
Node support
|
|
46
|
+
|
|
42
47
|
- Node >= 18 recommended (uses built-in fetch / AbortController)
|
|
43
48
|
|
|
44
49
|
---
|
|
@@ -46,11 +51,13 @@ Node support
|
|
|
46
51
|
## Quick start
|
|
47
52
|
|
|
48
53
|
ESM:
|
|
54
|
+
|
|
49
55
|
```
|
|
50
56
|
import { KrakenSpotRestClient, KrakenSpotWebsocketV2Client } from "@lynx-crypto/kraken-api";
|
|
51
57
|
```
|
|
52
58
|
|
|
53
59
|
CJS:
|
|
60
|
+
|
|
54
61
|
```
|
|
55
62
|
const { KrakenSpotRestClient, KrakenSpotWebsocketV2Client } = require("@lynx-crypto/kraken-api");
|
|
56
63
|
```
|
|
@@ -63,17 +70,17 @@ const { KrakenSpotRestClient, KrakenSpotWebsocketV2Client } = require("@lynx-cry
|
|
|
63
70
|
|
|
64
71
|
```ts
|
|
65
72
|
const kraken = new KrakenSpotRestClient({
|
|
66
|
-
// Optional:
|
|
67
|
-
// baseUrl: "https://api.kraken.com",
|
|
68
|
-
// timeoutMs: 10_000,
|
|
69
|
-
// userAgent: "my-app/1.0.0",
|
|
73
|
+
// Optional:
|
|
74
|
+
// baseUrl: "https://api.kraken.com",
|
|
75
|
+
// timeoutMs: 10_000,
|
|
76
|
+
// userAgent: "my-app/1.0.0",
|
|
70
77
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
78
|
+
// Required for private endpoints:
|
|
79
|
+
apiKey: process.env.KRAKEN_API_KEY,
|
|
80
|
+
apiSecret: process.env.KRAKEN_API_SECRET,
|
|
74
81
|
|
|
75
|
-
|
|
76
|
-
|
|
82
|
+
// Optional logger:
|
|
83
|
+
// logger: console,
|
|
77
84
|
});
|
|
78
85
|
```
|
|
79
86
|
|
|
@@ -82,6 +89,7 @@ const kraken = new KrakenSpotRestClient({
|
|
|
82
89
|
(Your exact public endpoints depend on what you’ve implemented in src/spot/rest.)
|
|
83
90
|
|
|
84
91
|
Example shape:
|
|
92
|
+
|
|
85
93
|
```ts
|
|
86
94
|
const serverTime = await kraken.public.getServerTime();
|
|
87
95
|
```
|
|
@@ -107,29 +115,30 @@ This package provides a top-level v2 WS client that creates:
|
|
|
107
115
|
|
|
108
116
|
```ts
|
|
109
117
|
const ws = new KrakenSpotWebsocketV2Client({
|
|
110
|
-
// Optional override URLs:
|
|
111
|
-
// publicUrl: "wss://ws.kraken.com/v2",
|
|
112
|
-
// privateUrl: "wss://ws-auth.kraken.com/v2",
|
|
118
|
+
// Optional override URLs:
|
|
119
|
+
// publicUrl: "wss://ws.kraken.com/v2",
|
|
120
|
+
// privateUrl: "wss://ws-auth.kraken.com/v2",
|
|
113
121
|
|
|
114
|
-
|
|
115
|
-
|
|
122
|
+
// IMPORTANT: private WS requires a session token
|
|
123
|
+
authToken: process.env.KRAKEN_WS_AUTH_TOKEN,
|
|
116
124
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
125
|
+
// Optional connection tuning:
|
|
126
|
+
// autoReconnect: true,
|
|
127
|
+
// reconnectDelayMs: 1_000,
|
|
128
|
+
// requestTimeoutMs: 10_000,
|
|
121
129
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
// Optional WS implementation:
|
|
126
|
-
// - In Node, ws is used by default.
|
|
127
|
-
// - In browsers, pass the browser WebSocket if needed.
|
|
128
|
-
// WebSocketImpl: WebSocket,
|
|
130
|
+
// Optional logger:
|
|
131
|
+
// logger: console,
|
|
129
132
|
|
|
133
|
+
// Optional WS implementation:
|
|
134
|
+
// - In Node, ws is used by default.
|
|
135
|
+
// - In browsers, pass the browser WebSocket if needed.
|
|
136
|
+
// WebSocketImpl: WebSocket,
|
|
130
137
|
});
|
|
131
138
|
```
|
|
139
|
+
|
|
132
140
|
Available sub-APIs:
|
|
141
|
+
|
|
133
142
|
- `ws.admin` (public connection)
|
|
134
143
|
- `ws.marketData` (public connection)
|
|
135
144
|
- `ws.userData` (private connection)
|
|
@@ -138,6 +147,7 @@ Available sub-APIs:
|
|
|
138
147
|
### Connect
|
|
139
148
|
|
|
140
149
|
You can connect explicitly:
|
|
150
|
+
|
|
141
151
|
```ts
|
|
142
152
|
await ws.publicConnection.connect();
|
|
143
153
|
await ws.privateConnection.connect();
|
|
@@ -153,9 +163,9 @@ The underlying `KrakenWebsocketBase` supports message fan-out:
|
|
|
153
163
|
|
|
154
164
|
```ts
|
|
155
165
|
const unsubscribe = ws.publicConnection.addMessageHandler((msg) => {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
166
|
+
// msg is already JSON-parsed when possible
|
|
167
|
+
// route based on msg.channel / msg.type, etc.
|
|
168
|
+
// console.log(msg);
|
|
159
169
|
});
|
|
160
170
|
|
|
161
171
|
// later:
|
|
@@ -169,9 +179,10 @@ unsubscribe();
|
|
|
169
179
|
Admin utilities exist on the public connection (ex: ping/status/heartbeat).
|
|
170
180
|
|
|
171
181
|
Example:
|
|
182
|
+
|
|
172
183
|
```ts
|
|
173
184
|
const pong = await ws.admin.ping({ reqId: 123 });
|
|
174
|
-
if (!pong.success) console.error(
|
|
185
|
+
if (!pong.success) console.error('ping failed:', pong.error);
|
|
175
186
|
```
|
|
176
187
|
|
|
177
188
|
---
|
|
@@ -182,6 +193,7 @@ Market data subscriptions live on ws.marketData (public connection).
|
|
|
182
193
|
(Exact channel helpers depend on your implemented market-data modules.)
|
|
183
194
|
|
|
184
195
|
Typical pattern:
|
|
196
|
+
|
|
185
197
|
1. call subscribe helper (await ack)
|
|
186
198
|
2. listen via `addMessageHandler` and route messages by channel/type
|
|
187
199
|
|
|
@@ -197,6 +209,7 @@ Implemented channels:
|
|
|
197
209
|
- balances (balance snapshots + ledger-derived updates)
|
|
198
210
|
|
|
199
211
|
Example (executions):
|
|
212
|
+
|
|
200
213
|
```
|
|
201
214
|
const ack = await ws.userData.subscribeExecutions({
|
|
202
215
|
snap_trades: true,
|
|
@@ -208,6 +221,7 @@ if (!ack.success) console.error("executions subscribe error:", ack.error);
|
|
|
208
221
|
```
|
|
209
222
|
|
|
210
223
|
Then route messages:
|
|
224
|
+
|
|
211
225
|
```
|
|
212
226
|
ws.privateConnection.addMessageHandler((msg: any) => {
|
|
213
227
|
if (msg?.channel === "executions" && (msg.type === "snapshot" || msg.type === "update")) {
|
|
@@ -219,7 +233,8 @@ ws.privateConnection.addMessageHandler((msg: any) => {
|
|
|
219
233
|
```
|
|
220
234
|
|
|
221
235
|
Example (balances):
|
|
222
|
-
|
|
236
|
+
|
|
237
|
+
````ts
|
|
223
238
|
const ack2 = await ws.userData.subscribeBalances({ snapshot: true });
|
|
224
239
|
if (!ack2.success) console.error("balances subscribe error:", ack2.error);
|
|
225
240
|
|
|
@@ -268,9 +283,10 @@ const res = await ws.userTrading.addOrder({
|
|
|
268
283
|
|
|
269
284
|
if (res.success) console.log("order_id:", res.result?.order_id);
|
|
270
285
|
else console.error("add_order error:", res.error);
|
|
271
|
-
|
|
286
|
+
````
|
|
272
287
|
|
|
273
288
|
Dead Man’s Switch:
|
|
289
|
+
|
|
274
290
|
```
|
|
275
291
|
// recommended: refresh every 15–30s with timeout=60
|
|
276
292
|
await ws.userTrading.cancelAllOrdersAfter({ timeout: 60 });
|