@gmx-io/sdk 2.6.0-next.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 (50) hide show
  1. package/README.md +25 -0
  2. package/build/cjs/src/abis/ChainlinkPriceFeedProvider.js +27 -0
  3. package/build/cjs/src/abis/Pausable.js +11 -0
  4. package/build/cjs/src/abis/index.js +4 -0
  5. package/build/cjs/src/clients/v2/index.js +20 -1
  6. package/build/cjs/src/configs/express.js +1 -0
  7. package/build/cjs/src/configs/tokens.js +3 -0
  8. package/build/cjs/src/utils/__tests__/numbers.spec.js +15 -0
  9. package/build/cjs/src/utils/multichain/__tests__/api.spec.js +11 -1
  10. package/build/cjs/src/utils/multichain/api.js +3 -2
  11. package/build/cjs/src/utils/numbers/utils.js +8 -2
  12. package/build/cjs/src/utils/prices/api.js +15 -1
  13. package/build/cjs/src/utils/stream/WsStreamClient.js +363 -0
  14. package/build/cjs/src/utils/stream/__tests__/WsStreamClient.spec.js +383 -0
  15. package/build/cjs/src/utils/stream/__tests__/stream.integration.spec.js +133 -0
  16. package/build/cjs/src/utils/stream/index.js +11 -0
  17. package/build/cjs/src/utils/stream/subscription.js +61 -0
  18. package/build/cjs/src/utils/stream/types.js +2 -0
  19. package/build/esm/src/abis/ChainlinkPriceFeedProvider.js +25 -0
  20. package/build/esm/src/abis/Pausable.js +9 -0
  21. package/build/esm/src/abis/index.js +4 -0
  22. package/build/esm/src/clients/v2/index.js +21 -2
  23. package/build/esm/src/configs/express.js +1 -0
  24. package/build/esm/src/configs/tokens.js +3 -0
  25. package/build/esm/src/utils/__tests__/numbers.spec.js +15 -0
  26. package/build/esm/src/utils/multichain/__tests__/api.spec.js +11 -1
  27. package/build/esm/src/utils/multichain/api.js +2 -1
  28. package/build/esm/src/utils/numbers/utils.js +8 -2
  29. package/build/esm/src/utils/prices/api.js +12 -0
  30. package/build/esm/src/utils/stream/WsStreamClient.js +355 -0
  31. package/build/esm/src/utils/stream/__tests__/WsStreamClient.spec.js +381 -0
  32. package/build/esm/src/utils/stream/__tests__/stream.integration.spec.js +131 -0
  33. package/build/esm/src/utils/stream/index.js +5 -0
  34. package/build/esm/src/utils/stream/subscription.js +57 -0
  35. package/build/esm/src/utils/stream/types.js +1 -0
  36. package/build/tsconfig.tsbuildinfo +1 -1
  37. package/build/types/src/abis/ChainlinkPriceFeedProvider.d.ts +41 -0
  38. package/build/types/src/abis/Pausable.d.ts +12 -0
  39. package/build/types/src/abis/index.d.ts +51 -0
  40. package/build/types/src/clients/v2/index.d.ts +23 -5
  41. package/build/types/src/utils/multichain/api.d.ts +2 -1
  42. package/build/types/src/utils/orderTransactions/api.d.ts +2 -0
  43. package/build/types/src/utils/prices/api.d.ts +8 -0
  44. package/build/types/src/utils/stream/WsStreamClient.d.ts +63 -0
  45. package/build/types/src/utils/stream/__tests__/WsStreamClient.spec.d.ts +1 -0
  46. package/build/types/src/utils/stream/__tests__/stream.integration.spec.d.ts +1 -0
  47. package/build/types/src/utils/stream/index.d.ts +5 -0
  48. package/build/types/src/utils/stream/subscription.d.ts +3 -0
  49. package/build/types/src/utils/stream/types.d.ts +43 -0
  50. package/package.json +14 -2
@@ -0,0 +1,41 @@
1
+ declare const _default: readonly [{
2
+ readonly inputs: readonly [{
3
+ readonly internalType: "address";
4
+ readonly name: "token";
5
+ readonly type: "address";
6
+ }, {
7
+ readonly internalType: "bytes";
8
+ readonly name: "";
9
+ readonly type: "bytes";
10
+ }];
11
+ readonly name: "getOraclePrice";
12
+ readonly outputs: readonly [{
13
+ readonly components: readonly [{
14
+ readonly internalType: "address";
15
+ readonly name: "token";
16
+ readonly type: "address";
17
+ }, {
18
+ readonly internalType: "uint256";
19
+ readonly name: "min";
20
+ readonly type: "uint256";
21
+ }, {
22
+ readonly internalType: "uint256";
23
+ readonly name: "max";
24
+ readonly type: "uint256";
25
+ }, {
26
+ readonly internalType: "uint256";
27
+ readonly name: "timestamp";
28
+ readonly type: "uint256";
29
+ }, {
30
+ readonly internalType: "address";
31
+ readonly name: "provider";
32
+ readonly type: "address";
33
+ }];
34
+ readonly internalType: "struct OracleUtils.ValidatedPrice";
35
+ readonly name: "";
36
+ readonly type: "tuple";
37
+ }];
38
+ readonly stateMutability: "view";
39
+ readonly type: "function";
40
+ }];
41
+ export default _default;
@@ -0,0 +1,12 @@
1
+ declare const _default: readonly [{
2
+ readonly inputs: readonly [];
3
+ readonly name: "paused";
4
+ readonly outputs: readonly [{
5
+ readonly internalType: "bool";
6
+ readonly name: "";
7
+ readonly type: "bool";
8
+ }];
9
+ readonly stateMutability: "view";
10
+ readonly type: "function";
11
+ }];
12
+ export default _default;
@@ -238,6 +238,46 @@ export declare const abis: {
238
238
  readonly stateMutability: "view";
239
239
  readonly type: "function";
240
240
  }];
241
+ ChainlinkPriceFeedProvider: readonly [{
242
+ readonly inputs: readonly [{
243
+ readonly internalType: "address";
244
+ readonly name: "token";
245
+ readonly type: "address";
246
+ }, {
247
+ readonly internalType: "bytes";
248
+ readonly name: "";
249
+ readonly type: "bytes";
250
+ }];
251
+ readonly name: "getOraclePrice";
252
+ readonly outputs: readonly [{
253
+ readonly components: readonly [{
254
+ readonly internalType: "address";
255
+ readonly name: "token";
256
+ readonly type: "address";
257
+ }, {
258
+ readonly internalType: "uint256";
259
+ readonly name: "min";
260
+ readonly type: "uint256";
261
+ }, {
262
+ readonly internalType: "uint256";
263
+ readonly name: "max";
264
+ readonly type: "uint256";
265
+ }, {
266
+ readonly internalType: "uint256";
267
+ readonly name: "timestamp";
268
+ readonly type: "uint256";
269
+ }, {
270
+ readonly internalType: "address";
271
+ readonly name: "provider";
272
+ readonly type: "address";
273
+ }];
274
+ readonly internalType: "struct OracleUtils.ValidatedPrice";
275
+ readonly name: "";
276
+ readonly type: "tuple";
277
+ }];
278
+ readonly stateMutability: "view";
279
+ readonly type: "function";
280
+ }];
241
281
  ClaimHandler: readonly [{
242
282
  readonly inputs: readonly [{
243
283
  readonly internalType: "contract RoleStore";
@@ -22176,6 +22216,17 @@ export declare const abis: {
22176
22216
  readonly stateMutability: "payable";
22177
22217
  readonly type: "receive";
22178
22218
  }];
22219
+ Pausable: readonly [{
22220
+ readonly inputs: readonly [];
22221
+ readonly name: "paused";
22222
+ readonly outputs: readonly [{
22223
+ readonly internalType: "bool";
22224
+ readonly name: "";
22225
+ readonly type: "bool";
22226
+ }];
22227
+ readonly stateMutability: "view";
22228
+ readonly type: "function";
22229
+ }];
22179
22230
  ReferralStorage: readonly [{
22180
22231
  readonly inputs: readonly [{
22181
22232
  readonly internalType: "address";
@@ -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>;
@@ -47,6 +47,7 @@ export type SameChainWithdrawRequest = {
47
47
  * Encodes `MultichainTransferRouter.transferOut(BridgeOutParams)` as a classic wallet tx.
48
48
  */
49
49
  export declare function buildSameChainWithdrawTxn(request: SameChainWithdrawRequest): BuildTxnResult;
50
+ export declare const DEFAULT_CROSS_CHAIN_WITHDRAW_SLIPPAGE_BPS = 50;
50
51
  /**
51
52
  * `BridgeOutParams.data` for cross-chain delivery is a single `uint32 dstEid`
52
53
  * pointing at the destination LayerZero endpoint; the on-chain provider takes
@@ -62,7 +63,7 @@ export declare function buildCrossChainWithdrawBridgeOutParams(params: {
62
63
  dstChainId: AnyChainId;
63
64
  /** Settlement-chain Stargate pool address handling this token. */
64
65
  stargateAddress: string;
65
- /** Slippage tolerance in basis points for `minAmountOut`. Default 0 = no slippage allowed. */
66
+ /** Slippage tolerance in basis points for `minAmountOut`. Default 50; 0 = no slippage allowed. */
66
67
  slippageBps?: number;
67
68
  }): BridgeOutParams;
68
69
  export declare function executeSameChainDeposit(signer: IAbstractSigner, request: SameChainDepositRequest): Promise<string>;
@@ -23,6 +23,7 @@ export type PrepareOrderRequest = {
23
23
  manualSwapPath?: string[];
24
24
  acceptablePriceImpactBps?: bigint;
25
25
  executionFeeBufferBps?: number;
26
+ skipExecutionFeePriorityFeeAllowance?: boolean;
26
27
  twapConfig?: {
27
28
  duration: number;
28
29
  parts: number;
@@ -174,6 +175,7 @@ export type PrepareCollateralRequest = {
174
175
  amount: bigint;
175
176
  slippage?: number;
176
177
  executionFeeBufferBps?: number;
178
+ skipExecutionFeePriorityFeeAllowance?: boolean;
177
179
  gasPaymentToken?: string;
178
180
  uiFeeReceiver?: string;
179
181
  mode: TransactionMode;
@@ -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.6.0-next.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",