@stxapp/stx-typescript 0.11.0 → 0.12.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.
@@ -311,22 +311,8 @@ var FEATURES = [
311
311
  "event_players",
312
312
  "leaderboard",
313
313
  "loyalty",
314
- "combos",
315
- "orders.post_only",
316
- "orders.fill_or_kill"
314
+ "combos"
317
315
  ];
318
- var API_FEATURES = {
319
- "orders.post_only": "order_post_only",
320
- "orders.fill_or_kill": "order_fill_or_kill"
321
- };
322
- function apiVersionFrom(body) {
323
- const version = body?.api_version;
324
- const features = body?.features;
325
- return {
326
- apiVersion: typeof version === "string" ? version : null,
327
- features: Array.isArray(features) ? features.filter((f) => typeof f === "string") : null
328
- };
329
- }
330
316
  var MEMBER_SCOPES = [
331
317
  "profile.read",
332
318
  "balance.read",
@@ -415,9 +401,7 @@ async function discoverCapabilities(probes) {
415
401
  swallow(probes.signedGet("/api/v1/portfolio/loyalty?limit=1")),
416
402
  swallow(probes.joinReason(`rfq:${NOBODY}`))
417
403
  ]);
418
- const info = version ? apiVersionFrom(version.status === 200 ? json(version) : null) : null;
419
- const apiVersion = info?.apiVersion;
420
- const listed = (name) => version === null ? null : version.status === 200 || version.status === 404 || version.status === 403 && json(version) === null ? !!info?.features?.includes(name) : null;
404
+ const apiVersion = version?.status === 200 ? json(version)?.api_version : null;
421
405
  const meta = oauthMetadataFrom(discovery);
422
406
  const scopes = meta ? strings(meta.scopes_supported) : [];
423
407
  const grants = meta ? strings(meta.grant_types_supported) : [];
@@ -439,9 +423,7 @@ async function discoverCapabilities(probes) {
439
423
  leaderboard: board === null ? null : board.status >= 200 && board.status < 300 ? true : board.status === 404 ? false : null,
440
424
  // Switched off answers 404 ("Loyalty program is not enabled").
441
425
  loyalty: loyalty === null ? null : loyalty.status >= 200 && loyalty.status < 300 ? true : loyalty.status === 404 ? false : null,
442
- combos: combos === null ? null : combos !== "unmatched topic",
443
- "orders.post_only": listed(API_FEATURES["orders.post_only"]),
444
- "orders.fill_or_kill": listed(API_FEATURES["orders.fill_or_kill"])
426
+ combos: combos === null ? null : combos !== "unmatched topic"
445
427
  };
446
428
  return new Capabilities({
447
429
  host: probes.host,
@@ -1274,7 +1256,7 @@ function credentialsFromSettings(keyId, privateKey, signer) {
1274
1256
  }
1275
1257
 
1276
1258
  // src/version.ts
1277
- var VERSION = "0.11.0";
1259
+ var VERSION = "0.12.0";
1278
1260
  var USER_AGENT = `stx-typescript/${VERSION}`;
1279
1261
  var ASYNC_DISPOSE = Symbol.asyncDispose ?? /* @__PURE__ */ Symbol.for("Symbol.asyncDispose");
1280
1262
 
@@ -2046,7 +2028,6 @@ var STX = class _STX {
2046
2028
  #fetch;
2047
2029
  #me;
2048
2030
  #capabilities;
2049
- #apiVersion;
2050
2031
  /** Test hook: how retries wait. */
2051
2032
  _sleep = sleep;
2052
2033
  constructor(opts = {}) {
@@ -2108,7 +2089,7 @@ var STX = class _STX {
2108
2089
  // ------------------------------------------------------------------
2109
2090
  /**
2110
2091
  * Which features this exchange has, from what it publishes: its OAuth
2111
- * discovery document, `/api_version`, a signed GET of the routes that not
2092
+ * discovery document, a signed GET of the routes that not
2112
2093
  * every exchange has yet, and a join of a combos topic nobody owns. Nothing
2113
2094
  * is created or changed. Checked once per client and cached; pass
2114
2095
  * `{ refresh: true }` to check again.
@@ -2150,81 +2131,12 @@ var STX = class _STX {
2150
2131
  clearTimeout(timer);
2151
2132
  }
2152
2133
  }
2153
- // GET /api_version, cached on the client. A 404 is an answer (no feature
2154
- // list), and so is a bare 403 from the edge (not JSON, no x-stx-error), which
2155
- // some exchanges send for this path while serving the API. Any other failure
2156
- // throws, so an outage or a block with a reason is never read as "absent".
2157
- #apiVersionInfo() {
2158
- if (!this.#apiVersion) {
2159
- const run = this.#fetchApiVersion();
2160
- this.#apiVersion = run;
2161
- run.catch(() => {
2162
- if (this.#apiVersion === run) this.#apiVersion = void 0;
2163
- });
2164
- }
2165
- return this.#apiVersion;
2166
- }
2167
- async #fetchApiVersion() {
2168
- const request = {
2169
- operationId: "api_version",
2170
- method: "GET",
2171
- path: "/api_version",
2172
- body: void 0,
2173
- idempotent: true
2174
- };
2175
- const controller = new AbortController();
2176
- const timer = setTimeout(() => controller.abort(), this.timeoutMs);
2177
- let res;
2178
- let text;
2179
- try {
2180
- res = await this.#fetch(this.baseUrl + request.path, {
2181
- method: "GET",
2182
- headers: { "User-Agent": USER_AGENT, Accept: "application/json" },
2183
- signal: controller.signal
2184
- });
2185
- text = await res.text();
2186
- } catch (err) {
2187
- throw new STXTransportException(`GET /api_version failed: ${err?.message ?? String(err)}`, {
2188
- method: "GET",
2189
- path: request.path,
2190
- cause: err
2191
- });
2192
- } finally {
2193
- clearTimeout(timer);
2194
- }
2195
- if (res.status === 404 || res.status === 403 && !res.headers.get("x-stx-error") && isEdgeBlock(403, text)) {
2196
- return { apiVersion: null, features: null };
2197
- }
2198
- return apiVersionFrom(
2199
- decodeResponse(request, res.status, text, {
2200
- retryAfter: res.headers.get("Retry-After"),
2201
- stxError: res.headers.get("x-stx-error"),
2202
- cfId: res.headers.get("x-amz-cf-id")
2203
- })
2204
- );
2205
- }
2206
- // Older builds ignore a fill_mode they do not know and would place a normal
2207
- // order, so nothing is sent unless the exchange lists the mode.
2208
- async #requireFillModes(modes) {
2209
- for (const mode of [...new Set(modes)].sort()) {
2210
- const feature = `orders.${mode}`;
2211
- const info = await this.#apiVersionInfo();
2212
- if (!info.features?.includes(API_FEATURES[feature])) {
2213
- throw new STXNotAvailableException(feature, this.host, { apiVersion: info.apiVersion });
2214
- }
2215
- }
2216
- }
2217
2134
  // A typed error for the exchange's refusals of a fill mode, else undefined.
2218
- async #fillModeError(err, mode) {
2135
+ #fillModeError(err, mode) {
2219
2136
  if (!(err instanceof STXValidationException || err instanceof STXRejectedException)) return void 0;
2220
2137
  const base = { statusCode: err.statusCode, body: err.body, method: err.method, path: err.path, cause: err };
2221
2138
  const off = NOT_ENABLED.exec(err.message);
2222
- if (off) {
2223
- const cached = this.#apiVersion;
2224
- this.#apiVersion = void 0;
2225
- const apiVersion = cached ? (await cached.catch(() => null))?.apiVersion : void 0;
2226
- return new STXNotAvailableException(`orders.${off[1] ?? off[2]}`, this.host, { ...base, apiVersion });
2227
- }
2139
+ if (off) return new STXNotAvailableException(`orders.${off[1] ?? off[2]}`, this.host, base);
2228
2140
  if (!(err instanceof STXRejectedException)) return void 0;
2229
2141
  const notAccepted = NOT_ACCEPTED.exec(err.message);
2230
2142
  if (notAccepted) return new STXFillModeNotAllowedException(err.message, { ...base, fillMode: notAccepted[1] });
@@ -2591,10 +2503,11 @@ var STX = class _STX {
2591
2503
  * comes back `accepted` (or `delayed` in play); follow it on the `orders`
2592
2504
  * channel or with `order()` to see it `filled` or `cancelled`.
2593
2505
  *
2594
- * Before sending either mode, the client checks once that the exchange offers
2595
- * it (`GET /api_version`, cached) and throws `STXNotAvailableException`
2596
- * (`orders.post_only`, `orders.fill_or_kill`) without sending the order when
2597
- * it does not. A combination market throws `STXFillModeNotAllowedException`.
2506
+ * The exchange decides whether it takes either mode. Where a mode is not
2507
+ * enabled it rejects the order and this throws `STXNotAvailableException`
2508
+ * (`orders.post_only`, `orders.fill_or_kill`). An exchange on a release from
2509
+ * before fill modes places the order as a normal limit order. A combination
2510
+ * market throws `STXFillModeNotAllowedException`.
2598
2511
  *
2599
2512
  * The exchange validates the order: a bad price step or a fractional
2600
2513
  * quantity throws `STXValidationException` (400) or `STXRejectedException`
@@ -2608,12 +2521,11 @@ var STX = class _STX {
2608
2521
  const body = orderBody(marketId, action, orderType, opts ?? {});
2609
2522
  const mode = body.fill_mode;
2610
2523
  if (mode === void 0) return this.#one("orders_post", { body });
2611
- await this.#requireFillModes([mode]);
2612
2524
  let order;
2613
2525
  try {
2614
2526
  order = await this.#one("orders_post", { body });
2615
2527
  } catch (err) {
2616
- throw await this.#fillModeError(err, mode) ?? err;
2528
+ throw this.#fillModeError(err, mode) ?? err;
2617
2529
  }
2618
2530
  if (mode === "post_only" && order.status === "cancelled" && order.cancellation_reason === "auto_matching") {
2619
2531
  throw new STXPostOnlyWouldMatchException(
@@ -2630,19 +2542,17 @@ var STX = class _STX {
2630
2542
  * matched comes back as its order, `cancelled` with `cancellation_reason`
2631
2543
  * `auto_matching`.
2632
2544
  *
2633
- * When any order has a `fillMode` other than `"normal"`, the exchange is
2634
- * checked first as in `placeOrder()`, and `STXNotAvailableException` is
2635
- * thrown with nothing sent.
2545
+ * When the exchange rejects the call because a `fillMode` is not enabled
2546
+ * there, this throws `STXNotAvailableException`, as `placeOrder()` does.
2636
2547
  */
2637
2548
  async placeOrders(orders) {
2638
2549
  const body = { orders: orders.map(normalizeOrder) };
2639
2550
  const modes = body.orders.map((o) => o.fill_mode).filter((m) => typeof m === "string");
2640
- if (modes.length) await this.#requireFillModes(modes);
2641
2551
  let raw;
2642
2552
  try {
2643
2553
  raw = await this.#list("orders_batched_post", { body });
2644
2554
  } catch (err) {
2645
- throw (modes.length ? await this.#fillModeError(err, void 0) : void 0) ?? err;
2555
+ throw (modes.length ? this.#fillModeError(err, void 0) : void 0) ?? err;
2646
2556
  }
2647
2557
  return raw.map(toBatchOrderResult);
2648
2558
  }
@@ -4015,4 +3925,4 @@ export {
4015
3925
  orderBody,
4016
3926
  STX
4017
3927
  };
4018
- //# sourceMappingURL=chunk-2PYJI3JS.js.map
3928
+ //# sourceMappingURL=chunk-JJRK3Z27.js.map