@n1xyz/nord-ts 0.1.6 → 0.1.8
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/actions.d.ts +57 -0
- package/dist/actions.js +186 -0
- package/dist/bundle.js +79181 -0
- package/dist/{nord/client → client}/Nord.d.ts +90 -107
- package/dist/{nord/client → client}/Nord.js +157 -218
- package/dist/{nord/client → client}/NordAdmin.d.ts +68 -102
- package/dist/{nord/client → client}/NordAdmin.js +109 -140
- package/dist/{nord/client → client}/NordUser.d.ts +86 -98
- package/dist/{nord/client → client}/NordUser.js +204 -271
- package/dist/const.js +5 -8
- package/dist/{nord/utils/NordError.js → error.js} +7 -5
- package/dist/gen/nord_pb.js +88 -92
- package/dist/gen/openapi.d.ts +39 -0
- package/dist/gen/openapi.js +1 -2
- package/dist/index.d.ts +6 -1
- package/dist/index.js +10 -21
- package/dist/types.d.ts +5 -50
- package/dist/types.js +21 -83
- package/dist/utils.d.ts +8 -11
- package/dist/utils.js +55 -90
- package/dist/websocket/NordWebSocketClient.js +12 -17
- package/dist/{nord/models → websocket}/Subscriber.d.ts +1 -1
- package/dist/{nord/models → websocket}/Subscriber.js +6 -7
- package/dist/websocket/events.js +1 -2
- package/dist/websocket/index.d.ts +19 -2
- package/dist/websocket/index.js +80 -5
- package/package.json +2 -3
- package/dist/api/client.d.ts +0 -14
- package/dist/api/client.js +0 -45
- package/dist/bridge/client.d.ts +0 -151
- package/dist/bridge/client.js +0 -434
- package/dist/bridge/const.d.ts +0 -23
- package/dist/bridge/const.js +0 -47
- package/dist/bridge/index.d.ts +0 -4
- package/dist/bridge/index.js +0 -23
- package/dist/bridge/types.d.ts +0 -120
- package/dist/bridge/types.js +0 -18
- package/dist/bridge/utils.d.ts +0 -64
- package/dist/bridge/utils.js +0 -131
- package/dist/gen/common.d.ts +0 -68
- package/dist/gen/common.js +0 -215
- package/dist/gen/nord.d.ts +0 -882
- package/dist/gen/nord.js +0 -6520
- package/dist/idl/bridge.d.ts +0 -569
- package/dist/idl/bridge.js +0 -8
- package/dist/idl/bridge.json +0 -1506
- package/dist/idl/index.d.ts +0 -607
- package/dist/idl/index.js +0 -8
- package/dist/nord/api/actions.d.ts +0 -126
- package/dist/nord/api/actions.js +0 -397
- package/dist/nord/api/core.d.ts +0 -16
- package/dist/nord/api/core.js +0 -81
- package/dist/nord/api/market.d.ts +0 -36
- package/dist/nord/api/market.js +0 -96
- package/dist/nord/api/metrics.d.ts +0 -67
- package/dist/nord/api/metrics.js +0 -229
- package/dist/nord/api/queries.d.ts +0 -46
- package/dist/nord/api/queries.js +0 -109
- package/dist/nord/api/triggers.d.ts +0 -7
- package/dist/nord/api/triggers.js +0 -38
- package/dist/nord/client/NordClient.d.ts +0 -33
- package/dist/nord/client/NordClient.js +0 -45
- package/dist/nord/index.d.ts +0 -11
- package/dist/nord/index.js +0 -36
- package/src/const.ts +0 -34
- package/src/gen/.gitkeep +0 -0
- package/src/gen/nord_pb.ts +0 -5053
- package/src/gen/openapi.ts +0 -2864
- package/src/index.ts +0 -5
- package/src/nord/api/actions.ts +0 -648
- package/src/nord/api/core.ts +0 -96
- package/src/nord/api/metrics.ts +0 -269
- package/src/nord/client/Nord.ts +0 -937
- package/src/nord/client/NordAdmin.ts +0 -514
- package/src/nord/client/NordClient.ts +0 -79
- package/src/nord/client/NordUser.ts +0 -1211
- package/src/nord/index.ts +0 -25
- package/src/nord/models/Subscriber.ts +0 -56
- package/src/nord/utils/NordError.ts +0 -76
- package/src/types.ts +0 -377
- package/src/utils.ts +0 -269
- package/src/websocket/NordWebSocketClient.ts +0 -316
- package/src/websocket/events.ts +0 -31
- package/src/websocket/index.ts +0 -2
- /package/dist/{nord/utils/NordError.d.ts → error.d.ts} +0 -0
package/src/nord/api/core.ts
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { SubscriptionPattern } from "../../types";
|
|
2
|
-
import { NordWebSocketClient } from "../../websocket/index";
|
|
3
|
-
import { NordError } from "../utils/NordError";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Initialize a WebSocket client for Nord
|
|
7
|
-
*
|
|
8
|
-
* Connects to the Nord WebSocket endpoint with support for multiple subscription types:
|
|
9
|
-
* - trades@SYMBOL - For trade updates
|
|
10
|
-
* - deltas@SYMBOL - For orderbook delta updates
|
|
11
|
-
* - account@ACCOUNT_ID - For user-specific updates
|
|
12
|
-
*
|
|
13
|
-
* @param webServerUrl - Base URL for the Nord web server
|
|
14
|
-
* @param subscriptions - Array of subscriptions (e.g., ["trades@BTCUSDC", "deltas@BTCUSDC", "account@42"])
|
|
15
|
-
* @returns WebSocket client
|
|
16
|
-
* @throws {NordError} If initialization fails or invalid subscription is provided
|
|
17
|
-
*/
|
|
18
|
-
export function initWebSocketClient(
|
|
19
|
-
webServerUrl: string,
|
|
20
|
-
subscriptions?: SubscriptionPattern[] | "trades" | "delta" | "account",
|
|
21
|
-
): NordWebSocketClient {
|
|
22
|
-
try {
|
|
23
|
-
// Determine URL and subscriptions based on parameters
|
|
24
|
-
let wsUrl = webServerUrl.replace(/^http/, "ws") + `/ws`;
|
|
25
|
-
|
|
26
|
-
// Validate subscriptions parameter
|
|
27
|
-
if (typeof subscriptions === "string") {
|
|
28
|
-
// Legacy mode - handle endpoint string
|
|
29
|
-
if (
|
|
30
|
-
subscriptions === "trades" ||
|
|
31
|
-
subscriptions === "delta" ||
|
|
32
|
-
subscriptions === "account"
|
|
33
|
-
) {
|
|
34
|
-
wsUrl += `/${subscriptions}`;
|
|
35
|
-
} else {
|
|
36
|
-
throw new NordError(
|
|
37
|
-
`Invalid endpoint: ${subscriptions}. Must be "trades", "deltas", or "account".`,
|
|
38
|
-
);
|
|
39
|
-
}
|
|
40
|
-
} else if (Array.isArray(subscriptions) && subscriptions.length > 0) {
|
|
41
|
-
// New mode - validate and combine subscriptions in URL
|
|
42
|
-
subscriptions.forEach(validateSubscription);
|
|
43
|
-
wsUrl += `/${subscriptions.join("&")}`;
|
|
44
|
-
} else {
|
|
45
|
-
// Default to trades endpoint if no subscriptions specified
|
|
46
|
-
wsUrl += `/trades`;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
console.log(`Initializing WebSocket client with URL: ${wsUrl}`);
|
|
50
|
-
|
|
51
|
-
// Create and connect the WebSocket client
|
|
52
|
-
const ws = new NordWebSocketClient(wsUrl);
|
|
53
|
-
|
|
54
|
-
// Add error handler
|
|
55
|
-
ws.on("error", (error) => {
|
|
56
|
-
console.error("Nord WebSocket error:", error);
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
// Add connected handler for debugging
|
|
60
|
-
ws.on("connected", () => {
|
|
61
|
-
console.log("Nord WebSocket connected successfully");
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
// Connect the WebSocket
|
|
65
|
-
ws.connect();
|
|
66
|
-
return ws;
|
|
67
|
-
} catch (error) {
|
|
68
|
-
console.error("Failed to initialize WebSocket client:", error);
|
|
69
|
-
throw new NordError("Failed to initialize WebSocket client", {
|
|
70
|
-
cause: error,
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Validates a subscription string follows the correct format
|
|
77
|
-
*
|
|
78
|
-
* @param subscription - The subscription to validate
|
|
79
|
-
* @throws {NordError} If the subscription format is invalid
|
|
80
|
-
*/
|
|
81
|
-
function validateSubscription(subscription: string): void {
|
|
82
|
-
const [type, param] = subscription.split("@");
|
|
83
|
-
|
|
84
|
-
if (!type || !param || !["trades", "deltas", "account"].includes(type)) {
|
|
85
|
-
throw new NordError(
|
|
86
|
-
`Invalid subscription format: ${subscription}. Expected format: "trades@SYMBOL", "deltas@SYMBOL", or "account@ID"`,
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Additional validation for account subscriptions
|
|
91
|
-
if (type === "account" && isNaN(Number(param))) {
|
|
92
|
-
throw new NordError(
|
|
93
|
-
`Invalid account ID in subscription: ${subscription}. Account ID must be a number.`,
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
}
|
package/src/nord/api/metrics.ts
DELETED
|
@@ -1,269 +0,0 @@
|
|
|
1
|
-
import { AggregateMetrics, PeakTpsPeriodUnit } from "../../types";
|
|
2
|
-
import { checkedFetch } from "../../utils";
|
|
3
|
-
import { NordError } from "../utils/NordError";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Time periods for metrics queries
|
|
7
|
-
*/
|
|
8
|
-
export enum MetricPeriod {
|
|
9
|
-
ONE_MINUTE = "1m",
|
|
10
|
-
FIVE_MINUTES = "5m",
|
|
11
|
-
FIFTEEN_MINUTES = "15m",
|
|
12
|
-
ONE_HOUR = "1h",
|
|
13
|
-
FOUR_HOURS = "4h",
|
|
14
|
-
ONE_DAY = "24h",
|
|
15
|
-
ONE_WEEK = "7d",
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Fetch aggregate metrics from the Nord API
|
|
20
|
-
*
|
|
21
|
-
* @param webServerUrl - Base URL for the Nord web server
|
|
22
|
-
* @param txPeakTpsPeriod - Period for peak TPS calculation
|
|
23
|
-
* @param txPeakTpsPeriodUnit - Unit for peak TPS period
|
|
24
|
-
* @returns Aggregate metrics
|
|
25
|
-
* @throws {NordError} If the request fails
|
|
26
|
-
*/
|
|
27
|
-
export async function aggregateMetrics(
|
|
28
|
-
webServerUrl: string,
|
|
29
|
-
txPeakTpsPeriod = 1,
|
|
30
|
-
txPeakTpsPeriodUnit: PeakTpsPeriodUnit = PeakTpsPeriodUnit.Day,
|
|
31
|
-
): Promise<AggregateMetrics> {
|
|
32
|
-
try {
|
|
33
|
-
const response = await checkedFetch(
|
|
34
|
-
`${webServerUrl}/metrics?tx_peak_tps_period=${txPeakTpsPeriod}&tx_peak_tps_period_unit=${txPeakTpsPeriodUnit}`,
|
|
35
|
-
);
|
|
36
|
-
|
|
37
|
-
// Get the raw text response (Prometheus format)
|
|
38
|
-
const text = await response.text();
|
|
39
|
-
|
|
40
|
-
// Parse the Prometheus-formatted metrics text into an AggregateMetrics object
|
|
41
|
-
const metrics: AggregateMetrics = {
|
|
42
|
-
blocks_total: 0,
|
|
43
|
-
tx_total: extractMetricValue(text, "nord_requests_ok_count"),
|
|
44
|
-
tx_tps: calculateTps(text),
|
|
45
|
-
tx_tps_peak: calculatePeakTps(text),
|
|
46
|
-
request_latency_average: extractLatency(text),
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
return metrics;
|
|
50
|
-
} catch (error) {
|
|
51
|
-
throw new NordError("Failed to fetch aggregate metrics", { cause: error });
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Extract a metric value from Prometheus-formatted text
|
|
57
|
-
*
|
|
58
|
-
* @param text - Prometheus-formatted metrics text
|
|
59
|
-
* @param metricName - Name of the metric to extract
|
|
60
|
-
* @returns The metric value as a number, or 0 if not found
|
|
61
|
-
*/
|
|
62
|
-
function extractMetricValue(text: string, metricName: string): number {
|
|
63
|
-
const regex = new RegExp(`^${metricName}\\s+([\\d.]+)`, "m");
|
|
64
|
-
const match = text.match(regex);
|
|
65
|
-
return match ? parseFloat(match[1]) : 0;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Calculate TPS from Prometheus metrics
|
|
70
|
-
*
|
|
71
|
-
* @param text - Prometheus-formatted metrics text
|
|
72
|
-
* @returns Calculated TPS value
|
|
73
|
-
*/
|
|
74
|
-
function calculateTps(text: string): number {
|
|
75
|
-
// Use the request count and latency to estimate TPS
|
|
76
|
-
const requestCount = extractMetricValue(text, "nord_requests_ok_count");
|
|
77
|
-
const latencySum = extractSummaryValue(text, "nord_requests_ok_latency_sum");
|
|
78
|
-
const latencyCount = extractSummaryValue(
|
|
79
|
-
text,
|
|
80
|
-
"nord_requests_ok_latency_count",
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
if (latencySum > 0 && latencyCount > 0) {
|
|
84
|
-
// Average latency in seconds
|
|
85
|
-
const avgLatency = latencySum / latencyCount;
|
|
86
|
-
// If we have valid latency data, estimate TPS as requests per second
|
|
87
|
-
return avgLatency > 0 ? requestCount / (latencyCount * avgLatency) : 0;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Fallback: just return a small fraction of the total request count
|
|
91
|
-
return requestCount > 0 ? requestCount / 100 : 0;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Calculate peak TPS from Prometheus metrics
|
|
96
|
-
*
|
|
97
|
-
* @param text - Prometheus-formatted metrics text
|
|
98
|
-
* @returns Calculated peak TPS value
|
|
99
|
-
*/
|
|
100
|
-
function calculatePeakTps(text: string): number {
|
|
101
|
-
// For peak TPS, we'll use a simple heuristic: 2x the current TPS estimate
|
|
102
|
-
// TODO: fix this
|
|
103
|
-
return calculateTps(text) * 2;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Extract latency from Prometheus metrics
|
|
108
|
-
*
|
|
109
|
-
* @param text - Prometheus-formatted metrics text
|
|
110
|
-
* @returns Average latency in seconds
|
|
111
|
-
*/
|
|
112
|
-
function extractLatency(text: string): number {
|
|
113
|
-
// TODO: fix - using average for latency is kinda wack. ok to merge for now but should change.
|
|
114
|
-
const latencySum = extractSummaryValue(text, "nord_requests_ok_latency_sum");
|
|
115
|
-
const latencyCount = extractSummaryValue(
|
|
116
|
-
text,
|
|
117
|
-
"nord_requests_ok_latency_count",
|
|
118
|
-
);
|
|
119
|
-
|
|
120
|
-
return latencyCount > 0 ? latencySum / latencyCount : 0;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Extract a summary value from Prometheus-formatted text
|
|
125
|
-
*
|
|
126
|
-
* @param text - Prometheus-formatted metrics text
|
|
127
|
-
* @param metricName - Name of the metric to extract
|
|
128
|
-
* @returns The metric value as a number, or 0 if not found
|
|
129
|
-
*/
|
|
130
|
-
function extractSummaryValue(text: string, metricName: string): number {
|
|
131
|
-
const regex = new RegExp(`^${metricName}\\s+([\\d.]+)`, "m");
|
|
132
|
-
const match = text.match(regex);
|
|
133
|
-
return match ? parseFloat(match[1]) : 0;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* Get current transactions per second
|
|
138
|
-
*
|
|
139
|
-
* @param webServerUrl - Base URL for the Nord web server
|
|
140
|
-
* @param period - Time period for the query
|
|
141
|
-
* @returns Current TPS value
|
|
142
|
-
* @throws {NordError} If the request fails
|
|
143
|
-
*/
|
|
144
|
-
export async function getCurrentTps(
|
|
145
|
-
webServerUrl: string,
|
|
146
|
-
period: string = "1m",
|
|
147
|
-
): Promise<number> {
|
|
148
|
-
try {
|
|
149
|
-
// nord_tx_count doesn't exist in the metrics, use nord_requests_ok_count instead
|
|
150
|
-
return await queryPrometheus(
|
|
151
|
-
webServerUrl,
|
|
152
|
-
`sum(rate(nord_requests_ok_count[${period}]))`,
|
|
153
|
-
);
|
|
154
|
-
} catch (error) {
|
|
155
|
-
throw new NordError(`Failed to get current TPS for period ${period}`, {
|
|
156
|
-
cause: error,
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Get peak transactions per second
|
|
163
|
-
*
|
|
164
|
-
* @param webServerUrl - Base URL for the Nord web server
|
|
165
|
-
* @param period - Time period for the query
|
|
166
|
-
* @returns Peak TPS value
|
|
167
|
-
* @throws {NordError} If the request fails
|
|
168
|
-
*/
|
|
169
|
-
export async function getPeakTps(
|
|
170
|
-
webServerUrl: string,
|
|
171
|
-
period: string = "24h",
|
|
172
|
-
): Promise<number> {
|
|
173
|
-
try {
|
|
174
|
-
// nord_tx_count doesn't exist in the metrics, use nord_requests_ok_count instead
|
|
175
|
-
return await queryPrometheus(
|
|
176
|
-
webServerUrl,
|
|
177
|
-
`max_over_time(sum(rate(nord_requests_ok_count[1m]))[${period}:])`,
|
|
178
|
-
);
|
|
179
|
-
} catch (error) {
|
|
180
|
-
throw new NordError(`Failed to get peak TPS for period ${period}`, {
|
|
181
|
-
cause: error,
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* Get median transaction latency
|
|
188
|
-
*
|
|
189
|
-
* @param webServerUrl - Base URL for the Nord web server
|
|
190
|
-
* @param period - Time period for the query
|
|
191
|
-
* @returns Median latency in milliseconds
|
|
192
|
-
* @throws {NordError} If the request fails
|
|
193
|
-
*/
|
|
194
|
-
export async function getMedianLatency(
|
|
195
|
-
webServerUrl: string,
|
|
196
|
-
period: string = "1m",
|
|
197
|
-
): Promise<number> {
|
|
198
|
-
try {
|
|
199
|
-
// nord_tx_latency_ms doesn't exist, use nord_requests_ok_latency instead
|
|
200
|
-
// which contains the latency data in the summary metric
|
|
201
|
-
return await queryPrometheus(
|
|
202
|
-
webServerUrl,
|
|
203
|
-
`quantile_over_time(0.5, nord_requests_ok_latency[${period}]) * 1000`, // Convert to milliseconds
|
|
204
|
-
);
|
|
205
|
-
} catch (error) {
|
|
206
|
-
throw new NordError(`Failed to get median latency for period ${period}`, {
|
|
207
|
-
cause: error,
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* Get total transaction count
|
|
214
|
-
*
|
|
215
|
-
* @param webServerUrl - Base URL for the Nord web server
|
|
216
|
-
* @returns Total transaction count
|
|
217
|
-
* @throws {NordError} If the request fails
|
|
218
|
-
*/
|
|
219
|
-
export async function getTotalTransactions(
|
|
220
|
-
webServerUrl: string,
|
|
221
|
-
): Promise<number> {
|
|
222
|
-
try {
|
|
223
|
-
// nord_tx_count doesn't exist, use nord_requests_ok_count instead
|
|
224
|
-
return await queryPrometheus(webServerUrl, "sum(nord_requests_ok_count)");
|
|
225
|
-
} catch (error) {
|
|
226
|
-
throw new NordError("Failed to get total transactions", { cause: error });
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* Query Prometheus metrics
|
|
232
|
-
*
|
|
233
|
-
* @param webServerUrl - Base URL for the Nord web server
|
|
234
|
-
* @param params - Prometheus query parameters
|
|
235
|
-
* @returns Query result as a number
|
|
236
|
-
* @throws {NordError} If the request fails
|
|
237
|
-
*/
|
|
238
|
-
export async function queryPrometheus(
|
|
239
|
-
webServerUrl: string,
|
|
240
|
-
params: string,
|
|
241
|
-
): Promise<number> {
|
|
242
|
-
try {
|
|
243
|
-
const response = await checkedFetch(
|
|
244
|
-
`${webServerUrl}/prometheus?query=${encodeURIComponent(params)}`,
|
|
245
|
-
);
|
|
246
|
-
|
|
247
|
-
// Handle raw text response
|
|
248
|
-
const text = await response.text();
|
|
249
|
-
try {
|
|
250
|
-
// Try to parse as JSON first
|
|
251
|
-
const data = JSON.parse(text);
|
|
252
|
-
return data.data.result[0]?.value[1] || 0;
|
|
253
|
-
} catch (error) {
|
|
254
|
-
console.log("Prometheus query failed:", error);
|
|
255
|
-
// Try to find a number in the response
|
|
256
|
-
const numberMatch = text.match(/[\d.]+/);
|
|
257
|
-
if (numberMatch) {
|
|
258
|
-
return parseFloat(numberMatch[0]);
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
// Return 0 if no number is found
|
|
262
|
-
return 0;
|
|
263
|
-
}
|
|
264
|
-
} catch (error) {
|
|
265
|
-
throw new NordError(`Failed to query Prometheus: ${params}`, {
|
|
266
|
-
cause: error,
|
|
267
|
-
});
|
|
268
|
-
}
|
|
269
|
-
}
|