@n1xyz/nord-ts 0.1.6 → 0.1.7

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.
Files changed (39) hide show
  1. package/dist/actions.d.ts +57 -0
  2. package/dist/actions.js +229 -0
  3. package/dist/client/Nord.d.ts +379 -0
  4. package/dist/client/Nord.js +718 -0
  5. package/dist/client/NordAdmin.d.ts +225 -0
  6. package/dist/client/NordAdmin.js +394 -0
  7. package/dist/client/NordUser.d.ts +350 -0
  8. package/dist/client/NordUser.js +743 -0
  9. package/dist/error.d.ts +35 -0
  10. package/dist/error.js +49 -0
  11. package/dist/gen/openapi.d.ts +40 -0
  12. package/dist/index.d.ts +6 -1
  13. package/dist/index.js +29 -1
  14. package/dist/nord/client/NordAdmin.js +2 -0
  15. package/dist/types.d.ts +4 -50
  16. package/dist/types.js +1 -24
  17. package/dist/utils.d.ts +8 -11
  18. package/dist/utils.js +54 -41
  19. package/dist/websocket/Subscriber.d.ts +37 -0
  20. package/dist/websocket/Subscriber.js +25 -0
  21. package/dist/websocket/index.d.ts +19 -2
  22. package/dist/websocket/index.js +82 -2
  23. package/package.json +1 -1
  24. package/src/actions.ts +333 -0
  25. package/src/{nord/client → client}/Nord.ts +207 -210
  26. package/src/{nord/client → client}/NordAdmin.ts +123 -153
  27. package/src/{nord/client → client}/NordUser.ts +216 -305
  28. package/src/gen/openapi.ts +40 -0
  29. package/src/index.ts +7 -1
  30. package/src/types.ts +4 -54
  31. package/src/utils.ts +44 -47
  32. package/src/{nord/models → websocket}/Subscriber.ts +2 -2
  33. package/src/websocket/index.ts +105 -2
  34. package/src/nord/api/actions.ts +0 -648
  35. package/src/nord/api/core.ts +0 -96
  36. package/src/nord/api/metrics.ts +0 -269
  37. package/src/nord/client/NordClient.ts +0 -79
  38. package/src/nord/index.ts +0 -25
  39. /package/src/{nord/utils/NordError.ts → error.ts} +0 -0
@@ -0,0 +1,350 @@
1
+ import { PublicKey, Transaction, SendOptions } from "@solana/web3.js";
2
+ import Decimal from "decimal.js";
3
+ import { FillMode, Side, SPLTokenInfo, QuoteSize, TriggerKind } from "../types";
4
+ import * as proto from "../gen/nord_pb";
5
+ import { BigIntValue } from "../utils";
6
+ import { Nord } from "./Nord";
7
+ /**
8
+ * Parameters for individual atomic subactions (user-friendly version)
9
+ */
10
+ export interface UserAtomicSubaction {
11
+ /** The type of action to perform. */
12
+ kind: "place" | "cancel";
13
+ /** The market ID to place the order in. */
14
+ marketId?: number;
15
+ /** The order ID to cancel. */
16
+ orderId?: BigIntValue;
17
+ /** Order side (bid or ask) */
18
+ side?: Side;
19
+ /** Fill mode (limit, market, etc.) */
20
+ fillMode?: FillMode;
21
+ /** Whether the order is reduce-only. */
22
+ isReduceOnly?: boolean;
23
+ /** The size of the order. */
24
+ size?: Decimal.Value;
25
+ /** Order price */
26
+ price?: Decimal.Value;
27
+ /** Quote size object (for market-style placement) */
28
+ quoteSize?: QuoteSize;
29
+ /** The client order ID of the order. */
30
+ clientOrderId?: BigIntValue;
31
+ }
32
+ /**
33
+ * User class for interacting with the Nord protocol
34
+ */
35
+ export declare class NordUser {
36
+ readonly nord: Nord;
37
+ readonly sessionSignFn: (message: Uint8Array) => Promise<Uint8Array>;
38
+ readonly transactionSignFn: (tx: Transaction) => Promise<Transaction>;
39
+ sessionId?: bigint;
40
+ sessionPubKey: PublicKey;
41
+ publicKey: PublicKey;
42
+ lastTs: number;
43
+ private nonce;
44
+ /** User balances by token symbol */
45
+ balances: {
46
+ [key: string]: {
47
+ accountId: number;
48
+ balance: number;
49
+ symbol: string;
50
+ }[];
51
+ };
52
+ /** User positions by account ID */
53
+ positions: {
54
+ [key: string]: {
55
+ marketId: number;
56
+ openOrders: number;
57
+ perp?: {
58
+ baseSize: number;
59
+ price: number;
60
+ updatedFundingRateIndex: number;
61
+ fundingPaymentPnl: number;
62
+ sizePricePnl: number;
63
+ isLong: boolean;
64
+ };
65
+ actionId: number;
66
+ }[];
67
+ };
68
+ /** User margins by account ID */
69
+ margins: {
70
+ [key: string]: {
71
+ omf: number;
72
+ mf: number;
73
+ imf: number;
74
+ cmf: number;
75
+ mmf: number;
76
+ pon: number;
77
+ pn: number;
78
+ bankruptcy: boolean;
79
+ };
80
+ };
81
+ /** User's account IDs */
82
+ accountIds?: number[];
83
+ /** SPL token information */
84
+ splTokenInfos: SPLTokenInfo[];
85
+ /**
86
+ * Create a new NordUser instance
87
+ *
88
+ * @param nord - Nord client instance
89
+ * @param sessionSignFn - Function to sign messages with the user's session key
90
+ * @param transactionSignFn - Function to sign transactions with the user's wallet (optional)
91
+ * @param sessionId - Existing session identifier
92
+ * @param sessionPubKey - Session public key
93
+ * @param publicKey - Wallet public key
94
+ * @throws {NordError} If required parameters are missing
95
+ */
96
+ constructor({ nord, sessionSignFn, transactionSignFn, sessionId, sessionPubKey, publicKey, }: Readonly<{
97
+ nord: Nord;
98
+ sessionSignFn: (message: Uint8Array) => Promise<Uint8Array>;
99
+ transactionSignFn: (tx: Transaction) => Promise<Transaction>;
100
+ sessionId?: bigint;
101
+ sessionPubKey: Uint8Array;
102
+ publicKey: PublicKey;
103
+ }>);
104
+ /**
105
+ * Create a NordUser from a private key
106
+ *
107
+ * @param nord - Nord instance
108
+ * @param privateKey - Private key as string or Uint8Array
109
+ * @returns NordUser instance
110
+ * @throws {NordError} If the private key is invalid
111
+ */
112
+ static fromPrivateKey(nord: Nord, privateKey: string | Uint8Array): NordUser;
113
+ /**
114
+ * Get the associated token account for a token mint
115
+ *
116
+ * @param mint - Token mint address
117
+ * @returns Associated token account address
118
+ * @throws {NordError} If required parameters are missing or operation fails
119
+ */
120
+ getAssociatedTokenAccount(mint: PublicKey): Promise<PublicKey>;
121
+ /**
122
+ * Deposit SPL tokens to the app
123
+ *
124
+ * @param amount - Amount to deposit
125
+ * @param tokenId - Token ID
126
+ * @param recipient - Recipient address; defaults to the user's address
127
+ * @returns Transaction signature
128
+ * @deprecated Use deposit instead
129
+ * @throws {NordError} If required parameters are missing or operation fails
130
+ */
131
+ depositSpl(amount: number, tokenId: number, recipient?: PublicKey): Promise<string>;
132
+ /**
133
+ * Deposit SPL tokens to the app
134
+ *
135
+ * @param amount - Amount to deposit
136
+ * @param tokenId - Token ID
137
+ * @param recipient - Recipient address; defaults to the user's address
138
+ * @param sendOptions - Send options for .sendTransaction
139
+ * @returns Transaction signature
140
+ * @throws {NordError} If required parameters are missing or operation fails
141
+ */
142
+ deposit({ amount, tokenId, recipient, sendOptions, }: Readonly<{
143
+ amount: number;
144
+ tokenId: number;
145
+ recipient?: PublicKey;
146
+ sendOptions?: SendOptions;
147
+ }>): Promise<string>;
148
+ /**
149
+ * Get a new nonce for actions
150
+ *
151
+ * @returns Nonce as number
152
+ */
153
+ getNonce(): number;
154
+ private submitSessionAction;
155
+ /**
156
+ * Update account IDs for this user
157
+ *
158
+ * @throws {NordError} If the operation fails
159
+ */
160
+ updateAccountId(): Promise<void>;
161
+ /**
162
+ * Fetch user information including balances and orders
163
+ *
164
+ * @throws {NordError} If the operation fails
165
+ */
166
+ fetchInfo(): Promise<void>;
167
+ /**
168
+ * Refresh the user's session
169
+ *
170
+ * @throws {NordError} If the operation fails
171
+ */
172
+ refreshSession(): Promise<void>;
173
+ /**
174
+ * Revoke a session
175
+ *
176
+ * @param sessionId - Session ID to revoke
177
+ * @throws {NordError} If the operation fails
178
+ */
179
+ revokeSession(sessionId: BigIntValue): Promise<void>;
180
+ /**
181
+ * Checks if the session is valid
182
+ * @private
183
+ * @throws {NordError} If the session is not valid
184
+ */
185
+ private checkSessionValidity;
186
+ /**
187
+ * Withdraw tokens from the exchange
188
+ *
189
+ * @param tokenId - Token ID to withdraw
190
+ * @param amount - Amount to withdraw
191
+ * @throws {NordError} If the operation fails
192
+ */
193
+ withdraw({ amount, tokenId, }: Readonly<{
194
+ tokenId: number;
195
+ amount: number;
196
+ }>): Promise<{
197
+ actionId: bigint;
198
+ }>;
199
+ /**
200
+ * Place an order on the exchange
201
+ *
202
+ * @param marketId - Target market identifier
203
+ * @param side - Order side
204
+ * @param fillMode - Fill mode (limit, market, etc.)
205
+ * @param isReduceOnly - Reduce-only flag
206
+ * @param size - Base size to place
207
+ * @param price - Limit price
208
+ * @param quoteSize - Quote-sized order representation
209
+ * @param accountId - Account executing the order
210
+ * @param clientOrderId - Optional client-specified identifier
211
+ * @returns Object containing actionId, orderId (if posted), fills, and clientOrderId
212
+ * @throws {NordError} If the operation fails
213
+ */
214
+ placeOrder({ marketId, side, fillMode, isReduceOnly, size, price, quoteSize, accountId, clientOrderId, }: Readonly<{
215
+ marketId: number;
216
+ side: Side;
217
+ fillMode: FillMode;
218
+ isReduceOnly: boolean;
219
+ size?: Decimal.Value;
220
+ price?: Decimal.Value;
221
+ quoteSize?: QuoteSize;
222
+ accountId?: number;
223
+ clientOrderId?: BigIntValue;
224
+ }>): Promise<{
225
+ actionId: bigint;
226
+ orderId?: bigint;
227
+ fills: proto.Receipt_Trade[];
228
+ }>;
229
+ /**
230
+ * Cancel an order
231
+ *
232
+ * @param orderId - Order ID to cancel
233
+ * @param providedAccountId - Account ID that placed the order
234
+ * @returns Object containing actionId, cancelled orderId, and accountId
235
+ * @throws {NordError} If the operation fails
236
+ */
237
+ cancelOrder(orderId: BigIntValue, providedAccountId?: number): Promise<{
238
+ actionId: bigint;
239
+ orderId: bigint;
240
+ accountId: number;
241
+ }>;
242
+ /**
243
+ * Add a trigger for the current session
244
+ *
245
+ * @param marketId - Market to watch
246
+ * @param side - Order side for the trigger
247
+ * @param kind - Stop-loss or take-profit trigger type
248
+ * @param triggerPrice - Price that activates the trigger
249
+ * @param limitPrice - Limit price placed once the trigger fires
250
+ * @param accountId - Account executing the trigger
251
+ * @returns Object containing the actionId of the submitted trigger
252
+ * @throws {NordError} If the operation fails
253
+ */
254
+ addTrigger({ marketId, side, kind, triggerPrice, limitPrice, accountId, }: Readonly<{
255
+ marketId: number;
256
+ side: Side;
257
+ kind: TriggerKind;
258
+ triggerPrice: Decimal.Value;
259
+ limitPrice?: Decimal.Value;
260
+ accountId?: number;
261
+ }>): Promise<{
262
+ actionId: bigint;
263
+ }>;
264
+ /**
265
+ * Remove a trigger for the current session
266
+ *
267
+ * @param marketId - Market the trigger belongs to
268
+ * @param side - Order side for the trigger
269
+ * @param kind - Stop-loss or take-profit trigger type
270
+ * @param accountId - Account executing the trigger
271
+ * @returns Object containing the actionId of the removal action
272
+ * @throws {NordError} If the operation fails
273
+ */
274
+ removeTrigger({ marketId, side, kind, accountId, }: Readonly<{
275
+ marketId: number;
276
+ side: Side;
277
+ kind: TriggerKind;
278
+ accountId?: number;
279
+ }>): Promise<{
280
+ actionId: bigint;
281
+ }>;
282
+ /**
283
+ * Transfer tokens to another account
284
+ *
285
+ * @param tokenId - Token identifier to move
286
+ * @param amount - Amount to transfer
287
+ * @param fromAccountId - Source account id
288
+ * @param toAccountId - Destination account id
289
+ * @throws {NordError} If the operation fails
290
+ */
291
+ transferToAccount({ tokenId, amount, fromAccountId, toAccountId, }: Readonly<{
292
+ tokenId: number;
293
+ amount: Decimal.Value;
294
+ fromAccountId?: number;
295
+ toAccountId?: number;
296
+ }>): Promise<{
297
+ actionId: bigint;
298
+ newAccountId?: number;
299
+ }>;
300
+ /**
301
+ * Execute up to four place/cancel operations atomically.
302
+ * Per Market:
303
+ * 1. cancels can only be in the start (one cannot predict future order ids)
304
+ * 2. intermediate trades can trade only
305
+ * 3. placements go last
306
+ *
307
+ * Across Markets, order action can be any
308
+ *
309
+ * @param userActions array of user-friendly subactions
310
+ * @param providedAccountId optional account performing the action (defaults to first account)
311
+ */
312
+ atomic(userActions: UserAtomicSubaction[], providedAccountId?: number): Promise<{
313
+ actionId: bigint;
314
+ results: proto.Receipt_AtomicSubactionResultKind[];
315
+ }>;
316
+ /**
317
+ * Helper function to retry a promise with exponential backoff
318
+ *
319
+ * @param fn - Function to retry
320
+ * @param maxRetries - Maximum number of retries
321
+ * @param initialDelay - Initial delay in milliseconds
322
+ * @returns Promise result
323
+ */
324
+ private retryWithBackoff;
325
+ /**
326
+ * Get user's token balances on Solana chain using mintAddr
327
+ *
328
+ * @param options - Optional parameters
329
+ * @param options.includeZeroBalances - Whether to include tokens with zero balance (default: true)
330
+ * @param options.includeTokenAccounts - Whether to include token account addresses in the result (default: false)
331
+ * @param options.maxConcurrent - Maximum number of concurrent requests (default: 5)
332
+ * @param options.maxRetries - Maximum number of retries for rate-limited requests (default: 3)
333
+ * @returns Object with token balances and optional token account addresses
334
+ * @throws {NordError} If required parameters are missing or operation fails
335
+ */
336
+ getSolanaBalances(options?: {
337
+ includeZeroBalances?: boolean;
338
+ includeTokenAccounts?: boolean;
339
+ maxConcurrent?: number;
340
+ maxRetries?: number;
341
+ }): Promise<{
342
+ balances: {
343
+ [symbol: string]: number;
344
+ };
345
+ tokenAccounts?: {
346
+ [symbol: string]: string;
347
+ };
348
+ }>;
349
+ protected submitSignedAction(kind: proto.Action["kind"], makeSignedMessage: (message: Uint8Array) => Promise<Uint8Array>): Promise<proto.Receipt>;
350
+ }