@n1xyz/nord-ts 0.0.18-8121ed05.0 → 0.0.19
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/.claude/settings.local.json +11 -0
- package/.local/qa.ts +77 -0
- package/.local/test-atomic.ts +112 -0
- package/check.sh +4 -0
- package/default.nix +47 -0
- package/package.json +20 -27
- package/src/index.ts +0 -16
- package/src/nord/api/actions.ts +131 -9
- package/src/nord/api/core.ts +0 -71
- package/src/nord/client/Nord.ts +142 -76
- package/src/nord/client/NordUser.ts +171 -50
- package/src/nord/index.ts +0 -2
- package/src/nord/models/Subscriber.ts +2 -2
- package/src/types.ts +55 -216
- package/src/utils.ts +6 -42
- package/src/websocket/NordWebSocketClient.ts +23 -15
- package/src/websocket/index.ts +1 -1
- package/tests/utils.spec.ts +1 -34
- package/dist/bridge/client.d.ts +0 -151
- package/dist/bridge/client.js +0 -434
- package/dist/bridge/const.d.ts +0 -23
- package/dist/bridge/const.js +0 -47
- package/dist/bridge/index.d.ts +0 -4
- package/dist/bridge/index.js +0 -23
- package/dist/bridge/types.d.ts +0 -120
- package/dist/bridge/types.js +0 -18
- package/dist/bridge/utils.d.ts +0 -64
- package/dist/bridge/utils.js +0 -131
- package/dist/const.d.ts +0 -8
- package/dist/const.js +0 -30
- package/dist/gen/common.d.ts +0 -68
- package/dist/gen/common.js +0 -215
- package/dist/gen/nord.d.ts +0 -853
- package/dist/gen/nord.js +0 -6368
- package/dist/idl/bridge.d.ts +0 -569
- package/dist/idl/bridge.js +0 -8
- package/dist/idl/bridge.json +0 -1506
- package/dist/idl/index.d.ts +0 -607
- package/dist/idl/index.js +0 -8
- package/dist/index.d.ts +0 -6
- package/dist/index.js +0 -30
- package/dist/nord/api/actions.d.ts +0 -106
- package/dist/nord/api/actions.js +0 -256
- package/dist/nord/api/core.d.ts +0 -49
- package/dist/nord/api/core.js +0 -164
- package/dist/nord/api/market.d.ts +0 -36
- package/dist/nord/api/market.js +0 -96
- package/dist/nord/api/metrics.d.ts +0 -67
- package/dist/nord/api/metrics.js +0 -229
- package/dist/nord/api/queries.d.ts +0 -46
- package/dist/nord/api/queries.js +0 -109
- package/dist/nord/client/Nord.d.ts +0 -284
- package/dist/nord/client/Nord.js +0 -491
- package/dist/nord/client/NordUser.d.ts +0 -287
- package/dist/nord/client/NordUser.js +0 -595
- package/dist/nord/index.d.ts +0 -9
- package/dist/nord/index.js +0 -33
- package/dist/nord/models/Subscriber.d.ts +0 -37
- package/dist/nord/models/Subscriber.js +0 -25
- package/dist/nord/utils/NordError.d.ts +0 -35
- package/dist/nord/utils/NordError.js +0 -49
- package/dist/types.d.ts +0 -407
- package/dist/types.js +0 -103
- package/dist/utils.d.ts +0 -116
- package/dist/utils.js +0 -271
- package/dist/websocket/NordWebSocketClient.d.ts +0 -68
- package/dist/websocket/NordWebSocketClient.js +0 -338
- package/dist/websocket/events.d.ts +0 -19
- package/dist/websocket/events.js +0 -2
- package/dist/websocket/index.d.ts +0 -2
- package/dist/websocket/index.js +0 -5
- package/jest.config.ts +0 -9
- package/nodemon.json +0 -4
- package/protoc-generate.sh +0 -23
- package/src/idl/bridge.json +0 -1506
- package/src/nord/api/market.ts +0 -122
- package/src/nord/api/queries.ts +0 -135
package/src/nord/index.ts
CHANGED
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
DeltaEvent,
|
|
5
5
|
OrderbookResponse,
|
|
6
6
|
SubscriberConfig,
|
|
7
|
-
|
|
7
|
+
StreamTrade,
|
|
8
8
|
Trades,
|
|
9
9
|
} from "../../types";
|
|
10
10
|
import { MAX_BUFFER_LEN } from "../../utils";
|
|
@@ -49,7 +49,7 @@ export interface OrderbookSubscription extends EventEmitter {
|
|
|
49
49
|
* Interface for trade subscription
|
|
50
50
|
*/
|
|
51
51
|
export interface TradeSubscription extends EventEmitter {
|
|
52
|
-
on(event: "message", listener: (data:
|
|
52
|
+
on(event: "message", listener: (data: StreamTrade[]) => void): this;
|
|
53
53
|
on(event: "error", listener: (error: Error) => void): this;
|
|
54
54
|
close(): void;
|
|
55
55
|
removeAllListeners(event?: string): this;
|
package/src/types.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as proto from "./gen/nord";
|
|
2
|
+
import type { components } from "./gen/openapi.ts";
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* The peak TPS rate is queried over the specified period.
|
|
@@ -46,6 +47,46 @@ export interface NordConfig {
|
|
|
46
47
|
initWebSockets?: boolean;
|
|
47
48
|
}
|
|
48
49
|
|
|
50
|
+
export type Info = components["schemas"]["Info2"];
|
|
51
|
+
export type Market = Info["markets"][number];
|
|
52
|
+
export type Token = Info["tokens"][number];
|
|
53
|
+
export type Account = components["schemas"]["Account"];
|
|
54
|
+
export type TradesResponse =
|
|
55
|
+
components["schemas"]["PageResult_for_String_and_Trade"];
|
|
56
|
+
export type User = components["schemas"]["User"];
|
|
57
|
+
export type OrderbookResponse = components["schemas"]["OrderbookInfo"];
|
|
58
|
+
export type MarketStats = components["schemas"]["MarketStats"];
|
|
59
|
+
export type OrderbookInfo = components["schemas"]["OrderbookInfo"];
|
|
60
|
+
export type MarketStatsFromApi = components["schemas"]["MarketStats"];
|
|
61
|
+
export type TradeFromApi = components["schemas"]["Trade"];
|
|
62
|
+
// Generic PageResult type - note that K and V must match existing schema keys
|
|
63
|
+
export type PageResult<K extends string, V extends string> = K extends "String"
|
|
64
|
+
? V extends "OrderInfo"
|
|
65
|
+
? components["schemas"]["PageResult_for_String_and_OrderInfo"]
|
|
66
|
+
: V extends "Trade"
|
|
67
|
+
? components["schemas"]["PageResult_for_String_and_Trade"]
|
|
68
|
+
: never
|
|
69
|
+
: never;
|
|
70
|
+
export type PageResultStringOrderInfo =
|
|
71
|
+
components["schemas"]["PageResult_for_String_and_OrderInfo"];
|
|
72
|
+
export type PageResultStringTrade =
|
|
73
|
+
components["schemas"]["PageResult_for_String_and_Trade"];
|
|
74
|
+
export type OrderInfoFromApi = components["schemas"]["OrderInfo"];
|
|
75
|
+
export type OpenOrder = components["schemas"]["OpenOrder"];
|
|
76
|
+
export type Balance = components["schemas"]["Balance"];
|
|
77
|
+
export type PositionSummary = components["schemas"]["PositionSummary"];
|
|
78
|
+
export type PerpPosition = components["schemas"]["PerpPosition"];
|
|
79
|
+
export type AccountMarginsView = components["schemas"]["AccountMarginsView"];
|
|
80
|
+
export type SideSummary = components["schemas"]["SideSummary"];
|
|
81
|
+
export type UserSession = components["schemas"]["UserSession"];
|
|
82
|
+
export type ActionsItem = components["schemas"]["ActionsItem"];
|
|
83
|
+
export type FillRole = components["schemas"]["FillRole"];
|
|
84
|
+
export type PerpMarketStatsFromApi = components["schemas"]["PerpMarketStats"];
|
|
85
|
+
export type SideFromApi = components["schemas"]["Side"];
|
|
86
|
+
export type FillModeFromApi = components["schemas"]["FillMode"];
|
|
87
|
+
export type PlacementOrigin = components["schemas"]["PlacementOrigin"];
|
|
88
|
+
export type FinalizationReason = components["schemas"]["FinalizationReason"];
|
|
89
|
+
|
|
49
90
|
/**
|
|
50
91
|
* Configuration options for the Nord client
|
|
51
92
|
*/
|
|
@@ -87,28 +128,6 @@ export interface SubscriberConfig {
|
|
|
87
128
|
maxBufferLen?: number;
|
|
88
129
|
}
|
|
89
130
|
|
|
90
|
-
export interface Market {
|
|
91
|
-
marketId: number;
|
|
92
|
-
symbol: string;
|
|
93
|
-
priceDecimals: number;
|
|
94
|
-
sizeDecimals: number;
|
|
95
|
-
baseTokenId: number;
|
|
96
|
-
quoteTokenId: number;
|
|
97
|
-
frozen: boolean;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export interface Token {
|
|
101
|
-
tokenId: number;
|
|
102
|
-
symbol: string;
|
|
103
|
-
decimals: number;
|
|
104
|
-
mintAddr: string;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export interface Info {
|
|
108
|
-
markets: Market[];
|
|
109
|
-
tokens: Token[];
|
|
110
|
-
}
|
|
111
|
-
|
|
112
131
|
export interface DeltaEvent {
|
|
113
132
|
last_update_id: number;
|
|
114
133
|
update_id: number;
|
|
@@ -117,7 +136,7 @@ export interface DeltaEvent {
|
|
|
117
136
|
bids: OrderbookEntry[];
|
|
118
137
|
}
|
|
119
138
|
|
|
120
|
-
export interface
|
|
139
|
+
export interface StreamTrade {
|
|
121
140
|
side: Side;
|
|
122
141
|
price: number;
|
|
123
142
|
size: number;
|
|
@@ -128,95 +147,26 @@ export interface Trades {
|
|
|
128
147
|
last_update_id: number;
|
|
129
148
|
update_id: number;
|
|
130
149
|
market_symbol: string;
|
|
131
|
-
trades:
|
|
150
|
+
trades: StreamTrade[];
|
|
132
151
|
}
|
|
133
152
|
|
|
134
|
-
export interface
|
|
153
|
+
export interface LocalOrderInfo {
|
|
135
154
|
id: number;
|
|
136
155
|
reduce_only: boolean;
|
|
137
156
|
limit_price: number;
|
|
138
157
|
size: number;
|
|
139
158
|
account_id: number;
|
|
159
|
+
sender_tracking_id?: number;
|
|
140
160
|
}
|
|
141
161
|
|
|
142
162
|
export interface HashMap<T> {
|
|
143
163
|
[key: number]: T;
|
|
144
164
|
}
|
|
145
165
|
|
|
146
|
-
export interface Account {
|
|
147
|
-
last_update_id: number;
|
|
148
|
-
update_id: number;
|
|
149
|
-
account_id: number;
|
|
150
|
-
fills: HashMap<number>;
|
|
151
|
-
places: HashMap<OrderInfo>;
|
|
152
|
-
cancels: HashMap<OrderInfo>;
|
|
153
|
-
balances: HashMap<number>;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Query the transactions in the specified L2 block.
|
|
158
|
-
* block_number specifies the block number to query.
|
|
159
|
-
* If not specified, transactions from latest block are returned.
|
|
160
|
-
*/
|
|
161
|
-
export interface BlockQuery {
|
|
162
|
-
block_number?: number;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Response for BlockQuery.
|
|
167
|
-
* block_number specifies the block number being returned.
|
|
168
|
-
* actions are the list of transactions from the block.
|
|
169
|
-
*/
|
|
170
|
-
export interface BlockResponse {
|
|
171
|
-
block_number: number;
|
|
172
|
-
actions: ActionInfo[];
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* Response for BlockSummaryQuery.
|
|
177
|
-
* block_summary is the summary of upto the last N blocks.
|
|
178
|
-
* The server can return fewer than last_n block summary if
|
|
179
|
-
* fewer blocks are available or if it exceeds a max cap.
|
|
180
|
-
*/
|
|
181
|
-
export interface BlockSummaryResponse {
|
|
182
|
-
block_summary: BlockSummary[];
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Query the action for the specified action id.
|
|
187
|
-
* action_id specifies the action to query.
|
|
188
|
-
*/
|
|
189
|
-
export interface ActionQuery {
|
|
190
|
-
action_id: number;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
/**
|
|
194
|
-
* Response for ActionQuery.
|
|
195
|
-
* block_number the block the action is part of.
|
|
196
|
-
* If the action is not yet included in any block, null is returned.
|
|
197
|
-
* action the transaction.
|
|
198
|
-
*/
|
|
199
166
|
export interface ActionResponse {
|
|
200
|
-
|
|
167
|
+
actionId: number;
|
|
201
168
|
action: proto.Action;
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Query the recent actions
|
|
206
|
-
* last_n requests last N actions.
|
|
207
|
-
*/
|
|
208
|
-
export interface ActionsQuery {
|
|
209
|
-
last_n: number;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* Response for ActionsQuery.
|
|
214
|
-
* actions returns upto the last N actions.
|
|
215
|
-
* The server can return fewer than last_n actions if
|
|
216
|
-
* fewer actions are available or if it exceeds a max cap.
|
|
217
|
-
*/
|
|
218
|
-
export interface ActionsResponse {
|
|
219
|
-
actions: ActionsExtendedInfo[];
|
|
169
|
+
physicalExecTime: Date;
|
|
220
170
|
}
|
|
221
171
|
|
|
222
172
|
/**
|
|
@@ -231,31 +181,6 @@ export interface BlockSummary {
|
|
|
231
181
|
to_action_id: number;
|
|
232
182
|
}
|
|
233
183
|
|
|
234
|
-
/**
|
|
235
|
-
* Info about the block transaction.
|
|
236
|
-
* action_id is the action identifier.
|
|
237
|
-
* action in protobuf format.
|
|
238
|
-
* exec_timestamp is the execution timestamp.
|
|
239
|
-
*/
|
|
240
|
-
export interface ActionInfo {
|
|
241
|
-
action_id: number;
|
|
242
|
-
action: proto.Action;
|
|
243
|
-
exec_timestamp: number;
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
/**
|
|
247
|
-
* Extended info about the block transaction.
|
|
248
|
-
* block_number the block the action is part of.
|
|
249
|
-
* If the action is not yet included in any block, null is returned.
|
|
250
|
-
* action_id of the action.
|
|
251
|
-
* action the transaction.
|
|
252
|
-
*/
|
|
253
|
-
export interface ActionsExtendedInfo {
|
|
254
|
-
block_number?: number;
|
|
255
|
-
action_id: number;
|
|
256
|
-
action: proto.Action;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
184
|
/**
|
|
260
185
|
* Aggregate metrics
|
|
261
186
|
* blocks_total: Total number of L2 blocks.
|
|
@@ -272,54 +197,6 @@ export interface AggregateMetrics {
|
|
|
272
197
|
request_latency_average: number;
|
|
273
198
|
}
|
|
274
199
|
|
|
275
|
-
// The JSON types returned by rollman, that need to be translated to TS format.
|
|
276
|
-
export interface RollmanBlockResponse {
|
|
277
|
-
block_number: number;
|
|
278
|
-
actions: RollmanActionInfo[];
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
export interface RollmanActionResponse {
|
|
282
|
-
block_number?: number;
|
|
283
|
-
action_pb: Uint8Array;
|
|
284
|
-
exec_timestamp: number;
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
export interface RollmanActionsResponse {
|
|
288
|
-
actions: RollmanActionExtendedInfo[];
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
export interface RollmanActionInfo {
|
|
292
|
-
action_id: number;
|
|
293
|
-
action_pb: Uint8Array;
|
|
294
|
-
exec_timestamp: number;
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
export interface RollmanActionExtendedInfo {
|
|
298
|
-
block_number?: number;
|
|
299
|
-
action_id: number;
|
|
300
|
-
action_pb: Uint8Array;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
export interface MarketsStatsResponse {
|
|
304
|
-
markets: MarketStats[];
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
export interface MarketStats {
|
|
308
|
-
market_id: number;
|
|
309
|
-
index_price: [number, number];
|
|
310
|
-
volume_24h: number;
|
|
311
|
-
high_24h: number;
|
|
312
|
-
low_24h: number;
|
|
313
|
-
perp_stats?: PerpMarketStats;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
export interface PerpMarketStats {
|
|
317
|
-
mark_price?: number;
|
|
318
|
-
funding_rate?: number;
|
|
319
|
-
next_funding_time?: Date;
|
|
320
|
-
open_interest?: number;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
200
|
/**
|
|
324
201
|
* Converts a `FillMode` enum to its corresponding protobuf representation.
|
|
325
202
|
*
|
|
@@ -337,41 +214,6 @@ export function fillModeToProtoFillMode(x: FillMode): proto.FillMode {
|
|
|
337
214
|
throw new Error("Invalid fill mode");
|
|
338
215
|
}
|
|
339
216
|
|
|
340
|
-
/**
|
|
341
|
-
* Response for trades query with pagination support
|
|
342
|
-
*/
|
|
343
|
-
export interface TradesResponse {
|
|
344
|
-
trades: Trade[];
|
|
345
|
-
accountId: number;
|
|
346
|
-
since?: string; // RFC3339 timestamp
|
|
347
|
-
until?: string; // RFC3339 timestamp
|
|
348
|
-
nextPageId?: string;
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
/**
|
|
352
|
-
* Query parameters for trades endpoint
|
|
353
|
-
*/
|
|
354
|
-
export interface TradesQuery {
|
|
355
|
-
accountId: number;
|
|
356
|
-
since?: string; // RFC3339 timestamp
|
|
357
|
-
until?: string; // RFC3339 timestamp
|
|
358
|
-
pageId?: string;
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
/**
|
|
362
|
-
* Response for user account IDs query
|
|
363
|
-
*/
|
|
364
|
-
export interface UserAccountIdsResponse {
|
|
365
|
-
accountIds: number[];
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
/**
|
|
369
|
-
* Query parameters for user account IDs
|
|
370
|
-
*/
|
|
371
|
-
export interface UserAccountIdsQuery {
|
|
372
|
-
pubkey: string; // secp256k1 public key in hex format
|
|
373
|
-
}
|
|
374
|
-
|
|
375
217
|
/**
|
|
376
218
|
* Orderbook entry representing price and size
|
|
377
219
|
*/
|
|
@@ -380,16 +222,6 @@ export interface OrderbookEntry {
|
|
|
380
222
|
size: number;
|
|
381
223
|
}
|
|
382
224
|
|
|
383
|
-
/**
|
|
384
|
-
* Response for orderbook query
|
|
385
|
-
*/
|
|
386
|
-
export interface OrderbookResponse {
|
|
387
|
-
symbol: string;
|
|
388
|
-
asks: OrderbookEntry[];
|
|
389
|
-
bids: OrderbookEntry[];
|
|
390
|
-
timestamp: number;
|
|
391
|
-
}
|
|
392
|
-
|
|
393
225
|
/**
|
|
394
226
|
* Query parameters for orderbook
|
|
395
227
|
*
|
|
@@ -440,7 +272,7 @@ export interface WebSocketSubscription {
|
|
|
440
272
|
export interface WebSocketTradeUpdate {
|
|
441
273
|
e: WebSocketMessageType.TradeUpdate;
|
|
442
274
|
symbol: string;
|
|
443
|
-
trades:
|
|
275
|
+
trades: StreamTrade[];
|
|
444
276
|
timestamp: number;
|
|
445
277
|
}
|
|
446
278
|
|
|
@@ -472,3 +304,10 @@ export type WebSocketMessage =
|
|
|
472
304
|
| WebSocketTradeUpdate
|
|
473
305
|
| WebSocketDeltaUpdate
|
|
474
306
|
| WebSocketAccountUpdate;
|
|
307
|
+
|
|
308
|
+
export interface SPLTokenInfo {
|
|
309
|
+
mint: string;
|
|
310
|
+
precision: number;
|
|
311
|
+
tokenId: number;
|
|
312
|
+
name: string;
|
|
313
|
+
}
|
package/src/utils.ts
CHANGED
|
@@ -25,6 +25,12 @@ export function panic(message: string): never {
|
|
|
25
25
|
throw new Error(message);
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
export function isRfc3339(s: string): boolean {
|
|
29
|
+
const REGEX =
|
|
30
|
+
/^((?:(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}(?:\.\d+)?))(Z|[\+-]\d{2}:\d{2})?)$/;
|
|
31
|
+
return REGEX.test(s);
|
|
32
|
+
}
|
|
33
|
+
|
|
28
34
|
export function assert(predicate: boolean, message?: string): void {
|
|
29
35
|
if (!predicate) panic(message ?? "Assertion violated");
|
|
30
36
|
}
|
|
@@ -38,26 +44,6 @@ export function optExpect<T>(value: T | undefined, message: string): T {
|
|
|
38
44
|
if (value === undefined) throw new Error(message);
|
|
39
45
|
return value as T;
|
|
40
46
|
}
|
|
41
|
-
/**
|
|
42
|
-
* Unwraps optional value with default error message
|
|
43
|
-
* @param value
|
|
44
|
-
* @returns
|
|
45
|
-
*/
|
|
46
|
-
export function optUnwrap<T>(value: T | undefined): T {
|
|
47
|
-
return optExpect(value, "Optional contains no value");
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Applies function to value if it's defined, or passes `undefined` through
|
|
51
|
-
* @param value Optional value to map
|
|
52
|
-
* @param mapFn Mapper function
|
|
53
|
-
* @returns Either mapped value or undefined
|
|
54
|
-
*/
|
|
55
|
-
export function optMap<T, U>(
|
|
56
|
-
value: T | undefined,
|
|
57
|
-
mapFn: (arg: T) => U,
|
|
58
|
-
): U | undefined {
|
|
59
|
-
return value !== undefined ? mapFn(value) : undefined;
|
|
60
|
-
}
|
|
61
47
|
/** Behaves same as `node-fetch/fetch` but throws if response is a failure
|
|
62
48
|
*
|
|
63
49
|
* @param url Request HTTP URL
|
|
@@ -190,28 +176,6 @@ export const toScaledU128 = makeToScaledBigUint({
|
|
|
190
176
|
exponent: 56,
|
|
191
177
|
});
|
|
192
178
|
|
|
193
|
-
const U64_MAX = (1n << 64n) - 1n;
|
|
194
|
-
const U128_MAX = (1n << 128n) - 1n;
|
|
195
|
-
/**
|
|
196
|
-
* Converts U128 into pair of U64 numbers, to pass it through protobuf
|
|
197
|
-
* @param value integer, must fit U128 limits
|
|
198
|
-
* @returns Pair of U64 integers which represent original number split in two
|
|
199
|
-
*/
|
|
200
|
-
export function bigIntToProtoU128(value: bigint): proto.U128 {
|
|
201
|
-
if (value < 0n) {
|
|
202
|
-
throw new Error(`Negative number (${value})`);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
if (value > U128_MAX) {
|
|
206
|
-
throw new Error(`U128 overflow (${value})`);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
return {
|
|
210
|
-
lo: value & U64_MAX,
|
|
211
|
-
hi: (value >> 64n) & U64_MAX,
|
|
212
|
-
};
|
|
213
|
-
}
|
|
214
|
-
|
|
215
179
|
/**
|
|
216
180
|
* Encodes any protobuf message into a length-delimited format,
|
|
217
181
|
* i.e. prefixed with its length encoded as varint
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import WebSocket from "ws";
|
|
2
1
|
import { EventEmitter } from "events";
|
|
2
|
+
import WebSocket from "ws";
|
|
3
3
|
import {
|
|
4
|
+
WebSocketAccountUpdate,
|
|
5
|
+
WebSocketDeltaUpdate,
|
|
4
6
|
WebSocketMessage,
|
|
5
7
|
WebSocketMessageType,
|
|
6
8
|
WebSocketSubscription,
|
|
7
9
|
WebSocketTradeUpdate,
|
|
8
|
-
WebSocketDeltaUpdate,
|
|
9
|
-
WebSocketAccountUpdate,
|
|
10
10
|
} from "../types";
|
|
11
11
|
import { NordWebSocketClientEvents } from "./events";
|
|
12
12
|
|
|
@@ -394,19 +394,27 @@ export class NordWebSocketClient
|
|
|
394
394
|
* @param message WebSocket message
|
|
395
395
|
*/
|
|
396
396
|
private handleMessage(message: WebSocketMessage): void {
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
break;
|
|
401
|
-
case WebSocketMessageType.DeltaUpdate:
|
|
402
|
-
this.emit("delta", message as WebSocketDeltaUpdate);
|
|
403
|
-
break;
|
|
404
|
-
case WebSocketMessageType.AccountUpdate:
|
|
405
|
-
this.emit("account", message as WebSocketAccountUpdate);
|
|
406
|
-
break;
|
|
407
|
-
default:
|
|
408
|
-
this.emit("error", new Error(`Unknown message type: ${message.e}`));
|
|
397
|
+
if (!message || typeof message !== "object") {
|
|
398
|
+
this.emit("error", new Error(`Unexpected message type: ${message}`));
|
|
399
|
+
return;
|
|
409
400
|
}
|
|
401
|
+
|
|
402
|
+
const hasOwn = (k: string) =>
|
|
403
|
+
Object.prototype.hasOwnProperty.call(message, k);
|
|
404
|
+
if (hasOwn("trades")) {
|
|
405
|
+
this.emit("trades", message as WebSocketTradeUpdate);
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
if (hasOwn("delta")) {
|
|
409
|
+
this.emit("delta", message as WebSocketDeltaUpdate);
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
if (hasOwn("account")) {
|
|
413
|
+
this.emit("account", message as WebSocketAccountUpdate);
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
this.emit("error", new Error(`Unexpected message type: ${message}`));
|
|
410
418
|
}
|
|
411
419
|
|
|
412
420
|
/**
|
package/src/websocket/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { NordWebSocketClient } from "./NordWebSocketClient";
|
|
2
|
-
export { NordWebSocketEvents, NordWebSocketClientEvents } from "./events";
|
|
2
|
+
export type { NordWebSocketEvents, NordWebSocketClientEvents } from "./events";
|
package/tests/utils.spec.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { describe, it, expect } from "@jest/globals";
|
|
2
2
|
import {
|
|
3
|
-
bigIntToProtoU128,
|
|
4
3
|
toScaledU64,
|
|
5
4
|
toScaledU128,
|
|
6
5
|
encodeLengthDelimited,
|
|
@@ -91,38 +90,6 @@ describe("toScaledU128", () => {
|
|
|
91
90
|
});
|
|
92
91
|
});
|
|
93
92
|
|
|
94
|
-
const U64_MAX = (1n << 64n) - 1n;
|
|
95
|
-
const U128_MAX = (1n << 128n) - 1n;
|
|
96
|
-
|
|
97
|
-
describe("bigIntToProtoU128", () => {
|
|
98
|
-
const success: Array<[bigint, { lo: bigint; hi: bigint }]> = [
|
|
99
|
-
[0n, { lo: 0n, hi: 0n }],
|
|
100
|
-
[1n, { lo: 1n, hi: 0n }],
|
|
101
|
-
[U64_MAX - 1n, { lo: U64_MAX - 1n, hi: 0n }],
|
|
102
|
-
[U64_MAX, { lo: U64_MAX, hi: 0n }],
|
|
103
|
-
[U64_MAX + 1n, { lo: 0n, hi: 1n }],
|
|
104
|
-
[U128_MAX - 1n, { lo: U64_MAX - 1n, hi: U64_MAX }],
|
|
105
|
-
[U128_MAX, { lo: U64_MAX, hi: U64_MAX }],
|
|
106
|
-
];
|
|
107
|
-
|
|
108
|
-
success.forEach((sample, index) => {
|
|
109
|
-
it(`Conversion to Protobuf's U128 should succeed: case ${index}`, () => {
|
|
110
|
-
expect(bigIntToProtoU128(sample[0])).toEqual(sample[1]);
|
|
111
|
-
});
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
const failure: Array<[bigint, string]> = [
|
|
115
|
-
[-1n, "Negative number"],
|
|
116
|
-
[U128_MAX + 1n, "U128 overflow"],
|
|
117
|
-
];
|
|
118
|
-
|
|
119
|
-
failure.forEach((sample, index) => {
|
|
120
|
-
it(`Conversion to Protobuf's U128 should fail: case ${index}`, () => {
|
|
121
|
-
expect(() => bigIntToProtoU128(sample[0])).toThrow(sample[1]);
|
|
122
|
-
});
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
|
|
126
93
|
describe("proto.Action encode-decode loop", () => {
|
|
127
94
|
const action: proto.Action = {
|
|
128
95
|
currentTimestamp: 0n,
|
|
@@ -137,7 +104,7 @@ describe("proto.Action encode-decode loop", () => {
|
|
|
137
104
|
isReduceOnly: false,
|
|
138
105
|
price: 12n,
|
|
139
106
|
size: 39n,
|
|
140
|
-
quoteSize: {
|
|
107
|
+
quoteSize: { size: 54n, price: 55n },
|
|
141
108
|
senderAccountId: undefined,
|
|
142
109
|
delegatorAccountId: undefined,
|
|
143
110
|
clientOrderId: 350n,
|