@unicitylabs/sphere-sdk 0.14.6 → 0.14.8-dev.1

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.js CHANGED
@@ -9414,12 +9414,15 @@ function pause(ms, signal) {
9414
9414
  });
9415
9415
  }
9416
9416
  async function retryTransient(attempt, signal, intervalMs) {
9417
+ let retried = false;
9417
9418
  for (; ; ) {
9419
+ if (signal.aborted) throw signal.reason;
9418
9420
  try {
9419
- return await attempt();
9421
+ return { value: await attempt(), retried };
9420
9422
  } catch (err) {
9421
9423
  if (signal.aborted) throw err;
9422
9424
  if (!isTransientGatewayError(err)) throw err;
9425
+ retried = true;
9423
9426
  await pause(intervalMs, signal);
9424
9427
  }
9425
9428
  }
@@ -9445,7 +9448,7 @@ function certificationDeadline(timeoutMs, external, label) {
9445
9448
  };
9446
9449
  }
9447
9450
  async function awaitProofBounded(deps, transaction, signal) {
9448
- return retryTransient(
9451
+ const { value } = await retryTransient(
9449
9452
  () => waitInclusionProof(
9450
9453
  deps.client,
9451
9454
  deps.trustBase,
@@ -9457,6 +9460,7 @@ async function awaitProofBounded(deps, transaction, signal) {
9457
9460
  signal,
9458
9461
  deps.intervalMs
9459
9462
  );
9463
+ return value;
9460
9464
  }
9461
9465
 
9462
9466
  // token-engine/errors.ts
@@ -10052,14 +10056,14 @@ var SphereTokenEngine = class {
10052
10056
  let submitError = null;
10053
10057
  let submitRejectedCleanly = false;
10054
10058
  try {
10055
- const response = await retryTransient(
10059
+ const { value: response, retried } = await retryTransient(
10056
10060
  () => this.deps.client.submitCertificationRequest(certificationData),
10057
10061
  signal,
10058
10062
  this.deps.proofPollIntervalMs ?? DEFAULT_PROOF_POLL_INTERVAL_MS
10059
10063
  );
10060
10064
  if (response.status !== CertificationStatus.SUCCESS) {
10061
10065
  submitError = new SphereError(`${failLabel}: ${response.status}`, failCode);
10062
- submitRejectedCleanly = CLEAN_REJECT_STATUSES.has(response.status);
10066
+ submitRejectedCleanly = !retried && CLEAN_REJECT_STATUSES.has(response.status);
10063
10067
  }
10064
10068
  } catch (err) {
10065
10069
  submitError = err ?? new SphereError(`${failLabel}: submit failed`, failCode);
@@ -11456,6 +11460,9 @@ function classifyError(e) {
11456
11460
  }
11457
11461
  return "other";
11458
11462
  }
11463
+ function firstNonConflictError(outcomes) {
11464
+ return outcomes.find((o) => o.error !== void 0 && classifyError(o.error) !== "conflict");
11465
+ }
11459
11466
  function summarize(outcomes) {
11460
11467
  const committed = outcomes.filter((o) => o.certified);
11461
11468
  const rank = { "keep-open": 3, conflict: 2, other: 1 };
@@ -11516,14 +11523,15 @@ var TransferMachine = class {
11516
11523
  ...r.payload.memo !== void 0 ? { memo: r.payload.memo } : {}
11517
11524
  };
11518
11525
  const outcomes = await this.resumeOps(ctx, engine, r);
11519
- const { committed, cls, firstError } = summarize(outcomes);
11526
+ const { committed } = summarize(outcomes);
11520
11527
  const conflicts = this.toConflicts(engine, r, outcomes);
11521
11528
  if (committed.length === 0 && conflicts.length > 0) throw conflicts[0].error;
11522
11529
  let deliveryPending = false;
11523
11530
  if (committed.length > 0) {
11524
11531
  deliveryPending = await this.deliverSet(r.transferId, r.payload.recipient, r.payload.memo, committed);
11525
11532
  }
11526
- if (cls !== null && cls !== "conflict") throw firstError;
11533
+ const blocking = firstNonConflictError(outcomes);
11534
+ if (blocking !== void 0) throw blocking.error;
11527
11535
  await this.applyCommitted(engine, r.transferId, committed);
11528
11536
  if (conflicts.length > 0) {
11529
11537
  return { deliveryPending, partial: await this.settlePartial(engine, r, committed, conflicts) };
@@ -11549,16 +11557,37 @@ var TransferMachine = class {
11549
11557
  ...plan.memo !== void 0 ? { memo: plan.memo } : {}
11550
11558
  };
11551
11559
  const outcomes = [];
11560
+ const certified = [];
11552
11561
  for (let at = 0; at < plan.ops.length; at += CERTIFY_WIDTH) {
11553
11562
  const batch = plan.ops.slice(at, at + CERTIFY_WIDTH);
11554
11563
  const settled = await Promise.all(
11555
- batch.map((op) => this.certifyOne(ctx, engine, op, plan.sources.get(op.sourceTokenId)))
11564
+ batch.map(
11565
+ (op) => this.certifyOne(ctx, engine, op, plan.sources.get(op.sourceTokenId)).then((o) => {
11566
+ if (o.certified) {
11567
+ certified.push(o);
11568
+ this.emitProgress(plan, certified);
11569
+ }
11570
+ return o;
11571
+ })
11572
+ )
11556
11573
  );
11557
11574
  outcomes.push(...settled);
11558
11575
  if (settled.some((o) => o.error !== void 0 && !o.certified)) break;
11559
11576
  }
11560
11577
  return outcomes;
11561
11578
  }
11579
+ /** In-flight snapshot: `tokenTransfers` holds the legs certified SO FAR. */
11580
+ emitProgress(plan, certified) {
11581
+ this.deps.emit("transfer:updated", {
11582
+ id: plan.transferId,
11583
+ status: "submitted",
11584
+ tokens: [],
11585
+ tokenTransfers: certified.map((o) => ({
11586
+ sourceTokenId: o.op.sourceTokenId,
11587
+ method: o.op.kind
11588
+ }))
11589
+ });
11590
+ }
11562
11591
  async certifyOne(ctx, engine, op, source) {
11563
11592
  if (source === void 0) {
11564
11593
  return {