@gmx-io/sdk 1.7.0 → 1.8.2

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 (40) hide show
  1. package/build/cjs/src/clients/v2/index.js +4 -0
  2. package/build/cjs/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +16 -0
  3. package/build/cjs/src/codegen/prebuilt/hashedMarketConfigKeys.json +136 -0
  4. package/build/cjs/src/codegen/prebuilt/hashedMarketValuesKeys.json +30 -0
  5. package/build/cjs/src/configs/express.js +1 -0
  6. package/build/cjs/src/configs/markets.js +14 -0
  7. package/build/cjs/src/configs/tokens.js +22 -0
  8. package/build/cjs/src/utils/express/utils/gelatoRelayUtils.js +2 -0
  9. package/build/cjs/src/utils/express/utils/gmxRelayUtils.js +145 -0
  10. package/build/cjs/src/utils/express/utils/gmxRelayUtils.spec.js +163 -0
  11. package/build/cjs/src/utils/express/utils/index.js +1 -0
  12. package/build/cjs/src/utils/tradeHistory/utils.js +3 -0
  13. package/build/cjs/src/utils/yield/api.js +13 -0
  14. package/build/cjs/src/utils/yield/types.js +2 -0
  15. package/build/esm/src/clients/v2/index.js +4 -0
  16. package/build/esm/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +16 -0
  17. package/build/esm/src/codegen/prebuilt/hashedMarketConfigKeys.json +136 -0
  18. package/build/esm/src/codegen/prebuilt/hashedMarketValuesKeys.json +30 -0
  19. package/build/esm/src/configs/express.js +1 -0
  20. package/build/esm/src/configs/markets.js +14 -0
  21. package/build/esm/src/configs/tokens.js +22 -0
  22. package/build/esm/src/utils/express/utils/gelatoRelayUtils.js +2 -0
  23. package/build/esm/src/utils/express/utils/gmxRelayUtils.js +137 -0
  24. package/build/esm/src/utils/express/utils/gmxRelayUtils.spec.js +161 -0
  25. package/build/esm/src/utils/express/utils/index.js +1 -0
  26. package/build/esm/src/utils/tradeHistory/utils.js +3 -0
  27. package/build/esm/src/utils/yield/api.js +9 -0
  28. package/build/esm/src/utils/yield/types.js +1 -0
  29. package/build/tsconfig.tsbuildinfo +1 -1
  30. package/build/types/src/clients/v2/index.d.ts +3 -0
  31. package/build/types/src/codegen/prebuilt/index.d.ts +182 -0
  32. package/build/types/src/configs/express.d.ts +1 -0
  33. package/build/types/src/utils/express/utils/gelatoRelayUtils.d.ts +2 -0
  34. package/build/types/src/utils/express/utils/gmxRelayUtils.d.ts +61 -0
  35. package/build/types/src/utils/express/utils/gmxRelayUtils.spec.d.ts +1 -0
  36. package/build/types/src/utils/express/utils/index.d.ts +1 -0
  37. package/build/types/src/utils/tradeHistory/types.d.ts +2 -1
  38. package/build/types/src/utils/yield/api.d.ts +5 -0
  39. package/build/types/src/utils/yield/types.d.ts +29 -0
  40. package/package.json +1 -1
@@ -0,0 +1,161 @@
1
+ import { afterEach, describe, expect, it, vi } from "vitest";
2
+ import { ARBITRUM } from "../../../configs/chains";
3
+ import { GmxRelayError, getGmxRelayTaskStatus, isPermanentRelayError, sendToGmxRelay, waitForGmxRelayTask, } from "./gmxRelayUtils";
4
+ const TASK_ID = `0x${"ab".repeat(32)}`;
5
+ const TXN_DATA = {
6
+ callData: "0xdeadbeef",
7
+ to: "0x0000000000000000000000000000000000000001",
8
+ feeToken: "0x0000000000000000000000000000000000000002",
9
+ feeAmount: 123n,
10
+ };
11
+ function jsonResponse(body, status = 200) {
12
+ return new Response(JSON.stringify(body), { status, headers: { "Content-Type": "application/json" } });
13
+ }
14
+ describe("sendToGmxRelay", () => {
15
+ afterEach(() => {
16
+ vi.unstubAllGlobals();
17
+ });
18
+ it("carries the API's trace id on a refusal", async () => {
19
+ vi.stubGlobal("fetch", vi.fn(async () => new Response(JSON.stringify({ message: "nope" }), {
20
+ status: 400,
21
+ headers: { "Content-Type": "application/json", "X-Trace-Id": "tr-1" },
22
+ })));
23
+ const error = await sendToGmxRelay({ chainId: ARBITRUM, txnData: TXN_DATA }).catch((e) => e);
24
+ expect(error).toBeInstanceOf(GmxRelayError);
25
+ expect(error.data).toEqual({ traceId: "tr-1" });
26
+ });
27
+ it("posts bare calldata without the Gelato fee suffix", async () => {
28
+ const fetchMock = vi.fn(async () => jsonResponse({ taskId: TASK_ID, status: "pending" }));
29
+ vi.stubGlobal("fetch", fetchMock);
30
+ const result = await sendToGmxRelay({ chainId: ARBITRUM, txnData: TXN_DATA });
31
+ expect(result).toEqual({ taskId: TASK_ID, status: "pending" });
32
+ const [url, init] = fetchMock.mock.calls[0];
33
+ expect(url).toContain("/v1/relay/submit");
34
+ expect(JSON.parse(String(init.body))).toEqual({ to: TXN_DATA.to, data: TXN_DATA.callData });
35
+ });
36
+ it("honours an explicit api url", async () => {
37
+ const fetchMock = vi.fn(async () => jsonResponse({ taskId: TASK_ID, status: "pending" }));
38
+ vi.stubGlobal("fetch", fetchMock);
39
+ await sendToGmxRelay({ chainId: ARBITRUM, txnData: TXN_DATA, apiUrl: "https://api.example.com/" });
40
+ const [url] = fetchMock.mock.calls[0];
41
+ expect(url).toBe("https://api.example.com/v1/relay/submit");
42
+ });
43
+ it("raises a permanent error when the api rejects the operation", async () => {
44
+ vi.stubGlobal("fetch", vi.fn(async () => jsonResponse({ message: "to is not an allowlisted relay router" }, 400)));
45
+ const error = await sendToGmxRelay({ chainId: ARBITRUM, txnData: TXN_DATA }).catch((e) => e);
46
+ expect(error).toBeInstanceOf(GmxRelayError);
47
+ expect(error.message).toContain("allowlisted relay router");
48
+ expect(isPermanentRelayError(error)).toBe(true);
49
+ });
50
+ it("treats a rate-limited response as retryable rather than permanent", async () => {
51
+ vi.stubGlobal("fetch", vi.fn(async () => jsonResponse({ message: "slow down" }, 429)));
52
+ const error = await sendToGmxRelay({ chainId: ARBITRUM, txnData: TXN_DATA }).catch((e) => e);
53
+ expect(isPermanentRelayError(error)).toBe(false);
54
+ });
55
+ it("keeps a transport failure retryable", async () => {
56
+ vi.stubGlobal("fetch", vi.fn(async () => {
57
+ throw new Error("network down");
58
+ }));
59
+ const error = await sendToGmxRelay({ chainId: ARBITRUM, txnData: TXN_DATA }).catch((e) => e);
60
+ expect(error).toBeInstanceOf(GmxRelayError);
61
+ expect(isPermanentRelayError(error)).toBe(false);
62
+ });
63
+ });
64
+ describe("getGmxRelayTaskStatus", () => {
65
+ afterEach(() => {
66
+ vi.unstubAllGlobals();
67
+ });
68
+ it("posts the taskId and returns the status view", async () => {
69
+ const fetchMock = vi.fn(async () => jsonResponse({ taskId: TASK_ID, status: "executed", txHash: "0xhash" }));
70
+ vi.stubGlobal("fetch", fetchMock);
71
+ const view = await getGmxRelayTaskStatus({ chainId: ARBITRUM, taskId: TASK_ID });
72
+ expect(view.status).toBe("executed");
73
+ const [url, init] = fetchMock.mock.calls[0];
74
+ expect(url).toContain("/v1/relay/status");
75
+ expect(JSON.parse(String(init.body))).toEqual({ taskId: TASK_ID });
76
+ });
77
+ });
78
+ describe("waitForGmxRelayTask", () => {
79
+ afterEach(() => {
80
+ vi.unstubAllGlobals();
81
+ });
82
+ it("polls until the operation leaves the pending state", async () => {
83
+ let call = 0;
84
+ vi.stubGlobal("fetch", vi.fn(async () => {
85
+ call++;
86
+ return call < 3
87
+ ? jsonResponse({ taskId: TASK_ID, status: "pending" })
88
+ : jsonResponse({ taskId: TASK_ID, status: "executed", txHash: "0xhash" });
89
+ }));
90
+ const result = await waitForGmxRelayTask({ chainId: ARBITRUM, taskId: TASK_ID, pollingInterval: 1 });
91
+ expect(result.status).toBe("success");
92
+ expect(result.transactionHash).toBe("0xhash");
93
+ expect(call).toBe(3);
94
+ });
95
+ it("keeps polling through a transient status failure", async () => {
96
+ let call = 0;
97
+ vi.stubGlobal("fetch", vi.fn(async () => {
98
+ call++;
99
+ if (call === 1)
100
+ throw new Error("network blip");
101
+ if (call === 2)
102
+ return jsonResponse({ message: "upstream" }, 503);
103
+ if (call === 3)
104
+ return jsonResponse({ message: "no relay operation" }, 404);
105
+ return jsonResponse({ taskId: TASK_ID, status: "executed", txHash: "0xhash" });
106
+ }));
107
+ const result = await waitForGmxRelayTask({ chainId: ARBITRUM, taskId: TASK_ID, pollingInterval: 1 });
108
+ expect(result.status).toBe("success");
109
+ expect(result.transactionHash).toBe("0xhash");
110
+ expect(call).toBe(4);
111
+ });
112
+ it("gives up immediately when the status request itself is rejected", async () => {
113
+ const fetchMock = vi.fn(async () => jsonResponse({ message: "taskId must be a 32-byte hex string" }, 400));
114
+ vi.stubGlobal("fetch", fetchMock);
115
+ const error = await waitForGmxRelayTask({ chainId: ARBITRUM, taskId: TASK_ID, pollingInterval: 1 }).catch((e) => e);
116
+ expect(error).toBeInstanceOf(GmxRelayError);
117
+ expect(fetchMock).toHaveBeenCalledTimes(1);
118
+ });
119
+ it("reports an unresolved status rather than a failure when the window closes on errors", async () => {
120
+ vi.stubGlobal("fetch", vi.fn(async () => jsonResponse({ message: "upstream" }, 503)));
121
+ const result = await waitForGmxRelayTask({
122
+ chainId: ARBITRUM,
123
+ taskId: TASK_ID,
124
+ pollingInterval: 1,
125
+ timeout: 5,
126
+ });
127
+ expect(result.status).toBe("pending");
128
+ expect(result.message).toContain("Could not read");
129
+ });
130
+ it("reports a revert as a failure carrying the reason and the raw revert bytes", async () => {
131
+ vi.stubGlobal("fetch", vi.fn(async () => jsonResponse({
132
+ taskId: TASK_ID,
133
+ status: "reverted",
134
+ txHash: "0xhash",
135
+ reason: "InsufficientRelayFee",
136
+ revertData: "0x08c379a0deadbeef",
137
+ })));
138
+ const result = await waitForGmxRelayTask({ chainId: ARBITRUM, taskId: TASK_ID, pollingInterval: 1 });
139
+ expect(result.status).toBe("failed");
140
+ expect(result.relayStatus).toBe("reverted");
141
+ expect(result.message).toBe("InsufficientRelayFee");
142
+ expect(result.revertData).toBe("0x08c379a0deadbeef");
143
+ });
144
+ it("never resolves an undetermined outcome as success or failure", async () => {
145
+ vi.stubGlobal("fetch", vi.fn(async () => jsonResponse({ taskId: TASK_ID, status: "unknown", txHash: "0xhash" })));
146
+ const result = await waitForGmxRelayTask({ chainId: ARBITRUM, taskId: TASK_ID, pollingInterval: 1 });
147
+ expect(result.status).toBe("pending");
148
+ expect(result.relayStatus).toBe("unknown");
149
+ });
150
+ it("gives up as pending once the wait window elapses", async () => {
151
+ vi.stubGlobal("fetch", vi.fn(async () => jsonResponse({ taskId: TASK_ID, status: "pending" })));
152
+ const result = await waitForGmxRelayTask({
153
+ chainId: ARBITRUM,
154
+ taskId: TASK_ID,
155
+ timeout: 5,
156
+ pollingInterval: 10,
157
+ });
158
+ expect(result.status).toBe("pending");
159
+ expect(result.message).toMatch(/Timed out/);
160
+ });
161
+ });
@@ -3,6 +3,7 @@ export * from "./oracleParamsUtils";
3
3
  export * from "./relayParamsUtils";
4
4
  export * from "./batchOrderUtils";
5
5
  export * from "./gelatoRelayUtils";
6
+ export * from "./gmxRelayUtils";
6
7
  export * from "./globalExpressParams";
7
8
  export * from "./expressParamsUtils";
8
9
  export * from "./estimateExpressParams";
@@ -114,6 +114,9 @@ export function createRawTradeActionTransformer(marketsInfoData, wrappedToken, t
114
114
  ? parseContractPrice(BigInt(rawAction.indexTokenPriceMax), indexToken.decimals)
115
115
  : undefined,
116
116
  orderType,
117
+ decreasePositionSwapType: rawAction.decreasePositionSwapType === null || rawAction.decreasePositionSwapType === undefined
118
+ ? undefined
119
+ : Number(rawAction.decreasePositionSwapType),
117
120
  orderKey: rawAction.orderKey,
118
121
  isLong: rawAction.isLong,
119
122
  pnlUsd: rawAction.pnlUsd ? BigInt(rawAction.pnlUsd) : undefined,
@@ -0,0 +1,9 @@
1
+ export async function fetchApiGmPoolYieldPnl(ctx, params) {
2
+ return ctx.api.fetchJson("/v1/yield/gm-pools", {
3
+ query: {
4
+ period: params?.period,
5
+ pools: params?.pools,
6
+ includeComponents: params?.includeComponents,
7
+ },
8
+ });
9
+ }
@@ -0,0 +1 @@
1
+ export {};