@n1xyz/nord-ts 0.0.12 → 0.0.15
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/.prettierignore +1 -0
- package/README.md +9 -6
- package/dist/bridge/client.d.ts +8 -7
- package/dist/bridge/client.js +38 -37
- package/dist/bridge/index.d.ts +1 -2
- package/dist/bridge/index.js +2 -2
- package/dist/bridge/types.d.ts +5 -3
- package/dist/bridge/types.js +3 -1
- package/dist/gen/nord.d.ts +66 -5
- package/dist/gen/nord.js +449 -22
- package/dist/idl/bridge.json +1493 -0
- package/dist/idl/index.d.ts +585 -0
- package/dist/idl/index.js +8 -0
- package/dist/nord/api/core.d.ts +8 -8
- package/dist/nord/api/core.js +58 -15
- package/dist/nord/api/metrics.js +7 -4
- package/dist/nord/client/Nord.d.ts +57 -72
- package/dist/nord/client/Nord.js +134 -125
- package/dist/nord/client/NordUser.d.ts +0 -17
- package/dist/nord/client/NordUser.js +4 -35
- package/dist/types.d.ts +38 -15
- package/dist/types.js +2 -2
- package/dist/websocket/NordWebSocketClient.d.ts +0 -3
- package/dist/websocket/NordWebSocketClient.js +10 -13
- package/dist/websocket/events.d.ts +2 -2
- package/docs/assets/hierarchy.js +1 -1
- package/docs/assets/navigation.js +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/classes/Nord.html +99 -100
- package/docs/classes/NordError.html +6 -6
- package/docs/classes/NordUser.html +48 -53
- package/docs/classes/NordWebSocketClient.html +6 -11
- package/docs/classes/SolanaBridgeClient.html +23 -23
- package/docs/classes/Subscriber.html +3 -3
- package/docs/enums/FillMode.html +2 -2
- package/docs/enums/KeyType.html +2 -2
- package/docs/enums/MetricPeriod.html +2 -2
- package/docs/enums/PdaSeedType.html +2 -2
- package/docs/enums/PeakTpsPeriodUnit.html +2 -2
- package/docs/enums/Side.html +2 -2
- package/docs/enums/WebSocketMessageType.html +3 -3
- package/docs/functions/actionQueryRollman.html +1 -1
- package/docs/functions/actionsQueryRollman.html +1 -1
- package/docs/functions/aggregateMetrics-1.html +1 -1
- package/docs/functions/assert.html +1 -1
- package/docs/functions/bigIntToProtoU128.html +1 -1
- package/docs/functions/bridgeToBN.html +1 -1
- package/docs/functions/bufferToHex.html +1 -1
- package/docs/functions/cancelOrder.html +1 -1
- package/docs/functions/checkPubKeyLength.html +1 -1
- package/docs/functions/checkedFetch.html +1 -1
- package/docs/functions/createSession.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/findPda.html +1 -1
- package/docs/functions/findToken.html +1 -1
- package/docs/functions/fromBN.html +1 -1
- package/docs/functions/getAccount.html +1 -1
- package/docs/functions/getActionNonce.html +1 -1
- package/docs/functions/getCurrentTps.html +1 -1
- package/docs/functions/getInfo.html +1 -1
- package/docs/functions/{queryLastNBlocks.html → getLastActionId.html} +3 -3
- package/docs/functions/getMedianLatency.html +1 -1
- package/docs/functions/getOrderbook.html +1 -1
- package/docs/functions/getPeakTps.html +1 -1
- package/docs/functions/getTimestamp.html +1 -1
- package/docs/functions/getTotalTransactions.html +1 -1
- package/docs/functions/getTrades.html +1 -1
- package/docs/functions/getUserAccountIds.html +1 -1
- package/docs/functions/hexToBuffer.html +1 -1
- package/docs/functions/initWebSocketClient.html +9 -9
- package/docs/functions/keypairFromPrivateKey.html +1 -1
- package/docs/functions/makeSigningFunction.html +1 -1
- package/docs/functions/makeWalletSignFn.html +1 -1
- package/docs/functions/marketsStats.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/placeOrder.html +1 -1
- package/docs/functions/queryAction.html +1 -1
- package/docs/functions/queryPrometheus.html +1 -1
- package/docs/functions/queryRecentActions.html +4 -3
- package/docs/functions/revokeSession.html +1 -1
- package/docs/functions/shortenPublicKey.html +1 -1
- package/docs/functions/signAction.html +1 -1
- package/docs/functions/toBN.html +1 -1
- package/docs/functions/toScaledU128.html +1 -1
- package/docs/functions/toScaledU64.html +1 -1
- package/docs/functions/transfer.html +1 -1
- package/docs/functions/withdraw.html +1 -1
- package/docs/hierarchy.html +1 -1
- package/docs/index.html +2 -2
- package/docs/interfaces/Account.html +2 -2
- package/docs/interfaces/ActionInfo.html +2 -2
- package/docs/interfaces/ActionNonceResponse.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/BlockFacts.html +5 -5
- 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/DepositSplParams.html +5 -5
- 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 +19 -9
- package/docs/interfaces/NordWebSocketClientEvents.html +2 -2
- package/docs/interfaces/NordWebSocketEvents.html +3 -3
- package/docs/interfaces/Order.html +2 -2
- package/docs/interfaces/OrderInfo.html +2 -2
- package/docs/interfaces/OrderbookEntry.html +2 -2
- package/docs/interfaces/OrderbookQuery.html +2 -2
- package/docs/interfaces/OrderbookResponse.html +2 -2
- package/docs/interfaces/OrderbookSubscription.html +5 -5
- 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/SPLTokenInfo.html +5 -5
- package/docs/interfaces/SolanaBridgeConfig.html +5 -5
- package/docs/interfaces/StateFacts.html +5 -5
- package/docs/interfaces/SubscriberConfig.html +2 -2
- package/docs/interfaces/TimestampResponse.html +2 -2
- package/docs/interfaces/Token.html +2 -2
- package/docs/interfaces/TokenInfo.html +3 -2
- package/docs/interfaces/Trade.html +2 -2
- package/docs/interfaces/TradeSubscription.html +5 -5
- package/docs/interfaces/Trades.html +2 -2
- package/docs/interfaces/TradesQuery.html +2 -2
- package/docs/interfaces/TradesResponse.html +2 -2
- package/docs/interfaces/TransferParams.html +4 -4
- package/docs/interfaces/UserAccountIdsQuery.html +2 -2
- package/docs/interfaces/UserAccountIdsResponse.html +2 -2
- package/docs/interfaces/WebSocketAccountUpdate.html +6 -0
- package/docs/interfaces/WebSocketDeltaUpdate.html +3 -3
- package/docs/interfaces/WebSocketSubscription.html +3 -3
- package/docs/interfaces/WebSocketTradeUpdate.html +3 -3
- package/docs/interfaces/WithdrawalClaim.html +7 -7
- package/docs/interfaces/WithdrawalParams.html +4 -4
- package/docs/modules.html +1 -1
- package/docs/types/BigIntValue.html +1 -1
- package/docs/types/SubscriptionPattern.html +4 -0
- package/docs/types/SubscriptionType.html +2 -0
- package/docs/types/WebSocketMessage.html +1 -1
- package/docs/variables/DEBUG_KEYS.html +1 -1
- package/docs/variables/DEFAULT_FUNDING_AMOUNTS.html +1 -1
- package/docs/variables/DEV_TOKEN_INFOS.html +1 -1
- package/docs/variables/DEV_URL.html +1 -1
- package/docs/variables/MAX_BUFFER_LEN.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/docs/variables/_private.html +1 -1
- package/idl-generate.sh +4 -0
- package/package.json +4 -3
- package/src/bridge/client.ts +54 -43
- package/src/bridge/index.ts +1 -2
- package/src/bridge/types.ts +5 -3
- package/src/gen/nord.ts +518 -19
- package/src/idl/bridge.json +1493 -0
- package/src/idl/index.ts +2 -0
- package/src/nord/api/core.ts +70 -20
- package/src/nord/api/metrics.ts +28 -20
- package/src/nord/api/queries.ts +7 -11
- package/src/nord/client/Nord.ts +174 -168
- package/src/nord/client/NordUser.ts +32 -98
- package/src/types.ts +40 -15
- package/src/websocket/NordWebSocketClient.ts +11 -14
- package/src/websocket/events.ts +2 -2
- package/docs/functions/blockQueryRollman.html +0 -6
- package/docs/functions/blockSummaryQueryRollman.html +0 -6
- package/docs/functions/queryBlock.html +0 -6
- package/docs/functions/queryRecentBlocks.html +0 -6
- package/docs/interfaces/WebSocketUserUpdate.html +0 -6
- package/src/idl/bridge.ts +0 -930
- package/test.ts +0 -107
package/src/idl/index.ts
ADDED
package/src/nord/api/core.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Account, Info } from "../../types";
|
|
1
|
+
import { Account, Info, SubscriptionPattern } from "../../types";
|
|
2
2
|
import { checkedFetch } from "../../utils";
|
|
3
3
|
import { NordWebSocketClient } from "../../websocket/index";
|
|
4
4
|
import { NordError } from "../utils/NordError";
|
|
@@ -65,9 +65,7 @@ export async function getAccount(
|
|
|
65
65
|
accountId: number,
|
|
66
66
|
): Promise<Account> {
|
|
67
67
|
try {
|
|
68
|
-
const response = await checkedFetch(
|
|
69
|
-
`${webServerUrl}/account?account_id=${accountId}`,
|
|
70
|
-
);
|
|
68
|
+
const response = await checkedFetch(`${webServerUrl}/account/${accountId}`);
|
|
71
69
|
return await response.json();
|
|
72
70
|
} catch (error) {
|
|
73
71
|
throw new NordError(`Failed to get account ${accountId}`, { cause: error });
|
|
@@ -77,27 +75,55 @@ export async function getAccount(
|
|
|
77
75
|
/**
|
|
78
76
|
* Initialize a WebSocket client for Nord
|
|
79
77
|
*
|
|
80
|
-
* Connects to
|
|
81
|
-
* -
|
|
82
|
-
* -
|
|
83
|
-
* -
|
|
78
|
+
* Connects to the Nord WebSocket endpoint with support for multiple subscription types:
|
|
79
|
+
* - trades@SYMBOL - For trade updates
|
|
80
|
+
* - deltas@SYMBOL - For orderbook delta updates
|
|
81
|
+
* - account@ACCOUNT_ID - For user-specific updates
|
|
84
82
|
*
|
|
85
83
|
* @param webServerUrl - Base URL for the Nord web server
|
|
86
|
-
* @param
|
|
87
|
-
* @param initialSubscriptions - Optional array of initial subscriptions (e.g., ["trades@BTCUSDC"])
|
|
84
|
+
* @param subscriptions - Array of subscriptions (e.g., ["trades@BTCUSDC", "deltas@BTCUSDC", "account@42"])
|
|
88
85
|
* @returns WebSocket client
|
|
86
|
+
* @throws {NordError} If initialization fails or invalid subscription is provided
|
|
89
87
|
*/
|
|
90
88
|
export function initWebSocketClient(
|
|
91
89
|
webServerUrl: string,
|
|
92
|
-
|
|
93
|
-
initialSubscriptions?:
|
|
90
|
+
subscriptions?: SubscriptionPattern[] | "trades" | "delta" | "account",
|
|
91
|
+
initialSubscriptions?: SubscriptionPattern[],
|
|
94
92
|
): NordWebSocketClient {
|
|
95
93
|
try {
|
|
96
|
-
//
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
94
|
+
// Determine URL and subscriptions based on parameters
|
|
95
|
+
let wsUrl = webServerUrl.replace(/^http/, "ws") + `/ws`;
|
|
96
|
+
let wsSubscriptions: SubscriptionPattern[] = [];
|
|
97
|
+
|
|
98
|
+
// Validate subscriptions parameter
|
|
99
|
+
if (typeof subscriptions === "string") {
|
|
100
|
+
// Legacy mode - handle endpoint string
|
|
101
|
+
if (
|
|
102
|
+
subscriptions === "trades" ||
|
|
103
|
+
subscriptions === "delta" ||
|
|
104
|
+
subscriptions === "account"
|
|
105
|
+
) {
|
|
106
|
+
wsUrl += `/${subscriptions}`;
|
|
107
|
+
// If initialSubscriptions provided, use them
|
|
108
|
+
if (initialSubscriptions && initialSubscriptions.length > 0) {
|
|
109
|
+
// Validate initialSubscriptions
|
|
110
|
+
initialSubscriptions.forEach(validateSubscription);
|
|
111
|
+
wsSubscriptions = initialSubscriptions;
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
114
|
+
throw new NordError(
|
|
115
|
+
`Invalid endpoint: ${subscriptions}. Must be "trades", "deltas", or "account".`,
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
} else if (Array.isArray(subscriptions) && subscriptions.length > 0) {
|
|
119
|
+
// New mode - validate and combine subscriptions in URL
|
|
120
|
+
subscriptions.forEach(validateSubscription);
|
|
121
|
+
wsUrl += `/${subscriptions.join("&")}`;
|
|
122
|
+
} else {
|
|
123
|
+
// Default to trades endpoint if no subscriptions specified
|
|
124
|
+
wsUrl += `/trades`;
|
|
125
|
+
}
|
|
126
|
+
|
|
101
127
|
console.log(`Initializing WebSocket client with URL: ${wsUrl}`);
|
|
102
128
|
|
|
103
129
|
// Create and connect the WebSocket client
|
|
@@ -112,9 +138,10 @@ export function initWebSocketClient(
|
|
|
112
138
|
ws.on("connected", () => {
|
|
113
139
|
console.log("Nord WebSocket connected successfully");
|
|
114
140
|
|
|
115
|
-
// Subscribe to
|
|
116
|
-
|
|
117
|
-
|
|
141
|
+
// Subscribe to additional subscriptions if provided
|
|
142
|
+
// For new format, these are already part of the URL
|
|
143
|
+
if (wsSubscriptions.length > 0) {
|
|
144
|
+
ws.subscribe(wsSubscriptions);
|
|
118
145
|
}
|
|
119
146
|
});
|
|
120
147
|
|
|
@@ -128,3 +155,26 @@ export function initWebSocketClient(
|
|
|
128
155
|
});
|
|
129
156
|
}
|
|
130
157
|
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Validates a subscription string follows the correct format
|
|
161
|
+
*
|
|
162
|
+
* @param subscription - The subscription to validate
|
|
163
|
+
* @throws {NordError} If the subscription format is invalid
|
|
164
|
+
*/
|
|
165
|
+
function validateSubscription(subscription: string): void {
|
|
166
|
+
const [type, param] = subscription.split("@");
|
|
167
|
+
|
|
168
|
+
if (!type || !param || !["trades", "deltas", "account"].includes(type)) {
|
|
169
|
+
throw new NordError(
|
|
170
|
+
`Invalid subscription format: ${subscription}. Expected format: "trades@SYMBOL", "deltas@SYMBOL", or "account@ID"`,
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// Additional validation for account subscriptions
|
|
175
|
+
if (type === "account" && isNaN(Number(param))) {
|
|
176
|
+
throw new NordError(
|
|
177
|
+
`Invalid account ID in subscription: ${subscription}. Account ID must be a number.`,
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
}
|
package/src/nord/api/metrics.ts
CHANGED
|
@@ -33,19 +33,19 @@ export async function aggregateMetrics(
|
|
|
33
33
|
const response = await checkedFetch(
|
|
34
34
|
`${webServerUrl}/metrics?tx_peak_tps_period=${txPeakTpsPeriod}&tx_peak_tps_period_unit=${txPeakTpsPeriodUnit}`,
|
|
35
35
|
);
|
|
36
|
-
|
|
36
|
+
|
|
37
37
|
// Get the raw text response (Prometheus format)
|
|
38
38
|
const text = await response.text();
|
|
39
|
-
|
|
39
|
+
|
|
40
40
|
// Parse the Prometheus-formatted metrics text into an AggregateMetrics object
|
|
41
41
|
const metrics: AggregateMetrics = {
|
|
42
42
|
blocks_total: 0,
|
|
43
43
|
tx_total: extractMetricValue(text, "nord_requests_ok_count"),
|
|
44
44
|
tx_tps: calculateTps(text),
|
|
45
45
|
tx_tps_peak: calculatePeakTps(text),
|
|
46
|
-
request_latency_average: extractLatency(text)
|
|
46
|
+
request_latency_average: extractLatency(text),
|
|
47
47
|
};
|
|
48
|
-
|
|
48
|
+
|
|
49
49
|
return metrics;
|
|
50
50
|
} catch (error) {
|
|
51
51
|
throw new NordError("Failed to fetch aggregate metrics", { cause: error });
|
|
@@ -54,20 +54,20 @@ export async function aggregateMetrics(
|
|
|
54
54
|
|
|
55
55
|
/**
|
|
56
56
|
* Extract a metric value from Prometheus-formatted text
|
|
57
|
-
*
|
|
57
|
+
*
|
|
58
58
|
* @param text - Prometheus-formatted metrics text
|
|
59
59
|
* @param metricName - Name of the metric to extract
|
|
60
60
|
* @returns The metric value as a number, or 0 if not found
|
|
61
61
|
*/
|
|
62
62
|
function extractMetricValue(text: string, metricName: string): number {
|
|
63
|
-
const regex = new RegExp(`^${metricName}\\s+([\\d.]+)`,
|
|
63
|
+
const regex = new RegExp(`^${metricName}\\s+([\\d.]+)`, "m");
|
|
64
64
|
const match = text.match(regex);
|
|
65
65
|
return match ? parseFloat(match[1]) : 0;
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
/**
|
|
69
69
|
* Calculate TPS from Prometheus metrics
|
|
70
|
-
*
|
|
70
|
+
*
|
|
71
71
|
* @param text - Prometheus-formatted metrics text
|
|
72
72
|
* @returns Calculated TPS value
|
|
73
73
|
*/
|
|
@@ -75,52 +75,60 @@ function calculateTps(text: string): number {
|
|
|
75
75
|
// Use the request count and latency to estimate TPS
|
|
76
76
|
const requestCount = extractMetricValue(text, "nord_requests_ok_count");
|
|
77
77
|
const latencySum = extractSummaryValue(text, "nord_requests_ok_latency_sum");
|
|
78
|
-
const latencyCount = extractSummaryValue(
|
|
79
|
-
|
|
78
|
+
const latencyCount = extractSummaryValue(
|
|
79
|
+
text,
|
|
80
|
+
"nord_requests_ok_latency_count",
|
|
81
|
+
);
|
|
82
|
+
|
|
80
83
|
if (latencySum > 0 && latencyCount > 0) {
|
|
81
84
|
// Average latency in seconds
|
|
82
85
|
const avgLatency = latencySum / latencyCount;
|
|
83
86
|
// If we have valid latency data, estimate TPS as requests per second
|
|
84
87
|
return avgLatency > 0 ? requestCount / (latencyCount * avgLatency) : 0;
|
|
85
88
|
}
|
|
86
|
-
|
|
89
|
+
|
|
87
90
|
// Fallback: just return a small fraction of the total request count
|
|
88
91
|
return requestCount > 0 ? requestCount / 100 : 0;
|
|
89
92
|
}
|
|
90
93
|
|
|
91
94
|
/**
|
|
92
95
|
* Calculate peak TPS from Prometheus metrics
|
|
93
|
-
*
|
|
96
|
+
*
|
|
94
97
|
* @param text - Prometheus-formatted metrics text
|
|
95
98
|
* @returns Calculated peak TPS value
|
|
96
99
|
*/
|
|
97
100
|
function calculatePeakTps(text: string): number {
|
|
98
101
|
// For peak TPS, we'll use a simple heuristic: 2x the current TPS estimate
|
|
102
|
+
// TODO: fix this
|
|
99
103
|
return calculateTps(text) * 2;
|
|
100
104
|
}
|
|
101
105
|
|
|
102
106
|
/**
|
|
103
107
|
* Extract latency from Prometheus metrics
|
|
104
|
-
*
|
|
108
|
+
*
|
|
105
109
|
* @param text - Prometheus-formatted metrics text
|
|
106
110
|
* @returns Average latency in seconds
|
|
107
111
|
*/
|
|
108
112
|
function extractLatency(text: string): number {
|
|
113
|
+
// TODO: fix - using average for latency is kinda wack. ok to merge for now but should change.
|
|
109
114
|
const latencySum = extractSummaryValue(text, "nord_requests_ok_latency_sum");
|
|
110
|
-
const latencyCount = extractSummaryValue(
|
|
111
|
-
|
|
115
|
+
const latencyCount = extractSummaryValue(
|
|
116
|
+
text,
|
|
117
|
+
"nord_requests_ok_latency_count",
|
|
118
|
+
);
|
|
119
|
+
|
|
112
120
|
return latencyCount > 0 ? latencySum / latencyCount : 0;
|
|
113
121
|
}
|
|
114
122
|
|
|
115
123
|
/**
|
|
116
124
|
* Extract a summary value from Prometheus-formatted text
|
|
117
|
-
*
|
|
125
|
+
*
|
|
118
126
|
* @param text - Prometheus-formatted metrics text
|
|
119
127
|
* @param metricName - Name of the metric to extract
|
|
120
128
|
* @returns The metric value as a number, or 0 if not found
|
|
121
129
|
*/
|
|
122
130
|
function extractSummaryValue(text: string, metricName: string): number {
|
|
123
|
-
const regex = new RegExp(`^${metricName}\\s+([\\d.]+)`,
|
|
131
|
+
const regex = new RegExp(`^${metricName}\\s+([\\d.]+)`, "m");
|
|
124
132
|
const match = text.match(regex);
|
|
125
133
|
return match ? parseFloat(match[1]) : 0;
|
|
126
134
|
}
|
|
@@ -235,21 +243,21 @@ export async function queryPrometheus(
|
|
|
235
243
|
const response = await checkedFetch(
|
|
236
244
|
`${webServerUrl}/prometheus?query=${encodeURIComponent(params)}`,
|
|
237
245
|
);
|
|
238
|
-
|
|
246
|
+
|
|
239
247
|
// Handle raw text response
|
|
240
248
|
const text = await response.text();
|
|
241
249
|
try {
|
|
242
250
|
// Try to parse as JSON first
|
|
243
251
|
const data = JSON.parse(text);
|
|
244
252
|
return data.data.result[0]?.value[1] || 0;
|
|
245
|
-
} catch (
|
|
246
|
-
|
|
253
|
+
} catch (error) {
|
|
254
|
+
console.log("Prometheus query failed:", error);
|
|
247
255
|
// Try to find a number in the response
|
|
248
256
|
const numberMatch = text.match(/[\d.]+/);
|
|
249
257
|
if (numberMatch) {
|
|
250
258
|
return parseFloat(numberMatch[0]);
|
|
251
259
|
}
|
|
252
|
-
|
|
260
|
+
|
|
253
261
|
// Return 0 if no number is found
|
|
254
262
|
return 0;
|
|
255
263
|
}
|
package/src/nord/api/queries.ts
CHANGED
|
@@ -2,17 +2,12 @@ import {
|
|
|
2
2
|
ActionQuery,
|
|
3
3
|
ActionResponse,
|
|
4
4
|
ActionsResponse,
|
|
5
|
-
BlockQuery,
|
|
6
|
-
BlockResponse,
|
|
7
|
-
BlockSummaryResponse,
|
|
8
5
|
RollmanActionResponse,
|
|
9
6
|
RollmanActionsResponse,
|
|
10
|
-
RollmanBlockResponse,
|
|
11
7
|
} from "../../types";
|
|
12
8
|
import { checkedFetch } from "../../utils";
|
|
13
9
|
import { NordError } from "../utils/NordError";
|
|
14
10
|
|
|
15
|
-
|
|
16
11
|
/**
|
|
17
12
|
* Query a specific action
|
|
18
13
|
*
|
|
@@ -60,9 +55,12 @@ export async function queryRecentActions(
|
|
|
60
55
|
);
|
|
61
56
|
return await response.json();
|
|
62
57
|
} catch (error) {
|
|
63
|
-
throw new NordError(
|
|
64
|
-
|
|
65
|
-
|
|
58
|
+
throw new NordError(
|
|
59
|
+
`Failed to query recent actions (from ${from} to ${to})`,
|
|
60
|
+
{
|
|
61
|
+
cause: error,
|
|
62
|
+
},
|
|
63
|
+
);
|
|
66
64
|
}
|
|
67
65
|
}
|
|
68
66
|
|
|
@@ -73,9 +71,7 @@ export async function queryRecentActions(
|
|
|
73
71
|
* @returns Last action ID
|
|
74
72
|
* @throws {NordError} If the request fails
|
|
75
73
|
*/
|
|
76
|
-
export async function getLastActionId(
|
|
77
|
-
webServerUrl: string,
|
|
78
|
-
): Promise<number> {
|
|
74
|
+
export async function getLastActionId(webServerUrl: string): Promise<number> {
|
|
79
75
|
try {
|
|
80
76
|
const response = await checkedFetch(`${webServerUrl}/last_actionid`);
|
|
81
77
|
const data = await response.json();
|