@n1xyz/nord-ts 0.0.4 → 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/dist/bridge/client.d.ts +4 -4
- package/dist/bridge/client.js +4 -4
- package/dist/bridge/const.d.ts +2 -2
- package/dist/bridge/const.js +22 -22
- package/dist/bridge/index.d.ts +4 -4
- package/dist/bridge/types.d.ts +3 -3
- package/dist/bridge/utils.d.ts +4 -4
- package/dist/bridge/utils.js +10 -10
- package/dist/const.d.ts +1 -1
- package/dist/const.js +17 -17
- package/dist/gen/nord.d.ts +1 -4
- package/dist/idl/bridge.d.ts +1 -1
- package/dist/idl/bridge.js +185 -185
- package/dist/index.d.ts +8 -8
- package/dist/nord/api/actions.d.ts +3 -3
- package/dist/nord/api/actions.js +21 -21
- package/dist/nord/api/core.d.ts +3 -3
- package/dist/nord/api/core.js +12 -12
- package/dist/nord/api/market.d.ts +1 -1
- package/dist/nord/api/market.js +12 -12
- package/dist/nord/api/metrics.d.ts +1 -1
- package/dist/nord/api/metrics.js +6 -6
- package/dist/nord/api/queries.d.ts +1 -1
- package/dist/nord/api/queries.js +9 -9
- package/dist/nord/client/Nord.d.ts +4 -4
- package/dist/nord/client/Nord.js +18 -18
- package/dist/nord/client/NordUser.d.ts +7 -7
- package/dist/nord/client/NordUser.js +45 -49
- package/dist/nord/index.d.ts +9 -9
- package/dist/nord/models/Subscriber.d.ts +6 -6
- package/dist/nord/models/Subscriber.js +1 -5
- package/dist/nord/utils/NordError.js +2 -2
- package/dist/types.d.ts +1 -1
- package/dist/types.js +1 -1
- package/dist/utils.d.ts +6 -6
- package/dist/utils.js +8 -8
- package/dist/websocket/NordWebSocketClient.d.ts +2 -2
- package/dist/websocket/NordWebSocketClient.js +38 -38
- package/dist/websocket/events.d.ts +1 -1
- package/dist/websocket/index.d.ts +2 -2
- 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 +190 -31
- 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 -0
- 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 -0
- 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 -0
- 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 -70
- 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 +12 -13
- package/src/bridge/client.ts +10 -10
- package/src/bridge/const.ts +24 -24
- package/src/bridge/index.ts +4 -4
- package/src/bridge/types.ts +10 -10
- package/src/bridge/utils.ts +14 -14
- package/src/const.ts +18 -18
- package/src/gen/nord.ts +1 -4
- package/src/idl/bridge.ts +186 -186
- package/src/index.ts +8 -8
- package/src/nord/api/actions.ts +25 -25
- package/src/nord/api/core.ts +16 -16
- package/src/nord/api/market.ts +15 -15
- package/src/nord/api/metrics.ts +16 -16
- package/src/nord/api/queries.ts +12 -12
- package/src/nord/client/Nord.ts +27 -27
- package/src/nord/client/NordUser.ts +59 -63
- package/src/nord/index.ts +9 -9
- package/src/nord/models/Subscriber.ts +11 -12
- package/src/nord/utils/NordError.ts +2 -2
- package/src/types.ts +12 -12
- package/src/utils.ts +20 -20
- package/src/websocket/NordWebSocketClient.ts +42 -42
- package/src/websocket/events.ts +1 -1
- package/src/websocket/index.ts +2 -2
- package/tests/utils.spec.ts +24 -24
- package/tsconfig.eslint.json +12 -0
- package/.eslintignore +0 -1
- package/.eslintrc.js +0 -31
- 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/bridge/NordUser.d.ts +0 -78
- package/dist/bridge/NordUser.js +0 -196
- package/dist/client.d.ts +0 -70
- package/dist/client.js +0 -129
- package/dist/constants/endpoints.d.ts +0 -65
- package/dist/constants/endpoints.js +0 -68
- package/dist/models/account.d.ts +0 -58
- package/dist/models/account.js +0 -6
- package/dist/models/index.d.ts +0 -8
- package/dist/models/index.js +0 -28
- package/dist/models/market.d.ts +0 -137
- package/dist/models/market.js +0 -6
- package/dist/models/order.d.ts +0 -211
- package/dist/models/order.js +0 -6
- package/dist/models/token.d.ts +0 -50
- package/dist/models/token.js +0 -6
- package/dist/nord/Nord.d.ts +0 -249
- package/dist/nord/Nord.js +0 -388
- package/dist/nord/NordError.d.ts +0 -23
- package/dist/nord/NordError.js +0 -48
- package/dist/nord/NordImpl.d.ts +0 -11
- package/dist/nord/NordImpl.js +0 -26
- package/dist/nord/NordUser.d.ts +0 -243
- package/dist/nord/NordUser.js +0 -481
- package/dist/nord/Subscriber.d.ts +0 -37
- package/dist/nord/Subscriber.js +0 -29
- package/dist/nord/actions.d.ts +0 -101
- package/dist/nord/actions.js +0 -254
- package/dist/nord/api/index.d.ts +0 -1
- package/dist/nord/api/index.js +0 -17
- package/dist/nord/api/orderFunctions.d.ts +0 -168
- package/dist/nord/api/orderFunctions.js +0 -133
- package/dist/nord/core.d.ts +0 -48
- package/dist/nord/core.js +0 -97
- package/dist/nord/market.d.ts +0 -36
- package/dist/nord/market.js +0 -90
- package/dist/nord/metrics.d.ts +0 -67
- package/dist/nord/metrics.js +0 -124
- package/dist/nord/queries.d.ts +0 -81
- package/dist/nord/queries.js +0 -181
- package/dist/nord/types.d.ts +0 -88
- package/dist/nord/types.js +0 -2
- package/dist/nord/websocket.d.ts +0 -49
- package/dist/nord/websocket.js +0 -107
- package/dist/operations/account.d.ts +0 -58
- package/dist/operations/account.js +0 -112
- package/dist/operations/market.d.ts +0 -65
- package/dist/operations/market.js +0 -131
- package/dist/operations/orders.d.ts +0 -57
- package/dist/operations/orders.js +0 -129
- package/dist/solana/NordUser.d.ts +0 -78
- package/dist/solana/NordUser.js +0 -196
- package/dist/solana/client.d.ts +0 -139
- package/dist/solana/client.js +0 -360
- package/dist/solana/const.d.ts +0 -23
- package/dist/solana/const.js +0 -47
- package/dist/solana/index.d.ts +0 -5
- package/dist/solana/index.js +0 -23
- package/dist/solana/types.d.ts +0 -118
- package/dist/solana/types.js +0 -16
- package/dist/solana/utils.d.ts +0 -64
- package/dist/solana/utils.js +0 -131
- package/dist/types/api.d.ts +0 -152
- package/dist/types/api.js +0 -6
- package/dist/types/config.d.ts +0 -34
- package/dist/types/config.js +0 -6
- package/dist/utils/errors.d.ts +0 -96
- package/dist/utils/errors.js +0 -132
- package/dist/utils/http.d.ts +0 -35
- package/dist/utils/http.js +0 -105
- package/dist/websocket/client.d.ts +0 -93
- package/dist/websocket/client.js +0 -222
- package/dist/websocket.d.ts +0 -55
- package/dist/websocket.js +0 -211
- package/docs/interfaces/ERC20TokenInfo.html +0 -5
- 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/types.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import * as proto from
|
|
1
|
+
import * as proto from "./gen/nord";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* The peak TPS rate is queried over the specified period.
|
|
@@ -10,11 +10,11 @@ import * as proto from './gen/nord';
|
|
|
10
10
|
* AggregateMetrics.txPeakTpsPeriodUnit = "w" => Peak TPS over last week.
|
|
11
11
|
*/
|
|
12
12
|
export enum PeakTpsPeriodUnit {
|
|
13
|
-
Hour =
|
|
14
|
-
Day =
|
|
15
|
-
Week =
|
|
16
|
-
Month =
|
|
17
|
-
Year =
|
|
13
|
+
Hour = "h",
|
|
14
|
+
Day = "d",
|
|
15
|
+
Week = "w",
|
|
16
|
+
Month = "m",
|
|
17
|
+
Year = "y",
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
/**
|
|
@@ -324,7 +324,7 @@ export function fillModeToProtoFillMode(x: FillMode): proto.FillMode {
|
|
|
324
324
|
return proto.FillMode.IMMEDIATE_OR_CANCEL;
|
|
325
325
|
}
|
|
326
326
|
if (x === FillMode.FillOrKill) return proto.FillMode.FILL_OR_KILL;
|
|
327
|
-
throw new Error(
|
|
327
|
+
throw new Error("Invalid fill mode");
|
|
328
328
|
}
|
|
329
329
|
|
|
330
330
|
/**
|
|
@@ -409,11 +409,11 @@ export interface ActionNonceResponse {
|
|
|
409
409
|
* WebSocket message types
|
|
410
410
|
*/
|
|
411
411
|
export enum WebSocketMessageType {
|
|
412
|
-
Subscribe =
|
|
413
|
-
Unsubscribe =
|
|
414
|
-
TradeUpdate =
|
|
415
|
-
DeltaUpdate =
|
|
416
|
-
UserUpdate =
|
|
412
|
+
Subscribe = "subscribe",
|
|
413
|
+
Unsubscribe = "unsubscribe",
|
|
414
|
+
TradeUpdate = "trade",
|
|
415
|
+
DeltaUpdate = "delta",
|
|
416
|
+
UserUpdate = "user",
|
|
417
417
|
}
|
|
418
418
|
|
|
419
419
|
/**
|
package/src/utils.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { Decimal } from
|
|
2
|
-
import { ed25519 } from
|
|
3
|
-
import { bls12_381 as bls } from
|
|
4
|
-
import { secp256k1 as secp } from
|
|
5
|
-
import { sha256 } from
|
|
6
|
-
import { KeyType, type Market, type Token } from
|
|
7
|
-
import * as proto from
|
|
8
|
-
import { BinaryReader, BinaryWriter } from
|
|
9
|
-
import { ethers } from
|
|
10
|
-
import fetch from
|
|
11
|
-
import { RequestInfo, RequestInit, Response } from
|
|
12
|
-
import { BN } from
|
|
1
|
+
import { Decimal } from "decimal.js";
|
|
2
|
+
import { ed25519 } from "@noble/curves/ed25519";
|
|
3
|
+
import { bls12_381 as bls } from "@noble/curves/bls12-381";
|
|
4
|
+
import { secp256k1 as secp } from "@noble/curves/secp256k1";
|
|
5
|
+
import { sha256 } from "@noble/hashes/sha256";
|
|
6
|
+
import { KeyType, type Market, type Token } from "./types";
|
|
7
|
+
import * as proto from "./gen/nord";
|
|
8
|
+
import { BinaryReader, BinaryWriter } from "@bufbuild/protobuf/wire";
|
|
9
|
+
import { ethers } from "ethers";
|
|
10
|
+
import fetch from "node-fetch";
|
|
11
|
+
import { RequestInfo, RequestInit, Response } from "node-fetch";
|
|
12
|
+
import { BN } from "@coral-xyz/anchor";
|
|
13
13
|
|
|
14
14
|
export const SESSION_TTL: bigint = 60n * 60n * 24n * 30n;
|
|
15
15
|
export const ZERO_DECIMAL = new Decimal(0);
|
|
@@ -25,7 +25,7 @@ export function panic(message: string): never {
|
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
export function assert(predicate: boolean, message?: string): void {
|
|
28
|
-
if (!predicate) panic(message ??
|
|
28
|
+
if (!predicate) panic(message ?? "Assertion violated");
|
|
29
29
|
}
|
|
30
30
|
/**
|
|
31
31
|
* Extracts value out of optional if it's defined, or throws error if it's not
|
|
@@ -43,7 +43,7 @@ export function optExpect<T>(value: T | undefined, message: string): T {
|
|
|
43
43
|
* @returns
|
|
44
44
|
*/
|
|
45
45
|
export function optUnwrap<T>(value: T | undefined): T {
|
|
46
|
-
return optExpect(value,
|
|
46
|
+
return optExpect(value, "Optional contains no value");
|
|
47
47
|
}
|
|
48
48
|
/**
|
|
49
49
|
* Applies function to value if it's defined, or passes `undefined` through
|
|
@@ -93,7 +93,7 @@ export function signAction(
|
|
|
93
93
|
} else if (keyType === KeyType.Secp256k1) {
|
|
94
94
|
sig = secp.sign(sha256(action), sk).toCompactRawBytes();
|
|
95
95
|
} else {
|
|
96
|
-
throw new Error(
|
|
96
|
+
throw new Error("Invalid key type");
|
|
97
97
|
}
|
|
98
98
|
return new Uint8Array([...action, ...sig]);
|
|
99
99
|
}
|
|
@@ -273,16 +273,16 @@ export function decodeLengthDelimited<T, M extends proto.MessageFns<T>>(
|
|
|
273
273
|
export function checkPubKeyLength(keyType: KeyType, len: number): void {
|
|
274
274
|
if (keyType === KeyType.Bls12_381) {
|
|
275
275
|
throw new Error(
|
|
276
|
-
|
|
276
|
+
"Cannot create a user using Bls12_381, use Ed25119 or Secp256k1 instead.",
|
|
277
277
|
);
|
|
278
278
|
}
|
|
279
279
|
|
|
280
280
|
if (len !== 32 && keyType === KeyType.Ed25519) {
|
|
281
|
-
throw new Error(
|
|
281
|
+
throw new Error("Ed25519 pubkeys must be 32 length.");
|
|
282
282
|
}
|
|
283
283
|
|
|
284
284
|
if (len !== 33 && keyType === KeyType.Secp256k1) {
|
|
285
|
-
throw new Error(
|
|
285
|
+
throw new Error("Secp256k1 pubkeys must be 33 length.");
|
|
286
286
|
}
|
|
287
287
|
}
|
|
288
288
|
|
|
@@ -309,12 +309,12 @@ export function findToken(tokens: Token[], tokenId: number): Token {
|
|
|
309
309
|
*/
|
|
310
310
|
export function toBN(amount: number, decimals: number): BN {
|
|
311
311
|
const amountString = amount.toFixed(decimals);
|
|
312
|
-
const [whole, fraction] = amountString.split(
|
|
312
|
+
const [whole, fraction] = amountString.split(".");
|
|
313
313
|
|
|
314
314
|
// Convert to smallest units (no decimals)
|
|
315
315
|
const wholeBN = new BN(whole).mul(new BN(10).pow(new BN(decimals)));
|
|
316
316
|
const fractionBN = fraction
|
|
317
|
-
? new BN(fraction.padEnd(decimals,
|
|
317
|
+
? new BN(fraction.padEnd(decimals, "0").slice(0, decimals))
|
|
318
318
|
: new BN(0);
|
|
319
319
|
|
|
320
320
|
return wholeBN.add(fractionBN);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import WebSocket from
|
|
2
|
-
import { EventEmitter } from
|
|
1
|
+
import WebSocket from "ws";
|
|
2
|
+
import { EventEmitter } from "events";
|
|
3
3
|
import {
|
|
4
4
|
WebSocketMessage,
|
|
5
5
|
WebSocketMessageType,
|
|
@@ -7,8 +7,8 @@ import {
|
|
|
7
7
|
WebSocketTradeUpdate,
|
|
8
8
|
WebSocketDeltaUpdate,
|
|
9
9
|
WebSocketUserUpdate,
|
|
10
|
-
} from
|
|
11
|
-
import { NordWebSocketClientEvents } from
|
|
10
|
+
} from "../types";
|
|
11
|
+
import { NordWebSocketClientEvents } from "./events";
|
|
12
12
|
|
|
13
13
|
// Define a type that works for both Node.js ws and browser WebSocket
|
|
14
14
|
type BrowserWebSocket = {
|
|
@@ -27,7 +27,7 @@ type BrowserWebSocket = {
|
|
|
27
27
|
|
|
28
28
|
type WebSocketInstance = WebSocket | BrowserWebSocket;
|
|
29
29
|
|
|
30
|
-
const VALID_STREAM_TYPES = [
|
|
30
|
+
const VALID_STREAM_TYPES = ["trades", "deltas", "user"];
|
|
31
31
|
|
|
32
32
|
// Constants for WebSocket readyState
|
|
33
33
|
const WS_OPEN = 1;
|
|
@@ -68,7 +68,7 @@ export class NordWebSocketClient
|
|
|
68
68
|
// Check if we're in a browser environment
|
|
69
69
|
// The most reliable way is to check for Node.js process
|
|
70
70
|
this.isBrowser =
|
|
71
|
-
typeof process ===
|
|
71
|
+
typeof process === "undefined" ||
|
|
72
72
|
!process.versions ||
|
|
73
73
|
!process.versions.node;
|
|
74
74
|
}
|
|
@@ -79,7 +79,7 @@ export class NordWebSocketClient
|
|
|
79
79
|
* @throws Error if stream format is invalid
|
|
80
80
|
*/
|
|
81
81
|
private validateStream(stream: string): void {
|
|
82
|
-
const [type, params] = stream.split(
|
|
82
|
+
const [type, params] = stream.split("@");
|
|
83
83
|
|
|
84
84
|
if (!type || !params) {
|
|
85
85
|
throw new Error(
|
|
@@ -89,7 +89,7 @@ export class NordWebSocketClient
|
|
|
89
89
|
|
|
90
90
|
// Extract the endpoint from the URL
|
|
91
91
|
const urlPath = new URL(this.url).pathname;
|
|
92
|
-
const endpoint = urlPath.split(
|
|
92
|
+
const endpoint = urlPath.split("/").pop();
|
|
93
93
|
|
|
94
94
|
// Ensure the stream type matches the endpoint we're connected to
|
|
95
95
|
if (endpoint && type !== endpoint) {
|
|
@@ -100,11 +100,11 @@ export class NordWebSocketClient
|
|
|
100
100
|
|
|
101
101
|
if (!VALID_STREAM_TYPES.includes(type)) {
|
|
102
102
|
throw new Error(
|
|
103
|
-
`Invalid stream type: ${type}. Valid types are: ${VALID_STREAM_TYPES.join(
|
|
103
|
+
`Invalid stream type: ${type}. Valid types are: ${VALID_STREAM_TYPES.join(", ")}`,
|
|
104
104
|
);
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
if (type ===
|
|
107
|
+
if (type === "user" && !/^\d+$/.test(params)) {
|
|
108
108
|
throw new Error(
|
|
109
109
|
`Invalid user ID in stream: ${params}. Expected numeric ID`,
|
|
110
110
|
);
|
|
@@ -134,7 +134,7 @@ export class NordWebSocketClient
|
|
|
134
134
|
|
|
135
135
|
// Set timeout for pong response
|
|
136
136
|
this.pingTimeout = setTimeout(() => {
|
|
137
|
-
this.emit(
|
|
137
|
+
this.emit("error", new Error("WebSocket ping timeout"));
|
|
138
138
|
this.close();
|
|
139
139
|
this.reconnect();
|
|
140
140
|
}, 5000); // 5 second timeout
|
|
@@ -148,12 +148,12 @@ export class NordWebSocketClient
|
|
|
148
148
|
private getWebSocketClass(): any {
|
|
149
149
|
if (this.isBrowser) {
|
|
150
150
|
// In browser environments
|
|
151
|
-
// @ts-
|
|
152
|
-
if (typeof globalThis !==
|
|
153
|
-
// @ts-
|
|
151
|
+
// @ts-expect-error - Check for WebSocket in globalThis
|
|
152
|
+
if (typeof globalThis !== "undefined" && globalThis.WebSocket) {
|
|
153
|
+
// @ts-expect-error - Return WebSocket from globalThis
|
|
154
154
|
return globalThis.WebSocket;
|
|
155
155
|
}
|
|
156
|
-
throw new Error(
|
|
156
|
+
throw new Error("WebSocket is not available in this environment");
|
|
157
157
|
} else {
|
|
158
158
|
// In Node.js
|
|
159
159
|
return WebSocket;
|
|
@@ -176,7 +176,7 @@ export class NordWebSocketClient
|
|
|
176
176
|
this.ws = new WebSocketClass(this.url) as BrowserWebSocket;
|
|
177
177
|
|
|
178
178
|
(this.ws as BrowserWebSocket).onopen = () => {
|
|
179
|
-
this.emit(
|
|
179
|
+
this.emit("connected");
|
|
180
180
|
this.reconnectAttempts = 0;
|
|
181
181
|
this.reconnectDelay = 1000;
|
|
182
182
|
|
|
@@ -194,7 +194,7 @@ export class NordWebSocketClient
|
|
|
194
194
|
this.handleMessage(message);
|
|
195
195
|
} catch (error) {
|
|
196
196
|
this.emit(
|
|
197
|
-
|
|
197
|
+
"error",
|
|
198
198
|
new Error(
|
|
199
199
|
`Failed to parse message: ${error instanceof Error ? error.message : String(error)}`,
|
|
200
200
|
),
|
|
@@ -204,25 +204,25 @@ export class NordWebSocketClient
|
|
|
204
204
|
|
|
205
205
|
(this.ws as BrowserWebSocket).onclose = (event: any) => {
|
|
206
206
|
const reason =
|
|
207
|
-
event && event.reason ? ` Reason: ${event.reason}` :
|
|
208
|
-
const code = event && event.code ? ` Code: ${event.code}` :
|
|
209
|
-
this.emit(
|
|
207
|
+
event && event.reason ? ` Reason: ${event.reason}` : "";
|
|
208
|
+
const code = event && event.code ? ` Code: ${event.code}` : "";
|
|
209
|
+
this.emit("disconnected");
|
|
210
210
|
console.log(`WebSocket closed.${code}${reason}`);
|
|
211
211
|
this.reconnect();
|
|
212
212
|
};
|
|
213
213
|
|
|
214
214
|
(this.ws as BrowserWebSocket).onerror = (event: any) => {
|
|
215
|
-
const errorMsg = `WebSocket error: ${event && event.type ? event.type :
|
|
215
|
+
const errorMsg = `WebSocket error: ${event && event.type ? event.type : "unknown"}`;
|
|
216
216
|
console.error(errorMsg, event);
|
|
217
|
-
this.emit(
|
|
217
|
+
this.emit("error", new Error(errorMsg));
|
|
218
218
|
};
|
|
219
219
|
} else {
|
|
220
220
|
// Node.js WebSocket setup
|
|
221
221
|
const nodeWs = new WebSocketClass(this.url) as WebSocket;
|
|
222
222
|
this.ws = nodeWs;
|
|
223
223
|
|
|
224
|
-
nodeWs.on(
|
|
225
|
-
this.emit(
|
|
224
|
+
nodeWs.on("open", () => {
|
|
225
|
+
this.emit("connected");
|
|
226
226
|
this.reconnectAttempts = 0;
|
|
227
227
|
this.reconnectDelay = 1000;
|
|
228
228
|
this.setupHeartbeat();
|
|
@@ -233,13 +233,13 @@ export class NordWebSocketClient
|
|
|
233
233
|
}
|
|
234
234
|
});
|
|
235
235
|
|
|
236
|
-
nodeWs.on(
|
|
236
|
+
nodeWs.on("message", (data: WebSocket.Data) => {
|
|
237
237
|
try {
|
|
238
238
|
const message = JSON.parse(data.toString()) as WebSocketMessage;
|
|
239
239
|
this.handleMessage(message);
|
|
240
240
|
} catch (error) {
|
|
241
241
|
this.emit(
|
|
242
|
-
|
|
242
|
+
"error",
|
|
243
243
|
new Error(
|
|
244
244
|
`Failed to parse message: ${error instanceof Error ? error.message : String(error)}`,
|
|
245
245
|
),
|
|
@@ -247,8 +247,8 @@ export class NordWebSocketClient
|
|
|
247
247
|
}
|
|
248
248
|
});
|
|
249
249
|
|
|
250
|
-
nodeWs.on(
|
|
251
|
-
this.emit(
|
|
250
|
+
nodeWs.on("close", (code: number, reason: string) => {
|
|
251
|
+
this.emit("disconnected");
|
|
252
252
|
console.log(`WebSocket closed. Code: ${code} Reason: ${reason}`);
|
|
253
253
|
if (this.pingInterval) {
|
|
254
254
|
clearInterval(this.pingInterval);
|
|
@@ -259,12 +259,12 @@ export class NordWebSocketClient
|
|
|
259
259
|
this.reconnect();
|
|
260
260
|
});
|
|
261
261
|
|
|
262
|
-
nodeWs.on(
|
|
263
|
-
console.error(
|
|
264
|
-
this.emit(
|
|
262
|
+
nodeWs.on("error", (error: Error) => {
|
|
263
|
+
console.error("WebSocket error:", error);
|
|
264
|
+
this.emit("error", error);
|
|
265
265
|
});
|
|
266
266
|
|
|
267
|
-
nodeWs.on(
|
|
267
|
+
nodeWs.on("pong", () => {
|
|
268
268
|
if (this.pingTimeout) {
|
|
269
269
|
clearTimeout(this.pingTimeout);
|
|
270
270
|
}
|
|
@@ -273,7 +273,7 @@ export class NordWebSocketClient
|
|
|
273
273
|
} catch (error) {
|
|
274
274
|
const errorMsg = `Failed to initialize WebSocket: ${error instanceof Error ? error.message : String(error)}`;
|
|
275
275
|
console.error(errorMsg);
|
|
276
|
-
this.emit(
|
|
276
|
+
this.emit("error", new Error(errorMsg));
|
|
277
277
|
}
|
|
278
278
|
}
|
|
279
279
|
|
|
@@ -287,7 +287,7 @@ export class NordWebSocketClient
|
|
|
287
287
|
streams.forEach((stream) => this.validateStream(stream));
|
|
288
288
|
} catch (error) {
|
|
289
289
|
this.emit(
|
|
290
|
-
|
|
290
|
+
"error",
|
|
291
291
|
error instanceof Error ? error : new Error(String(error)),
|
|
292
292
|
);
|
|
293
293
|
return;
|
|
@@ -318,7 +318,7 @@ export class NordWebSocketClient
|
|
|
318
318
|
streams.forEach((stream) => this.subscriptions.add(stream));
|
|
319
319
|
} catch (error) {
|
|
320
320
|
this.emit(
|
|
321
|
-
|
|
321
|
+
"error",
|
|
322
322
|
error instanceof Error ? error : new Error(String(error)),
|
|
323
323
|
);
|
|
324
324
|
}
|
|
@@ -334,7 +334,7 @@ export class NordWebSocketClient
|
|
|
334
334
|
streams.forEach((stream) => this.validateStream(stream));
|
|
335
335
|
} catch (error) {
|
|
336
336
|
this.emit(
|
|
337
|
-
|
|
337
|
+
"error",
|
|
338
338
|
error instanceof Error ? error : new Error(String(error)),
|
|
339
339
|
);
|
|
340
340
|
return;
|
|
@@ -365,7 +365,7 @@ export class NordWebSocketClient
|
|
|
365
365
|
streams.forEach((stream) => this.subscriptions.delete(stream));
|
|
366
366
|
} catch (error) {
|
|
367
367
|
this.emit(
|
|
368
|
-
|
|
368
|
+
"error",
|
|
369
369
|
error instanceof Error ? error : new Error(String(error)),
|
|
370
370
|
);
|
|
371
371
|
}
|
|
@@ -401,16 +401,16 @@ export class NordWebSocketClient
|
|
|
401
401
|
private handleMessage(message: WebSocketMessage): void {
|
|
402
402
|
switch (message.type) {
|
|
403
403
|
case WebSocketMessageType.TradeUpdate:
|
|
404
|
-
this.emit(
|
|
404
|
+
this.emit("trade", message as WebSocketTradeUpdate);
|
|
405
405
|
break;
|
|
406
406
|
case WebSocketMessageType.DeltaUpdate:
|
|
407
|
-
this.emit(
|
|
407
|
+
this.emit("delta", message as WebSocketDeltaUpdate);
|
|
408
408
|
break;
|
|
409
409
|
case WebSocketMessageType.UserUpdate:
|
|
410
|
-
this.emit(
|
|
410
|
+
this.emit("user", message as WebSocketUserUpdate);
|
|
411
411
|
break;
|
|
412
412
|
default:
|
|
413
|
-
this.emit(
|
|
413
|
+
this.emit("error", new Error(`Unknown message type: ${message.type}`));
|
|
414
414
|
}
|
|
415
415
|
}
|
|
416
416
|
|
|
@@ -419,7 +419,7 @@ export class NordWebSocketClient
|
|
|
419
419
|
*/
|
|
420
420
|
private reconnect(): void {
|
|
421
421
|
if (this.reconnectAttempts >= this.maxReconnectAttempts) {
|
|
422
|
-
this.emit(
|
|
422
|
+
this.emit("error", new Error("Max reconnection attempts reached"));
|
|
423
423
|
return;
|
|
424
424
|
}
|
|
425
425
|
|
package/src/websocket/events.ts
CHANGED
package/src/websocket/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { NordWebSocketClient } from
|
|
2
|
-
export { NordWebSocketEvents, NordWebSocketClientEvents } from
|
|
1
|
+
export { NordWebSocketClient } from "./NordWebSocketClient";
|
|
2
|
+
export { NordWebSocketEvents, NordWebSocketClientEvents } from "./events";
|
package/tests/utils.spec.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { describe, it, expect } from
|
|
1
|
+
import { describe, it, expect } from "@jest/globals";
|
|
2
2
|
import {
|
|
3
3
|
bigIntToProtoU128,
|
|
4
4
|
toScaledU64,
|
|
5
5
|
toScaledU128,
|
|
6
6
|
encodeLengthDelimited,
|
|
7
7
|
decodeLengthDelimited,
|
|
8
|
-
} from
|
|
9
|
-
import Decimal from
|
|
10
|
-
import * as proto from
|
|
8
|
+
} from "../src/utils";
|
|
9
|
+
import Decimal from "decimal.js";
|
|
10
|
+
import * as proto from "../src/gen/nord";
|
|
11
11
|
|
|
12
|
-
describe(
|
|
12
|
+
describe("toScaledU64", () => {
|
|
13
13
|
// Used as scaling medium
|
|
14
14
|
const D64 = Decimal.clone({
|
|
15
15
|
precision: 20,
|
|
@@ -35,12 +35,12 @@ describe('toScaledU64', () => {
|
|
|
35
35
|
});
|
|
36
36
|
|
|
37
37
|
const failure: Array<[Decimal.Value, number, string]> = [
|
|
38
|
-
[-1, 0,
|
|
39
|
-
[-1000, 2,
|
|
40
|
-
[0.1, 0,
|
|
41
|
-
[0.0001, 3,
|
|
42
|
-
[MaxDec.add(1), 0,
|
|
43
|
-
[MaxDec.add(1).div(1000), 3,
|
|
38
|
+
[-1, 0, "Number is negative"],
|
|
39
|
+
[-1000, 2, "Number is negative"],
|
|
40
|
+
[0.1, 0, "Precision loss"],
|
|
41
|
+
[0.0001, 3, "Precision loss"],
|
|
42
|
+
[MaxDec.add(1), 0, "Integer is out of range"],
|
|
43
|
+
[MaxDec.add(1).div(1000), 3, "Integer is out of range"],
|
|
44
44
|
];
|
|
45
45
|
|
|
46
46
|
failure.forEach((sample) => {
|
|
@@ -50,7 +50,7 @@ describe('toScaledU64', () => {
|
|
|
50
50
|
});
|
|
51
51
|
});
|
|
52
52
|
|
|
53
|
-
describe(
|
|
53
|
+
describe("toScaledU128", () => {
|
|
54
54
|
// Used as scaling medium
|
|
55
55
|
const D128 = Decimal.clone({
|
|
56
56
|
precision: 40,
|
|
@@ -76,12 +76,12 @@ describe('toScaledU128', () => {
|
|
|
76
76
|
});
|
|
77
77
|
|
|
78
78
|
const failure: Array<[Decimal.Value, number, string]> = [
|
|
79
|
-
[-1, 0,
|
|
80
|
-
[-1000, 2,
|
|
81
|
-
[0.1, 0,
|
|
82
|
-
[0.0001, 3,
|
|
83
|
-
[MaxDec.add(1), 0,
|
|
84
|
-
[MaxDec.add(1).div(1000), 3,
|
|
79
|
+
[-1, 0, "Number is negative"],
|
|
80
|
+
[-1000, 2, "Number is negative"],
|
|
81
|
+
[0.1, 0, "Precision loss"],
|
|
82
|
+
[0.0001, 3, "Precision loss"],
|
|
83
|
+
[MaxDec.add(1), 0, "Integer is out of range"],
|
|
84
|
+
[MaxDec.add(1).div(1000), 3, "Integer is out of range"],
|
|
85
85
|
];
|
|
86
86
|
|
|
87
87
|
failure.forEach((sample) => {
|
|
@@ -94,7 +94,7 @@ describe('toScaledU128', () => {
|
|
|
94
94
|
const U64_MAX = (1n << 64n) - 1n;
|
|
95
95
|
const U128_MAX = (1n << 128n) - 1n;
|
|
96
96
|
|
|
97
|
-
describe(
|
|
97
|
+
describe("bigIntToProtoU128", () => {
|
|
98
98
|
const success: Array<[bigint, { lo: bigint; hi: bigint }]> = [
|
|
99
99
|
[0n, { lo: 0n, hi: 0n }],
|
|
100
100
|
[1n, { lo: 1n, hi: 0n }],
|
|
@@ -112,8 +112,8 @@ describe('bigIntToProtoU128', () => {
|
|
|
112
112
|
});
|
|
113
113
|
|
|
114
114
|
const failure: Array<[bigint, string]> = [
|
|
115
|
-
[-1n,
|
|
116
|
-
[U128_MAX + 1n,
|
|
115
|
+
[-1n, "Negative number"],
|
|
116
|
+
[U128_MAX + 1n, "U128 overflow"],
|
|
117
117
|
];
|
|
118
118
|
|
|
119
119
|
failure.forEach((sample, index) => {
|
|
@@ -123,12 +123,12 @@ describe('bigIntToProtoU128', () => {
|
|
|
123
123
|
});
|
|
124
124
|
});
|
|
125
125
|
|
|
126
|
-
describe(
|
|
126
|
+
describe("proto.Action encode-decode loop", () => {
|
|
127
127
|
const action: proto.Action = {
|
|
128
128
|
currentTimestamp: 0n,
|
|
129
129
|
nonce: 0,
|
|
130
130
|
kind: {
|
|
131
|
-
$case:
|
|
131
|
+
$case: "placeOrder",
|
|
132
132
|
value: {
|
|
133
133
|
sessionId: 42n,
|
|
134
134
|
marketId: 9,
|
|
@@ -145,7 +145,7 @@ describe('proto.Action encode-decode loop', () => {
|
|
|
145
145
|
},
|
|
146
146
|
};
|
|
147
147
|
|
|
148
|
-
it(
|
|
148
|
+
it("action encode-decode roundabout should succeed", () => {
|
|
149
149
|
const encoded = encodeLengthDelimited(action, proto.Action);
|
|
150
150
|
const decoded: proto.Action = decodeLengthDelimited(encoded, proto.Action);
|
|
151
151
|
|
package/.eslintignore
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
src/gen/*
|
package/.eslintrc.js
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
root: true,
|
|
3
|
-
env: {
|
|
4
|
-
browser: true,
|
|
5
|
-
es2021: true,
|
|
6
|
-
node: true,
|
|
7
|
-
},
|
|
8
|
-
extends: [
|
|
9
|
-
"prettier",
|
|
10
|
-
"plugin:@typescript-eslint/eslint-recommended",
|
|
11
|
-
"plugin:@typescript-eslint/recommended",
|
|
12
|
-
],
|
|
13
|
-
parser: "@typescript-eslint/parser",
|
|
14
|
-
plugins: ["@typescript-eslint"],
|
|
15
|
-
parserOptions: {
|
|
16
|
-
ecmaVersion: "latest",
|
|
17
|
-
sourceType: "module",
|
|
18
|
-
project: "./tsconfig.json",
|
|
19
|
-
tsconfigRootDir: __dirname,
|
|
20
|
-
},
|
|
21
|
-
rules: {
|
|
22
|
-
"@typescript-eslint/no-explicit-any": "warn",
|
|
23
|
-
"@typescript-eslint/no-unused-vars": ["warn", { argsIgnorePattern: "^_" }],
|
|
24
|
-
},
|
|
25
|
-
ignorePatterns: [
|
|
26
|
-
"dist",
|
|
27
|
-
"node_modules",
|
|
28
|
-
".git",
|
|
29
|
-
"docs",
|
|
30
|
-
],
|
|
31
|
-
};
|
package/dist/abis/ERC20_ABI.d.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
export declare const ERC20_ABI: ({
|
|
2
|
-
type: string;
|
|
3
|
-
name: string;
|
|
4
|
-
inputs: {
|
|
5
|
-
name: string;
|
|
6
|
-
type: string;
|
|
7
|
-
internalType: string;
|
|
8
|
-
}[];
|
|
9
|
-
outputs: {
|
|
10
|
-
name: string;
|
|
11
|
-
type: string;
|
|
12
|
-
internalType: string;
|
|
13
|
-
}[];
|
|
14
|
-
stateMutability: string;
|
|
15
|
-
anonymous?: undefined;
|
|
16
|
-
} | {
|
|
17
|
-
type: string;
|
|
18
|
-
name: string;
|
|
19
|
-
inputs: {
|
|
20
|
-
name: string;
|
|
21
|
-
type: string;
|
|
22
|
-
indexed: boolean;
|
|
23
|
-
internalType: string;
|
|
24
|
-
}[];
|
|
25
|
-
anonymous: boolean;
|
|
26
|
-
outputs?: undefined;
|
|
27
|
-
stateMutability?: undefined;
|
|
28
|
-
} | {
|
|
29
|
-
type: string;
|
|
30
|
-
name: string;
|
|
31
|
-
inputs: {
|
|
32
|
-
name: string;
|
|
33
|
-
type: string;
|
|
34
|
-
internalType: string;
|
|
35
|
-
}[];
|
|
36
|
-
outputs?: undefined;
|
|
37
|
-
stateMutability?: undefined;
|
|
38
|
-
anonymous?: undefined;
|
|
39
|
-
})[];
|