@gmx-io/sdk 1.6.4 → 1.7.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 (84) hide show
  1. package/README.md +48 -0
  2. package/build/cjs/scripts/subsquid-codegen.js +1 -1
  3. package/build/cjs/src/clients/v1/testUtil.js +1 -1
  4. package/build/cjs/src/clients/v2/__tests__/data.spec.js +5 -5
  5. package/build/cjs/src/clients/v2/__tests__/decrease.spec.js +6 -6
  6. package/build/cjs/src/clients/v2/__tests__/editCancel.spec.js +12 -12
  7. package/build/cjs/src/clients/v2/__tests__/errors.spec.js +1 -47
  8. package/build/cjs/src/clients/v2/__tests__/errorsLifecycle.spec.js +53 -0
  9. package/build/cjs/src/clients/v2/__tests__/globalCleanup.js +80 -0
  10. package/build/cjs/src/clients/v2/__tests__/increase.spec.js +14 -14
  11. package/build/cjs/src/clients/v2/__tests__/multichain.spec.js +5 -6
  12. package/build/cjs/src/clients/v2/__tests__/prepareErrors.spec.js +167 -0
  13. package/build/cjs/src/clients/v2/__tests__/setupAllowance.js +32 -0
  14. package/build/cjs/src/clients/v2/__tests__/subaccount.spec.js +16 -123
  15. package/build/cjs/src/clients/v2/__tests__/subaccountLifecycle.spec.js +108 -0
  16. package/build/cjs/src/clients/v2/__tests__/testUtil.js +38 -5
  17. package/build/cjs/src/clients/v2/__tests__/tradingCapacity.spec.js +371 -0
  18. package/build/cjs/src/clients/v2/index.js +6 -1
  19. package/build/cjs/src/codegen/subsquid.js +284 -20
  20. package/build/cjs/src/configs/__tests__/markets.spec.js +8 -0
  21. package/build/cjs/src/utils/__tests__/markets.spec.js +60 -0
  22. package/build/cjs/src/utils/__tests__/numbers.spec.js +39 -0
  23. package/build/cjs/src/utils/__tests__/positionPnlCap.spec.js +78 -0
  24. package/build/cjs/src/utils/__tests__/positions.spec.js +16 -4
  25. package/build/cjs/src/utils/markets/api.js +90 -2
  26. package/build/cjs/src/utils/markets/multicall.js +4 -6
  27. package/build/cjs/src/utils/markets/types.js +8 -0
  28. package/build/cjs/src/utils/markets/utils.js +67 -9
  29. package/build/cjs/src/utils/numbers/utils.js +28 -9
  30. package/build/cjs/src/utils/orderTransactions/api.js +160 -2
  31. package/build/cjs/src/utils/positions/utils.js +1 -1
  32. package/build/cjs/src/utils/tradeHistory/__tests__/utils.spec.js +64 -0
  33. package/build/cjs/src/utils/tradeHistory/utils.js +2 -0
  34. package/build/esm/scripts/subsquid-codegen.js +1 -1
  35. package/build/esm/src/clients/v1/testUtil.js +1 -1
  36. package/build/esm/src/clients/v2/__tests__/data.spec.js +6 -6
  37. package/build/esm/src/clients/v2/__tests__/decrease.spec.js +7 -7
  38. package/build/esm/src/clients/v2/__tests__/editCancel.spec.js +13 -13
  39. package/build/esm/src/clients/v2/__tests__/errors.spec.js +2 -48
  40. package/build/esm/src/clients/v2/__tests__/errorsLifecycle.spec.js +51 -0
  41. package/build/esm/src/clients/v2/__tests__/globalCleanup.js +77 -0
  42. package/build/esm/src/clients/v2/__tests__/increase.spec.js +15 -15
  43. package/build/esm/src/clients/v2/__tests__/multichain.spec.js +6 -7
  44. package/build/esm/src/clients/v2/__tests__/prepareErrors.spec.js +165 -0
  45. package/build/esm/src/clients/v2/__tests__/setupAllowance.js +30 -0
  46. package/build/esm/src/clients/v2/__tests__/subaccount.spec.js +17 -124
  47. package/build/esm/src/clients/v2/__tests__/subaccountLifecycle.spec.js +106 -0
  48. package/build/esm/src/clients/v2/__tests__/testUtil.js +33 -4
  49. package/build/esm/src/clients/v2/__tests__/tradingCapacity.spec.js +369 -0
  50. package/build/esm/src/clients/v2/index.js +6 -2
  51. package/build/esm/src/codegen/subsquid.js +282 -18
  52. package/build/esm/src/configs/__tests__/markets.spec.js +9 -1
  53. package/build/esm/src/utils/__tests__/markets.spec.js +61 -1
  54. package/build/esm/src/utils/__tests__/numbers.spec.js +40 -1
  55. package/build/esm/src/utils/__tests__/positionPnlCap.spec.js +76 -0
  56. package/build/esm/src/utils/__tests__/positions.spec.js +17 -5
  57. package/build/esm/src/utils/markets/api.js +88 -2
  58. package/build/esm/src/utils/markets/multicall.js +4 -6
  59. package/build/esm/src/utils/markets/types.js +8 -0
  60. package/build/esm/src/utils/markets/utils.js +65 -8
  61. package/build/esm/src/utils/numbers/utils.js +24 -7
  62. package/build/esm/src/utils/orderTransactions/api.js +157 -1
  63. package/build/esm/src/utils/positions/utils.js +2 -2
  64. package/build/esm/src/utils/tradeHistory/__tests__/utils.spec.js +62 -0
  65. package/build/esm/src/utils/tradeHistory/utils.js +2 -0
  66. package/build/tsconfig.tsbuildinfo +1 -1
  67. package/build/types/src/clients/v2/__tests__/errorsLifecycle.spec.d.ts +1 -0
  68. package/build/types/src/clients/v2/__tests__/globalCleanup.d.ts +1 -0
  69. package/build/types/src/clients/v2/__tests__/prepareErrors.spec.d.ts +1 -0
  70. package/build/types/src/clients/v2/__tests__/setupAllowance.d.ts +1 -0
  71. package/build/types/src/clients/v2/__tests__/subaccountLifecycle.spec.d.ts +1 -0
  72. package/build/types/src/clients/v2/__tests__/testUtil.d.ts +7 -1
  73. package/build/types/src/clients/v2/__tests__/tradingCapacity.spec.d.ts +1 -0
  74. package/build/types/src/clients/v2/index.d.ts +8 -5
  75. package/build/types/src/codegen/subsquid.d.ts +1141 -51
  76. package/build/types/src/utils/__tests__/positionPnlCap.spec.d.ts +1 -0
  77. package/build/types/src/utils/markets/api.d.ts +6 -2
  78. package/build/types/src/utils/markets/types.d.ts +28 -1
  79. package/build/types/src/utils/markets/utils.d.ts +7 -8
  80. package/build/types/src/utils/numbers/utils.d.ts +2 -0
  81. package/build/types/src/utils/orderTransactions/api.d.ts +50 -0
  82. package/build/types/src/utils/tradeHistory/__tests__/utils.spec.d.ts +1 -0
  83. package/build/types/src/utils/tradeHistory/types.d.ts +2 -0
  84. package/package.json +2 -1
package/README.md CHANGED
@@ -230,3 +230,51 @@ sdk.orders.swap({
230
230
  ```
231
231
 
232
232
  Note the distinction between `payTokenAddress` and `collateralTokenAddress`. These represent the ERC20 token addresses for payment and collateral respectively. Some markets use synthetic tokens, so you'll need to provide the correct underlying token address. For example, the BTC/USD [WETH-USDC] market has a synthetic BTC token as its `indexTokenAddress`, so you should pass the WBTC address instead of BTC.
233
+
234
+ ### JIT-aware trading capacity
235
+
236
+ Use the v2 API client to get the current increase-order capacity for one market side:
237
+
238
+ ```typescript
239
+ import { GmxApiSdk } from "@gmx-io/sdk/v2";
240
+
241
+ const api = new GmxApiSdk({ chainId: 42161 });
242
+ const capacity = await api.getTradingCapacity({
243
+ symbol: "ETH/USD [WETH-USDC]",
244
+ direction: "long",
245
+ });
246
+
247
+ if (capacity.marketDataStatus !== "fresh") {
248
+ throw new Error("Trading capacity is based on stale market data");
249
+ }
250
+
251
+ const safeAccountAgnosticSize = capacity.baseAvailableLiquidity;
252
+ const indicativeGlobalCapacity =
253
+ capacity.jitDataStatus === "available" ? capacity.availableLiquidity : capacity.baseAvailableLiquidity;
254
+ ```
255
+
256
+ `availableLiquidity` is the current global JIT-aware capacity and remains capped by max open interest.
257
+ It is indicative because the keeper can restrict JIT by account. `baseAvailableLiquidity` is the native capacity without
258
+ JIT, but it is still a market-level snapshot: a collateral swap or intervening market state can reduce the capacity
259
+ available to a specific order. The keeper can use JIT for market, limit, stop-market, and TWAP increases, but availability
260
+ is evaluated again when each order executes. Do not size an order while `marketDataStatus` is `stale`. Treat
261
+ `prepareOrder` as the final request-aware validation; it returns a typed warning when capacity is not authoritative for
262
+ the specific request.
263
+
264
+ Prepare failures can be narrowed to the typed API contract:
265
+
266
+ ```typescript
267
+ import { parsePrepareOrderError } from "@gmx-io/sdk/v2";
268
+
269
+ try {
270
+ await api.prepareOrder(request);
271
+ } catch (error) {
272
+ const prepareError = parsePrepareOrderError(error);
273
+ if (prepareError?.code === "INSUFFICIENT_LIQUIDITY") {
274
+ console.log(prepareError.details.availableLiquidity, prepareError.details.requestedSizeUsd);
275
+ }
276
+ }
277
+ ```
278
+
279
+ The helper uses the capacity returned by `/v1/markets/trading-capacity`. Existing raw JIT access through
280
+ `fetchJitLiquidityInfo()` remains available for integrations that need shift data.
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const config = {
4
- schema: "https://gmx.squids.live/gmx-synthetics-arbitrum@bac941/api/graphql",
4
+ schema: "https://gmx.squids.live/gmx-synthetics-arbitrum@c9407b/api/graphql",
5
5
  overwrite: true,
6
6
  debug: true,
7
7
  generates: {
@@ -17,6 +17,6 @@ exports.arbitrumSdkConfig = {
17
17
  oracleUrl: "https://arbitrum-api.gmxinfra.io",
18
18
  rpcUrl: "https://arb1.arbitrum.io/rpc",
19
19
  walletClient: client,
20
- subsquidUrl: "https://gmx.squids.live/gmx-synthetics-arbitrum@bac941/api/graphql",
20
+ subsquidUrl: "https://gmx.squids.live/gmx-synthetics-arbitrum@c9407b/api/graphql",
21
21
  };
22
22
  exports.arbitrumSdk = new index_1.GmxSdk(exports.arbitrumSdkConfig);
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const vitest_1 = require("vitest");
4
4
  const testUtil_1 = require("./testUtil");
5
5
  const sdk = (0, testUtil_1.getTestSdk)();
6
- const signer = (0, testUtil_1.getTestSigner)();
6
+ const signer = (0, testUtil_1.getOrCreateTestSigner)();
7
7
  (0, vitest_1.describe)("GmxApiSdk — data fetching", () => {
8
8
  (0, vitest_1.it)("fetchMarketsInfo", async () => {
9
9
  const result = await sdk.fetchMarketsInfo();
@@ -53,8 +53,8 @@ const signer = (0, testUtil_1.getTestSigner)();
53
53
  });
54
54
  });
55
55
  (0, vitest_1.describe)("positions & orders", () => {
56
- const account = signer?.address;
57
- vitest_1.it.skipIf(!signer)("fetchPositionsInfo returns positions for account", async () => {
56
+ const account = signer.address;
57
+ (0, vitest_1.it)("fetchPositionsInfo returns positions for account", async () => {
58
58
  const positions = await sdk.fetchPositionsInfo({ address: account });
59
59
  (0, vitest_1.expect)(Array.isArray(positions)).toBe(true);
60
60
  if (positions.length > 0) {
@@ -63,7 +63,7 @@ const signer = (0, testUtil_1.getTestSigner)();
63
63
  (0, vitest_1.expect)(positions[0].sizeInUsd).toBeDefined();
64
64
  }
65
65
  });
66
- vitest_1.it.skipIf(!signer)("fetchPositionsInfo with includeRelatedOrders", async () => {
66
+ (0, vitest_1.it)("fetchPositionsInfo with includeRelatedOrders", async () => {
67
67
  const [without, withOrders] = await Promise.all([
68
68
  sdk.fetchPositionsInfo({ address: account }),
69
69
  sdk.fetchPositionsInfo({ address: account, includeRelatedOrders: true }),
@@ -71,7 +71,7 @@ const signer = (0, testUtil_1.getTestSigner)();
71
71
  (0, vitest_1.expect)(Array.isArray(withOrders)).toBe(true);
72
72
  (0, vitest_1.expect)(withOrders.length).toBeGreaterThanOrEqual(without.length);
73
73
  });
74
- vitest_1.it.skipIf(!signer)("fetchOrders returns orders array", async () => {
74
+ (0, vitest_1.it)("fetchOrders returns orders array", async () => {
75
75
  const orders = await sdk.fetchOrders({ address: account });
76
76
  (0, vitest_1.expect)(Array.isArray(orders)).toBe(true);
77
77
  });
@@ -191,7 +191,7 @@ async function ensureLongPosition() {
191
191
  (0, vitest_1.expect)(prepared.estimates).toBeDefined();
192
192
  });
193
193
  });
194
- (0, vitest_1.describe)("TWAP decrease", () => {
194
+ vitest_1.describe.skipIf(!(0, testUtil_1.shouldRunTwap)())("TWAP decrease", () => {
195
195
  (0, vitest_1.afterAll)(async () => {
196
196
  try {
197
197
  const prepared = await sdk.prepareCancelOrder({ all: true, mode: "express", from: account });
@@ -280,7 +280,7 @@ async function ensureLongPosition() {
280
280
  (0, vitest_1.expect)(prepared300.estimates.acceptablePrice).toBeGreaterThan(0n);
281
281
  // Size and fees unchanged between slippage variants
282
282
  (0, vitest_1.expect)(prepared30.estimates.sizeDeltaUsd).toBe(prepared300.estimates.sizeDeltaUsd);
283
- (0, vitest_1.expect)(prepared30.estimates.positionFeeUsd).toBe(prepared300.estimates.positionFeeUsd);
283
+ (0, vitest_1.expect)((0, testUtil_1.withinOneBp)(prepared30.estimates.positionFeeUsd, prepared300.estimates.positionFeeUsd)).toBe(true);
284
284
  });
285
285
  });
286
286
  (0, vitest_1.describe)("express conditional decrease", () => {
@@ -324,8 +324,8 @@ async function ensureLongPosition() {
324
324
  (0, vitest_1.expect)(prepared.estimates.executionFeeAmount).toBeGreaterThan(0n);
325
325
  (0, vitest_1.expect)(prepared.estimates.acceptablePrice).toBeLessThan(triggerPrice);
326
326
  (0, vitest_1.expect)(submitted.status).toBeDefined();
327
- const status = await (0, testUtil_1.waitForOrderStatus)(sdk, submitted.requestId);
328
- (0, vitest_1.expect)(status.status).toBe("executed");
327
+ const status = await (0, testUtil_1.waitForOrderPlaced)(sdk, submitted.requestId);
328
+ (0, vitest_1.expect)(testUtil_1.PLACED_OK_STATUSES).toContain(status.status);
329
329
  const orders = await (0, testUtil_1.waitForOrdersUpdate)(sdk, account, (o) => o.length > 0, 30000);
330
330
  (0, vitest_1.expect)(orders.length).toBeGreaterThan(0);
331
331
  });
@@ -348,8 +348,8 @@ async function ensureLongPosition() {
348
348
  (0, vitest_1.expect)(prepared.estimates.sizeDeltaUsd).toBe(testUtil_1.TEST_SIZE_USD);
349
349
  (0, vitest_1.expect)(prepared.estimates.acceptablePrice).toBeGreaterThan(0n);
350
350
  (0, vitest_1.expect)(submitted.status).toBeDefined();
351
- const status = await (0, testUtil_1.waitForOrderStatus)(sdk, submitted.requestId);
352
- (0, vitest_1.expect)(status.status).toBe("executed");
351
+ const status = await (0, testUtil_1.waitForOrderPlaced)(sdk, submitted.requestId);
352
+ (0, vitest_1.expect)(testUtil_1.PLACED_OK_STATUSES).toContain(status.status);
353
353
  const orders = await (0, testUtil_1.waitForOrdersUpdate)(sdk, account, (o) => o.length > 0, 30000);
354
354
  (0, vitest_1.expect)(orders.length).toBeGreaterThan(0);
355
355
  });
@@ -34,8 +34,8 @@ async function createLimitOrder(sdkInstance, triggerPrice = LIMIT_TRIGGER_PRICE)
34
34
  mode: "express",
35
35
  from: account,
36
36
  }, signer);
37
- const status = await (0, testUtil_1.waitForOrderStatus)(sdkInstance, result.requestId);
38
- (0, vitest_1.expect)(status.status).toBe("executed");
37
+ const status = await (0, testUtil_1.waitForOrderPlaced)(sdkInstance, result.requestId);
38
+ (0, vitest_1.expect)(testUtil_1.PLACED_OK_STATUSES).toContain(status.status);
39
39
  return result;
40
40
  }
41
41
  (0, vitest_1.describe)("edit & cancel orders", () => {
@@ -81,8 +81,8 @@ async function createLimitOrder(sdkInstance, triggerPrice = LIMIT_TRIGGER_PRICE)
81
81
  (0, vitest_1.expect)(prepared.payloadType).toBe("typed-data");
82
82
  const submitted = await signAndSubmit(sdk, prepared);
83
83
  (0, vitest_1.expect)(submitted.status).toBeDefined();
84
- const status = await (0, testUtil_1.waitForOrderStatus)(sdk, submitted.requestId);
85
- (0, vitest_1.expect)(status.status).toBe("executed");
84
+ const status = await (0, testUtil_1.waitForOrderPlaced)(sdk, submitted.requestId);
85
+ (0, vitest_1.expect)(testUtil_1.PLACED_OK_STATUSES).toContain(status.status);
86
86
  const ordersAfter = await (0, testUtil_1.waitForOrdersUpdate)(sdk, account, (ords) => {
87
87
  const edited = ords.find((o) => o.key === orderId);
88
88
  return edited ? BigInt(edited.triggerPrice) !== triggerBefore : false;
@@ -103,8 +103,8 @@ async function createLimitOrder(sdkInstance, triggerPrice = LIMIT_TRIGGER_PRICE)
103
103
  (0, vitest_1.expect)(prepared.payloadType).toBe("typed-data");
104
104
  const submitted = await signAndSubmit(sdk, prepared);
105
105
  (0, vitest_1.expect)(submitted.status).toBeDefined();
106
- const status = await (0, testUtil_1.waitForOrderStatus)(sdk, submitted.requestId);
107
- (0, vitest_1.expect)(status.status).toBe("executed");
106
+ const status = await (0, testUtil_1.waitForOrderPlaced)(sdk, submitted.requestId);
107
+ (0, vitest_1.expect)(testUtil_1.PLACED_OK_STATUSES).toContain(status.status);
108
108
  const ordersAfter = await (0, testUtil_1.waitForOrdersUpdate)(sdk, account, (ords) => {
109
109
  return !ords.find((o) => o.key === orderId);
110
110
  }, 30000);
@@ -129,8 +129,8 @@ async function createLimitOrder(sdkInstance, triggerPrice = LIMIT_TRIGGER_PRICE)
129
129
  (0, vitest_1.expect)(prepared.payloadType).toBe("typed-data");
130
130
  const submitted = await signAndSubmit(sdk, prepared);
131
131
  (0, vitest_1.expect)(submitted.status).toBeDefined();
132
- const status = await (0, testUtil_1.waitForOrderStatus)(sdk, submitted.requestId);
133
- (0, vitest_1.expect)(status.status).toBe("executed");
132
+ const status = await (0, testUtil_1.waitForOrderPlaced)(sdk, submitted.requestId);
133
+ (0, vitest_1.expect)(testUtil_1.PLACED_OK_STATUSES).toContain(status.status);
134
134
  });
135
135
  });
136
136
  (0, vitest_1.describe)("1CT edit + cancel", () => {
@@ -178,8 +178,8 @@ async function createLimitOrder(sdkInstance, triggerPrice = LIMIT_TRIGGER_PRICE)
178
178
  },
179
179
  });
180
180
  (0, vitest_1.expect)(submitted.requestId).toBeDefined();
181
- const status = await (0, testUtil_1.waitForOrderStatus)(sdk, submitted.requestId);
182
- (0, vitest_1.expect)(status.status).toBe("executed");
181
+ const status = await (0, testUtil_1.waitForOrderPlaced)(sdk, submitted.requestId);
182
+ (0, vitest_1.expect)(testUtil_1.PLACED_OK_STATUSES).toContain(status.status);
183
183
  const updatedOrders = await (0, testUtil_1.waitForOrdersUpdate)(sdk, account, (ords) => {
184
184
  const edited = ords.find((o) => o.key === orderId);
185
185
  return edited ? BigInt(edited.triggerPrice) !== triggerBefore : false;
@@ -219,8 +219,8 @@ async function createLimitOrder(sdkInstance, triggerPrice = LIMIT_TRIGGER_PRICE)
219
219
  },
220
220
  });
221
221
  (0, vitest_1.expect)(submitted.requestId).toBeDefined();
222
- const status = await (0, testUtil_1.waitForOrderStatus)(sdk, submitted.requestId);
223
- (0, vitest_1.expect)(status.status).toBe("executed");
222
+ const status = await (0, testUtil_1.waitForOrderPlaced)(sdk, submitted.requestId);
223
+ (0, vitest_1.expect)(testUtil_1.PLACED_OK_STATUSES).toContain(status.status);
224
224
  const updatedOrders = await (0, testUtil_1.waitForOrdersUpdate)(sdk, account, (ords) => {
225
225
  return !ords.find((o) => o.key === orderId);
226
226
  }, 30000);
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const vitest_1 = require("vitest");
4
4
  const testUtil_1 = require("./testUtil");
5
5
  const sdk = (0, testUtil_1.getTestSdk)();
6
- const signer = (0, testUtil_1.requireSigner)();
6
+ const signer = (0, testUtil_1.getOrCreateTestSigner)();
7
7
  const account = signer.address;
8
8
  (0, vitest_1.describe)("validation errors", () => {
9
9
  (0, vitest_1.describe)("prepare validation", () => {
@@ -214,50 +214,4 @@ const account = signer.address;
214
214
  })).rejects.toThrow();
215
215
  });
216
216
  });
217
- (0, vitest_1.describe)("order lifecycle tracking", () => {
218
- (0, vitest_1.it)("status transitions from initial to executed", async () => {
219
- const { submitted } = await (0, testUtil_1.expressFlow)(sdk, signer, {
220
- kind: "increase",
221
- symbol: testUtil_1.TEST_SYMBOL,
222
- direction: "long",
223
- orderType: "market",
224
- size: testUtil_1.TEST_SIZE_USD,
225
- collateralToken: "USDC",
226
- collateralToPay: testUtil_1.TEST_COLLATERAL,
227
- mode: "express",
228
- from: account,
229
- });
230
- const initial = await sdk.fetchOrderStatus({ requestId: submitted.requestId });
231
- (0, vitest_1.expect)(["relay_accepted", "relay_pending", "relay_submitted", "created", "executed"]).toContain(initial.status);
232
- (0, vitest_1.expect)(initial.requestId).toBe(submitted.requestId);
233
- const terminal = await (0, testUtil_1.waitForOrderStatus)(sdk, submitted.requestId);
234
- (0, vitest_1.expect)(terminal.status).toBe("executed");
235
- (0, vitest_1.expect)(terminal.requestId).toBe(submitted.requestId);
236
- });
237
- (0, vitest_1.it)("terminal status has populated fields", async () => {
238
- const { submitted } = await (0, testUtil_1.expressFlow)(sdk, signer, {
239
- kind: "increase",
240
- symbol: testUtil_1.TEST_SYMBOL,
241
- direction: "long",
242
- orderType: "market",
243
- size: testUtil_1.TEST_SIZE_USD,
244
- collateralToken: "USDC",
245
- collateralToPay: testUtil_1.TEST_COLLATERAL,
246
- mode: "express",
247
- from: account,
248
- });
249
- const status = await (0, testUtil_1.waitForOrderStatus)(sdk, submitted.requestId);
250
- (0, vitest_1.expect)(status.status).toBe("executed");
251
- (0, vitest_1.expect)(status.requestId).toBeDefined();
252
- // createdTxnHash may not be populated immediately for all executed orders
253
- if (status.createdTxnHash)
254
- (0, vitest_1.expect)(status.createdTxnHash.startsWith("0x")).toBe(true);
255
- if (status.executionTxnHash)
256
- (0, vitest_1.expect)(status.executionTxnHash.startsWith("0x")).toBe(true);
257
- if (status.createdAt)
258
- (0, vitest_1.expect)(typeof status.createdAt).toBe("string");
259
- if (status.updatedAt)
260
- (0, vitest_1.expect)(typeof status.updatedAt).toBe("string");
261
- });
262
- });
263
217
  });
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const vitest_1 = require("vitest");
4
+ const testUtil_1 = require("./testUtil");
5
+ const sdk = (0, testUtil_1.getTestSdk)();
6
+ const signer = (0, testUtil_1.requireSigner)();
7
+ const account = signer.address;
8
+ (0, vitest_1.describe)("order lifecycle tracking", () => {
9
+ (0, vitest_1.it)("status transitions from initial to executed", async () => {
10
+ const { submitted } = await (0, testUtil_1.expressFlow)(sdk, signer, {
11
+ kind: "increase",
12
+ symbol: testUtil_1.TEST_SYMBOL,
13
+ direction: "long",
14
+ orderType: "market",
15
+ size: testUtil_1.TEST_SIZE_USD,
16
+ collateralToken: "USDC",
17
+ collateralToPay: testUtil_1.TEST_COLLATERAL,
18
+ mode: "express",
19
+ from: account,
20
+ });
21
+ const initial = await sdk.fetchOrderStatus({ requestId: submitted.requestId });
22
+ (0, vitest_1.expect)(["relay_accepted", "relay_pending", "relay_submitted", "created", "executed"]).toContain(initial.status);
23
+ (0, vitest_1.expect)(initial.requestId).toBe(submitted.requestId);
24
+ const terminal = await (0, testUtil_1.waitForOrderStatus)(sdk, submitted.requestId);
25
+ (0, vitest_1.expect)(terminal.status).toBe("executed");
26
+ (0, vitest_1.expect)(terminal.requestId).toBe(submitted.requestId);
27
+ });
28
+ (0, vitest_1.it)("terminal status has populated fields", async () => {
29
+ const { submitted } = await (0, testUtil_1.expressFlow)(sdk, signer, {
30
+ kind: "increase",
31
+ symbol: testUtil_1.TEST_SYMBOL,
32
+ direction: "long",
33
+ orderType: "market",
34
+ size: testUtil_1.TEST_SIZE_USD,
35
+ collateralToken: "USDC",
36
+ collateralToPay: testUtil_1.TEST_COLLATERAL,
37
+ mode: "express",
38
+ from: account,
39
+ });
40
+ const status = await (0, testUtil_1.waitForOrderStatus)(sdk, submitted.requestId);
41
+ (0, vitest_1.expect)(status.status).toBe("executed");
42
+ (0, vitest_1.expect)(status.requestId).toBeDefined();
43
+ // createdTxnHash may not be populated immediately for all executed orders
44
+ if (status.createdTxnHash)
45
+ (0, vitest_1.expect)(status.createdTxnHash.startsWith("0x")).toBe(true);
46
+ if (status.executionTxnHash)
47
+ (0, vitest_1.expect)(status.executionTxnHash.startsWith("0x")).toBe(true);
48
+ if (status.createdAt)
49
+ (0, vitest_1.expect)(typeof status.createdAt).toBe("string");
50
+ if (status.updatedAt)
51
+ (0, vitest_1.expect)(typeof status.updatedAt).toBe("string");
52
+ });
53
+ });
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const testUtil_1 = require("./testUtil");
4
+ // The wallet is shared between runs, so anything left behind keeps holding collateral.
5
+ async function sweep(label) {
6
+ const signer = (0, testUtil_1.getTestSigner)();
7
+ if (!signer)
8
+ return;
9
+ const sdk = (0, testUtil_1.getTestSdk)();
10
+ const account = signer.address;
11
+ try {
12
+ const orders = await sdk.fetchOrders({ address: account });
13
+ if (orders.length > 0) {
14
+ const prepared = await sdk.prepareCancelOrder({ all: true, mode: "express", from: account });
15
+ const signature = await sdk.signOrder(prepared, signer);
16
+ await sdk.submitOrder({
17
+ mode: prepared.mode,
18
+ requestId: prepared.requestId,
19
+ signature,
20
+ from: account,
21
+ idempotencyKey: prepared.idempotencyKey,
22
+ eip712Data: {
23
+ batchParams: prepared.payload.batchParams,
24
+ relayParams: prepared.payload.relayParams,
25
+ },
26
+ });
27
+ // eslint-disable-next-line no-console
28
+ console.log(`[cleanup:${label}] cancelled ${orders.length} resting order(s)`);
29
+ }
30
+ }
31
+ catch (error) {
32
+ // eslint-disable-next-line no-console
33
+ console.warn(`[cleanup:${label}] cancel-all failed:`, error.message);
34
+ }
35
+ // A close can settle after the snapshot, so re-read instead of trusting one pass.
36
+ for (let attempt = 0; attempt < 4; attempt++) {
37
+ let positions;
38
+ try {
39
+ positions = await sdk.fetchPositionsInfo({ address: account });
40
+ }
41
+ catch (error) {
42
+ // eslint-disable-next-line no-console
43
+ console.warn(`[cleanup:${label}] fetchPositions failed:`, error.message);
44
+ return;
45
+ }
46
+ const open = positions.filter((p) => BigInt(p.sizeInUsd) > 0n);
47
+ if (open.length === 0)
48
+ return;
49
+ for (const position of open) {
50
+ try {
51
+ const { submitted } = await (0, testUtil_1.expressFlow)(sdk, signer, {
52
+ kind: "decrease",
53
+ symbol: position.marketAddress,
54
+ direction: position.isLong ? "long" : "short",
55
+ orderType: "market",
56
+ size: BigInt(position.sizeInUsd),
57
+ collateralToken: position.collateralTokenAddress,
58
+ // Settle into USDC, or the wallet drifts into the index token.
59
+ receiveToken: "USDC",
60
+ mode: "express",
61
+ from: account,
62
+ });
63
+ await (0, testUtil_1.waitForOrderStatus)(sdk, submitted.requestId);
64
+ // eslint-disable-next-line no-console
65
+ console.log(`[cleanup:${label}] closed position on ${position.marketAddress}`);
66
+ }
67
+ catch (error) {
68
+ // eslint-disable-next-line no-console
69
+ console.warn(`[cleanup:${label}] close failed for ${position.marketAddress}:`, error.message);
70
+ }
71
+ }
72
+ }
73
+ }
74
+ async function setup() {
75
+ await sweep("before");
76
+ return async () => {
77
+ await sweep("after");
78
+ };
79
+ }
80
+ exports.default = setup;
@@ -87,7 +87,7 @@ async function cancelAllOrders() {
87
87
  (0, vitest_1.expect)(status.requestId).toBe(submitted.requestId);
88
88
  });
89
89
  (0, vitest_1.it)("larger collateral: position fee scales with size not collateral", async () => {
90
- const collateral = { amount: 2000000n, token: "USDC" }; // 2 USDC vs 1 USDC in TEST_COLLATERAL
90
+ const collateral = { amount: testUtil_1.TEST_COLLATERAL.amount * 2n, token: "USDC" };
91
91
  const [small, large] = await Promise.all([
92
92
  sdk.prepareOrder({
93
93
  kind: "increase",
@@ -111,7 +111,7 @@ async function cancelAllOrders() {
111
111
  }),
112
112
  ]);
113
113
  // Same size → same position fee regardless of collateral amount
114
- (0, vitest_1.expect)(small.estimates.positionFeeUsd).toBe(large.estimates.positionFeeUsd);
114
+ (0, vitest_1.expect)((0, testUtil_1.withinOneBp)(small.estimates.positionFeeUsd, large.estimates.positionFeeUsd)).toBe(true);
115
115
  (0, vitest_1.expect)(small.estimates.sizeDeltaUsd).toBe(large.estimates.sizeDeltaUsd);
116
116
  });
117
117
  });
@@ -124,7 +124,7 @@ async function cancelAllOrders() {
124
124
  orderType: "market",
125
125
  size: testUtil_1.TEST_SIZE_USD,
126
126
  collateralToken: "USDC",
127
- collateralToPay: { amount: 1000000n, token: "USDC" },
127
+ collateralToPay: testUtil_1.TEST_COLLATERAL,
128
128
  mode: "express",
129
129
  from: account,
130
130
  });
@@ -144,7 +144,7 @@ async function cancelAllOrders() {
144
144
  direction: "long",
145
145
  orderType: "market",
146
146
  size: testUtil_1.TEST_SIZE_USD,
147
- collateralToPay: { amount: 1000000n, token: "USDC" },
147
+ collateralToPay: testUtil_1.TEST_COLLATERAL,
148
148
  mode: "express",
149
149
  from: account,
150
150
  });
@@ -162,7 +162,7 @@ async function cancelAllOrders() {
162
162
  orderType: "market",
163
163
  size: testUtil_1.TEST_SIZE_USD,
164
164
  collateralToken: "USDC",
165
- collateralToPay: { amount: 1000000n, token: "USDC" },
165
+ collateralToPay: testUtil_1.TEST_COLLATERAL,
166
166
  mode: "express",
167
167
  from: account,
168
168
  }),
@@ -172,7 +172,7 @@ async function cancelAllOrders() {
172
172
  direction: "long",
173
173
  orderType: "market",
174
174
  size: testUtil_1.TEST_SIZE_USD,
175
- collateralToPay: { amount: 1000000n, token: "USDC" },
175
+ collateralToPay: testUtil_1.TEST_COLLATERAL,
176
176
  mode: "express",
177
177
  from: account,
178
178
  }),
@@ -194,7 +194,7 @@ async function cancelAllOrders() {
194
194
  direction: "long",
195
195
  orderType: "market",
196
196
  size: testUtil_1.TEST_SIZE_USD,
197
- collateralToPay: { amount: 1000000n, token: "USDC" },
197
+ collateralToPay: testUtil_1.TEST_COLLATERAL,
198
198
  mode: "express",
199
199
  from: account,
200
200
  }),
@@ -204,7 +204,7 @@ async function cancelAllOrders() {
204
204
  direction: "long",
205
205
  orderType: "market",
206
206
  size: testUtil_1.TEST_SIZE_USD,
207
- collateralToPay: { amount: 1000000n, token: "USDC" },
207
+ collateralToPay: testUtil_1.TEST_COLLATERAL,
208
208
  manualSwapPath: [ETH_USD_MARKET_ADDRESS],
209
209
  mode: "express",
210
210
  from: account,
@@ -386,7 +386,7 @@ async function cancelAllOrders() {
386
386
  })).rejects.toThrow();
387
387
  });
388
388
  });
389
- (0, vitest_1.describe)("TWAP increase", () => {
389
+ vitest_1.describe.skipIf(!(0, testUtil_1.shouldRunTwap)())("TWAP increase", () => {
390
390
  (0, vitest_1.afterAll)(cancelAllOrders);
391
391
  (0, vitest_1.it)("TWAP market increase — creates sub-orders", async () => {
392
392
  const twapSize = 20n * 10n ** 30n; // $20
@@ -484,9 +484,9 @@ async function cancelAllOrders() {
484
484
  (0, vitest_1.expect)(prepared.estimates.positionFeeUsd).toBeGreaterThan(0n);
485
485
  (0, vitest_1.expect)(prepared.estimates.acceptablePrice).toBeGreaterThan(triggerPrice);
486
486
  (0, vitest_1.expect)(submitted.status).toBeDefined();
487
- const status = await (0, testUtil_1.waitForOrderStatus)(sdk, submitted.requestId);
487
+ const status = await (0, testUtil_1.waitForOrderPlaced)(sdk, submitted.requestId);
488
488
  (0, vitest_1.expect)(status.requestId).toBe(submitted.requestId);
489
- (0, vitest_1.expect)(status.status).toBe("executed");
489
+ (0, vitest_1.expect)(testUtil_1.PLACED_OK_STATUSES).toContain(status.status);
490
490
  const orders = await (0, testUtil_1.waitForOrdersUpdate)(sdk, account, (o) => o.length > 0, 30000);
491
491
  (0, vitest_1.expect)(orders.length).toBeGreaterThan(0);
492
492
  const order = orders.find((o) => o.isLong && BigInt(o.sizeDeltaUsd) > 0n);
@@ -525,7 +525,7 @@ async function cancelAllOrders() {
525
525
  sdk.prepareOrder({ ...baseRequest, slippage: 300 }),
526
526
  ]);
527
527
  (0, vitest_1.expect)(prepared30.estimates.sizeDeltaUsd).toBe(prepared300.estimates.sizeDeltaUsd);
528
- (0, vitest_1.expect)(prepared30.estimates.positionFeeUsd).toBe(prepared300.estimates.positionFeeUsd);
528
+ (0, vitest_1.expect)((0, testUtil_1.withinOneBp)(prepared30.estimates.positionFeeUsd, prepared300.estimates.positionFeeUsd)).toBe(true);
529
529
  });
530
530
  });
531
531
  (0, vitest_1.describe)("executionFeeBufferBps", () => {
@@ -552,8 +552,8 @@ async function cancelAllOrders() {
552
552
  sdk.prepareOrder({ ...baseRequest, executionFeeBufferBps: 5000 }),
553
553
  ]);
554
554
  (0, vitest_1.expect)(prepared0.estimates.sizeDeltaUsd).toBe(prepared5000.estimates.sizeDeltaUsd);
555
- (0, vitest_1.expect)(prepared0.estimates.positionFeeUsd).toBe(prepared5000.estimates.positionFeeUsd);
556
- (0, vitest_1.expect)(prepared0.estimates.acceptablePrice).toBe(prepared5000.estimates.acceptablePrice);
555
+ (0, vitest_1.expect)((0, testUtil_1.withinOneBp)(prepared0.estimates.positionFeeUsd, prepared5000.estimates.positionFeeUsd)).toBe(true);
556
+ (0, vitest_1.expect)((0, testUtil_1.withinOneBp)(prepared0.estimates.acceptablePrice, prepared5000.estimates.acceptablePrice)).toBe(true);
557
557
  });
558
558
  });
559
559
  (0, vitest_1.describe)("express convenience method", () => {
@@ -8,12 +8,11 @@ const testUtil_1 = require("./testUtil");
8
8
  (0, multichainTestUtil_1.expectArbitrumSettlement)();
9
9
  });
10
10
  const sdk = (0, testUtil_1.getTestSdk)();
11
- const signer = (0, testUtil_1.getTestSigner)();
12
- const account = signer?.address ?? "";
13
- const hasSigner = signer !== undefined;
11
+ const signer = (0, testUtil_1.getOrCreateTestSigner)();
12
+ const account = signer.address;
14
13
  const DEPOSIT_AMOUNT = 1000000n; // 1 USDC
15
14
  const WITHDRAW_AMOUNT = 500000n; // 0.5 USDC
16
- vitest_1.describe.skipIf(!hasSigner)("multichain same-chain (build only)", () => {
15
+ (0, vitest_1.describe)("multichain same-chain (build only)", () => {
17
16
  (0, vitest_1.it)("deposit: multicall(sendTokens + bridgeIn)", () => {
18
17
  const built = sdk.buildSameChainDepositTxn({
19
18
  tokenAddress: multichainTestUtil_1.USDC_ARBITRUM,
@@ -35,7 +34,7 @@ vitest_1.describe.skipIf(!hasSigner)("multichain same-chain (build only)", () =>
35
34
  (0, vitest_1.expect)(built.value).toBe(0n);
36
35
  });
37
36
  });
38
- vitest_1.describe.skipIf(!hasSigner)("multichain cross-chain deposit (Base → Arbitrum, API-driven)", () => {
37
+ (0, vitest_1.describe)("multichain cross-chain deposit (Base → Arbitrum, API-driven)", () => {
39
38
  (0, vitest_1.it)("prepare: server resolves pools + quotes nativeFee + composeGas (tokenSymbol)", async () => {
40
39
  const prepared = await sdk.prepareCrossChainDeposit({
41
40
  srcChainId: multichainTestUtil_1.TEST_SOURCE_CHAIN_ID,
@@ -79,7 +78,7 @@ vitest_1.describe.skipIf(!hasSigner)("multichain cross-chain deposit (Base → A
79
78
  (0, vitest_1.expect)(result.nativeFee).toBeGreaterThan(0n);
80
79
  });
81
80
  });
82
- vitest_1.describe.skipIf(!hasSigner)("multichain cross-chain withdraw (Arbitrum → Base, via Gelato)", () => {
81
+ (0, vitest_1.describe)("multichain cross-chain withdraw (Arbitrum → Base, via Gelato)", () => {
83
82
  (0, vitest_1.it)("prepare: returns typed-data + gasPaymentParams", async () => {
84
83
  const bridgeOutParams = sdk.buildCrossChainWithdrawBridgeOutParams({
85
84
  tokenAddress: multichainTestUtil_1.USDC_ARBITRUM,