@n1xyz/nord-ts 0.0.21 → 0.0.22
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/api/client.d.ts +14 -0
- package/dist/api/client.js +45 -0
- package/dist/bridge/client.d.ts +151 -0
- package/dist/bridge/client.js +434 -0
- package/dist/bridge/const.d.ts +23 -0
- package/dist/bridge/const.js +47 -0
- package/dist/bridge/index.d.ts +4 -0
- package/dist/bridge/index.js +23 -0
- package/dist/bridge/types.d.ts +120 -0
- package/dist/bridge/types.js +18 -0
- package/dist/bridge/utils.d.ts +64 -0
- package/dist/bridge/utils.js +131 -0
- package/dist/gen/common.d.ts +68 -0
- package/dist/gen/common.js +215 -0
- package/dist/gen/nord_pb.d.ts +3651 -0
- package/dist/gen/nord_pb.js +892 -0
- package/dist/gen/openapi.d.ts +241 -2
- package/dist/idl/bridge.d.ts +569 -0
- package/dist/idl/bridge.js +8 -0
- package/dist/idl/bridge.json +1506 -0
- package/dist/idl/index.d.ts +607 -0
- package/dist/idl/index.js +8 -0
- package/dist/nord/api/actions.d.ts +30 -72
- package/dist/nord/api/actions.js +179 -200
- package/dist/nord/api/market.d.ts +36 -0
- package/dist/nord/api/market.js +96 -0
- package/dist/nord/api/queries.d.ts +46 -0
- package/dist/nord/api/queries.js +109 -0
- package/dist/nord/client/Nord.js +3 -3
- package/dist/nord/client/NordUser.d.ts +26 -13
- package/dist/nord/client/NordUser.js +13 -10
- package/dist/types.d.ts +12 -1
- package/dist/types.js +29 -2
- package/dist/utils.d.ts +6 -20
- package/dist/utils.js +17 -35
- package/dist/websocket/NordWebSocketClient.js +2 -6
- package/package.json +26 -23
- package/src/gen/nord_pb.ts +4172 -0
- package/src/gen/openapi.ts +241 -2
- package/src/nord/api/actions.ts +249 -370
- package/src/nord/client/Nord.ts +3 -3
- package/src/nord/client/NordUser.ts +40 -19
- package/src/types.ts +32 -1
- package/src/utils.ts +24 -43
- package/src/websocket/NordWebSocketClient.ts +2 -8
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.marketsStats = marketsStats;
|
|
4
|
+
exports.getTrades = getTrades;
|
|
5
|
+
exports.getUserAccountIds = getUserAccountIds;
|
|
6
|
+
exports.getOrderbook = getOrderbook;
|
|
7
|
+
const utils_1 = require("../../utils");
|
|
8
|
+
const NordError_1 = require("../utils/NordError");
|
|
9
|
+
/**
|
|
10
|
+
* Get market statistics
|
|
11
|
+
*
|
|
12
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
13
|
+
* @returns Market statistics response
|
|
14
|
+
* @throws {NordError} If the request fails
|
|
15
|
+
*/
|
|
16
|
+
async function marketsStats(webServerUrl) {
|
|
17
|
+
try {
|
|
18
|
+
const response = await (0, utils_1.checkedFetch)(`${webServerUrl}/stats`);
|
|
19
|
+
return await response.json();
|
|
20
|
+
}
|
|
21
|
+
catch (error) {
|
|
22
|
+
throw new NordError_1.NordError("Failed to fetch markets stats", { cause: error });
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Get trades for a market
|
|
27
|
+
*
|
|
28
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
29
|
+
* @param query - Trades query parameters
|
|
30
|
+
* @returns Trades response
|
|
31
|
+
* @throws {NordError} If the request fails
|
|
32
|
+
*/
|
|
33
|
+
async function getTrades(webServerUrl, query) {
|
|
34
|
+
try {
|
|
35
|
+
const params = new URLSearchParams();
|
|
36
|
+
params.append("accountId", query.accountId.toString());
|
|
37
|
+
if (query.since) {
|
|
38
|
+
params.append("since", query.since);
|
|
39
|
+
}
|
|
40
|
+
if (query.until) {
|
|
41
|
+
params.append("until", query.until);
|
|
42
|
+
}
|
|
43
|
+
if (query.pageId) {
|
|
44
|
+
params.append("pageId", query.pageId);
|
|
45
|
+
}
|
|
46
|
+
const response = await (0, utils_1.checkedFetch)(`${webServerUrl}/trades?${params.toString()}`);
|
|
47
|
+
return await response.json();
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
throw new NordError_1.NordError("Failed to get trades", { cause: error });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Get user account IDs
|
|
55
|
+
*
|
|
56
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
57
|
+
* @param query - User account IDs query parameters
|
|
58
|
+
* @returns User account IDs response
|
|
59
|
+
* @throws {NordError} If the request fails
|
|
60
|
+
*/
|
|
61
|
+
async function getUserAccountIds(webServerUrl, query) {
|
|
62
|
+
try {
|
|
63
|
+
const response = await (0, utils_1.checkedFetch)(`${webServerUrl}/user/${query.pubkey.toString()}`);
|
|
64
|
+
return await response.json();
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
throw new NordError_1.NordError("Failed to get user account IDs", { cause: error });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Get orderbook for a market
|
|
72
|
+
*
|
|
73
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
74
|
+
* @param query - Orderbook query parameters
|
|
75
|
+
* @returns Orderbook response
|
|
76
|
+
* @throws {NordError} If the request fails
|
|
77
|
+
*/
|
|
78
|
+
async function getOrderbook(webServerUrl, query) {
|
|
79
|
+
try {
|
|
80
|
+
const params = new URLSearchParams();
|
|
81
|
+
if (query.symbol) {
|
|
82
|
+
params.append("symbol", query.symbol);
|
|
83
|
+
}
|
|
84
|
+
else if (query.market_id !== undefined) {
|
|
85
|
+
params.append("market_id", query.market_id.toString());
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
throw new NordError_1.NordError("Either symbol or market_id must be provided for orderbook query");
|
|
89
|
+
}
|
|
90
|
+
const response = await (0, utils_1.checkedFetch)(`${webServerUrl}/orderbook?${params.toString()}`);
|
|
91
|
+
return await response.json();
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
throw new NordError_1.NordError("Failed to get orderbook", { cause: error });
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { ActionQuery, ActionResponse, ActionsResponse, RollmanActionResponse, RollmanActionsResponse } from "../../types";
|
|
2
|
+
/**
|
|
3
|
+
* Query a specific action
|
|
4
|
+
*
|
|
5
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
6
|
+
* @param query - Action query parameters
|
|
7
|
+
* @returns Action response
|
|
8
|
+
* @throws {NordError} If the request fails
|
|
9
|
+
*/
|
|
10
|
+
export declare function queryAction(webServerUrl: string, query: ActionQuery): Promise<ActionResponse>;
|
|
11
|
+
/**
|
|
12
|
+
* Query recent actions
|
|
13
|
+
*
|
|
14
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
15
|
+
* @param from - Starting action index
|
|
16
|
+
* @param to - Ending action index
|
|
17
|
+
* @returns Actions response
|
|
18
|
+
* @throws {NordError} If the request fails
|
|
19
|
+
*/
|
|
20
|
+
export declare function queryRecentActions(webServerUrl: string, from: number, to: number): Promise<ActionsResponse>;
|
|
21
|
+
/**
|
|
22
|
+
* Get the last action ID
|
|
23
|
+
*
|
|
24
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
25
|
+
* @returns Last action ID
|
|
26
|
+
* @throws {NordError} If the request fails
|
|
27
|
+
*/
|
|
28
|
+
export declare function getLastActionId(webServerUrl: string): Promise<number>;
|
|
29
|
+
/**
|
|
30
|
+
* Query an action from Rollman
|
|
31
|
+
*
|
|
32
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
33
|
+
* @param query - Action query parameters
|
|
34
|
+
* @returns Rollman action response
|
|
35
|
+
* @throws {NordError} If the request fails
|
|
36
|
+
*/
|
|
37
|
+
export declare function actionQueryRollman(webServerUrl: string, query: ActionQuery): Promise<RollmanActionResponse>;
|
|
38
|
+
/**
|
|
39
|
+
* Query actions from Rollman
|
|
40
|
+
*
|
|
41
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
42
|
+
* @param last_n - Number of recent actions to query
|
|
43
|
+
* @returns Rollman actions response
|
|
44
|
+
* @throws {NordError} If the request fails
|
|
45
|
+
*/
|
|
46
|
+
export declare function actionsQueryRollman(webServerUrl: string, last_n: number): Promise<RollmanActionsResponse>;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.queryAction = queryAction;
|
|
4
|
+
exports.queryRecentActions = queryRecentActions;
|
|
5
|
+
exports.getLastActionId = getLastActionId;
|
|
6
|
+
exports.actionQueryRollman = actionQueryRollman;
|
|
7
|
+
exports.actionsQueryRollman = actionsQueryRollman;
|
|
8
|
+
const utils_1 = require("../../utils");
|
|
9
|
+
const NordError_1 = require("../utils/NordError");
|
|
10
|
+
/**
|
|
11
|
+
* Query a specific action
|
|
12
|
+
*
|
|
13
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
14
|
+
* @param query - Action query parameters
|
|
15
|
+
* @returns Action response
|
|
16
|
+
* @throws {NordError} If the request fails
|
|
17
|
+
*/
|
|
18
|
+
async function queryAction(webServerUrl, query) {
|
|
19
|
+
try {
|
|
20
|
+
const params = new URLSearchParams();
|
|
21
|
+
if (query.action_id !== undefined) {
|
|
22
|
+
params.append("action_id", query.action_id.toString());
|
|
23
|
+
}
|
|
24
|
+
const response = await (0, utils_1.checkedFetch)(`${webServerUrl}/action?${params.toString()}`);
|
|
25
|
+
return await response.json();
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
throw new NordError_1.NordError("Failed to query action", { cause: error });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Query recent actions
|
|
33
|
+
*
|
|
34
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
35
|
+
* @param from - Starting action index
|
|
36
|
+
* @param to - Ending action index
|
|
37
|
+
* @returns Actions response
|
|
38
|
+
* @throws {NordError} If the request fails
|
|
39
|
+
*/
|
|
40
|
+
async function queryRecentActions(webServerUrl, from, to) {
|
|
41
|
+
try {
|
|
42
|
+
const response = await (0, utils_1.checkedFetch)(`${webServerUrl}/actions?from=${from}&to=${to}`);
|
|
43
|
+
return await response.json();
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
throw new NordError_1.NordError(`Failed to query recent actions (from ${from} to ${to})`, {
|
|
47
|
+
cause: error,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Get the last action ID
|
|
53
|
+
*
|
|
54
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
55
|
+
* @returns Last action ID
|
|
56
|
+
* @throws {NordError} If the request fails
|
|
57
|
+
*/
|
|
58
|
+
async function getLastActionId(webServerUrl) {
|
|
59
|
+
try {
|
|
60
|
+
const response = await (0, utils_1.checkedFetch)(`${webServerUrl}/actions/last-id`);
|
|
61
|
+
const data = await response.json();
|
|
62
|
+
return data;
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
throw new NordError_1.NordError("Failed to get last action ID", {
|
|
66
|
+
cause: error,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Query an action from Rollman
|
|
72
|
+
*
|
|
73
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
74
|
+
* @param query - Action query parameters
|
|
75
|
+
* @returns Rollman action response
|
|
76
|
+
* @throws {NordError} If the request fails
|
|
77
|
+
*/
|
|
78
|
+
async function actionQueryRollman(webServerUrl, query) {
|
|
79
|
+
try {
|
|
80
|
+
const params = new URLSearchParams();
|
|
81
|
+
if (query.action_id !== undefined) {
|
|
82
|
+
params.append("action_id", query.action_id.toString());
|
|
83
|
+
}
|
|
84
|
+
const response = await (0, utils_1.checkedFetch)(`${webServerUrl}/rollman/action?${params.toString()}`);
|
|
85
|
+
return await response.json();
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
throw new NordError_1.NordError("Failed to query Rollman action", { cause: error });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Query actions from Rollman
|
|
93
|
+
*
|
|
94
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
95
|
+
* @param last_n - Number of recent actions to query
|
|
96
|
+
* @returns Rollman actions response
|
|
97
|
+
* @throws {NordError} If the request fails
|
|
98
|
+
*/
|
|
99
|
+
async function actionsQueryRollman(webServerUrl, last_n) {
|
|
100
|
+
try {
|
|
101
|
+
const response = await (0, utils_1.checkedFetch)(`${webServerUrl}/rollman/actions?last_n=${last_n}`);
|
|
102
|
+
return await response.json();
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
throw new NordError_1.NordError(`Failed to query Rollman actions (last ${last_n})`, {
|
|
106
|
+
cause: error,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
package/dist/nord/client/Nord.js
CHANGED
|
@@ -41,7 +41,7 @@ const proton_1 = require("@n1xyz/proton");
|
|
|
41
41
|
const web3_js_1 = require("@solana/web3.js");
|
|
42
42
|
const events_1 = require("events");
|
|
43
43
|
const openapi_fetch_1 = __importDefault(require("openapi-fetch"));
|
|
44
|
-
const proto = __importStar(require("../../gen/
|
|
44
|
+
const proto = __importStar(require("../../gen/nord_pb"));
|
|
45
45
|
const types_1 = require("../../types");
|
|
46
46
|
const utils = __importStar(require("../../utils"));
|
|
47
47
|
const core = __importStar(require("../api/core"));
|
|
@@ -240,8 +240,8 @@ class Nord {
|
|
|
240
240
|
});
|
|
241
241
|
return xs.map((x) => ({
|
|
242
242
|
actionId: x.actionId,
|
|
243
|
-
action: utils.decodeLengthDelimited(Buffer.from(x.payload, "base64"), proto.
|
|
244
|
-
physicalExecTime: new Date(x.physicalTime
|
|
243
|
+
action: utils.decodeLengthDelimited(Buffer.from(x.payload, "base64"), proto.ActionSchema),
|
|
244
|
+
physicalExecTime: new Date(x.physicalTime),
|
|
245
245
|
}));
|
|
246
246
|
}
|
|
247
247
|
/**
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Connection, PublicKey, Transaction } from "@solana/web3.js";
|
|
1
|
+
import { Connection, PublicKey, Transaction, SendOptions } from "@solana/web3.js";
|
|
2
2
|
import Decimal from "decimal.js";
|
|
3
|
-
import { FillMode, Side, SPLTokenInfo } from "../../types";
|
|
4
|
-
import * as proto from "../../gen/
|
|
3
|
+
import { FillMode, Side, SPLTokenInfo, QuoteSize } from "../../types";
|
|
4
|
+
import * as proto from "../../gen/nord_pb";
|
|
5
5
|
import { BigIntValue } from "../../utils";
|
|
6
6
|
import { Nord } from "./Nord";
|
|
7
7
|
/**
|
|
@@ -43,8 +43,8 @@ export interface PlaceOrderParams {
|
|
|
43
43
|
size?: Decimal.Value;
|
|
44
44
|
/** Order price */
|
|
45
45
|
price?: Decimal.Value;
|
|
46
|
-
/** Quote size (
|
|
47
|
-
quoteSize?:
|
|
46
|
+
/** Quote size object (requires non-zero price and size) */
|
|
47
|
+
quoteSize?: QuoteSize;
|
|
48
48
|
/** Account ID to place the order from */
|
|
49
49
|
accountId?: number;
|
|
50
50
|
}
|
|
@@ -83,8 +83,8 @@ export interface UserAtomicSubaction {
|
|
|
83
83
|
size?: Decimal.Value;
|
|
84
84
|
/** Order price */
|
|
85
85
|
price?: Decimal.Value;
|
|
86
|
-
/** Quote size (for market
|
|
87
|
-
quoteSize?:
|
|
86
|
+
/** Quote size object (for market-style placement) */
|
|
87
|
+
quoteSize?: QuoteSize;
|
|
88
88
|
/** The client order ID of the order. */
|
|
89
89
|
clientOrderId?: BigIntValue;
|
|
90
90
|
}
|
|
@@ -203,13 +203,15 @@ export declare class NordUser {
|
|
|
203
203
|
* @param amount - Amount to deposit
|
|
204
204
|
* @param tokenId - Token ID
|
|
205
205
|
* @param recipient - Recipient address; defaults to the user's address
|
|
206
|
+
* @param sendOptions - Send options for .sendTransaction
|
|
206
207
|
* @returns Transaction signature
|
|
207
208
|
* @throws {NordError} If required parameters are missing or operation fails
|
|
208
209
|
*/
|
|
209
|
-
deposit({ amount, tokenId, recipient, }: Readonly<{
|
|
210
|
+
deposit({ amount, tokenId, recipient, sendOptions, }: Readonly<{
|
|
210
211
|
amount: number;
|
|
211
212
|
tokenId: number;
|
|
212
213
|
recipient?: PublicKey;
|
|
214
|
+
sendOptions?: SendOptions;
|
|
213
215
|
}>): Promise<string>;
|
|
214
216
|
/**
|
|
215
217
|
* Get a new nonce for actions
|
|
@@ -265,19 +267,27 @@ export declare class NordUser {
|
|
|
265
267
|
* Place an order on the exchange
|
|
266
268
|
*
|
|
267
269
|
* @param params - Order parameters
|
|
268
|
-
* @returns
|
|
270
|
+
* @returns Object containing actionId, orderId (if posted), fills, and clientOrderId
|
|
269
271
|
* @throws {NordError} If the operation fails
|
|
270
272
|
*/
|
|
271
|
-
placeOrder(params: PlaceOrderParams): Promise<
|
|
273
|
+
placeOrder(params: PlaceOrderParams): Promise<{
|
|
274
|
+
actionId: bigint;
|
|
275
|
+
orderId?: bigint;
|
|
276
|
+
fills: proto.Receipt_Trade[];
|
|
277
|
+
}>;
|
|
272
278
|
/**
|
|
273
279
|
* Cancel an order
|
|
274
280
|
*
|
|
275
281
|
* @param orderId - Order ID to cancel
|
|
276
282
|
* @param providedAccountId - Account ID that placed the order
|
|
277
|
-
* @returns
|
|
283
|
+
* @returns Object containing actionId, cancelled orderId, and accountId
|
|
278
284
|
* @throws {NordError} If the operation fails
|
|
279
285
|
*/
|
|
280
|
-
cancelOrder(orderId: BigIntValue, providedAccountId?: number): Promise<
|
|
286
|
+
cancelOrder(orderId: BigIntValue, providedAccountId?: number): Promise<{
|
|
287
|
+
actionId: bigint;
|
|
288
|
+
orderId: bigint;
|
|
289
|
+
accountId: number;
|
|
290
|
+
}>;
|
|
281
291
|
/**
|
|
282
292
|
* Transfer tokens to another account
|
|
283
293
|
*
|
|
@@ -297,7 +307,10 @@ export declare class NordUser {
|
|
|
297
307
|
* @param userActions array of user-friendly subactions
|
|
298
308
|
* @param providedAccountId optional account performing the action (defaults to first account)
|
|
299
309
|
*/
|
|
300
|
-
atomic(userActions: UserAtomicSubaction[], providedAccountId?: number): Promise<
|
|
310
|
+
atomic(userActions: UserAtomicSubaction[], providedAccountId?: number): Promise<{
|
|
311
|
+
actionId: bigint;
|
|
312
|
+
results: proto.Receipt_AtomicSubactionResultKind[];
|
|
313
|
+
}>;
|
|
301
314
|
/**
|
|
302
315
|
* Helper function to retry a promise with exponential backoff
|
|
303
316
|
*
|
|
@@ -242,10 +242,11 @@ class NordUser {
|
|
|
242
242
|
* @param amount - Amount to deposit
|
|
243
243
|
* @param tokenId - Token ID
|
|
244
244
|
* @param recipient - Recipient address; defaults to the user's address
|
|
245
|
+
* @param sendOptions - Send options for .sendTransaction
|
|
245
246
|
* @returns Transaction signature
|
|
246
247
|
* @throws {NordError} If required parameters are missing or operation fails
|
|
247
248
|
*/
|
|
248
|
-
async deposit({ amount, tokenId, recipient, }) {
|
|
249
|
+
async deposit({ amount, tokenId, recipient, sendOptions, }) {
|
|
249
250
|
try {
|
|
250
251
|
// Find the token info
|
|
251
252
|
const tokenInfo = this.splTokenInfos.find((t) => t.tokenId === tokenId);
|
|
@@ -262,14 +263,14 @@ class NordUser {
|
|
|
262
263
|
mint,
|
|
263
264
|
sourceTokenAccount: fromAccount,
|
|
264
265
|
});
|
|
265
|
-
const { blockhash } = await this.connection.getLatestBlockhash(
|
|
266
|
+
const { blockhash } = await this.connection.getLatestBlockhash();
|
|
266
267
|
const tx = new web3_js_1.Transaction();
|
|
267
268
|
tx.add(ix);
|
|
268
269
|
tx.recentBlockhash = blockhash;
|
|
269
270
|
tx.feePayer = payer;
|
|
270
271
|
const signedTx = await this.transactionSignFn(tx);
|
|
271
272
|
signedTx.partialSign(extraSigner);
|
|
272
|
-
const signature = await this.connection.sendRawTransaction(signedTx.serialize());
|
|
273
|
+
const signature = await this.connection.sendRawTransaction(signedTx.serialize(), sendOptions);
|
|
273
274
|
return signature;
|
|
274
275
|
}
|
|
275
276
|
catch (error) {
|
|
@@ -345,10 +346,11 @@ class NordUser {
|
|
|
345
346
|
* @throws {NordError} If the operation fails
|
|
346
347
|
*/
|
|
347
348
|
async refreshSession() {
|
|
348
|
-
|
|
349
|
+
const result = await (0, actions_1.createSession)(this.nord.webServerUrl, this.walletSignFn, await this.nord.getTimestamp(), this.getNonce(), {
|
|
349
350
|
userPubkey: (0, utils_1.optExpect)(this.publicKey.toBytes(), "No user's public key"),
|
|
350
351
|
sessionPubkey: this.sessionPubKey,
|
|
351
352
|
});
|
|
353
|
+
this.sessionId = result.sessionId;
|
|
352
354
|
}
|
|
353
355
|
/**
|
|
354
356
|
* Revoke a session
|
|
@@ -404,7 +406,7 @@ class NordUser {
|
|
|
404
406
|
* Place an order on the exchange
|
|
405
407
|
*
|
|
406
408
|
* @param params - Order parameters
|
|
407
|
-
* @returns
|
|
409
|
+
* @returns Object containing actionId, orderId (if posted), fills, and clientOrderId
|
|
408
410
|
* @throws {NordError} If the operation fails
|
|
409
411
|
*/
|
|
410
412
|
async placeOrder(params) {
|
|
@@ -414,7 +416,7 @@ class NordUser {
|
|
|
414
416
|
if (!market) {
|
|
415
417
|
throw new NordError_1.NordError(`Market with ID ${params.marketId} not found`);
|
|
416
418
|
}
|
|
417
|
-
|
|
419
|
+
const result = await (0, actions_1.placeOrder)(this.nord.webServerUrl, this.sessionSignFn, await this.nord.getTimestamp(), this.getNonce(), {
|
|
418
420
|
sessionId: (0, utils_1.optExpect)(this.sessionId, "No session"),
|
|
419
421
|
senderId: params.accountId,
|
|
420
422
|
sizeDecimals: market.sizeDecimals,
|
|
@@ -427,6 +429,7 @@ class NordUser {
|
|
|
427
429
|
price: params.price,
|
|
428
430
|
quoteSize: params.quoteSize,
|
|
429
431
|
});
|
|
432
|
+
return result;
|
|
430
433
|
}
|
|
431
434
|
catch (error) {
|
|
432
435
|
throw new NordError_1.NordError("Failed to place order", { cause: error });
|
|
@@ -437,18 +440,19 @@ class NordUser {
|
|
|
437
440
|
*
|
|
438
441
|
* @param orderId - Order ID to cancel
|
|
439
442
|
* @param providedAccountId - Account ID that placed the order
|
|
440
|
-
* @returns
|
|
443
|
+
* @returns Object containing actionId, cancelled orderId, and accountId
|
|
441
444
|
* @throws {NordError} If the operation fails
|
|
442
445
|
*/
|
|
443
446
|
async cancelOrder(orderId, providedAccountId) {
|
|
444
447
|
const accountId = providedAccountId != null ? providedAccountId : this.accountIds?.[0];
|
|
445
448
|
try {
|
|
446
449
|
this.checkSessionValidity();
|
|
447
|
-
|
|
450
|
+
const result = await (0, actions_1.cancelOrder)(this.nord.webServerUrl, this.sessionSignFn, await this.nord.getTimestamp(), this.getNonce(), {
|
|
448
451
|
sessionId: (0, utils_1.optExpect)(this.sessionId, "No session"),
|
|
449
452
|
senderId: accountId,
|
|
450
453
|
orderId,
|
|
451
454
|
});
|
|
455
|
+
return result;
|
|
452
456
|
}
|
|
453
457
|
catch (error) {
|
|
454
458
|
throw new NordError_1.NordError(`Failed to cancel order ${orderId}`, {
|
|
@@ -514,8 +518,7 @@ class NordUser {
|
|
|
514
518
|
priceDecimals: market.priceDecimals,
|
|
515
519
|
size: act.size,
|
|
516
520
|
price: act.price,
|
|
517
|
-
|
|
518
|
-
quoteSizePrice: undefined,
|
|
521
|
+
quoteSize: act.quoteSize,
|
|
519
522
|
clientOrderId: act.clientOrderId,
|
|
520
523
|
};
|
|
521
524
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import * as proto from "./gen/
|
|
1
|
+
import * as proto from "./gen/nord_pb";
|
|
2
2
|
import type { components } from "./gen/openapi.ts";
|
|
3
|
+
import Decimal from "decimal.js";
|
|
3
4
|
/**
|
|
4
5
|
* The peak TPS rate is queried over the specified period.
|
|
5
6
|
* The period is specified in units of: {hour, day, week, month, year}.
|
|
@@ -249,3 +250,13 @@ export interface SPLTokenInfo {
|
|
|
249
250
|
tokenId: number;
|
|
250
251
|
name: string;
|
|
251
252
|
}
|
|
253
|
+
export declare class QuoteSize {
|
|
254
|
+
price: Decimal;
|
|
255
|
+
size: Decimal;
|
|
256
|
+
constructor(quotePrice: Decimal.Value, quoteSize: Decimal.Value);
|
|
257
|
+
value(): Decimal;
|
|
258
|
+
toScaledU64(marketPriceDecimals: number, marketSizeDecimals: number): {
|
|
259
|
+
price: bigint;
|
|
260
|
+
size: bigint;
|
|
261
|
+
};
|
|
262
|
+
}
|
package/dist/types.js
CHANGED
|
@@ -32,10 +32,15 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
32
32
|
return result;
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
35
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.WebSocketMessageType = exports.FillMode = exports.Side = exports.KeyType = exports.PeakTpsPeriodUnit = void 0;
|
|
39
|
+
exports.QuoteSize = exports.WebSocketMessageType = exports.FillMode = exports.Side = exports.KeyType = exports.PeakTpsPeriodUnit = void 0;
|
|
37
40
|
exports.fillModeToProtoFillMode = fillModeToProtoFillMode;
|
|
38
|
-
const proto = __importStar(require("./gen/
|
|
41
|
+
const proto = __importStar(require("./gen/nord_pb"));
|
|
42
|
+
const decimal_js_1 = __importDefault(require("decimal.js"));
|
|
43
|
+
const utils_1 = require("./utils");
|
|
39
44
|
/**
|
|
40
45
|
* The peak TPS rate is queried over the specified period.
|
|
41
46
|
* The period is specified in units of: {hour, day, week, month, year}.
|
|
@@ -99,3 +104,25 @@ var WebSocketMessageType;
|
|
|
99
104
|
WebSocketMessageType["DeltaUpdate"] = "delta";
|
|
100
105
|
WebSocketMessageType["AccountUpdate"] = "account";
|
|
101
106
|
})(WebSocketMessageType || (exports.WebSocketMessageType = WebSocketMessageType = {}));
|
|
107
|
+
// Positive decimal price and size.
|
|
108
|
+
class QuoteSize {
|
|
109
|
+
constructor(quotePrice, quoteSize) {
|
|
110
|
+
const p = new decimal_js_1.default(quotePrice);
|
|
111
|
+
const s = new decimal_js_1.default(quoteSize);
|
|
112
|
+
if (p.isZero() || s.isZero()) {
|
|
113
|
+
throw new Error("quotePrice and quoteSize must be non-zero");
|
|
114
|
+
}
|
|
115
|
+
this.price = p;
|
|
116
|
+
this.size = s;
|
|
117
|
+
}
|
|
118
|
+
value() {
|
|
119
|
+
return this.price.mul(this.size);
|
|
120
|
+
}
|
|
121
|
+
toScaledU64(marketPriceDecimals, marketSizeDecimals) {
|
|
122
|
+
return {
|
|
123
|
+
price: (0, utils_1.toScaledU64)(this.price, marketPriceDecimals),
|
|
124
|
+
size: (0, utils_1.toScaledU64)(this.size, marketSizeDecimals),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
exports.QuoteSize = QuoteSize;
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Decimal } from "decimal.js";
|
|
2
2
|
import { KeyType, type Market, type Token } from "./types";
|
|
3
|
-
import
|
|
3
|
+
import { type Message } from "@bufbuild/protobuf";
|
|
4
|
+
import type { GenMessage } from "@bufbuild/protobuf/codegenv2";
|
|
4
5
|
import { ethers } from "ethers";
|
|
5
6
|
import { RequestInfo, RequestInit, Response } from "node-fetch";
|
|
6
7
|
import { Keypair } from "@solana/web3.js";
|
|
@@ -68,30 +69,15 @@ export declare const toScaledU64: (x: Decimal.Value, decimals: number) => bigint
|
|
|
68
69
|
* @returns Rescaled unsigned integer
|
|
69
70
|
*/
|
|
70
71
|
export declare const toScaledU128: (x: Decimal.Value, decimals: number) => bigint;
|
|
71
|
-
/**
|
|
72
|
-
* Encodes any protobuf message into a length-delimited format,
|
|
73
|
-
* i.e. prefixed with its length encoded as varint
|
|
74
|
-
* @param message message object
|
|
75
|
-
* @param coder associated coder object which implements `MessageFns` interface
|
|
76
|
-
* @returns Encoded message as Uint8Array, prefixed with its length
|
|
77
|
-
*/
|
|
78
|
-
export declare function encodeLengthDelimited<T, M extends proto.MessageFns<T>>(message: T, coder: M): Uint8Array;
|
|
79
72
|
/**
|
|
80
73
|
* Decodes any protobuf message from a length-delimited format,
|
|
81
74
|
* i.e. prefixed with its length encoded as varint
|
|
82
75
|
*
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
*
|
|
86
|
-
* ```
|
|
87
|
-
* const foo: proto.Bar = decodeLengthDelimited(bytes, proto.Bar);
|
|
88
|
-
* ```
|
|
89
|
-
*
|
|
90
|
-
* @param bytes Byte array with encoded message
|
|
91
|
-
* @param coder associated coder object which implements `MessageFns` interface
|
|
92
|
-
* @returns Decoded Action as Uint8Array.
|
|
76
|
+
* @param bytes Byte array with encoded message
|
|
77
|
+
* @param schema Message schema for decoding
|
|
78
|
+
* @returns Decoded message
|
|
93
79
|
*/
|
|
94
|
-
export declare function decodeLengthDelimited<T
|
|
80
|
+
export declare function decodeLengthDelimited<T extends Message>(bytes: Uint8Array, schema: GenMessage<T>): T;
|
|
95
81
|
export declare function checkPubKeyLength(keyType: KeyType, len: number): void;
|
|
96
82
|
export declare function findMarket(markets: Market[], marketId: number): Market;
|
|
97
83
|
export declare function findToken(tokens: Token[], tokenId: number): Token;
|
package/dist/utils.js
CHANGED
|
@@ -11,7 +11,6 @@ exports.optExpect = optExpect;
|
|
|
11
11
|
exports.checkedFetch = checkedFetch;
|
|
12
12
|
exports.signAction = signAction;
|
|
13
13
|
exports.makeWalletSignFn = makeWalletSignFn;
|
|
14
|
-
exports.encodeLengthDelimited = encodeLengthDelimited;
|
|
15
14
|
exports.decodeLengthDelimited = decodeLengthDelimited;
|
|
16
15
|
exports.checkPubKeyLength = checkPubKeyLength;
|
|
17
16
|
exports.findMarket = findMarket;
|
|
@@ -24,6 +23,7 @@ const secp256k1_1 = require("@noble/curves/secp256k1");
|
|
|
24
23
|
const sha256_1 = require("@noble/hashes/sha256");
|
|
25
24
|
const types_1 = require("./types");
|
|
26
25
|
const wire_1 = require("@bufbuild/protobuf/wire");
|
|
26
|
+
const protobuf_1 = require("@bufbuild/protobuf");
|
|
27
27
|
const ethers_1 = require("ethers");
|
|
28
28
|
const node_fetch_1 = __importDefault(require("node-fetch"));
|
|
29
29
|
const web3_js_1 = require("@solana/web3.js");
|
|
@@ -101,6 +101,7 @@ function makeWalletSignFn(walletKey) {
|
|
|
101
101
|
const signingKey = new ethers_1.ethers.SigningKey(walletKey);
|
|
102
102
|
return async (message) => signingKey.sign(ethers_1.ethers.hashMessage(message)).serialized;
|
|
103
103
|
}
|
|
104
|
+
// Returned numbers do fit into specified bits range, or error is thrown.
|
|
104
105
|
function makeToScaledBigUint(params) {
|
|
105
106
|
const Dec = decimal_js_1.Decimal.clone({
|
|
106
107
|
precision: params.precision,
|
|
@@ -159,47 +160,28 @@ exports.toScaledU128 = makeToScaledBigUint({
|
|
|
159
160
|
precision: 40,
|
|
160
161
|
exponent: 56,
|
|
161
162
|
});
|
|
162
|
-
/**
|
|
163
|
-
* Encodes any protobuf message into a length-delimited format,
|
|
164
|
-
* i.e. prefixed with its length encoded as varint
|
|
165
|
-
* @param message message object
|
|
166
|
-
* @param coder associated coder object which implements `MessageFns` interface
|
|
167
|
-
* @returns Encoded message as Uint8Array, prefixed with its length
|
|
168
|
-
*/
|
|
169
|
-
function encodeLengthDelimited(message, coder) {
|
|
170
|
-
const encoded = coder.encode(message).finish();
|
|
171
|
-
if (encoded.byteLength > MAX_PAYLOAD_SIZE) {
|
|
172
|
-
throw new Error(`Encoded message size (${encoded.byteLength} bytes) is greater than max payload size (${MAX_PAYLOAD_SIZE} bytes).`);
|
|
173
|
-
}
|
|
174
|
-
const encodedLength = new wire_1.BinaryWriter().uint32(encoded.byteLength).finish();
|
|
175
|
-
return new Uint8Array([...encodedLength, ...encoded]);
|
|
176
|
-
}
|
|
177
163
|
/**
|
|
178
164
|
* Decodes any protobuf message from a length-delimited format,
|
|
179
165
|
* i.e. prefixed with its length encoded as varint
|
|
180
166
|
*
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
* ```
|
|
185
|
-
* const foo: proto.Bar = decodeLengthDelimited(bytes, proto.Bar);
|
|
186
|
-
* ```
|
|
187
|
-
*
|
|
188
|
-
* @param bytes Byte array with encoded message
|
|
189
|
-
* @param coder associated coder object which implements `MessageFns` interface
|
|
190
|
-
* @returns Decoded Action as Uint8Array.
|
|
167
|
+
* @param bytes Byte array with encoded message
|
|
168
|
+
* @param schema Message schema for decoding
|
|
169
|
+
* @returns Decoded message
|
|
191
170
|
*/
|
|
192
|
-
function decodeLengthDelimited(bytes,
|
|
193
|
-
|
|
194
|
-
const
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
171
|
+
function decodeLengthDelimited(bytes, schema) {
|
|
172
|
+
// use sizeDelimitedPeek to extract the message length and offset
|
|
173
|
+
const peekResult = (0, wire_1.sizeDelimitedPeek)(bytes);
|
|
174
|
+
if (peekResult.size === null || peekResult.offset === null) {
|
|
175
|
+
throw new Error("Failed to parse size-delimited message");
|
|
176
|
+
}
|
|
177
|
+
if (peekResult.size > MAX_PAYLOAD_SIZE) {
|
|
178
|
+
throw new Error(`Encoded message size (${peekResult.size} bytes) is greater than max payload size (${MAX_PAYLOAD_SIZE} bytes).`);
|
|
198
179
|
}
|
|
199
|
-
if (
|
|
200
|
-
throw new Error(`Encoded message size (${
|
|
180
|
+
if (peekResult.offset + peekResult.size > bytes.length) {
|
|
181
|
+
throw new Error(`Encoded message size (${peekResult.size} bytes) is greater than remaining buffer size (${bytes.length - peekResult.offset} bytes).`);
|
|
201
182
|
}
|
|
202
|
-
|
|
183
|
+
// decode the message using the offset and size from peek
|
|
184
|
+
return (0, protobuf_1.fromBinary)(schema, bytes.slice(peekResult.offset, peekResult.offset + peekResult.size));
|
|
203
185
|
}
|
|
204
186
|
function checkPubKeyLength(keyType, len) {
|
|
205
187
|
if (keyType === types_1.KeyType.Bls12_381) {
|