@n1xyz/nord-ts 0.0.1 → 0.0.4
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/.eslintrc.js +11 -0
- package/README.md +148 -65
- package/dist/bridge/NordUser.d.ts +78 -0
- package/dist/bridge/NordUser.js +196 -0
- package/dist/bridge/client.d.ts +150 -0
- package/dist/bridge/client.js +394 -0
- package/dist/bridge/const.d.ts +23 -0
- package/dist/bridge/const.js +47 -0
- package/dist/bridge/index.d.ts +5 -0
- package/dist/bridge/index.js +23 -0
- package/dist/bridge/types.d.ts +118 -0
- package/dist/bridge/types.js +16 -0
- package/dist/bridge/utils.d.ts +64 -0
- package/dist/bridge/utils.js +131 -0
- package/dist/client.d.ts +70 -0
- package/dist/client.js +129 -0
- package/dist/const.d.ts +2 -5
- package/dist/const.js +18 -22
- package/dist/constants/endpoints.d.ts +65 -0
- package/dist/constants/endpoints.js +68 -0
- package/dist/gen/common.d.ts +6 -1
- package/dist/gen/common.js +19 -9
- package/dist/gen/nord.d.ts +75 -17
- package/dist/gen/nord.js +987 -423
- package/dist/idl/bridge.d.ts +2 -0
- package/dist/idl/bridge.js +703 -0
- package/dist/index.d.ts +8 -5
- package/dist/index.js +18 -2
- package/dist/models/account.d.ts +58 -0
- package/dist/models/account.js +6 -0
- package/dist/models/index.d.ts +8 -0
- package/dist/models/index.js +28 -0
- package/dist/models/market.d.ts +137 -0
- package/dist/models/market.js +6 -0
- package/dist/models/order.d.ts +211 -0
- package/dist/models/order.js +6 -0
- package/dist/models/token.d.ts +50 -0
- package/dist/models/token.js +6 -0
- package/dist/nord/Nord.d.ts +222 -49
- package/dist/nord/Nord.js +290 -278
- package/dist/nord/NordError.d.ts +23 -0
- package/dist/nord/NordError.js +48 -0
- package/dist/nord/NordImpl.d.ts +6 -2
- package/dist/nord/NordImpl.js +21 -1
- package/dist/nord/NordUser.d.ts +208 -42
- package/dist/nord/NordUser.js +389 -157
- package/dist/nord/Subscriber.d.ts +37 -0
- package/dist/nord/Subscriber.js +29 -0
- package/dist/nord/api/actions.d.ts +101 -0
- package/dist/nord/api/actions.js +250 -0
- package/dist/nord/api/core.d.ts +49 -0
- package/dist/nord/api/core.js +121 -0
- package/dist/nord/api/index.d.ts +1 -0
- package/dist/nord/api/index.js +17 -0
- package/dist/nord/api/market.d.ts +36 -0
- package/dist/nord/api/market.js +98 -0
- package/dist/nord/api/metrics.d.ts +67 -0
- package/dist/nord/api/metrics.js +132 -0
- package/dist/nord/api/orderFunctions.d.ts +168 -0
- package/dist/nord/api/orderFunctions.js +133 -0
- package/dist/nord/api/queries.d.ts +81 -0
- package/dist/nord/api/queries.js +187 -0
- package/dist/nord/client/Nord.d.ts +335 -0
- package/dist/nord/client/Nord.js +532 -0
- package/dist/nord/client/NordUser.d.ts +320 -0
- package/dist/nord/client/NordUser.js +701 -0
- package/dist/nord/core.d.ts +48 -0
- package/dist/nord/core.js +97 -0
- package/dist/nord/index.d.ts +9 -2
- package/dist/nord/index.js +30 -6
- package/dist/nord/market.d.ts +36 -0
- package/dist/nord/market.js +90 -0
- package/dist/nord/metrics.d.ts +67 -0
- package/dist/nord/metrics.js +124 -0
- package/dist/nord/models/Subscriber.d.ts +37 -0
- package/dist/nord/models/Subscriber.js +29 -0
- package/dist/nord/queries.d.ts +81 -0
- package/dist/nord/queries.js +181 -0
- package/dist/nord/types.d.ts +88 -0
- package/dist/nord/types.js +2 -0
- package/dist/nord/utils/NordError.d.ts +35 -0
- package/dist/nord/utils/NordError.js +46 -0
- package/dist/nord/websocket.d.ts +49 -0
- package/dist/nord/websocket.js +107 -0
- package/dist/operations/account.d.ts +58 -0
- package/dist/operations/account.js +112 -0
- package/dist/operations/market.d.ts +65 -0
- package/dist/operations/market.js +131 -0
- package/dist/operations/orders.d.ts +57 -0
- package/dist/operations/orders.js +129 -0
- package/dist/solana/NordUser.d.ts +78 -0
- package/dist/solana/NordUser.js +196 -0
- package/dist/solana/client.d.ts +139 -0
- package/dist/solana/client.js +360 -0
- package/dist/solana/const.d.ts +23 -0
- package/dist/solana/const.js +47 -0
- package/dist/solana/index.d.ts +5 -0
- package/dist/solana/index.js +23 -0
- package/dist/solana/types.d.ts +118 -0
- package/dist/solana/types.js +16 -0
- package/dist/solana/utils.d.ts +64 -0
- package/dist/solana/utils.js +131 -0
- package/dist/types/api.d.ts +152 -0
- package/dist/types/api.js +6 -0
- package/dist/types/config.d.ts +34 -0
- package/dist/types/config.js +6 -0
- package/dist/types.d.ts +144 -87
- package/dist/types.js +13 -2
- package/dist/utils/errors.d.ts +96 -0
- package/dist/utils/errors.js +132 -0
- package/dist/utils/http.d.ts +35 -0
- package/dist/utils/http.js +105 -0
- package/dist/utils.d.ts +14 -5
- package/dist/utils.js +26 -7
- package/dist/websocket/NordWebSocketClient.d.ts +71 -0
- package/dist/websocket/NordWebSocketClient.js +343 -0
- package/dist/websocket/client.d.ts +93 -0
- package/dist/websocket/client.js +222 -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/dist/websocket.d.ts +55 -0
- package/dist/websocket.js +211 -0
- package/docs/assets/navigation.js +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/classes/Nord.html +2 -15
- package/docs/classes/NordUser.html +4 -4
- package/docs/enums/FillMode.html +2 -2
- package/docs/enums/KeyType.html +2 -2
- package/docs/enums/PeakTpsPeriodUnit.html +2 -2
- package/docs/enums/Side.html +2 -2
- package/docs/functions/assert.html +1 -1
- package/docs/functions/bigIntToProtoU128.html +1 -1
- package/docs/functions/checkPubKeyLength.html +1 -1
- package/docs/functions/checkedFetch.html +1 -1
- package/docs/functions/decodeLengthDelimited.html +1 -1
- package/docs/functions/encodeLengthDelimited.html +1 -1
- package/docs/functions/fillModeToProtoFillMode.html +1 -1
- package/docs/functions/findMarket.html +1 -1
- package/docs/functions/findToken.html +1 -1
- package/docs/functions/makeWalletSignFn.html +1 -1
- package/docs/functions/optExpect.html +1 -1
- package/docs/functions/optMap.html +1 -1
- package/docs/functions/optUnwrap.html +1 -1
- package/docs/functions/panic.html +1 -1
- package/docs/functions/signAction.html +1 -1
- package/docs/functions/toScaledU128.html +1 -1
- package/docs/functions/toScaledU64.html +1 -1
- package/docs/interfaces/Account.html +2 -2
- package/docs/interfaces/ActionInfo.html +2 -2
- package/docs/interfaces/ActionQuery.html +2 -2
- package/docs/interfaces/ActionResponse.html +2 -2
- package/docs/interfaces/ActionsExtendedInfo.html +2 -2
- package/docs/interfaces/ActionsQuery.html +2 -2
- package/docs/interfaces/ActionsResponse.html +2 -2
- package/docs/interfaces/AggregateMetrics.html +2 -2
- package/docs/interfaces/BlockQuery.html +2 -2
- package/docs/interfaces/BlockResponse.html +2 -2
- package/docs/interfaces/BlockSummary.html +2 -2
- package/docs/interfaces/BlockSummaryResponse.html +2 -2
- package/docs/interfaces/DeltaEvent.html +2 -2
- package/docs/interfaces/ERC20TokenInfo.html +2 -2
- package/docs/interfaces/Info.html +2 -2
- package/docs/interfaces/Market.html +2 -2
- package/docs/interfaces/MarketStats.html +2 -2
- package/docs/interfaces/MarketsStatsResponse.html +2 -2
- package/docs/interfaces/NordConfig.html +2 -2
- package/docs/interfaces/Order.html +2 -2
- package/docs/interfaces/OrderInfo.html +2 -2
- package/docs/interfaces/PerpMarketStats.html +2 -2
- package/docs/interfaces/RollmanActionExtendedInfo.html +2 -2
- package/docs/interfaces/RollmanActionInfo.html +2 -2
- package/docs/interfaces/RollmanActionResponse.html +2 -2
- package/docs/interfaces/RollmanActionsResponse.html +2 -2
- package/docs/interfaces/RollmanBlockResponse.html +2 -2
- package/docs/interfaces/SubscriberConfig.html +2 -2
- package/docs/interfaces/Token.html +2 -2
- package/docs/interfaces/Trade.html +2 -2
- package/docs/interfaces/Trades.html +2 -2
- package/docs/modules.html +0 -7
- package/docs/types/BigIntValue.html +1 -1
- package/docs/variables/DEBUG_KEYS.html +1 -1
- package/docs/variables/DEFAULT_FUNDING_AMOUNTS.html +1 -1
- package/docs/variables/DEV_CONTRACT_ADDRESS.html +1 -1
- package/docs/variables/DEV_TOKEN_INFOS.html +1 -1
- package/docs/variables/DEV_URL.html +1 -1
- package/docs/variables/ERC20_ABI.html +1 -1
- package/docs/variables/EVM_DEV_URL.html +1 -1
- package/docs/variables/FAUCET_PRIVATE_ADDRESS.html +1 -1
- package/docs/variables/MAX_BUFFER_LEN.html +1 -1
- package/docs/variables/NORD_GETTERS_FACET_ABI.html +1 -1
- package/docs/variables/NORD_RAMP_FACET_ABI.html +1 -1
- package/docs/variables/SESSION_TTL.html +1 -1
- package/docs/variables/WEBSERVER_DEV_URL.html +1 -1
- package/docs/variables/ZERO_DECIMAL.html +1 -1
- package/package.json +10 -12
- package/src/bridge/client.ts +487 -0
- package/src/bridge/const.ts +53 -0
- package/src/bridge/index.ts +7 -0
- package/src/bridge/types.ts +127 -0
- package/src/bridge/utils.ts +140 -0
- package/src/const.ts +20 -25
- package/src/gen/common.ts +27 -10
- package/src/gen/nord.ts +1044 -483
- package/src/idl/bridge.ts +702 -0
- package/src/index.ts +24 -5
- package/src/nord/{actions.ts → api/actions.ts} +33 -37
- package/src/nord/api/core.ts +130 -0
- package/src/nord/api/market.ts +125 -0
- package/src/nord/api/metrics.ts +154 -0
- package/src/nord/api/queries.ts +236 -0
- package/src/nord/client/Nord.ts +652 -0
- package/src/nord/client/NordUser.ts +1105 -0
- package/src/nord/index.ts +16 -2
- package/src/nord/models/Subscriber.ts +57 -0
- package/src/nord/utils/NordError.ts +72 -0
- package/src/types.ts +170 -99
- package/src/utils.ts +40 -19
- package/src/websocket/NordWebSocketClient.ts +432 -0
- package/src/websocket/events.ts +31 -0
- package/src/websocket/index.ts +2 -0
- package/tests/utils.spec.ts +24 -24
- package/docs/classes/Subscriber.html +0 -6
- package/docs/functions/createWebSocketSubscription.html +0 -12
- package/docs/interfaces/OrderbookOrder.html +0 -6
- package/docs/interfaces/OrderbookResponse.html +0 -10
- package/docs/interfaces/TradeInfo.html +0 -20
- package/docs/interfaces/TradesQueryParams.html +0 -10
- package/docs/interfaces/TradesResponse.html +0 -12
- package/src/abis/ERC20_ABI.ts +0 -310
- package/src/abis/NORD_GETTERS_FACET_ABI.ts +0 -192
- package/src/abis/NORD_RAMP_FACET_ABI.ts +0 -141
- package/src/abis/index.ts +0 -3
- package/src/nord/Nord.ts +0 -504
- package/src/nord/NordImpl.ts +0 -8
- package/src/nord/NordUser.ts +0 -469
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.queryBlock = queryBlock;
|
|
4
|
+
exports.queryLastNBlocks = queryLastNBlocks;
|
|
5
|
+
exports.queryRecentBlocks = queryRecentBlocks;
|
|
6
|
+
exports.queryAction = queryAction;
|
|
7
|
+
exports.queryRecentActions = queryRecentActions;
|
|
8
|
+
exports.blockQueryRollman = blockQueryRollman;
|
|
9
|
+
exports.blockSummaryQueryRollman = blockSummaryQueryRollman;
|
|
10
|
+
exports.actionQueryRollman = actionQueryRollman;
|
|
11
|
+
exports.actionsQueryRollman = actionsQueryRollman;
|
|
12
|
+
const utils_1 = require("../utils");
|
|
13
|
+
const NordError_1 = require("./NordError");
|
|
14
|
+
/**
|
|
15
|
+
* Query a specific block
|
|
16
|
+
*
|
|
17
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
18
|
+
* @param query - Block query parameters
|
|
19
|
+
* @returns Block response
|
|
20
|
+
* @throws {NordError} If the request fails
|
|
21
|
+
*/
|
|
22
|
+
async function queryBlock(webServerUrl, query) {
|
|
23
|
+
try {
|
|
24
|
+
const params = new URLSearchParams();
|
|
25
|
+
if (query.block_number !== undefined) {
|
|
26
|
+
params.append("block_height", query.block_number.toString());
|
|
27
|
+
}
|
|
28
|
+
const response = await (0, utils_1.checkedFetch)(`${webServerUrl}/block?${params.toString()}`);
|
|
29
|
+
return await response.json();
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
throw new NordError_1.NordError("Failed to query block", { cause: error });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Query the last N blocks
|
|
37
|
+
*
|
|
38
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
39
|
+
* @returns Block response for the last N blocks
|
|
40
|
+
* @throws {NordError} If the request fails
|
|
41
|
+
*/
|
|
42
|
+
async function queryLastNBlocks(webServerUrl) {
|
|
43
|
+
try {
|
|
44
|
+
const response = await (0, utils_1.checkedFetch)(`${webServerUrl}/blocks`);
|
|
45
|
+
return await response.json();
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
throw new NordError_1.NordError("Failed to query last N blocks", { cause: error });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Query recent blocks
|
|
53
|
+
*
|
|
54
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
55
|
+
* @param last_n - Number of recent blocks to query
|
|
56
|
+
* @returns Block summary response
|
|
57
|
+
* @throws {NordError} If the request fails
|
|
58
|
+
*/
|
|
59
|
+
async function queryRecentBlocks(webServerUrl, last_n) {
|
|
60
|
+
try {
|
|
61
|
+
const response = await (0, utils_1.checkedFetch)(`${webServerUrl}/blocks_summary?last_n=${last_n}`);
|
|
62
|
+
return await response.json();
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
throw new NordError_1.NordError(`Failed to query recent blocks (last ${last_n})`, { cause: error });
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Query a specific action
|
|
70
|
+
*
|
|
71
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
72
|
+
* @param query - Action query parameters
|
|
73
|
+
* @returns Action response
|
|
74
|
+
* @throws {NordError} If the request fails
|
|
75
|
+
*/
|
|
76
|
+
async function queryAction(webServerUrl, query) {
|
|
77
|
+
try {
|
|
78
|
+
const params = new URLSearchParams();
|
|
79
|
+
if (query.action_id !== undefined) {
|
|
80
|
+
params.append("action_id", query.action_id.toString());
|
|
81
|
+
}
|
|
82
|
+
const response = await (0, utils_1.checkedFetch)(`${webServerUrl}/action?${params.toString()}`);
|
|
83
|
+
return await response.json();
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
throw new NordError_1.NordError("Failed to query action", { cause: error });
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Query recent actions
|
|
91
|
+
*
|
|
92
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
93
|
+
* @param last_n - Number of recent actions to query
|
|
94
|
+
* @returns Actions response
|
|
95
|
+
* @throws {NordError} If the request fails
|
|
96
|
+
*/
|
|
97
|
+
async function queryRecentActions(webServerUrl, last_n) {
|
|
98
|
+
try {
|
|
99
|
+
const response = await (0, utils_1.checkedFetch)(`${webServerUrl}/actions?last_n=${last_n}`);
|
|
100
|
+
return await response.json();
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
throw new NordError_1.NordError(`Failed to query recent actions (last ${last_n})`, { cause: error });
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Query a block from Rollman
|
|
108
|
+
*
|
|
109
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
110
|
+
* @param query - Block query parameters
|
|
111
|
+
* @returns Rollman block response
|
|
112
|
+
* @throws {NordError} If the request fails
|
|
113
|
+
*/
|
|
114
|
+
async function blockQueryRollman(webServerUrl, query) {
|
|
115
|
+
try {
|
|
116
|
+
const params = new URLSearchParams();
|
|
117
|
+
if (query.block_number !== undefined) {
|
|
118
|
+
params.append("block_height", query.block_number.toString());
|
|
119
|
+
}
|
|
120
|
+
const response = await (0, utils_1.checkedFetch)(`${webServerUrl}/rollman/block?${params.toString()}`);
|
|
121
|
+
return await response.json();
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
throw new NordError_1.NordError("Failed to query Rollman block", { cause: error });
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Query block summaries from Rollman
|
|
129
|
+
*
|
|
130
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
131
|
+
* @param last_n - Number of recent blocks to query
|
|
132
|
+
* @returns Block summary response
|
|
133
|
+
* @throws {NordError} If the request fails
|
|
134
|
+
*/
|
|
135
|
+
async function blockSummaryQueryRollman(webServerUrl, last_n) {
|
|
136
|
+
try {
|
|
137
|
+
const response = await (0, utils_1.checkedFetch)(`${webServerUrl}/rollman/blocks_summary?last_n=${last_n}`);
|
|
138
|
+
return await response.json();
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
throw new NordError_1.NordError(`Failed to query Rollman block summaries (last ${last_n})`, { cause: error });
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Query an action from Rollman
|
|
146
|
+
*
|
|
147
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
148
|
+
* @param query - Action query parameters
|
|
149
|
+
* @returns Rollman action response
|
|
150
|
+
* @throws {NordError} If the request fails
|
|
151
|
+
*/
|
|
152
|
+
async function actionQueryRollman(webServerUrl, query) {
|
|
153
|
+
try {
|
|
154
|
+
const params = new URLSearchParams();
|
|
155
|
+
if (query.action_id !== undefined) {
|
|
156
|
+
params.append("action_id", query.action_id.toString());
|
|
157
|
+
}
|
|
158
|
+
const response = await (0, utils_1.checkedFetch)(`${webServerUrl}/rollman/action?${params.toString()}`);
|
|
159
|
+
return await response.json();
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
throw new NordError_1.NordError("Failed to query Rollman action", { cause: error });
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Query actions from Rollman
|
|
167
|
+
*
|
|
168
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
169
|
+
* @param last_n - Number of recent actions to query
|
|
170
|
+
* @returns Rollman actions response
|
|
171
|
+
* @throws {NordError} If the request fails
|
|
172
|
+
*/
|
|
173
|
+
async function actionsQueryRollman(webServerUrl, last_n) {
|
|
174
|
+
try {
|
|
175
|
+
const response = await (0, utils_1.checkedFetch)(`${webServerUrl}/rollman/actions?last_n=${last_n}`);
|
|
176
|
+
return await response.json();
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
throw new NordError_1.NordError(`Failed to query Rollman actions (last ${last_n})`, { cause: error });
|
|
180
|
+
}
|
|
181
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { EventEmitter } from "events";
|
|
2
|
+
import Decimal from "decimal.js";
|
|
3
|
+
import { Connection, Keypair, PublicKey } from '@solana/web3.js';
|
|
4
|
+
import { FillMode, Side } from "../types";
|
|
5
|
+
import { Nord } from "./Nord";
|
|
6
|
+
import { OrderbookResponse, Trade } from "../types";
|
|
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<string>;
|
|
17
|
+
/** Function to sign messages with the user's session key */
|
|
18
|
+
sessionSignFn: (message: Uint8Array) => Promise<Uint8Array>;
|
|
19
|
+
/** Solana public key (optional) */
|
|
20
|
+
solanaPublicKey?: PublicKey | string;
|
|
21
|
+
/** Solana keypair (optional) */
|
|
22
|
+
keypair?: Keypair;
|
|
23
|
+
/** Solana connection (optional) */
|
|
24
|
+
connection?: Connection;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Parameters for placing an order
|
|
28
|
+
*/
|
|
29
|
+
export interface PlaceOrderParams {
|
|
30
|
+
/** Market ID */
|
|
31
|
+
marketId: number;
|
|
32
|
+
/** Order side (bid or ask) */
|
|
33
|
+
side: Side;
|
|
34
|
+
/** Fill mode (limit, market, etc.) */
|
|
35
|
+
fillMode: FillMode;
|
|
36
|
+
/** Whether the order is reduce-only */
|
|
37
|
+
isReduceOnly: boolean;
|
|
38
|
+
/** Order size */
|
|
39
|
+
size?: Decimal.Value;
|
|
40
|
+
/** Order price */
|
|
41
|
+
price?: Decimal.Value;
|
|
42
|
+
/** Quote size (for market orders) */
|
|
43
|
+
quoteSize?: Decimal.Value;
|
|
44
|
+
/** Account ID to place the order from */
|
|
45
|
+
accountId?: number;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Parameters for transferring tokens between accounts
|
|
49
|
+
*/
|
|
50
|
+
export interface TransferParams {
|
|
51
|
+
/** Recipient user */
|
|
52
|
+
to: any;
|
|
53
|
+
/** Token ID to transfer */
|
|
54
|
+
tokenId: number;
|
|
55
|
+
/** Amount to transfer */
|
|
56
|
+
amount: Decimal.Value;
|
|
57
|
+
/** Source account ID */
|
|
58
|
+
fromAccountId: number;
|
|
59
|
+
/** Destination account ID */
|
|
60
|
+
toAccountId: number;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Parameters for creating a new account
|
|
64
|
+
*/
|
|
65
|
+
export interface CreateAccountParams {
|
|
66
|
+
/** Token ID for initial funding */
|
|
67
|
+
tokenId: number;
|
|
68
|
+
/** Initial funding amount */
|
|
69
|
+
amount: Decimal.Value;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Interface for orderbook subscription events
|
|
73
|
+
*/
|
|
74
|
+
export interface OrderbookSubscription extends EventEmitter {
|
|
75
|
+
on(event: 'message', listener: (data: OrderbookResponse) => void): this;
|
|
76
|
+
on(event: 'error', listener: (error: Error) => void): this;
|
|
77
|
+
close(): void;
|
|
78
|
+
removeAllListeners(event?: string): this;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Interface for trade subscription events
|
|
82
|
+
*/
|
|
83
|
+
export interface TradeSubscription extends EventEmitter {
|
|
84
|
+
on(event: 'message', listener: (data: Trade[]) => void): this;
|
|
85
|
+
on(event: 'error', listener: (error: Error) => void): this;
|
|
86
|
+
close(): void;
|
|
87
|
+
removeAllListeners(event?: string): this;
|
|
88
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for creating a NordError
|
|
3
|
+
*/
|
|
4
|
+
export interface NordErrorOptions {
|
|
5
|
+
/** The original error that caused this error */
|
|
6
|
+
cause?: unknown;
|
|
7
|
+
/** HTTP status code (if applicable) */
|
|
8
|
+
statusCode?: number;
|
|
9
|
+
/** Additional error details */
|
|
10
|
+
details?: Record<string, unknown>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Custom error class for Nord-related errors
|
|
14
|
+
*/
|
|
15
|
+
export declare class NordError extends Error {
|
|
16
|
+
/** The original error that caused this error */
|
|
17
|
+
readonly cause?: unknown;
|
|
18
|
+
/** HTTP status code (if applicable) */
|
|
19
|
+
readonly statusCode?: number;
|
|
20
|
+
/** Additional error details */
|
|
21
|
+
readonly details?: Record<string, unknown>;
|
|
22
|
+
/**
|
|
23
|
+
* Create a new NordError
|
|
24
|
+
*
|
|
25
|
+
* @param message - Error message
|
|
26
|
+
* @param options - Error options
|
|
27
|
+
*/
|
|
28
|
+
constructor(message: string, options?: NordErrorOptions);
|
|
29
|
+
/**
|
|
30
|
+
* Convert the error to a string representation
|
|
31
|
+
*
|
|
32
|
+
* @returns String representation of the error
|
|
33
|
+
*/
|
|
34
|
+
toString(): string;
|
|
35
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NordError = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Custom error class for Nord-related errors
|
|
6
|
+
*/
|
|
7
|
+
class NordError extends Error {
|
|
8
|
+
/**
|
|
9
|
+
* Create a new NordError
|
|
10
|
+
*
|
|
11
|
+
* @param message - Error message
|
|
12
|
+
* @param options - Error options
|
|
13
|
+
*/
|
|
14
|
+
constructor(message, options = {}) {
|
|
15
|
+
super(message);
|
|
16
|
+
this.name = 'NordError';
|
|
17
|
+
this.cause = options.cause;
|
|
18
|
+
this.statusCode = options.statusCode;
|
|
19
|
+
this.details = options.details;
|
|
20
|
+
// Capture stack trace
|
|
21
|
+
if (Error.captureStackTrace) {
|
|
22
|
+
Error.captureStackTrace(this, NordError);
|
|
23
|
+
}
|
|
24
|
+
// Handle nested errors
|
|
25
|
+
if (this.cause instanceof Error) {
|
|
26
|
+
this.stack =
|
|
27
|
+
this.stack + '\nCaused by: ' + (this.cause.stack || this.cause.message);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Convert the error to a string representation
|
|
32
|
+
*
|
|
33
|
+
* @returns String representation of the error
|
|
34
|
+
*/
|
|
35
|
+
toString() {
|
|
36
|
+
let result = `${this.name}: ${this.message}`;
|
|
37
|
+
if (this.statusCode) {
|
|
38
|
+
result += ` (Status: ${this.statusCode})`;
|
|
39
|
+
}
|
|
40
|
+
if (this.details && Object.keys(this.details).length > 0) {
|
|
41
|
+
result += `\nDetails: ${JSON.stringify(this.details, null, 2)}`;
|
|
42
|
+
}
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.NordError = NordError;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { DeltaEvent, Trades, Account } from "../types";
|
|
2
|
+
import { OrderbookSubscription, TradeSubscription } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Configuration for the Subscriber class
|
|
5
|
+
*/
|
|
6
|
+
export interface SubscriberConfig {
|
|
7
|
+
/** WebSocket stream URL */
|
|
8
|
+
streamURL: string;
|
|
9
|
+
/** Maximum buffer length */
|
|
10
|
+
maxBufferLen?: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Subscriber class for handling WebSocket subscriptions
|
|
14
|
+
*/
|
|
15
|
+
export declare class Subscriber {
|
|
16
|
+
/** WebSocket stream URL */
|
|
17
|
+
streamURL: string;
|
|
18
|
+
/** Buffer for events */
|
|
19
|
+
buffer: (DeltaEvent | Trades | Account)[];
|
|
20
|
+
/** Maximum buffer length */
|
|
21
|
+
maxBufferLen: number;
|
|
22
|
+
/**
|
|
23
|
+
* Create a new Subscriber instance
|
|
24
|
+
* @param config Subscriber configuration
|
|
25
|
+
*/
|
|
26
|
+
constructor(config: SubscriberConfig);
|
|
27
|
+
/**
|
|
28
|
+
* Subscribe to WebSocket events
|
|
29
|
+
*/
|
|
30
|
+
subscribe(): void;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Create an orderbook subscription
|
|
34
|
+
*
|
|
35
|
+
* @param ws - WebSocket client
|
|
36
|
+
* @param symbol - Market symbol to subscribe to
|
|
37
|
+
* @returns EventEmitter that emits orderbook updates
|
|
38
|
+
*/
|
|
39
|
+
export declare function createOrderbookSubscription(ws: any, // Using 'any' to avoid circular dependency, will be NordWebSocketClient
|
|
40
|
+
symbol: string): OrderbookSubscription;
|
|
41
|
+
/**
|
|
42
|
+
* Create a trade subscription
|
|
43
|
+
*
|
|
44
|
+
* @param ws - WebSocket client
|
|
45
|
+
* @param symbol - Market symbol to subscribe to
|
|
46
|
+
* @returns EventEmitter that emits trade updates
|
|
47
|
+
*/
|
|
48
|
+
export declare function createTradeSubscription(ws: any, // Using 'any' to avoid circular dependency, will be NordWebSocketClient
|
|
49
|
+
symbol: string): TradeSubscription;
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Subscriber = void 0;
|
|
7
|
+
exports.createOrderbookSubscription = createOrderbookSubscription;
|
|
8
|
+
exports.createTradeSubscription = createTradeSubscription;
|
|
9
|
+
const events_1 = require("events");
|
|
10
|
+
const ws_1 = __importDefault(require("ws"));
|
|
11
|
+
const utils_1 = require("../utils");
|
|
12
|
+
/**
|
|
13
|
+
* Subscriber class for handling WebSocket subscriptions
|
|
14
|
+
*/
|
|
15
|
+
class Subscriber {
|
|
16
|
+
/**
|
|
17
|
+
* Create a new Subscriber instance
|
|
18
|
+
* @param config Subscriber configuration
|
|
19
|
+
*/
|
|
20
|
+
constructor(config) {
|
|
21
|
+
this.streamURL = config.streamURL;
|
|
22
|
+
this.buffer = [];
|
|
23
|
+
this.maxBufferLen = config.maxBufferLen ?? utils_1.MAX_BUFFER_LEN;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Subscribe to WebSocket events
|
|
27
|
+
*/
|
|
28
|
+
subscribe() {
|
|
29
|
+
const ws = new ws_1.default(this.streamURL);
|
|
30
|
+
ws.on("open", () => { });
|
|
31
|
+
ws.on("message", (rawData) => {
|
|
32
|
+
const message = rawData.toLocaleString();
|
|
33
|
+
const event = JSON.parse(message);
|
|
34
|
+
this.buffer.push(event);
|
|
35
|
+
if (this.buffer.length > this.maxBufferLen) {
|
|
36
|
+
this.buffer.shift();
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
ws.on("close", () => { });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.Subscriber = Subscriber;
|
|
43
|
+
/**
|
|
44
|
+
* Create an orderbook subscription
|
|
45
|
+
*
|
|
46
|
+
* @param ws - WebSocket client
|
|
47
|
+
* @param symbol - Market symbol to subscribe to
|
|
48
|
+
* @returns EventEmitter that emits orderbook updates
|
|
49
|
+
*/
|
|
50
|
+
function createOrderbookSubscription(ws, // Using 'any' to avoid circular dependency, will be NordWebSocketClient
|
|
51
|
+
symbol) {
|
|
52
|
+
if (!symbol) {
|
|
53
|
+
throw new Error("Market symbol is required for orderbook subscription");
|
|
54
|
+
}
|
|
55
|
+
const emitter = new events_1.EventEmitter();
|
|
56
|
+
// Add close method
|
|
57
|
+
emitter.close = () => {
|
|
58
|
+
ws.unsubscribe([`deltas@${symbol}`]);
|
|
59
|
+
emitter.removeAllListeners();
|
|
60
|
+
};
|
|
61
|
+
// Subscribe to the orderbook stream
|
|
62
|
+
ws.on('delta', (update) => {
|
|
63
|
+
if (update.market_symbol === symbol) {
|
|
64
|
+
const orderbookUpdate = {
|
|
65
|
+
symbol: update.market_symbol,
|
|
66
|
+
asks: update.asks,
|
|
67
|
+
bids: update.bids,
|
|
68
|
+
timestamp: update.timestamp
|
|
69
|
+
};
|
|
70
|
+
emitter.emit('message', orderbookUpdate);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
ws.on('error', (error) => {
|
|
74
|
+
emitter.emit('error', error);
|
|
75
|
+
});
|
|
76
|
+
// Start the subscription
|
|
77
|
+
ws.subscribe([`deltas@${symbol}`]);
|
|
78
|
+
return emitter;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Create a trade subscription
|
|
82
|
+
*
|
|
83
|
+
* @param ws - WebSocket client
|
|
84
|
+
* @param symbol - Market symbol to subscribe to
|
|
85
|
+
* @returns EventEmitter that emits trade updates
|
|
86
|
+
*/
|
|
87
|
+
function createTradeSubscription(ws, // Using 'any' to avoid circular dependency, will be NordWebSocketClient
|
|
88
|
+
symbol) {
|
|
89
|
+
const emitter = new events_1.EventEmitter();
|
|
90
|
+
// Add close method
|
|
91
|
+
emitter.close = () => {
|
|
92
|
+
ws.unsubscribe([`trades@${symbol}`]);
|
|
93
|
+
emitter.removeAllListeners();
|
|
94
|
+
};
|
|
95
|
+
// Subscribe to the trades stream
|
|
96
|
+
ws.on('trade', (update) => {
|
|
97
|
+
if (update.symbol === symbol) {
|
|
98
|
+
emitter.emit('message', update.trades);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
ws.on('error', (error) => {
|
|
102
|
+
emitter.emit('error', error);
|
|
103
|
+
});
|
|
104
|
+
// Start the subscription
|
|
105
|
+
ws.subscribe([`trades@${symbol}`]);
|
|
106
|
+
return emitter;
|
|
107
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Account-related operations
|
|
3
|
+
* @module operations/account
|
|
4
|
+
*/
|
|
5
|
+
import { NordClientConfig } from '../types/config';
|
|
6
|
+
import { Account, Balance } from '../models';
|
|
7
|
+
/**
|
|
8
|
+
* Account operations implementation
|
|
9
|
+
*/
|
|
10
|
+
export declare class AccountOperations {
|
|
11
|
+
/**
|
|
12
|
+
* Client configuration
|
|
13
|
+
*/
|
|
14
|
+
private readonly config;
|
|
15
|
+
/**
|
|
16
|
+
* Create a new AccountOperations instance
|
|
17
|
+
*
|
|
18
|
+
* @param config - Client configuration
|
|
19
|
+
*/
|
|
20
|
+
constructor(config: NordClientConfig);
|
|
21
|
+
/**
|
|
22
|
+
* Get account information
|
|
23
|
+
*
|
|
24
|
+
* @param accountId - Account ID
|
|
25
|
+
* @returns Promise resolving to account information
|
|
26
|
+
* @throws {NordValidationError} If the accountId parameter is invalid
|
|
27
|
+
* @throws {NordApiError} If the API request fails
|
|
28
|
+
*/
|
|
29
|
+
getAccount(accountId: number): Promise<Account>;
|
|
30
|
+
/**
|
|
31
|
+
* Get all accounts for a specific address
|
|
32
|
+
*
|
|
33
|
+
* @param address - User's blockchain address
|
|
34
|
+
* @returns Promise resolving to an array of account IDs
|
|
35
|
+
* @throws {NordValidationError} If the address parameter is invalid
|
|
36
|
+
* @throws {NordApiError} If the API request fails
|
|
37
|
+
*/
|
|
38
|
+
getAccounts(address: string): Promise<number[]>;
|
|
39
|
+
/**
|
|
40
|
+
* Get balance for a specific token in an account
|
|
41
|
+
*
|
|
42
|
+
* @param accountId - Account ID
|
|
43
|
+
* @param token - Token symbol (e.g., "BTC")
|
|
44
|
+
* @returns Promise resolving to balance information
|
|
45
|
+
* @throws {NordValidationError} If any parameters are invalid
|
|
46
|
+
* @throws {NordApiError} If the API request fails
|
|
47
|
+
*/
|
|
48
|
+
getBalance(accountId: number, token: string): Promise<Balance>;
|
|
49
|
+
/**
|
|
50
|
+
* Get all balances for an account
|
|
51
|
+
*
|
|
52
|
+
* @param accountId - Account ID
|
|
53
|
+
* @returns Promise resolving to an array of balances
|
|
54
|
+
* @throws {NordValidationError} If the accountId parameter is invalid
|
|
55
|
+
* @throws {NordApiError} If the API request fails
|
|
56
|
+
*/
|
|
57
|
+
getBalances(accountId: number): Promise<Balance[]>;
|
|
58
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Account-related operations
|
|
4
|
+
* @module operations/account
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.AccountOperations = void 0;
|
|
8
|
+
const errors_1 = require("../utils/errors");
|
|
9
|
+
const http_1 = require("../utils/http");
|
|
10
|
+
const endpoints_1 = require("../constants/endpoints");
|
|
11
|
+
/**
|
|
12
|
+
* Account operations implementation
|
|
13
|
+
*/
|
|
14
|
+
class AccountOperations {
|
|
15
|
+
/**
|
|
16
|
+
* Create a new AccountOperations instance
|
|
17
|
+
*
|
|
18
|
+
* @param config - Client configuration
|
|
19
|
+
*/
|
|
20
|
+
constructor(config) {
|
|
21
|
+
this.config = config;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Get account information
|
|
25
|
+
*
|
|
26
|
+
* @param accountId - Account ID
|
|
27
|
+
* @returns Promise resolving to account information
|
|
28
|
+
* @throws {NordValidationError} If the accountId parameter is invalid
|
|
29
|
+
* @throws {NordApiError} If the API request fails
|
|
30
|
+
*/
|
|
31
|
+
async getAccount(accountId) {
|
|
32
|
+
if (accountId === undefined || accountId === null) {
|
|
33
|
+
throw new errors_1.NordValidationError('Account ID is required', 'accountId');
|
|
34
|
+
}
|
|
35
|
+
try {
|
|
36
|
+
const endpoint = `${endpoints_1.ENDPOINTS.ACCOUNT}/${accountId}`;
|
|
37
|
+
const response = await (0, http_1.httpGet)(this.config.webServerUrl, endpoint);
|
|
38
|
+
return response;
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
throw new errors_1.NordApiError(`Failed to fetch account ${accountId}`, error instanceof errors_1.NordApiError ? error.statusCode : 500, endpoints_1.ENDPOINTS.ACCOUNT, error instanceof Error ? error : undefined);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Get all accounts for a specific address
|
|
46
|
+
*
|
|
47
|
+
* @param address - User's blockchain address
|
|
48
|
+
* @returns Promise resolving to an array of account IDs
|
|
49
|
+
* @throws {NordValidationError} If the address parameter is invalid
|
|
50
|
+
* @throws {NordApiError} If the API request fails
|
|
51
|
+
*/
|
|
52
|
+
async getAccounts(address) {
|
|
53
|
+
if (!address) {
|
|
54
|
+
throw new errors_1.NordValidationError('Address is required', 'address');
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
const endpoint = `${endpoints_1.ENDPOINTS.ACCOUNTS}?address=${encodeURIComponent(address)}`;
|
|
58
|
+
const response = await (0, http_1.httpGet)(this.config.webServerUrl, endpoint);
|
|
59
|
+
return response.accountIds;
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
throw new errors_1.NordApiError(`Failed to fetch accounts for address ${address}`, error instanceof errors_1.NordApiError ? error.statusCode : 500, endpoints_1.ENDPOINTS.ACCOUNTS, error instanceof Error ? error : undefined);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Get balance for a specific token in an account
|
|
67
|
+
*
|
|
68
|
+
* @param accountId - Account ID
|
|
69
|
+
* @param token - Token symbol (e.g., "BTC")
|
|
70
|
+
* @returns Promise resolving to balance information
|
|
71
|
+
* @throws {NordValidationError} If any parameters are invalid
|
|
72
|
+
* @throws {NordApiError} If the API request fails
|
|
73
|
+
*/
|
|
74
|
+
async getBalance(accountId, token) {
|
|
75
|
+
if (accountId === undefined || accountId === null) {
|
|
76
|
+
throw new errors_1.NordValidationError('Account ID is required', 'accountId');
|
|
77
|
+
}
|
|
78
|
+
if (!token) {
|
|
79
|
+
throw new errors_1.NordValidationError('Token symbol is required', 'token');
|
|
80
|
+
}
|
|
81
|
+
try {
|
|
82
|
+
const endpoint = `${endpoints_1.ENDPOINTS.BALANCE}?accountId=${accountId}&token=${encodeURIComponent(token)}`;
|
|
83
|
+
const response = await (0, http_1.httpGet)(this.config.webServerUrl, endpoint);
|
|
84
|
+
return response;
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
throw new errors_1.NordApiError(`Failed to fetch balance for account ${accountId} and token ${token}`, error instanceof errors_1.NordApiError ? error.statusCode : 500, endpoints_1.ENDPOINTS.BALANCE, error instanceof Error ? error : undefined);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Get all balances for an account
|
|
92
|
+
*
|
|
93
|
+
* @param accountId - Account ID
|
|
94
|
+
* @returns Promise resolving to an array of balances
|
|
95
|
+
* @throws {NordValidationError} If the accountId parameter is invalid
|
|
96
|
+
* @throws {NordApiError} If the API request fails
|
|
97
|
+
*/
|
|
98
|
+
async getBalances(accountId) {
|
|
99
|
+
if (accountId === undefined || accountId === null) {
|
|
100
|
+
throw new errors_1.NordValidationError('Account ID is required', 'accountId');
|
|
101
|
+
}
|
|
102
|
+
try {
|
|
103
|
+
const endpoint = `${endpoints_1.ENDPOINTS.BALANCES}?accountId=${accountId}`;
|
|
104
|
+
const response = await (0, http_1.httpGet)(this.config.webServerUrl, endpoint);
|
|
105
|
+
return response;
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
throw new errors_1.NordApiError(`Failed to fetch balances for account ${accountId}`, error instanceof errors_1.NordApiError ? error.statusCode : 500, endpoints_1.ENDPOINTS.BALANCES, error instanceof Error ? error : undefined);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
exports.AccountOperations = AccountOperations;
|