@rhea-finance/confidential-swap 0.2.6 → 0.2.8

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.
package/dist/index.cjs CHANGED
@@ -33,12 +33,15 @@ __export(index_exports, {
33
33
  createConfidentialSwapClient: () => createConfidentialSwapClient,
34
34
  createExecutionStore: () => createExecutionStore,
35
35
  deriveConfidentialAccountId: () => deriveConfidentialAccountId,
36
+ failures: () => failures,
36
37
  findCreditedBalanceChange: () => findCreditedBalanceChange,
37
38
  identityKey: () => identityKey,
38
39
  isRecoverableExecution: () => isRecoverableExecution,
40
+ isTransientPollError: () => isTransientPollError,
39
41
  isUserRejectedError: () => isUserRejectedError,
40
42
  needsWithdrawalReview: () => needsWithdrawalReview,
41
43
  normalizeChain: () => normalizeChain,
44
+ normalizeStatus: () => normalizeStatus,
42
45
  resolveFundingRoute: () => resolveFundingRoute,
43
46
  resolveIntentsCreditAsset: () => resolveIntentsCreditAsset,
44
47
  resolveLinkingWallet: () => resolveLinkingWallet,
@@ -49,6 +52,7 @@ __export(index_exports, {
49
52
  snapshotBalances: () => snapshotBalances,
50
53
  splitConfidentialRawAmount: () => splitConfidentialRawAmount,
51
54
  standards: () => standards,
55
+ successes: () => successes,
52
56
  throwIfAborted: () => throwIfAborted,
53
57
  userRejectedError: () => userRejectedError,
54
58
  validateRecipients: () => validateRecipients,
@@ -600,6 +604,18 @@ function record(value) {
600
604
  responseAssert(value && typeof value === "object" && !Array.isArray(value), "Expected an object response.");
601
605
  return value;
602
606
  }
607
+ function httpErrorMessage(data, status, content) {
608
+ if (data && typeof data === "object" && !Array.isArray(data)) {
609
+ const body = data;
610
+ const apiMessage = [body.message, body.error, body.msg].find(
611
+ (value) => typeof value === "string" && value.trim()
612
+ );
613
+ if (typeof apiMessage === "string") return apiMessage.trim();
614
+ }
615
+ const trimmed = content.trim();
616
+ if (trimmed && !trimmed.startsWith("<") && trimmed.length <= 300) return trimmed;
617
+ return `Service request failed (${status}).`;
618
+ }
603
619
  var HttpConfidentialApi = class {
604
620
  constructor(config) {
605
621
  this.config = config;
@@ -648,7 +664,13 @@ var HttpConfidentialApi = class {
648
664
  } catch {
649
665
  throw new ConfidentialSwapError("INVALID_RESPONSE", "Service returned invalid JSON.", response.status);
650
666
  }
651
- if (!response.ok) throw new ConfidentialSwapError(response.status === 401 || response.status === 403 ? "UNAUTHORIZED" : "HTTP_ERROR", `Service request failed (${response.status}).`, response.status);
667
+ if (!response.ok) {
668
+ throw new ConfidentialSwapError(
669
+ response.status === 401 || response.status === 403 ? "UNAUTHORIZED" : "HTTP_ERROR",
670
+ httpErrorMessage(data, response.status, content),
671
+ response.status
672
+ );
673
+ }
652
674
  return data;
653
675
  } catch (error) {
654
676
  throwIfAborted(signal);
@@ -1302,6 +1324,22 @@ function normalizeStatus(status) {
1302
1324
  }
1303
1325
  var successes = /* @__PURE__ */ new Set(["SUCCESS", "COMPLETED", "SOURCE-CONFIRMED"]);
1304
1326
  var failures = /* @__PURE__ */ new Set(["FAILED", "REFUNDED", "EXPIRED", "CANCELLED", "INCOMPLETE_DEPOSIT"]);
1327
+ function isTransientPollError(error) {
1328
+ if (error instanceof ConfidentialSwapError) {
1329
+ return error.code === "HTTP_ERROR" || error.code === "TIMEOUT" || error.code === "INVALID_RESPONSE";
1330
+ }
1331
+ if (error instanceof DOMException) {
1332
+ if (error.name === "AbortError") return false;
1333
+ if (error.name === "TimeoutError") return true;
1334
+ }
1335
+ if (error instanceof TypeError) return true;
1336
+ const message = String(
1337
+ error && typeof error === "object" && "message" in error ? error.message : error ?? ""
1338
+ );
1339
+ return /failed to fetch|networkerror|network request failed|load failed|econnreset|etimedout|socket/i.test(
1340
+ message
1341
+ );
1342
+ }
1305
1343
  function polling(options) {
1306
1344
  const result = { maxAttempts: options.maxAttempts ?? 0, intervalMs: options.intervalMs ?? 5e3 };
1307
1345
  assert(Number.isInteger(result.maxAttempts) && result.maxAttempts >= 0 && Number.isFinite(result.intervalMs) && result.intervalMs >= 0, "Invalid polling options.");
@@ -1311,7 +1349,18 @@ async function poll(operation, options, signal) {
1311
1349
  const config = polling(options);
1312
1350
  for (let attempt = 0; ; attempt++) {
1313
1351
  throwIfAborted(signal);
1314
- const result = await operation();
1352
+ let result;
1353
+ try {
1354
+ result = await operation();
1355
+ } catch (error) {
1356
+ throwIfAborted(signal);
1357
+ if (!isTransientPollError(error)) throw error;
1358
+ if (config.maxAttempts > 0 && attempt + 1 >= config.maxAttempts) {
1359
+ throw new ConfidentialSwapError("TIMEOUT", "Status remains unconfirmed; query or resume this execution, do not resubmit.");
1360
+ }
1361
+ await delay(config.intervalMs, signal);
1362
+ continue;
1363
+ }
1315
1364
  throwIfAborted(signal);
1316
1365
  if (result.done) return result.value;
1317
1366
  if (config.maxAttempts > 0 && attempt + 1 >= config.maxAttempts) {
@@ -2025,12 +2074,15 @@ function createConfidentialSwapClient(config) {
2025
2074
  createConfidentialSwapClient,
2026
2075
  createExecutionStore,
2027
2076
  deriveConfidentialAccountId,
2077
+ failures,
2028
2078
  findCreditedBalanceChange,
2029
2079
  identityKey,
2030
2080
  isRecoverableExecution,
2081
+ isTransientPollError,
2031
2082
  isUserRejectedError,
2032
2083
  needsWithdrawalReview,
2033
2084
  normalizeChain,
2085
+ normalizeStatus,
2034
2086
  resolveFundingRoute,
2035
2087
  resolveIntentsCreditAsset,
2036
2088
  resolveLinkingWallet,
@@ -2041,6 +2093,7 @@ function createConfidentialSwapClient(config) {
2041
2093
  snapshotBalances,
2042
2094
  splitConfidentialRawAmount,
2043
2095
  standards,
2096
+ successes,
2044
2097
  throwIfAborted,
2045
2098
  userRejectedError,
2046
2099
  validateRecipients,