@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.
Files changed (237) hide show
  1. package/.eslintrc.js +11 -0
  2. package/README.md +148 -65
  3. package/dist/bridge/NordUser.d.ts +78 -0
  4. package/dist/bridge/NordUser.js +196 -0
  5. package/dist/bridge/client.d.ts +150 -0
  6. package/dist/bridge/client.js +394 -0
  7. package/dist/bridge/const.d.ts +23 -0
  8. package/dist/bridge/const.js +47 -0
  9. package/dist/bridge/index.d.ts +5 -0
  10. package/dist/bridge/index.js +23 -0
  11. package/dist/bridge/types.d.ts +118 -0
  12. package/dist/bridge/types.js +16 -0
  13. package/dist/bridge/utils.d.ts +64 -0
  14. package/dist/bridge/utils.js +131 -0
  15. package/dist/client.d.ts +70 -0
  16. package/dist/client.js +129 -0
  17. package/dist/const.d.ts +2 -5
  18. package/dist/const.js +18 -22
  19. package/dist/constants/endpoints.d.ts +65 -0
  20. package/dist/constants/endpoints.js +68 -0
  21. package/dist/gen/common.d.ts +6 -1
  22. package/dist/gen/common.js +19 -9
  23. package/dist/gen/nord.d.ts +75 -17
  24. package/dist/gen/nord.js +987 -423
  25. package/dist/idl/bridge.d.ts +2 -0
  26. package/dist/idl/bridge.js +703 -0
  27. package/dist/index.d.ts +8 -5
  28. package/dist/index.js +18 -2
  29. package/dist/models/account.d.ts +58 -0
  30. package/dist/models/account.js +6 -0
  31. package/dist/models/index.d.ts +8 -0
  32. package/dist/models/index.js +28 -0
  33. package/dist/models/market.d.ts +137 -0
  34. package/dist/models/market.js +6 -0
  35. package/dist/models/order.d.ts +211 -0
  36. package/dist/models/order.js +6 -0
  37. package/dist/models/token.d.ts +50 -0
  38. package/dist/models/token.js +6 -0
  39. package/dist/nord/Nord.d.ts +222 -49
  40. package/dist/nord/Nord.js +290 -278
  41. package/dist/nord/NordError.d.ts +23 -0
  42. package/dist/nord/NordError.js +48 -0
  43. package/dist/nord/NordImpl.d.ts +6 -2
  44. package/dist/nord/NordImpl.js +21 -1
  45. package/dist/nord/NordUser.d.ts +208 -42
  46. package/dist/nord/NordUser.js +389 -157
  47. package/dist/nord/Subscriber.d.ts +37 -0
  48. package/dist/nord/Subscriber.js +29 -0
  49. package/dist/nord/api/actions.d.ts +101 -0
  50. package/dist/nord/api/actions.js +250 -0
  51. package/dist/nord/api/core.d.ts +49 -0
  52. package/dist/nord/api/core.js +121 -0
  53. package/dist/nord/api/index.d.ts +1 -0
  54. package/dist/nord/api/index.js +17 -0
  55. package/dist/nord/api/market.d.ts +36 -0
  56. package/dist/nord/api/market.js +98 -0
  57. package/dist/nord/api/metrics.d.ts +67 -0
  58. package/dist/nord/api/metrics.js +132 -0
  59. package/dist/nord/api/orderFunctions.d.ts +168 -0
  60. package/dist/nord/api/orderFunctions.js +133 -0
  61. package/dist/nord/api/queries.d.ts +81 -0
  62. package/dist/nord/api/queries.js +187 -0
  63. package/dist/nord/client/Nord.d.ts +335 -0
  64. package/dist/nord/client/Nord.js +532 -0
  65. package/dist/nord/client/NordUser.d.ts +320 -0
  66. package/dist/nord/client/NordUser.js +701 -0
  67. package/dist/nord/core.d.ts +48 -0
  68. package/dist/nord/core.js +97 -0
  69. package/dist/nord/index.d.ts +9 -2
  70. package/dist/nord/index.js +30 -6
  71. package/dist/nord/market.d.ts +36 -0
  72. package/dist/nord/market.js +90 -0
  73. package/dist/nord/metrics.d.ts +67 -0
  74. package/dist/nord/metrics.js +124 -0
  75. package/dist/nord/models/Subscriber.d.ts +37 -0
  76. package/dist/nord/models/Subscriber.js +29 -0
  77. package/dist/nord/queries.d.ts +81 -0
  78. package/dist/nord/queries.js +181 -0
  79. package/dist/nord/types.d.ts +88 -0
  80. package/dist/nord/types.js +2 -0
  81. package/dist/nord/utils/NordError.d.ts +35 -0
  82. package/dist/nord/utils/NordError.js +46 -0
  83. package/dist/nord/websocket.d.ts +49 -0
  84. package/dist/nord/websocket.js +107 -0
  85. package/dist/operations/account.d.ts +58 -0
  86. package/dist/operations/account.js +112 -0
  87. package/dist/operations/market.d.ts +65 -0
  88. package/dist/operations/market.js +131 -0
  89. package/dist/operations/orders.d.ts +57 -0
  90. package/dist/operations/orders.js +129 -0
  91. package/dist/solana/NordUser.d.ts +78 -0
  92. package/dist/solana/NordUser.js +196 -0
  93. package/dist/solana/client.d.ts +139 -0
  94. package/dist/solana/client.js +360 -0
  95. package/dist/solana/const.d.ts +23 -0
  96. package/dist/solana/const.js +47 -0
  97. package/dist/solana/index.d.ts +5 -0
  98. package/dist/solana/index.js +23 -0
  99. package/dist/solana/types.d.ts +118 -0
  100. package/dist/solana/types.js +16 -0
  101. package/dist/solana/utils.d.ts +64 -0
  102. package/dist/solana/utils.js +131 -0
  103. package/dist/types/api.d.ts +152 -0
  104. package/dist/types/api.js +6 -0
  105. package/dist/types/config.d.ts +34 -0
  106. package/dist/types/config.js +6 -0
  107. package/dist/types.d.ts +144 -87
  108. package/dist/types.js +13 -2
  109. package/dist/utils/errors.d.ts +96 -0
  110. package/dist/utils/errors.js +132 -0
  111. package/dist/utils/http.d.ts +35 -0
  112. package/dist/utils/http.js +105 -0
  113. package/dist/utils.d.ts +14 -5
  114. package/dist/utils.js +26 -7
  115. package/dist/websocket/NordWebSocketClient.d.ts +71 -0
  116. package/dist/websocket/NordWebSocketClient.js +343 -0
  117. package/dist/websocket/client.d.ts +93 -0
  118. package/dist/websocket/client.js +222 -0
  119. package/dist/websocket/events.d.ts +19 -0
  120. package/dist/websocket/events.js +2 -0
  121. package/dist/websocket/index.d.ts +2 -0
  122. package/dist/websocket/index.js +5 -0
  123. package/dist/websocket.d.ts +55 -0
  124. package/dist/websocket.js +211 -0
  125. package/docs/assets/navigation.js +1 -1
  126. package/docs/assets/search.js +1 -1
  127. package/docs/classes/Nord.html +2 -15
  128. package/docs/classes/NordUser.html +4 -4
  129. package/docs/enums/FillMode.html +2 -2
  130. package/docs/enums/KeyType.html +2 -2
  131. package/docs/enums/PeakTpsPeriodUnit.html +2 -2
  132. package/docs/enums/Side.html +2 -2
  133. package/docs/functions/assert.html +1 -1
  134. package/docs/functions/bigIntToProtoU128.html +1 -1
  135. package/docs/functions/checkPubKeyLength.html +1 -1
  136. package/docs/functions/checkedFetch.html +1 -1
  137. package/docs/functions/decodeLengthDelimited.html +1 -1
  138. package/docs/functions/encodeLengthDelimited.html +1 -1
  139. package/docs/functions/fillModeToProtoFillMode.html +1 -1
  140. package/docs/functions/findMarket.html +1 -1
  141. package/docs/functions/findToken.html +1 -1
  142. package/docs/functions/makeWalletSignFn.html +1 -1
  143. package/docs/functions/optExpect.html +1 -1
  144. package/docs/functions/optMap.html +1 -1
  145. package/docs/functions/optUnwrap.html +1 -1
  146. package/docs/functions/panic.html +1 -1
  147. package/docs/functions/signAction.html +1 -1
  148. package/docs/functions/toScaledU128.html +1 -1
  149. package/docs/functions/toScaledU64.html +1 -1
  150. package/docs/interfaces/Account.html +2 -2
  151. package/docs/interfaces/ActionInfo.html +2 -2
  152. package/docs/interfaces/ActionQuery.html +2 -2
  153. package/docs/interfaces/ActionResponse.html +2 -2
  154. package/docs/interfaces/ActionsExtendedInfo.html +2 -2
  155. package/docs/interfaces/ActionsQuery.html +2 -2
  156. package/docs/interfaces/ActionsResponse.html +2 -2
  157. package/docs/interfaces/AggregateMetrics.html +2 -2
  158. package/docs/interfaces/BlockQuery.html +2 -2
  159. package/docs/interfaces/BlockResponse.html +2 -2
  160. package/docs/interfaces/BlockSummary.html +2 -2
  161. package/docs/interfaces/BlockSummaryResponse.html +2 -2
  162. package/docs/interfaces/DeltaEvent.html +2 -2
  163. package/docs/interfaces/ERC20TokenInfo.html +2 -2
  164. package/docs/interfaces/Info.html +2 -2
  165. package/docs/interfaces/Market.html +2 -2
  166. package/docs/interfaces/MarketStats.html +2 -2
  167. package/docs/interfaces/MarketsStatsResponse.html +2 -2
  168. package/docs/interfaces/NordConfig.html +2 -2
  169. package/docs/interfaces/Order.html +2 -2
  170. package/docs/interfaces/OrderInfo.html +2 -2
  171. package/docs/interfaces/PerpMarketStats.html +2 -2
  172. package/docs/interfaces/RollmanActionExtendedInfo.html +2 -2
  173. package/docs/interfaces/RollmanActionInfo.html +2 -2
  174. package/docs/interfaces/RollmanActionResponse.html +2 -2
  175. package/docs/interfaces/RollmanActionsResponse.html +2 -2
  176. package/docs/interfaces/RollmanBlockResponse.html +2 -2
  177. package/docs/interfaces/SubscriberConfig.html +2 -2
  178. package/docs/interfaces/Token.html +2 -2
  179. package/docs/interfaces/Trade.html +2 -2
  180. package/docs/interfaces/Trades.html +2 -2
  181. package/docs/modules.html +0 -7
  182. package/docs/types/BigIntValue.html +1 -1
  183. package/docs/variables/DEBUG_KEYS.html +1 -1
  184. package/docs/variables/DEFAULT_FUNDING_AMOUNTS.html +1 -1
  185. package/docs/variables/DEV_CONTRACT_ADDRESS.html +1 -1
  186. package/docs/variables/DEV_TOKEN_INFOS.html +1 -1
  187. package/docs/variables/DEV_URL.html +1 -1
  188. package/docs/variables/ERC20_ABI.html +1 -1
  189. package/docs/variables/EVM_DEV_URL.html +1 -1
  190. package/docs/variables/FAUCET_PRIVATE_ADDRESS.html +1 -1
  191. package/docs/variables/MAX_BUFFER_LEN.html +1 -1
  192. package/docs/variables/NORD_GETTERS_FACET_ABI.html +1 -1
  193. package/docs/variables/NORD_RAMP_FACET_ABI.html +1 -1
  194. package/docs/variables/SESSION_TTL.html +1 -1
  195. package/docs/variables/WEBSERVER_DEV_URL.html +1 -1
  196. package/docs/variables/ZERO_DECIMAL.html +1 -1
  197. package/package.json +10 -12
  198. package/src/bridge/client.ts +487 -0
  199. package/src/bridge/const.ts +53 -0
  200. package/src/bridge/index.ts +7 -0
  201. package/src/bridge/types.ts +127 -0
  202. package/src/bridge/utils.ts +140 -0
  203. package/src/const.ts +20 -25
  204. package/src/gen/common.ts +27 -10
  205. package/src/gen/nord.ts +1044 -483
  206. package/src/idl/bridge.ts +702 -0
  207. package/src/index.ts +24 -5
  208. package/src/nord/{actions.ts → api/actions.ts} +33 -37
  209. package/src/nord/api/core.ts +130 -0
  210. package/src/nord/api/market.ts +125 -0
  211. package/src/nord/api/metrics.ts +154 -0
  212. package/src/nord/api/queries.ts +236 -0
  213. package/src/nord/client/Nord.ts +652 -0
  214. package/src/nord/client/NordUser.ts +1105 -0
  215. package/src/nord/index.ts +16 -2
  216. package/src/nord/models/Subscriber.ts +57 -0
  217. package/src/nord/utils/NordError.ts +72 -0
  218. package/src/types.ts +170 -99
  219. package/src/utils.ts +40 -19
  220. package/src/websocket/NordWebSocketClient.ts +432 -0
  221. package/src/websocket/events.ts +31 -0
  222. package/src/websocket/index.ts +2 -0
  223. package/tests/utils.spec.ts +24 -24
  224. package/docs/classes/Subscriber.html +0 -6
  225. package/docs/functions/createWebSocketSubscription.html +0 -12
  226. package/docs/interfaces/OrderbookOrder.html +0 -6
  227. package/docs/interfaces/OrderbookResponse.html +0 -10
  228. package/docs/interfaces/TradeInfo.html +0 -20
  229. package/docs/interfaces/TradesQueryParams.html +0 -10
  230. package/docs/interfaces/TradesResponse.html +0 -12
  231. package/src/abis/ERC20_ABI.ts +0 -310
  232. package/src/abis/NORD_GETTERS_FACET_ABI.ts +0 -192
  233. package/src/abis/NORD_RAMP_FACET_ABI.ts +0 -141
  234. package/src/abis/index.ts +0 -3
  235. package/src/nord/Nord.ts +0 -504
  236. package/src/nord/NordImpl.ts +0 -8
  237. 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,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -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;