@n1xyz/nord-ts 0.0.1 → 0.0.5
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/README.md +148 -65
- 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/const.d.ts +2 -5
- package/dist/const.js +2 -6
- package/dist/gen/common.d.ts +6 -1
- package/dist/gen/common.js +19 -9
- package/dist/gen/nord.d.ts +76 -21
- 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 +5 -2
- package/dist/index.js +18 -2
- package/dist/nord/{actions.d.ts → api/actions.d.ts} +6 -6
- package/dist/nord/{actions.js → api/actions.js} +6 -10
- package/dist/nord/api/core.d.ts +49 -0
- package/dist/nord/api/core.js +121 -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/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 +697 -0
- package/dist/nord/index.d.ts +9 -2
- package/dist/nord/index.js +30 -6
- package/dist/nord/models/Subscriber.d.ts +37 -0
- package/dist/nord/models/Subscriber.js +25 -0
- package/dist/nord/utils/NordError.d.ts +35 -0
- package/dist/nord/utils/NordError.js +46 -0
- package/dist/types.d.ts +143 -86
- package/dist/types.js +12 -1
- package/dist/utils.d.ts +9 -0
- package/dist/utils.js +20 -1
- package/dist/websocket/NordWebSocketClient.d.ts +71 -0
- package/dist/websocket/NordWebSocketClient.js +343 -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/docs/assets/hierarchy.js +1 -0
- package/docs/assets/highlight.css +16 -16
- package/docs/assets/icons.js +17 -14
- package/docs/assets/icons.svg +1 -1
- package/docs/assets/main.js +5 -4
- package/docs/assets/navigation.js +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/assets/style.css +1423 -1227
- package/docs/classes/Nord.html +189 -43
- package/docs/classes/NordError.html +24 -0
- package/docs/classes/NordUser.html +120 -35
- package/docs/classes/NordWebSocketClient.html +335 -0
- package/docs/classes/SolanaBridgeClient.html +86 -0
- package/docs/classes/Subscriber.html +10 -6
- package/docs/enums/FillMode.html +5 -5
- package/docs/enums/KeyType.html +4 -4
- package/docs/enums/MetricPeriod.html +9 -0
- package/docs/enums/PdaSeedType.html +9 -0
- package/docs/enums/PeakTpsPeriodUnit.html +7 -7
- package/docs/enums/Side.html +3 -3
- package/docs/enums/WebSocketMessageType.html +7 -0
- package/docs/functions/actionQueryRollman.html +6 -0
- package/docs/functions/actionsQueryRollman.html +6 -0
- package/docs/functions/aggregateMetrics-1.html +7 -0
- package/docs/functions/assert.html +1 -1
- package/docs/functions/bigIntToProtoU128.html +3 -3
- package/docs/functions/blockQueryRollman.html +6 -0
- package/docs/functions/blockSummaryQueryRollman.html +6 -0
- package/docs/functions/bridgeToBN.html +5 -0
- package/docs/functions/bufferToHex.html +4 -0
- package/docs/functions/cancelOrder.html +1 -0
- package/docs/functions/checkPubKeyLength.html +1 -1
- package/docs/functions/checkedFetch.html +4 -4
- package/docs/functions/createSession.html +1 -0
- package/docs/functions/decodeLengthDelimited.html +7 -6
- package/docs/functions/encodeLengthDelimited.html +4 -4
- package/docs/functions/fillModeToProtoFillMode.html +4 -4
- package/docs/functions/findMarket.html +1 -1
- package/docs/functions/findPda.html +6 -0
- package/docs/functions/findToken.html +1 -1
- package/docs/functions/fromBN.html +5 -0
- package/docs/functions/getAccount.html +6 -0
- package/docs/functions/getActionNonce.html +5 -0
- package/docs/functions/getCurrentTps.html +6 -0
- package/docs/functions/getInfo.html +5 -0
- package/docs/functions/getMedianLatency.html +6 -0
- package/docs/functions/getOrderbook.html +6 -0
- package/docs/functions/getPeakTps.html +6 -0
- package/docs/functions/getTimestamp.html +5 -0
- package/docs/functions/getTotalTransactions.html +5 -0
- package/docs/functions/getTrades.html +6 -0
- package/docs/functions/getUserAccountIds.html +6 -0
- package/docs/functions/hexToBuffer.html +4 -0
- package/docs/functions/initWebSocketClient.html +12 -0
- package/docs/functions/keypairFromPrivateKey.html +4 -0
- package/docs/functions/makeSigningFunction.html +4 -0
- package/docs/functions/makeWalletSignFn.html +5 -5
- package/docs/functions/marketsStats.html +5 -0
- package/docs/functions/optExpect.html +4 -4
- package/docs/functions/optMap.html +5 -5
- package/docs/functions/optUnwrap.html +2 -2
- package/docs/functions/panic.html +1 -1
- package/docs/functions/placeOrder.html +1 -0
- package/docs/functions/queryAction.html +6 -0
- package/docs/functions/queryBlock.html +6 -0
- package/docs/functions/queryLastNBlocks.html +5 -0
- package/docs/functions/queryPrometheus.html +6 -0
- package/docs/functions/queryRecentActions.html +6 -0
- package/docs/functions/queryRecentBlocks.html +6 -0
- package/docs/functions/revokeSession.html +1 -0
- package/docs/functions/shortenPublicKey.html +5 -0
- package/docs/functions/signAction.html +2 -2
- package/docs/functions/toBN.html +5 -0
- package/docs/functions/toScaledU128.html +5 -5
- package/docs/functions/toScaledU64.html +5 -5
- package/docs/functions/transfer.html +1 -0
- package/docs/functions/withdraw.html +1 -0
- package/docs/hierarchy.html +1 -0
- package/docs/index.html +39 -20
- package/docs/interfaces/Account.html +8 -8
- package/docs/interfaces/ActionInfo.html +8 -8
- package/docs/interfaces/ActionNonceResponse.html +3 -0
- package/docs/interfaces/ActionQuery.html +4 -4
- package/docs/interfaces/ActionResponse.html +8 -8
- package/docs/interfaces/ActionsExtendedInfo.html +10 -10
- package/docs/interfaces/ActionsQuery.html +5 -5
- package/docs/interfaces/ActionsResponse.html +6 -6
- package/docs/interfaces/AggregateMetrics.html +12 -12
- package/docs/interfaces/BlockFacts.html +10 -0
- package/docs/interfaces/BlockQuery.html +6 -6
- package/docs/interfaces/BlockResponse.html +6 -6
- package/docs/interfaces/BlockSummary.html +8 -8
- package/docs/interfaces/BlockSummaryResponse.html +6 -6
- package/docs/interfaces/DeltaEvent.html +6 -6
- package/docs/interfaces/DepositSplParams.html +10 -0
- package/docs/interfaces/Info.html +3 -3
- package/docs/interfaces/Market.html +8 -6
- package/docs/interfaces/MarketStats.html +7 -7
- package/docs/interfaces/MarketsStatsResponse.html +2 -2
- package/docs/interfaces/NordConfig.html +14 -5
- package/docs/interfaces/NordWebSocketClientEvents.html +4 -0
- package/docs/interfaces/NordWebSocketEvents.html +8 -0
- package/docs/interfaces/Order.html +6 -6
- package/docs/interfaces/OrderInfo.html +6 -6
- package/docs/interfaces/OrderbookEntry.html +4 -0
- package/docs/interfaces/OrderbookQuery.html +6 -0
- package/docs/interfaces/OrderbookResponse.html +6 -10
- package/docs/interfaces/OrderbookSubscription.html +159 -0
- package/docs/interfaces/PerpMarketStats.html +5 -5
- package/docs/interfaces/RollmanActionExtendedInfo.html +4 -4
- package/docs/interfaces/RollmanActionInfo.html +4 -4
- package/docs/interfaces/RollmanActionResponse.html +4 -4
- package/docs/interfaces/RollmanActionsResponse.html +2 -2
- package/docs/interfaces/RollmanBlockResponse.html +3 -3
- package/docs/interfaces/SPLTokenInfo.html +10 -0
- package/docs/interfaces/SolanaBridgeConfig.html +10 -0
- package/docs/interfaces/StateFacts.html +10 -0
- package/docs/interfaces/SubscriberConfig.html +3 -3
- package/docs/interfaces/TimestampResponse.html +3 -0
- package/docs/interfaces/Token.html +5 -5
- package/docs/interfaces/TokenInfo.html +5 -0
- package/docs/interfaces/Trade.html +5 -5
- package/docs/interfaces/TradeSubscription.html +159 -0
- package/docs/interfaces/Trades.html +5 -5
- package/docs/interfaces/TradesQuery.html +6 -0
- package/docs/interfaces/TradesResponse.html +7 -12
- package/docs/interfaces/TransferParams.html +8 -0
- package/docs/interfaces/UserAccountIdsQuery.html +3 -0
- package/docs/interfaces/UserAccountIdsResponse.html +3 -0
- package/docs/interfaces/WebSocketDeltaUpdate.html +9 -0
- package/docs/interfaces/WebSocketSubscription.html +4 -0
- package/docs/interfaces/WebSocketTradeUpdate.html +6 -0
- package/docs/interfaces/WebSocketUserUpdate.html +6 -0
- package/docs/interfaces/WithdrawalClaim.html +14 -0
- package/docs/interfaces/WithdrawalParams.html +8 -0
- package/docs/modules.html +1 -77
- package/docs/types/BigIntValue.html +2 -2
- package/docs/types/WebSocketMessage.html +1 -0
- 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 +2 -0
- package/eslint.config.mjs +66 -0
- package/package.json +20 -23
- 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 +4 -9
- package/src/gen/common.ts +27 -10
- package/src/gen/nord.ts +1045 -487
- package/src/idl/bridge.ts +702 -0
- package/src/index.ts +21 -2
- package/src/nord/{actions.ts → api/actions.ts} +12 -16
- 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 +1101 -0
- package/src/nord/index.ts +16 -2
- package/src/nord/models/Subscriber.ts +56 -0
- package/src/nord/utils/NordError.ts +72 -0
- package/src/types.ts +163 -92
- package/src/utils.ts +22 -1
- package/src/websocket/NordWebSocketClient.ts +432 -0
- package/src/websocket/events.ts +31 -0
- package/src/websocket/index.ts +2 -0
- package/tsconfig.eslint.json +12 -0
- package/.eslintignore +0 -1
- package/.eslintrc.js +0 -20
- package/dist/abis/ERC20_ABI.d.ts +0 -39
- package/dist/abis/ERC20_ABI.js +0 -313
- package/dist/abis/NORD_GETTERS_FACET_ABI.d.ts +0 -34
- package/dist/abis/NORD_GETTERS_FACET_ABI.js +0 -195
- package/dist/abis/NORD_RAMP_FACET_ABI.d.ts +0 -35
- package/dist/abis/NORD_RAMP_FACET_ABI.js +0 -144
- package/dist/abis/index.d.ts +0 -3
- package/dist/abis/index.js +0 -9
- package/dist/nord/Nord.d.ts +0 -76
- package/dist/nord/Nord.js +0 -376
- package/dist/nord/NordImpl.d.ts +0 -7
- package/dist/nord/NordImpl.js +0 -6
- package/dist/nord/NordUser.d.ts +0 -77
- package/dist/nord/NordUser.js +0 -249
- package/docs/functions/createWebSocketSubscription.html +0 -12
- package/docs/interfaces/ERC20TokenInfo.html +0 -5
- package/docs/interfaces/OrderbookOrder.html +0 -6
- package/docs/interfaces/TradeInfo.html +0 -20
- package/docs/interfaces/TradesQueryParams.html +0 -10
- package/docs/variables/DEV_CONTRACT_ADDRESS.html +0 -1
- package/docs/variables/ERC20_ABI.html +0 -1
- package/docs/variables/EVM_DEV_URL.html +0 -1
- package/docs/variables/FAUCET_PRIVATE_ADDRESS.html +0 -1
- package/docs/variables/NORD_GETTERS_FACET_ABI.html +0 -1
- package/docs/variables/NORD_RAMP_FACET_ABI.html +0 -1
- 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
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import Decimal from "decimal.js";
|
|
2
|
+
import * as proto from "../../gen/nord";
|
|
3
|
+
import { FillMode, fillModeToProtoFillMode, KeyType, Side } from "../../types";
|
|
3
4
|
import {
|
|
4
5
|
assert,
|
|
5
6
|
bigIntToProtoU128,
|
|
@@ -12,9 +13,7 @@ import {
|
|
|
12
13
|
SESSION_TTL,
|
|
13
14
|
toScaledU128,
|
|
14
15
|
toScaledU64,
|
|
15
|
-
} from "
|
|
16
|
-
import * as proto from "../gen/nord";
|
|
17
|
-
import Decimal from "decimal.js";
|
|
16
|
+
} from "../../utils";
|
|
18
17
|
|
|
19
18
|
async function sessionSign(
|
|
20
19
|
signFn: (message: Uint8Array) => Promise<Uint8Array>,
|
|
@@ -25,14 +24,11 @@ async function sessionSign(
|
|
|
25
24
|
}
|
|
26
25
|
|
|
27
26
|
async function walletSign(
|
|
28
|
-
walletSignFn: (message: string | Uint8Array) => Promise<
|
|
27
|
+
walletSignFn: (message: string | Uint8Array) => Promise<Uint8Array>,
|
|
29
28
|
message: Uint8Array,
|
|
30
29
|
): Promise<Uint8Array> {
|
|
31
30
|
const signature = await walletSignFn(message);
|
|
32
|
-
return new Uint8Array([
|
|
33
|
-
...message,
|
|
34
|
-
...ethers.getBytes(signature.slice(0, -2)),
|
|
35
|
-
]);
|
|
31
|
+
return new Uint8Array([...message, ...signature]);
|
|
36
32
|
}
|
|
37
33
|
|
|
38
34
|
function makeSendHttp(
|
|
@@ -72,7 +68,7 @@ async function sendAction(
|
|
|
72
68
|
|
|
73
69
|
async function createSessionImpl(
|
|
74
70
|
sendFn: (encoded: Uint8Array) => Promise<Uint8Array>,
|
|
75
|
-
walletSignFn: (message: string | Uint8Array) => Promise<
|
|
71
|
+
walletSignFn: (message: string | Uint8Array) => Promise<Uint8Array>,
|
|
76
72
|
currentTimestamp: bigint,
|
|
77
73
|
nonce: number,
|
|
78
74
|
params: {
|
|
@@ -82,7 +78,7 @@ async function createSessionImpl(
|
|
|
82
78
|
expiryTimestamp?: bigint;
|
|
83
79
|
},
|
|
84
80
|
): Promise<bigint> {
|
|
85
|
-
checkPubKeyLength(KeyType.
|
|
81
|
+
checkPubKeyLength(KeyType.Ed25519, params.userPubkey.length);
|
|
86
82
|
checkPubKeyLength(KeyType.Ed25519, params.sessionPubkey.length);
|
|
87
83
|
|
|
88
84
|
let expiry = 0n;
|
|
@@ -126,7 +122,7 @@ async function createSessionImpl(
|
|
|
126
122
|
|
|
127
123
|
export async function createSession(
|
|
128
124
|
serverUrl: string,
|
|
129
|
-
walletSignFn: (message: string | Uint8Array) => Promise<
|
|
125
|
+
walletSignFn: (message: string | Uint8Array) => Promise<Uint8Array>,
|
|
130
126
|
currentTimestamp: bigint,
|
|
131
127
|
nonce: number,
|
|
132
128
|
params: {
|
|
@@ -147,7 +143,7 @@ export async function createSession(
|
|
|
147
143
|
|
|
148
144
|
async function revokeSessionImpl(
|
|
149
145
|
sendFn: (encoded: Uint8Array) => Promise<Uint8Array>,
|
|
150
|
-
walletSignFn: (message: string | Uint8Array) => Promise<
|
|
146
|
+
walletSignFn: (message: string | Uint8Array) => Promise<Uint8Array>,
|
|
151
147
|
currentTimestamp: bigint,
|
|
152
148
|
nonce: number,
|
|
153
149
|
params: {
|
|
@@ -175,7 +171,7 @@ async function revokeSessionImpl(
|
|
|
175
171
|
|
|
176
172
|
export async function revokeSession(
|
|
177
173
|
serverUrl: string,
|
|
178
|
-
walletSignFn: (message: string | Uint8Array) => Promise<
|
|
174
|
+
walletSignFn: (message: string | Uint8Array) => Promise<Uint8Array>,
|
|
179
175
|
currentTimestamp: bigint,
|
|
180
176
|
nonce: number,
|
|
181
177
|
params: {
|
|
@@ -440,7 +436,7 @@ async function transferImpl(
|
|
|
440
436
|
|
|
441
437
|
if (resp.kind?.$case === "transferred") {
|
|
442
438
|
if (resp.kind.value.accountCreated) {
|
|
443
|
-
return resp.kind.value.
|
|
439
|
+
return resp.kind.value.toUserAccount;
|
|
444
440
|
} else {
|
|
445
441
|
return undefined;
|
|
446
442
|
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { Account, Info } from "../../types";
|
|
2
|
+
import { checkedFetch } from "../../utils";
|
|
3
|
+
import { NordWebSocketClient } from "../../websocket/index";
|
|
4
|
+
import { NordError } from "../utils/NordError";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Get the current timestamp from the Nord server
|
|
8
|
+
*
|
|
9
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
10
|
+
* @returns Current timestamp as a bigint
|
|
11
|
+
* @throws {NordError} If the request fails
|
|
12
|
+
*/
|
|
13
|
+
export async function getTimestamp(webServerUrl: string): Promise<bigint> {
|
|
14
|
+
try {
|
|
15
|
+
const response = await checkedFetch(`${webServerUrl}/timestamp`);
|
|
16
|
+
return BigInt(await response.json());
|
|
17
|
+
} catch (error) {
|
|
18
|
+
throw new NordError("Failed to get timestamp", { cause: error });
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Get the next action nonce from the Nord server
|
|
24
|
+
*
|
|
25
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
26
|
+
* @returns Next action nonce
|
|
27
|
+
* @throws {NordError} If the request fails
|
|
28
|
+
*/
|
|
29
|
+
export async function getActionNonce(webServerUrl: string): Promise<number> {
|
|
30
|
+
try {
|
|
31
|
+
const response = await checkedFetch(`${webServerUrl}/action_nonce`);
|
|
32
|
+
const data = await response.json();
|
|
33
|
+
return data.nonce;
|
|
34
|
+
} catch (error) {
|
|
35
|
+
throw new NordError("Failed to get action nonce", { cause: error });
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Get information about the Nord server
|
|
41
|
+
*
|
|
42
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
43
|
+
* @returns Information about markets and tokens
|
|
44
|
+
* @throws {NordError} If the request fails
|
|
45
|
+
*/
|
|
46
|
+
export async function getInfo(webServerUrl: string): Promise<Info> {
|
|
47
|
+
try {
|
|
48
|
+
const response = await checkedFetch(`${webServerUrl}/info`);
|
|
49
|
+
return await response.json();
|
|
50
|
+
} catch (error) {
|
|
51
|
+
throw new NordError("Failed to get info", { cause: error });
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Get account information
|
|
57
|
+
*
|
|
58
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
59
|
+
* @param accountId - Account ID to get information for
|
|
60
|
+
* @returns Account information
|
|
61
|
+
* @throws {NordError} If the request fails
|
|
62
|
+
*/
|
|
63
|
+
export async function getAccount(
|
|
64
|
+
webServerUrl: string,
|
|
65
|
+
accountId: number,
|
|
66
|
+
): Promise<Account> {
|
|
67
|
+
try {
|
|
68
|
+
const response = await checkedFetch(
|
|
69
|
+
`${webServerUrl}/account?account_id=${accountId}`,
|
|
70
|
+
);
|
|
71
|
+
return await response.json();
|
|
72
|
+
} catch (error) {
|
|
73
|
+
throw new NordError(`Failed to get account ${accountId}`, { cause: error });
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Initialize a WebSocket client for Nord
|
|
79
|
+
*
|
|
80
|
+
* Connects to one of the specific Nord WebSocket endpoints:
|
|
81
|
+
* - /ws/trades - For trade updates (default)
|
|
82
|
+
* - /ws/deltas - For orderbook delta updates
|
|
83
|
+
* - /ws/user - For user-specific updates
|
|
84
|
+
*
|
|
85
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
86
|
+
* @param endpoint - Specific WebSocket endpoint to connect to (trades, deltas, or user)
|
|
87
|
+
* @param initialSubscriptions - Optional array of initial subscriptions (e.g., ["trades@BTCUSDC"])
|
|
88
|
+
* @returns WebSocket client
|
|
89
|
+
*/
|
|
90
|
+
export function initWebSocketClient(
|
|
91
|
+
webServerUrl: string,
|
|
92
|
+
endpoint?: "trades" | "deltas" | "user",
|
|
93
|
+
initialSubscriptions?: string[],
|
|
94
|
+
): NordWebSocketClient {
|
|
95
|
+
try {
|
|
96
|
+
// Convert HTTP URL to WebSocket URL with specific endpoint
|
|
97
|
+
// If no specific endpoint is provided, we'll connect to trades by default
|
|
98
|
+
const specificEndpoint = endpoint || "trades";
|
|
99
|
+
const wsUrl =
|
|
100
|
+
webServerUrl.replace(/^http/, "ws") + `/ws/${specificEndpoint}`;
|
|
101
|
+
console.log(`Initializing WebSocket client with URL: ${wsUrl}`);
|
|
102
|
+
|
|
103
|
+
// Create and connect the WebSocket client
|
|
104
|
+
const ws = new NordWebSocketClient(wsUrl);
|
|
105
|
+
|
|
106
|
+
// Add error handler
|
|
107
|
+
ws.on("error", (error) => {
|
|
108
|
+
console.error("Nord WebSocket error:", error);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// Add connected handler for debugging
|
|
112
|
+
ws.on("connected", () => {
|
|
113
|
+
console.log("Nord WebSocket connected successfully");
|
|
114
|
+
|
|
115
|
+
// Subscribe to initial subscriptions if provided
|
|
116
|
+
if (initialSubscriptions && initialSubscriptions.length > 0) {
|
|
117
|
+
ws.subscribe(initialSubscriptions);
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
// Connect the WebSocket
|
|
122
|
+
ws.connect();
|
|
123
|
+
return ws;
|
|
124
|
+
} catch (error) {
|
|
125
|
+
console.error("Failed to initialize WebSocket client:", error);
|
|
126
|
+
throw new NordError("Failed to initialize WebSocket client", {
|
|
127
|
+
cause: error,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MarketsStatsResponse,
|
|
3
|
+
OrderbookQuery,
|
|
4
|
+
OrderbookResponse,
|
|
5
|
+
TradesQuery,
|
|
6
|
+
TradesResponse,
|
|
7
|
+
UserAccountIdsQuery,
|
|
8
|
+
UserAccountIdsResponse,
|
|
9
|
+
} from "../../types";
|
|
10
|
+
import { checkedFetch } from "../../utils";
|
|
11
|
+
import { NordError } from "../utils/NordError";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Get market statistics
|
|
15
|
+
*
|
|
16
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
17
|
+
* @returns Market statistics response
|
|
18
|
+
* @throws {NordError} If the request fails
|
|
19
|
+
*/
|
|
20
|
+
export async function marketsStats(
|
|
21
|
+
webServerUrl: string,
|
|
22
|
+
): Promise<MarketsStatsResponse> {
|
|
23
|
+
try {
|
|
24
|
+
const response = await checkedFetch(`${webServerUrl}/stats`);
|
|
25
|
+
return await response.json();
|
|
26
|
+
} catch (error) {
|
|
27
|
+
throw new NordError("Failed to fetch markets stats", { cause: error });
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Get trades for a market
|
|
33
|
+
*
|
|
34
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
35
|
+
* @param query - Trades query parameters
|
|
36
|
+
* @returns Trades response
|
|
37
|
+
* @throws {NordError} If the request fails
|
|
38
|
+
*/
|
|
39
|
+
export async function getTrades(
|
|
40
|
+
webServerUrl: string,
|
|
41
|
+
query: TradesQuery,
|
|
42
|
+
): Promise<TradesResponse> {
|
|
43
|
+
try {
|
|
44
|
+
const params = new URLSearchParams();
|
|
45
|
+
params.append("accountId", query.accountId.toString());
|
|
46
|
+
|
|
47
|
+
if (query.since) {
|
|
48
|
+
params.append("since", query.since);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (query.until) {
|
|
52
|
+
params.append("until", query.until);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (query.pageId) {
|
|
56
|
+
params.append("pageId", query.pageId);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const response = await checkedFetch(
|
|
60
|
+
`${webServerUrl}/trades?${params.toString()}`,
|
|
61
|
+
);
|
|
62
|
+
return await response.json();
|
|
63
|
+
} catch (error) {
|
|
64
|
+
throw new NordError("Failed to get trades", { cause: error });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Get user account IDs
|
|
70
|
+
*
|
|
71
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
72
|
+
* @param query - User account IDs query parameters
|
|
73
|
+
* @returns User account IDs response
|
|
74
|
+
* @throws {NordError} If the request fails
|
|
75
|
+
*/
|
|
76
|
+
export async function getUserAccountIds(
|
|
77
|
+
webServerUrl: string,
|
|
78
|
+
query: UserAccountIdsQuery,
|
|
79
|
+
): Promise<UserAccountIdsResponse> {
|
|
80
|
+
try {
|
|
81
|
+
const params = new URLSearchParams();
|
|
82
|
+
params.append("pubkey", query.pubkey);
|
|
83
|
+
|
|
84
|
+
const response = await checkedFetch(
|
|
85
|
+
`${webServerUrl}/user?${params.toString()}`,
|
|
86
|
+
);
|
|
87
|
+
return await response.json();
|
|
88
|
+
} catch (error) {
|
|
89
|
+
throw new NordError("Failed to get user account IDs", { cause: error });
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Get orderbook for a market
|
|
95
|
+
*
|
|
96
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
97
|
+
* @param query - Orderbook query parameters
|
|
98
|
+
* @returns Orderbook response
|
|
99
|
+
* @throws {NordError} If the request fails
|
|
100
|
+
*/
|
|
101
|
+
export async function getOrderbook(
|
|
102
|
+
webServerUrl: string,
|
|
103
|
+
query: OrderbookQuery,
|
|
104
|
+
): Promise<OrderbookResponse> {
|
|
105
|
+
try {
|
|
106
|
+
const params = new URLSearchParams();
|
|
107
|
+
|
|
108
|
+
if (query.symbol) {
|
|
109
|
+
params.append("symbol", query.symbol);
|
|
110
|
+
} else if (query.market_id !== undefined) {
|
|
111
|
+
params.append("market_id", query.market_id.toString());
|
|
112
|
+
} else {
|
|
113
|
+
throw new NordError(
|
|
114
|
+
"Either symbol or market_id must be provided for orderbook query",
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const response = await checkedFetch(
|
|
119
|
+
`${webServerUrl}/orderbook?${params.toString()}`,
|
|
120
|
+
);
|
|
121
|
+
return await response.json();
|
|
122
|
+
} catch (error) {
|
|
123
|
+
throw new NordError("Failed to get orderbook", { cause: error });
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
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
|
+
return await response.json();
|
|
37
|
+
} catch (error) {
|
|
38
|
+
throw new NordError("Failed to fetch aggregate metrics", { cause: error });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Get current transactions per second
|
|
44
|
+
*
|
|
45
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
46
|
+
* @param period - Time period for the query
|
|
47
|
+
* @returns Current TPS value
|
|
48
|
+
* @throws {NordError} If the request fails
|
|
49
|
+
*/
|
|
50
|
+
export async function getCurrentTps(
|
|
51
|
+
webServerUrl: string,
|
|
52
|
+
period: string = "1m",
|
|
53
|
+
): Promise<number> {
|
|
54
|
+
try {
|
|
55
|
+
return await queryPrometheus(
|
|
56
|
+
webServerUrl,
|
|
57
|
+
`sum(rate(nord_tx_count[${period}]))`,
|
|
58
|
+
);
|
|
59
|
+
} catch (error) {
|
|
60
|
+
throw new NordError(`Failed to get current TPS for period ${period}`, {
|
|
61
|
+
cause: error,
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Get peak transactions per second
|
|
68
|
+
*
|
|
69
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
70
|
+
* @param period - Time period for the query
|
|
71
|
+
* @returns Peak TPS value
|
|
72
|
+
* @throws {NordError} If the request fails
|
|
73
|
+
*/
|
|
74
|
+
export async function getPeakTps(
|
|
75
|
+
webServerUrl: string,
|
|
76
|
+
period: string = "24h",
|
|
77
|
+
): Promise<number> {
|
|
78
|
+
try {
|
|
79
|
+
return await queryPrometheus(
|
|
80
|
+
webServerUrl,
|
|
81
|
+
`max_over_time(sum(rate(nord_tx_count[1m]))[${period}:])`,
|
|
82
|
+
);
|
|
83
|
+
} catch (error) {
|
|
84
|
+
throw new NordError(`Failed to get peak TPS for period ${period}`, {
|
|
85
|
+
cause: error,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Get median transaction latency
|
|
92
|
+
*
|
|
93
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
94
|
+
* @param period - Time period for the query
|
|
95
|
+
* @returns Median latency in milliseconds
|
|
96
|
+
* @throws {NordError} If the request fails
|
|
97
|
+
*/
|
|
98
|
+
export async function getMedianLatency(
|
|
99
|
+
webServerUrl: string,
|
|
100
|
+
period: string = "1m",
|
|
101
|
+
): Promise<number> {
|
|
102
|
+
try {
|
|
103
|
+
return await queryPrometheus(
|
|
104
|
+
webServerUrl,
|
|
105
|
+
`quantile_over_time(0.5, nord_tx_latency_ms[${period}])`,
|
|
106
|
+
);
|
|
107
|
+
} catch (error) {
|
|
108
|
+
throw new NordError(`Failed to get median latency for period ${period}`, {
|
|
109
|
+
cause: error,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Get total transaction count
|
|
116
|
+
*
|
|
117
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
118
|
+
* @returns Total transaction count
|
|
119
|
+
* @throws {NordError} If the request fails
|
|
120
|
+
*/
|
|
121
|
+
export async function getTotalTransactions(
|
|
122
|
+
webServerUrl: string,
|
|
123
|
+
): Promise<number> {
|
|
124
|
+
try {
|
|
125
|
+
return await queryPrometheus(webServerUrl, "sum(nord_tx_count)");
|
|
126
|
+
} catch (error) {
|
|
127
|
+
throw new NordError("Failed to get total transactions", { cause: error });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Query Prometheus metrics
|
|
133
|
+
*
|
|
134
|
+
* @param webServerUrl - Base URL for the Nord web server
|
|
135
|
+
* @param params - Prometheus query parameters
|
|
136
|
+
* @returns Query result as a number
|
|
137
|
+
* @throws {NordError} If the request fails
|
|
138
|
+
*/
|
|
139
|
+
export async function queryPrometheus(
|
|
140
|
+
webServerUrl: string,
|
|
141
|
+
params: string,
|
|
142
|
+
): Promise<number> {
|
|
143
|
+
try {
|
|
144
|
+
const response = await checkedFetch(
|
|
145
|
+
`${webServerUrl}/prometheus?query=${encodeURIComponent(params)}`,
|
|
146
|
+
);
|
|
147
|
+
const data = await response.json();
|
|
148
|
+
return data.data.result[0]?.value[1] || 0;
|
|
149
|
+
} catch (error) {
|
|
150
|
+
throw new NordError(`Failed to query Prometheus: ${params}`, {
|
|
151
|
+
cause: error,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
}
|