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