@gmx-io/sdk 2.7.0 → 2.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (30) hide show
  1. package/build/cjs/src/clients/v2/index.js +20 -1
  2. package/build/cjs/src/utils/__tests__/numbers.spec.js +15 -0
  3. package/build/cjs/src/utils/numbers/utils.js +8 -2
  4. package/build/cjs/src/utils/prices/api.js +15 -1
  5. package/build/cjs/src/utils/stream/WsStreamClient.js +363 -0
  6. package/build/cjs/src/utils/stream/__tests__/WsStreamClient.spec.js +383 -0
  7. package/build/cjs/src/utils/stream/__tests__/stream.integration.spec.js +133 -0
  8. package/build/cjs/src/utils/stream/index.js +11 -0
  9. package/build/cjs/src/utils/stream/subscription.js +61 -0
  10. package/build/cjs/src/utils/stream/types.js +2 -0
  11. package/build/esm/src/clients/v2/index.js +21 -2
  12. package/build/esm/src/utils/__tests__/numbers.spec.js +15 -0
  13. package/build/esm/src/utils/numbers/utils.js +8 -2
  14. package/build/esm/src/utils/prices/api.js +12 -0
  15. package/build/esm/src/utils/stream/WsStreamClient.js +355 -0
  16. package/build/esm/src/utils/stream/__tests__/WsStreamClient.spec.js +381 -0
  17. package/build/esm/src/utils/stream/__tests__/stream.integration.spec.js +131 -0
  18. package/build/esm/src/utils/stream/index.js +5 -0
  19. package/build/esm/src/utils/stream/subscription.js +57 -0
  20. package/build/esm/src/utils/stream/types.js +1 -0
  21. package/build/tsconfig.tsbuildinfo +1 -1
  22. package/build/types/src/clients/v2/index.d.ts +23 -5
  23. package/build/types/src/utils/prices/api.d.ts +8 -0
  24. package/build/types/src/utils/stream/WsStreamClient.d.ts +63 -0
  25. package/build/types/src/utils/stream/__tests__/WsStreamClient.spec.d.ts +1 -0
  26. package/build/types/src/utils/stream/__tests__/stream.integration.spec.d.ts +1 -0
  27. package/build/types/src/utils/stream/index.d.ts +5 -0
  28. package/build/types/src/utils/stream/subscription.d.ts +3 -0
  29. package/build/types/src/utils/stream/types.d.ts +43 -0
  30. package/package.json +14 -2
@@ -11,12 +11,14 @@ import { isPrepareOrderError, parsePrepareOrderError } from "../../utils/orderTr
11
11
  import type { PrepareOrderError, PrepareOrderErrorCode, PrepareOrderFieldValidationErrors, PrepareOrderRequest, PrepareOrderResponse, OrderValidationWarning, SubmitOrderRequest, SubmitOrderResponse, PrepareEditOrderRequest, PrepareCancelOrderRequest, PrepareCollateralRequest, OrderStatusRequest, OrderStatusResponse } from "../../utils/orderTransactions/api";
12
12
  import type { TransitAuthorizationParams, TransitAuthorizationResponse, TransitFeeTierResponse, TransitOrder, TransitOrdersParams, TransitOrdersResponse, TransitQuote, TransitQuoteParams, TransitRoute, TransitRoutesParams } from "../../utils/paxos/types";
13
13
  import { PerformanceAnnualized, PerformanceParams, PerformanceSnapshots } from "../../utils/performance/types";
14
- import type { OhlcvParams } from "../../utils/prices/types";
14
+ import type { OhlcvCandle, OhlcvParams } from "../../utils/prices/types";
15
15
  import { MarketRates, RatesParams } from "../../utils/rates/types";
16
16
  import type { IAbstractSigner } from "../../utils/signer";
17
17
  import { StakingPowerResponse } from "../../utils/staking/types";
18
+ import type { StreamCandlePeriod, Subscription, WebSocketCtor } from "../../utils/stream";
18
19
  import type { SubaccountStatusRequest, SubaccountStatusResponse, SubaccountApprovalPrepareRequest, SubaccountApprovalPrepareResponse } from "../../utils/subaccount/api";
19
20
  import type { SdkSubaccountApproval, SdkSubaccountStatus } from "../../utils/subaccount/types";
21
+ import type { TokenPricesData } from "../../utils/tokens/types";
20
22
  import type { FetchTradesParams, SearchTradesParams, TradesListResponse } from "../../utils/trades/types";
21
23
  import type { UsdgBoostAprResponse } from "../../utils/usdgBoostApr/types";
22
24
  import type { AccountWhitelists } from "../../utils/whitelists/types";
@@ -47,6 +49,7 @@ export { PrivateKeySigner } from "../../utils/signer";
47
49
  export { HttpError } from "../../utils/http/http";
48
50
  export { HttpClientWithFallback } from "../../utils/http/httpFallback";
49
51
  export type { IHttp } from "../../utils/http/types";
52
+ export type { FrameMeta, StreamCandlePeriod, StreamConnectionStatus, Subscription, WebSocketCtor } from "../../utils/stream";
50
53
  export { getGasPaymentTokens } from "../../configs/express";
51
54
  export type { SubaccountStatusRequest, SubaccountStatusResponse, SubaccountApprovalPrepareRequest, SubaccountApprovalPrepareResponse, };
52
55
  export type { SdkSubaccountStatus } from "../../utils/subaccount/types";
@@ -57,10 +60,15 @@ export declare class GmxApiSdk {
57
60
  };
58
61
  private _subaccount;
59
62
  private preparedSubaccountApprovals;
60
- constructor({ chainId, apiUrl, api }: {
63
+ private readonly streamUrlOverride?;
64
+ private readonly webSocketImpl?;
65
+ private _streamClient?;
66
+ constructor({ chainId, apiUrl, api, streamUrl, webSocketImpl, }: {
61
67
  chainId: ContractsChainId;
62
68
  apiUrl?: string;
63
69
  api?: IHttp;
70
+ streamUrl?: string;
71
+ webSocketImpl?: WebSocketCtor;
64
72
  });
65
73
  private get subaccountClient();
66
74
  get subaccountAddress(): string | undefined;
@@ -76,8 +84,18 @@ export declare class GmxApiSdk {
76
84
  symbols?: string[];
77
85
  }): Promise<MarketTickerWithCapacity[]>;
78
86
  getTradingCapacity(params: GetTradingCapacityParams): Promise<TradingCapacity>;
79
- fetchTokensData(): Promise<import("../../utils/tokens").TokenData[]>;
80
- fetchTokens(): Promise<import("../../utils/tokens").Token[]>;
87
+ fetchTokensData(): Promise<import("../../utils/tokens/types").TokenData[]>;
88
+ fetchTokens(): Promise<import("../../utils/tokens/types").Token[]>;
89
+ private getStreamClient;
90
+ fetchTokenPrices(): Promise<{
91
+ prices: TokenPricesData;
92
+ originTs: number;
93
+ }>;
94
+ watchTokenPrices(): Subscription<TokenPricesData>;
95
+ watchCandles(params: {
96
+ symbol: string;
97
+ timeframe: StreamCandlePeriod;
98
+ }): Subscription<OhlcvCandle>;
81
99
  fetchPositionsInfo(params: {
82
100
  address: string;
83
101
  includeRelatedOrders?: boolean;
@@ -87,7 +105,7 @@ export declare class GmxApiSdk {
87
105
  }): Promise<import("../../utils/orders").ApiOrderInfo[]>;
88
106
  fetchTrades(params: FetchTradesParams): Promise<TradesListResponse>;
89
107
  searchTrades(params: SearchTradesParams): Promise<TradesListResponse>;
90
- fetchOhlcv(params: OhlcvParams): Promise<import("../../utils/prices/types").OhlcvCandle[]>;
108
+ fetchOhlcv(params: OhlcvParams): Promise<OhlcvCandle[]>;
91
109
  fetchPairs(): Promise<import("../../utils/pairs/types").Pair[]>;
92
110
  fetchRates(params?: RatesParams): Promise<MarketRates[]>;
93
111
  fetchApy(params?: ApyParams): Promise<ApyResponse>;
@@ -1,5 +1,13 @@
1
1
  import { IHttp } from "../http/types";
2
+ import type { TokenPricesData } from "../tokens/types";
2
3
  import { OhlcvCandle, OhlcvParams } from "./types";
3
4
  export declare function fetchApiOhlcv(ctx: {
4
5
  api: IHttp;
5
6
  }, params: OhlcvParams): Promise<OhlcvCandle[]>;
7
+ export declare function parseApiTokenPrices(raw: unknown): TokenPricesData;
8
+ export declare function fetchApiTokenPrices(ctx: {
9
+ api: IHttp;
10
+ }): Promise<{
11
+ prices: TokenPricesData;
12
+ originTs: number;
13
+ }>;
@@ -0,0 +1,63 @@
1
+ import { FrameMeta, StreamConnectionStatus, Unsubscribe, WebSocketCtor } from "./types";
2
+ export type ChannelFrame = {
3
+ data: unknown;
4
+ } & FrameMeta;
5
+ type ChannelListener = (frame: ChannelFrame) => void;
6
+ type ErrorListener = (error: {
7
+ message: string;
8
+ }) => void;
9
+ export declare function callListener<T>(listener: (value: T) => void, value: T): void;
10
+ export declare class WsStreamClient {
11
+ connectionStatus: StreamConnectionStatus;
12
+ private ws?;
13
+ private readonly url;
14
+ private readonly WebSocketImpl;
15
+ private readonly reconnectBaseMs;
16
+ private readonly reconnectMaxMs;
17
+ private readonly probeIntervalMs;
18
+ private readonly probeTimeoutMs;
19
+ private readonly random;
20
+ private readonly listeners;
21
+ private readonly connectionStatusListeners;
22
+ private readonly lastFrameByChannel;
23
+ private readonly originTsByChannel;
24
+ private readonly refusalByChannel;
25
+ private pendingOps;
26
+ private reconnectAttempt;
27
+ private reconnectTimer?;
28
+ private probeTimer?;
29
+ private probeTimeoutTimer?;
30
+ private connectTimeoutTimer?;
31
+ private idleCloseTimer?;
32
+ private messageCount;
33
+ private openedAt?;
34
+ private closedByUser;
35
+ constructor(params: {
36
+ url: string;
37
+ webSocketImpl?: WebSocketCtor;
38
+ reconnectBaseMs?: number;
39
+ reconnectMaxMs?: number;
40
+ probeIntervalMs?: number;
41
+ probeTimeoutMs?: number;
42
+ random?: () => number;
43
+ });
44
+ subscribe(channel: string, listener: ChannelListener, onError?: ErrorListener): Unsubscribe;
45
+ subscribeConnectionStatus(listener: (status: StreamConnectionStatus) => void): Unsubscribe;
46
+ private ensureConnected;
47
+ private canConnect;
48
+ private connect;
49
+ private onMessage;
50
+ private onOp;
51
+ private refuse;
52
+ private replay;
53
+ private onClose;
54
+ private noteSocketEnded;
55
+ private scheduleReconnect;
56
+ private startProbe;
57
+ private stopProbe;
58
+ private sendOp;
59
+ private setConnectionStatus;
60
+ private dropSocket;
61
+ close(): void;
62
+ }
63
+ export {};
@@ -0,0 +1,5 @@
1
+ export { WsStreamClient } from "./WsStreamClient";
2
+ export type { ChannelFrame } from "./WsStreamClient";
3
+ export { createChannelSubscription } from "./subscription";
4
+ export type { FrameMeta, StreamCandlePeriod, StreamConnectionStatus, Subscription, Unsubscribe, WebSocketCtor, WebSocketLike, } from "./types";
5
+ export declare function toStreamUrl(apiUrl: string): string;
@@ -0,0 +1,3 @@
1
+ import { Subscription } from "./types";
2
+ import { WsStreamClient } from "./WsStreamClient";
3
+ export declare function createChannelSubscription<T>(client: WsStreamClient, channel: string, transform: (raw: unknown) => T): Subscription<T>;
@@ -0,0 +1,43 @@
1
+ export type StreamConnectionStatus = "connecting" | "live" | "reconnecting" | "closed";
2
+ export type StreamCandlePeriod = "1m" | "5m" | "15m" | "1h" | "4h" | "1d";
3
+ export type Unsubscribe = () => void;
4
+ export type FrameMeta = {
5
+ serverTs: number;
6
+ receivedAt: number;
7
+ originTs?: number;
8
+ };
9
+ export interface Subscription<T> {
10
+ get(): T | undefined;
11
+ getMeta(): FrameMeta | undefined;
12
+ subscribe(listener: (value: T) => void): Unsubscribe;
13
+ subscribeConnectionStatus(listener: (status: StreamConnectionStatus) => void): Unsubscribe;
14
+ subscribeRefusal(listener: (error: {
15
+ message: string;
16
+ }) => void): Unsubscribe;
17
+ readonly connectionStatus: StreamConnectionStatus;
18
+ close(): void;
19
+ }
20
+ export interface WebSocketLike {
21
+ readyState: number;
22
+ send(data: string): void;
23
+ close(code?: number, reason?: string): void;
24
+ onopen: ((ev: any) => void) | null;
25
+ onmessage: ((ev: any) => void) | null;
26
+ onclose: ((ev: any) => void) | null;
27
+ onerror: ((ev: any) => void) | null;
28
+ }
29
+ export type WebSocketCtor = new (url: string, ...rest: any[]) => WebSocketLike;
30
+ export type StreamServerFrame = {
31
+ op: "ack";
32
+ channels: string[];
33
+ } | {
34
+ op: "error";
35
+ message: string;
36
+ channels: string[];
37
+ } | {
38
+ ch: string;
39
+ type: "snapshot";
40
+ serverTs: number;
41
+ originTs?: number;
42
+ data: unknown;
43
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gmx-io/sdk",
3
- "version": "2.7.0",
3
+ "version": "2.8.0",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -39,6 +39,9 @@
39
39
  "types/markets": [
40
40
  "./build/types/src/utils/markets/types.d.ts"
41
41
  ],
42
+ "utils/stream": [
43
+ "./build/types/src/utils/stream/index.d.ts"
44
+ ],
42
45
  "utils/prices": [
43
46
  "./build/types/src/utils/prices/index.d.ts"
44
47
  ],
@@ -216,6 +219,12 @@
216
219
  "require": "./build/cjs/src/utils/markets/index.js",
217
220
  "default": "./build/cjs/src/utils/markets/index.js"
218
221
  },
222
+ "./utils/stream": {
223
+ "types": "./build/types/src/utils/stream/index.d.ts",
224
+ "import": "./build/esm/src/utils/stream/index.js",
225
+ "require": "./build/cjs/src/utils/stream/index.js",
226
+ "default": "./build/cjs/src/utils/stream/index.js"
227
+ },
219
228
  "./types/markets": {
220
229
  "types": "./build/types/src/utils/markets/types.d.ts",
221
230
  "import": "./build/esm/src/utils/markets/types.js",
@@ -535,11 +544,13 @@
535
544
  "crypto-js": "4.2.0",
536
545
  "graphql": "15.8.0",
537
546
  "isomorphic-performance": "5.1.1",
547
+ "isomorphic-ws": "^5.0.0",
538
548
  "lodash": "4.18.1",
539
549
  "query-string": "7.1.1",
540
550
  "typescript": "5.4.2",
541
551
  "universal-perf-hooks": "1.0.1",
542
- "viem": "^2.48.11"
552
+ "viem": "^2.48.11",
553
+ "ws": "8.22.0"
543
554
  },
544
555
  "devDependencies": {
545
556
  "@babel/plugin-transform-private-property-in-object": "7.25.9",
@@ -549,6 +560,7 @@
549
560
  "@graphql-codegen/typescript-operations": "4.6.1",
550
561
  "@types/lodash": "4.14.198",
551
562
  "@types/node": "18.7.13",
563
+ "@types/ws": "^8.5.10",
552
564
  "lint-staged": "12.3.4",
553
565
  "prettier": "3.2.5",
554
566
  "ts-patch": "3.2.1",